From 524093ce1e7e992254b5ab58b8743f96a9184fde Mon Sep 17 00:00:00 2001 From: theoretical Date: Wed, 31 Dec 2014 14:27:55 -0500 Subject: [PATCH] Implement chmod function (no-op on Windows) --- include/fc/filesystem.hpp | 5 +++++ src/filesystem.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 9e8ebb9..4a05cd4 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -157,6 +157,11 @@ namespace fc { void copy( const path& from, const path& to ); void rename( const path& from, const path& to ); void resize_file( const path& file, size_t s ); + + // setuid, setgid not implemented. + // translates octal permission like 0755 to S_ stuff defined in sys/stat.h + // no-op on Windows. + void chmod( const path& p, int perm ); void create_hard_link( const path& from, const path& to ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index e06bb6e..db33f1c 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -17,6 +17,7 @@ # include #else #include + #include #include #endif @@ -265,6 +266,34 @@ namespace fc { ("f",f)("s",t)( "reason", fc::except_str() ) ); } } + + // setuid, setgid not implemented. + // translates octal permission like 0755 to S_ stuff defined in sys/stat.h + // no-op on Windows. + void chmod( const path& p, int perm ) + { +#ifndef WIN32 + mode_t actual_perm = + ((perm & 0400) ? S_IRUSR : 0) + | ((perm & 0200) ? S_IWUSR : 0) + | ((perm & 0100) ? S_IXUSR : 0) + + | ((perm & 0040) ? S_IRGRP : 0) + | ((perm & 0020) ? S_IWGRP : 0) + | ((perm & 0010) ? S_IXGRP : 0) + + | ((perm & 0004) ? S_IROTH : 0) + | ((perm & 0002) ? S_IWOTH : 0) + | ((perm & 0001) ? S_IXOTH : 0) + ; + + int result = ::chmod( p.string().c_str(), actual_perm ); + if( result != 0 ) + FC_THROW( "chmod operation failed on ${p}", ("p",p) ); +#endif + return; + } + void rename( const path& f, const path& t ) { try { boost::filesystem::rename( boost::filesystem::path(f), boost::filesystem::path(t) );