diff --git a/daemon/main/nzbget.cpp b/daemon/main/nzbget.cpp index 1bdbc9d74..874e01268 100644 --- a/daemon/main/nzbget.cpp +++ b/daemon/main/nzbget.cpp @@ -272,11 +272,23 @@ void NZBGet::Init() BootConfig(); #ifndef WIN32 - if (m_options->GetUMask() < 01000) + mode_t uMask = static_cast(m_options->GetUMask()); + if (uMask > 0 && uMask < 01000) { /* set newly created file permissions */ - umask(m_options->GetUMask()); + FileSystem::uMask = uMask; + umask(FileSystem::uMask); } + else + { + FileSystem::uMask = umask(0); + umask(FileSystem::uMask); + } + +#ifdef DEBUG + debug("Using %o umask", FileSystem::uMask); +#endif + #endif m_scanner->InitOptions(); diff --git a/daemon/postprocess/Unpack.cpp b/daemon/postprocess/Unpack.cpp index f2719928e..77ad0305a 100644 --- a/daemon/postprocess/Unpack.cpp +++ b/daemon/postprocess/Unpack.cpp @@ -735,7 +735,7 @@ bool UnpackController::Cleanup() #ifndef WIN32 // Fixing file or directory permissions overridden by the unpacker - FileSystem::SetFileOrDirPermissionsWithUMask(dstFile.Str(), g_Options->GetUMask()); + FileSystem::RestoreFileOrDirPermissions(dstFile.Str()); #endif extractedFiles.push_back(filename); @@ -743,7 +743,7 @@ bool UnpackController::Cleanup() #ifndef WIN32 // Fixing directory permissions overridden by the unpacker - FileSystem::SetDirPermissionsWithUMask(destDir, g_Options->GetUMask()); + FileSystem::RestoreDirPermissions(destDir); #endif } diff --git a/daemon/util/FileSystem.cpp b/daemon/util/FileSystem.cpp index 87bbc1828..47d9000b4 100644 --- a/daemon/util/FileSystem.cpp +++ b/daemon/util/FileSystem.cpp @@ -948,6 +948,9 @@ bool FileSystem::FlushDirBuffers(const char* filename, CString& errmsg) } #ifndef WIN32 + +mode_t FileSystem::uMask; + void FileSystem::FixExecPermission(const char* filename) { struct stat buffer; @@ -959,7 +962,7 @@ void FileSystem::FixExecPermission(const char* filename) } } -bool FileSystem::SetFileOrDirPermissionsWithUMask(const char* filename, int umask) +bool FileSystem::RestoreFileOrDirPermissions(const char* filename) { struct stat buffer; int ec = stat(filename, &buffer); @@ -975,27 +978,27 @@ bool FileSystem::SetFileOrDirPermissionsWithUMask(const char* filename, int umas if (S_ISDIR(buffer.st_mode)) { - return SetDirPermissionsWithUMask(filename, umask); + return RestoreDirPermissions(filename); } - return SetFilePermissionsWithUMask(filename, umask); + return RestoreFilePermissions(filename); } -bool FileSystem::SetFilePermissionsWithUMask(const char* filepath, int umask) +bool FileSystem::RestoreFilePermissions(const char* filepath) { mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // 0666 - return SetPermissionsWithUMask(filepath, mode, umask); + return RestorePermissions(filepath, mode); } -bool FileSystem::SetDirPermissionsWithUMask(const char* filename, int umask) +bool FileSystem::RestoreDirPermissions(const char* filename) { mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; // 0777 - return SetPermissionsWithUMask(filename, mode, umask); + return RestorePermissions(filename, mode); } -bool FileSystem::SetPermissionsWithUMask(const char* filename, mode_t mode, int umask) +bool FileSystem::RestorePermissions(const char* filename, mode_t mode) { - mode_t permissions = mode & ~umask; + mode_t permissions = mode & ~FileSystem::uMask; int ec = chmod(filename, permissions); if (ec == 0) { @@ -1013,6 +1016,7 @@ bool FileSystem::SetPermissionsWithUMask(const char* filename, mode_t mode, int return false; } + #endif #ifdef WIN32 diff --git a/daemon/util/FileSystem.h b/daemon/util/FileSystem.h index 59a2cdde8..29eefefde 100644 --- a/daemon/util/FileSystem.h +++ b/daemon/util/FileSystem.h @@ -62,10 +62,11 @@ class FileSystem #ifndef WIN32 static CString ExpandHomePath(const char* filename); static void FixExecPermission(const char* filename); - static bool SetFileOrDirPermissionsWithUMask(const char* filename, int umask); - static bool SetFilePermissionsWithUMask(const char* filename, int umask); - static bool SetDirPermissionsWithUMask(const char* filename, int umask); - static bool SetPermissionsWithUMask(const char* filename, mode_t mode, int umask); + static bool RestoreFileOrDirPermissions(const char* filename); + static bool RestoreFilePermissions(const char* filename); + static bool RestoreDirPermissions(const char* filename); + static bool RestorePermissions(const char* filename, mode_t mode); + static mode_t uMask; #endif static CString ExpandFileName(const char* filename); static CString GetExeFileName(const char* argv0);