From b8937d809e5bb4262314fa4d8689c350f29e859f Mon Sep 17 00:00:00 2001
From: Patrick Vos <itofzo@gmail.com>
Date: Sun, 1 Apr 2012 11:29:54 +0200
Subject: [PATCH] Add check if allowed to change permissions before changing
 permissions

On linux when download user and sickbeard user are not the same,
moving files will result in download user still owning the files.
When SickBeard tries to chmod permissions it results in error,
because only root or owner are allowed to do this.
Check if user is allowed to change permissions.
---
 sickbeard/helpers.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/sickbeard/helpers.py b/sickbeard/helpers.py
index fe15f8801..0dcef3908 100644
--- a/sickbeard/helpers.py
+++ b/sickbeard/helpers.py
@@ -446,6 +446,13 @@ def chmodAsParent(childPath):
     if childPath_mode == childMode:
         return
 
+    childPath_owner = os.stat(childPath).st_uid
+    user_id = os.geteuid()
+
+    if user_id !=0 and user_id != childPath_owner:
+        logger.log(u"Not running as root or owner of "+childPath+", not trying to set permissions", logger.DEBUG)
+        return
+
     try:
         ek.ek(os.chmod, childPath, childMode)
         logger.log(u"Setting permissions for %s to %o as parent directory has %o" % (childPath, childMode, parentMode), logger.DEBUG)
@@ -474,6 +481,13 @@ def fixSetGroupID(childPath):
         if childGID == parentGID:
             return
 
+        childPath_owner = os.stat(childPath).st_uid
+        user_id = os.geteuid()
+
+        if user_id !=0 and user_id != childPath_owner:
+            logger.log(u"Not running as root or owner of "+childPath+", not trying to set the set-group-ID", logger.DEBUG)
+            return
+
         try:
             ek.ek(os.chown, childPath, -1, parentGID)  #@UndefinedVariable - only available on UNIX
             logger.log(u"Respecting the set-group-ID bit on the parent directory for %s" % (childPath), logger.DEBUG)
-- 
GitLab