From 87f3ea2900bb41739d41811404a09eeba9a0f898 Mon Sep 17 00:00:00 2001 From: Innovation Date: Sun, 15 Dec 2024 16:07:15 -0600 Subject: [PATCH] Extremely basic (but working!) implementation of the restore method. --- libufps/ufps.py | 68 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/libufps/ufps.py b/libufps/ufps.py index e4655c1..724a3bf 100644 --- a/libufps/ufps.py +++ b/libufps/ufps.py @@ -9,6 +9,27 @@ class ufps: Primary class for libufps """ + I_PERMS = 0 + """I_PERMS = index for permissions""" + + I_USERN = 1 + """I_USERN = index for username""" + + I_GROUP = 2 + """I_GROUP = index for group""" + + I_ISDIR = 3 + """I_ISDIR = index for directory boolean""" + + I_ISLNK = 4 + """I_ISLNK = index for link boolean""" + + I_IPATH = 5 + """I_ISLNK = index for indexed path""" + + I_LPATH = 6 + """I_LPATH = index for linked path (None if I_ISLNK=False)""" + def __init__(self, basedir): """ Constructor method. @@ -98,9 +119,9 @@ class ufps: stats = os.stat(realpath) # Invoke a magic spell - # Magic number -3 is used as for this we only need the last 3 digits. + # Magic number -4 is used as for this we only need the last 4 digits. # Directory and link detection is handled later. - pPerms = oct(stats.st_mode)[-3:] + pPerms = oct(stats.st_mode)[-4:] # Get whether or not the path is a dir pIsDir = os.path.isdir(realpath) @@ -175,8 +196,8 @@ class ufps: def createIndex(self, path): """ - Create an idex by delving into subdirectories and recording information. - :rtype path: Path to directory or file + Create an index by delving into subdirectories and recording information. + :param path: Path to directory to create index of. :type path: str :return outputIndex: Returns file permission, owner, directory, and link information in a string to be written to a file. @@ -185,7 +206,6 @@ class ufps: outputIndex = "" realpath = self.basedir+path - #realpath = path findCommand = "find " + realpath + " -printf '%P\n'" fileList = subprocess.check_output(findCommand, shell=True, text=True) @@ -205,3 +225,41 @@ class ufps: outputIndex = outputIndex[:-1] return outputIndex + + + + def restore(self, index, path): + """ + Restore permissions according to index. + :param index: Path to ufps index file. + :type index: str + :param path: Path to directory to be restored. + :type path: str + :return results: Results of restoration (such as inconsistencies). + :rtype results: str + """ + realpath = self.basedir+path + print(realpath) + results = "" + file = open(index) + idx = file.read() + file.close() + + idx = idx.split('\n') + for p in idx: + indexEntry = p.split(' | ') + print(indexEntry) + indexRealPath = realpath + indexEntry[self.I_IPATH] + permsToSet = int(indexEntry[self.I_PERMS], base=8) + pathExists = os.path.exists(indexRealPath) + + if pathExists: + os.chmod(indexRealPath, permsToSet) + else: + # If the path doesn't exist, an inconsistency has been detected. + # We report this so that the user can manually rectify it. + results += "[INCONSISTENCY] File doesn't exist: " + indexEntry[self.I_IPATH] + "\n" + + return results + +# def diff(self, index):