Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix files/dirs permissions overridden by the unpacker #229

Merged
merged 18 commits into from
Apr 18, 2024
16 changes: 14 additions & 2 deletions daemon/main/nzbget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,23 @@ void NZBGet::Init()
BootConfig();

#ifndef WIN32
if (m_options->GetUMask() < 01000)
mode_t uMask = static_cast<mode_t>(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();
Expand Down
4 changes: 2 additions & 2 deletions daemon/postprocess/Unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,15 @@ 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);
}

#ifndef WIN32
// Fixing directory permissions overridden by the unpacker
FileSystem::SetDirPermissionsWithUMask(destDir, g_Options->GetUMask());
FileSystem::RestoreDirPermissions(destDir);
#endif

}
Expand Down
22 changes: 13 additions & 9 deletions daemon/util/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
{
Expand All @@ -1013,6 +1016,7 @@ bool FileSystem::SetPermissionsWithUMask(const char* filename, mode_t mode, int

return false;
}

#endif

#ifdef WIN32
Expand Down
9 changes: 5 additions & 4 deletions daemon/util/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down