Skip to content

Commit

Permalink
Fix file/directory permissions after unpacking (#222)
Browse files Browse the repository at this point in the history
- fixed file/directory permissions overriden by the unpacker;
- added the DEBUG definition for debug builds on POSIX systems.
  • Loading branch information
dnzbk authored Apr 15, 2024
1 parent 00179f7 commit f268896
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ project(

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g0 -pthread -g -Weverything -Wno-c++98-compat" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g0 -pthread -g -DDEBUG -Weverything -Wno-c++98-compat" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-O0 -g0 -pthread -g -Wall -Wextra" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-O0 -g0 -pthread -g -DDEBUG -Wall -Wextra" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "/Od /Zi /MTd /MP /W4 /EHs /DDEBUG /D_DEBUG /DWIN32 /wd4800 /wd4267" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} winmm.lib Dbghelp.lib libcpmtd.lib" CACHE STRING "" FORCE)
Expand Down
20 changes: 16 additions & 4 deletions daemon/postprocess/Unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2013-2018 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* Copyright (C) 2023-2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down Expand Up @@ -714,11 +715,11 @@ bool UnpackController::Cleanup()
{
// moving files back
DirBrowser dir(m_unpackDir);
const char* destDir = !m_finalDir.Empty() ? *m_finalDir : *m_destDir;
while (const char* filename = dir.Next())
{
BString<1024> srcFile("%s%c%s", *m_unpackDir, PATH_SEPARATOR, filename);
BString<1024> dstFile("%s%c%s", !m_finalDir.Empty() ? *m_finalDir : *m_destDir,
PATH_SEPARATOR, *FileSystem::MakeValidFilename(filename));
BString<1024> dstFile("%s%c%s", destDir, PATH_SEPARATOR, *FileSystem::MakeValidFilename(filename));

// silently overwrite existing files
FileSystem::DeleteFile(dstFile);
Expand All @@ -732,8 +733,19 @@ bool UnpackController::Cleanup()
ok = false;
}

#ifndef WIN32
// Fixing file or directory permissions overridden by the unpacker
FileSystem::SetFileOrDirPermissionsWithUMask(dstFile.Str(), g_Options->GetUMask());
#endif

extractedFiles.push_back(filename);
}

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

}

CString errmsg;
Expand Down
57 changes: 57 additions & 0 deletions daemon/util/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +22,7 @@
#include "nzbget.h"
#include "FileSystem.h"
#include "Util.h"
#include "Log.h"

const char* RESERVED_DEVICE_NAMES[] = { "CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
Expand Down Expand Up @@ -956,6 +958,61 @@ void FileSystem::FixExecPermission(const char* filename)
chmod(filename, buffer.st_mode);
}
}

bool FileSystem::SetFileOrDirPermissionsWithUMask(const char* filename, int umask)
{
struct stat buffer;
int ec = stat(filename, &buffer);
if (ec != 0)
{

#ifdef DEBUG
debug("Failed to read information for %s. Errno: %i", filename, ec);
#endif

return false;
}

if (S_ISDIR(buffer.st_mode))
{
return SetDirPermissionsWithUMask(filename, umask);
}

return SetFilePermissionsWithUMask(filename, umask);
}

bool FileSystem::SetFilePermissionsWithUMask(const char* filepath, int umask)
{
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // 0666
return SetPermissionsWithUMask(filepath, mode, umask);
}

bool FileSystem::SetDirPermissionsWithUMask(const char* filename, int umask)
{
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; // 0777
return SetPermissionsWithUMask(filename, mode, umask);
}

bool FileSystem::SetPermissionsWithUMask(const char* filename, mode_t mode, int umask)
{
mode_t permissions = mode & ~umask;
int ec = chmod(filename, permissions);
if (ec == 0)
{

#ifdef DEBUG
debug("Permissions %o was set for %s", permissions, filename);
#endif

return true;
}

#ifdef DEBUG
debug("Failed to set permissions for %s. Errno %i", filename, ec);
#endif

return false;
}
#endif

#ifdef WIN32
Expand Down
7 changes: 6 additions & 1 deletion daemon/util/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -14,7 +15,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


Expand Down Expand Up @@ -61,6 +62,10 @@ 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);
#endif
static CString ExpandFileName(const char* filename);
static CString GetExeFileName(const char* argv0);
Expand Down

0 comments on commit f268896

Please sign in to comment.