Set configured owner of files and directories after plc program upload

If 'plcworkdir_set_uid' is set to 1, all uploaded files and new created
directories in plc work dir will set to configured owner and group.
This commit is contained in:
2020-11-27 09:33:15 +01:00
parent 1b7c4cddc4
commit a07fa2e333
2 changed files with 16 additions and 8 deletions

View File

@@ -1194,23 +1194,31 @@ class RevPiPyLoad:
# Windowszeichen prüfen
filename = filename.replace("\\", "/")
# Absoluten Pfad prüfen
# Build absolut path, join will return last element, if absolute
dirname = os.path.join(self.plcworkdir, os.path.dirname(filename))
if self.plcworkdir not in os.path.abspath(dirname):
return False
# Ordner erzeugen
set_uid = self.plcuid if self.plcworkdir_set_uid else 0
set_gid = self.plcgid if self.plcworkdir_set_uid else 0
# Set permissions only to newly created directories
if not os.path.exists(dirname):
lst_subdir = dirname.lstrip(self.plcworkdir).split("/")
for i in range(len(lst_subdir)):
dir_part = os.path.join(self.plcworkdir, *lst_subdir[:i + 1])
if os.path.exists(dir_part):
# Do not change owner of existing directorys
continue
os.makedirs(dirname)
os.chown(dir_part, set_uid, set_gid)
# Datei erzeugen
try:
with open(filename, "wb") as fh:
fh.write(gzip.decompress(filedata.data))
if self.plcworkdir_set_uid:
os.chown(self.plcworkdir, self.plcuid, self.plcgid)
else:
os.chown(self.plcworkdir, 0, 0)
os.chown(filename, set_uid, set_gid)
return True
except Exception:
return False

View File

@@ -27,7 +27,7 @@ setup(
license="LGPLv3",
name="revpipyload",
version="0.9.2e",
version="0.9.2f",
scripts=[
"data/revpipyload",