diff --git a/tren.py b/tren.py index 608997f..1245396 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.203 2010/03/19 00:59:27 tundra Exp $" +RCSID = "$Id: tren.py,v 1.204 2010/03/19 02:07:02 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -68,17 +68,19 @@ if WINDOWS: try: - from win32api import GetFileAttributes + from win32api import GetFileAttributes, GetComputerName + import win32con from win32file import GetDriveType from win32wnet import WNetGetUniversalName from win32security import * + WIN32HOST = GetComputerName() WIN32ALL = True except: WIN32ALL = False -# Get unix password and group features +# Get Unix password and group features elif POSIX: import grp @@ -158,6 +160,8 @@ SINGLEINST = "SINGLEINST" # Indicates a single, not range, replacement instance WINDOWSGROUP = "WindowsGroup" # Returned on Windows w/o win32all WINDOWSUSER = "WindowsUser" # Reutrned on Windows w/o win32all +WINGROUPNOT = "GroupNotAvailable" # Returned when win32all can't get a group name +WINUSERNOT = "UserNotAvailable" # Returned when win32all can't get a user name ##### # Replacement Token Literals @@ -653,12 +657,12 @@ # Handle group name values elif order == ORDERBYGROUP: - sortkey = self.__GetFileGroupname(stats[ST_GID]) + sortkey = self.__GetFileGroupname(fullname) # Handle user name values elif order == ORDERBYUSER: - sortkey = self.__GetFileUsername(stats[ST_UID]) + sortkey = self.__GetFileUsername(fullname) # Save into storage @@ -773,13 +777,41 @@ # Determine File's Group Name ##### - def __GetFileGroupname(self, gid): + def __GetFileGroupname(self, fullname): - if OSNAME == "posix": - return grp.getgrgid(gid)[0] + if POSIX: + return grp.getgrgid(self.RenNames[fullname][STATS][ST_GID])[0] else: - return WINDOWSGROUP + retval = WINDOWSGROUP + + if WIN32ALL: + + try: + # Get the internal Win32 security group information for this file. + + hg = GetFileSecurity(fullname, GROUP_SECURITY_INFORMATION) + sidg = hg.GetSecurityDescriptorGroup() + + # We have to know who is hosting the filesystem for this file + + drive = fullname[0:3] + if GetDriveType(drive) == win32con.DRIVE_REMOTE: + fnhost = WNetGetUniversalName(drive, 1).split('\\')[2] + + else: + fnhost = WIN32HOST + + # Now we can translate the sids into names + + retval = LookupAccountSid(fnhost, sidg)[0] + + # On any error, just act like win32all isn't there + + except: + retval = WINGROUPNOT + + return retval # End of 'GetFileGroupname()' @@ -788,13 +820,42 @@ # Determine File's User Name ##### - def __GetFileUsername(self, uid): + def __GetFileUsername(self, fullname): - if OSNAME == "posix": - return pwd.getpwuid(uid)[0] + if POSIX: + return pwd.getpwuid(self.RenNames[fullname][STATS][ST_UID])[0] else: - return WINDOWSUSER + retval = WINDOWSUSER + + if WIN32ALL: + + try: + + # Get the internal Win32 security information for this file. + + ho = GetFileSecurity(fullname, OWNER_SECURITY_INFORMATION) + sido = ho.GetSecurityDescriptorOwner() + + # We have to know who is hosting the filesystem for this file + + drive = fullname[0:3] + if GetDriveType(drive) == win32con.DRIVE_REMOTE: + fnhost = WNetGetUniversalName(drive, 1).split('\\')[2] + + else: + fnhost = WIN32HOST + + # Now we can translate the sids into names + + retval = LookupAccountSid(fnhost, sido)[0] + + # On any error, just act like win32all isn't there + + except: + retval = WINUSERNOT + + return retval # End of 'GetFileUsername()' @@ -1129,7 +1190,7 @@ r[2] = str(self.RenNames[target][STATS][ST_GID]) elif r[2] == TOKFILGROUP: - r[2] = self.__GetFileGroupname(self.RenNames[target][STATS][ST_GID]) + r[2] = self.__GetFileGroupname(target) elif r[2] == TOKFILINODE: r[2] = str(self.RenNames[target][STATS][ST_INO]) @@ -1147,7 +1208,7 @@ r[2] = str(self.RenNames[target][STATS][ST_UID]) elif r[2] == TOKFILUSER: - r[2] = self.__GetFileUsername(self.RenNames[target][STATS][ST_UID]) + r[2] = self.__GetFileUsername(target) ### @@ -1190,7 +1251,7 @@ # Handle Windows variants - they act differently - if os.name != 'posix': + if not POSIX: pipe = os.popen(command, 'r') # Handle Unix variants