def unmountVolume(self, mountPath): if mountPath is None: return # ToDo: log this, maybe error out here if os.path.samefile("/", mountPath): raise ValueError('Can not unmount the root partition, this is definatley a bug') if os.path.ismount(mountPath): volumeTools.unmountVolume(mountPath)
def unmountVolume(self, mountPath): if mountPath is None: return # ToDo: log this, maybe error out here if os.path.samefile("/", mountPath): raise ValueError( 'Can not unmount the root partition, this is definatley a bug') if os.path.ismount(mountPath): volumeTools.unmountVolume(mountPath)
def cleanupItem(myClass, targetPath): '''Dispose of an item''' if targetPath is None: raise ValueError('cleanupItem called with an empty targetPath') targetPath = pathHelpers.normalizePath(targetPath) # -- confirm that this item is a managed item, or in a manged space managedItem = False managedMount = False managedSpace = False if targetPath in myClass.managedItems: managedItem = True managedSpace = True else: for thisManagedSpace in myClass.managedItems: if os.path.isdir(thisManagedSpace) and not os.path.islink(thisManagedSpace) and os.path.lexists(targetPath): if pathHelpers.pathInsideFolder(targetPath, thisManagedSpace) and os.lstat(targetPath)[stat.ST_DEV] == os.lstat(thisManagedSpace)[stat.ST_DEV]: managedSpace = True break if targetPath in myClass.managedMounts: managedMount = True if managedMount is False and managedSpace is False: raise ValueError('cleanupItem handed a path that was not in a managed space or a managed mount: ' + targetPath) if not os.path.lexists(targetPath): if True in [managedItem, managedSpace]: # the item no longer exists, we just have to clean it out of managedItems and/or managedMount if managedItem is True: myClass.managedItems.remove(targetPath) if managedMount is True: myClass.managedMounts.remove(targetPath) return else: raise ValueError('cleanupItem handed a path that does not exist: ' + targetPath) # -- find any managed items inside this one, and let them handle their business first if os.path.isdir(targetPath) and not os.path.islink(targetPath): for thisItem in myClass.findManagedItemsInsideDirectory(targetPath): myClass.cleanupItem(thisItem) # -- if this is a mount, unmount it if os.path.ismount(targetPath): volumeTools.unmountVolume(targetPath) # -- if this is in controlled space, wipe it if managedSpace is True: # handle the simple cases of a soft-link or a file if os.path.islink(targetPath) or os.path.isfile(targetPath): try: os.unlink(targetPath) except OSError: # assume that this was a permissions error, and try to chmod it into cooperating os.chmod(thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) os.unlink(thisFile) # handle folders else: # make sure that the permissions on the root folder are ok if not os.access(targetPath, os.R_OK | os.X_OK): os.chmod(targetPath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) # walk up the tree to remove any volumes mounted into the path for root, dirs, files in os.walk(targetPath, topdown=True): # unmount the directory if it is a volume if os.path.ismount(root): volumeTools.unmountVolume(root) # ToDo: log this dirs = [] # make sure we don't try to decend into folders that are no longer there continue # delete all files, continuing through failures for thisFile in [os.path.join(root, internalName) for internalName in files]: try: try: os.unlink(thisFile) except OSError: # assume that this was a permissions error, and try to chmod it into cooperating os.chmod(thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) os.unlink(thisFile) except: # ToDo: make this more specific pass # ToDo: log this # catch any symlinks for thisFolder in [os.path.join(root, internalName) for internalName in dirs]: # make sure we can make it into all sub-folders and delete them: if not os.access(thisFolder, os.R_OK | os.X_OK): os.chmod(thisFolder, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) if os.path.islink(thisFolder): try: os.unlink(thisFolder) except: # ToDo: make this more specific pass # ToDo: log this # now that there are no mounted volumes, there should be no files, so delete the folders for root, dirs, files in os.walk(targetPath, topdown=False): try: os.rmdir(root) except Exception, error: # ToDo: make this more specific sys.stderr.write('Unable to delete folder: "%s" got error: %s' % (root, str(error))) # ToDo: logging
def cleanupItem(myClass, targetPath): '''Dispose of an item''' if targetPath is None: raise ValueError('cleanupItem called with an empty targetPath') targetPath = pathHelpers.normalizePath(targetPath) # -- confirm that this item is a managed item, or in a manged space managedItem = False managedMount = False managedSpace = False if targetPath in myClass.managedItems: managedItem = True managedSpace = True else: for thisManagedSpace in myClass.managedItems: if os.path.isdir(thisManagedSpace) and not os.path.islink( thisManagedSpace) and os.path.lexists(targetPath): if pathHelpers.pathInsideFolder( targetPath, thisManagedSpace) and os.lstat( targetPath)[stat.ST_DEV] == os.lstat( thisManagedSpace)[stat.ST_DEV]: managedSpace = True break if targetPath in myClass.managedMounts: managedMount = True if managedMount is False and managedSpace is False: raise ValueError( 'cleanupItem handed a path that was not in a managed space or a managed mount: ' + targetPath) if not os.path.lexists(targetPath): if True in [managedItem, managedSpace]: # the item no longer exists, we just have to clean it out of managedItems and/or managedMount if managedItem is True: myClass.managedItems.remove(targetPath) if managedMount is True: myClass.managedMounts.remove(targetPath) return else: raise ValueError( 'cleanupItem handed a path that does not exist: ' + targetPath) # -- find any managed items inside this one, and let them handle their business first if os.path.isdir(targetPath) and not os.path.islink(targetPath): for thisItem in myClass.findManagedItemsInsideDirectory( targetPath): myClass.cleanupItem(thisItem) # -- if this is a mount, unmount it if os.path.ismount(targetPath): volumeTools.unmountVolume(targetPath) # -- if this is in controlled space, wipe it if managedSpace is True: # handle the simple cases of a soft-link or a file if os.path.islink(targetPath) or os.path.isfile(targetPath): try: os.unlink(targetPath) except OSError: # assume that this was a permissions error, and try to chmod it into cooperating os.chmod(thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) os.unlink(thisFile) # handle folders else: # make sure that the permissions on the root folder are ok if not os.access(targetPath, os.R_OK | os.X_OK): os.chmod( targetPath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) # walk up the tree to remove any volumes mounted into the path for root, dirs, files in os.walk(targetPath, topdown=True): # unmount the directory if it is a volume if os.path.ismount(root): volumeTools.unmountVolume(root) # ToDo: log this dirs = [ ] # make sure we don't try to decend into folders that are no longer there continue # delete all files, continuing through failures for thisFile in [ os.path.join(root, internalName) for internalName in files ]: try: try: os.unlink(thisFile) except OSError: # assume that this was a permissions error, and try to chmod it into cooperating os.chmod( thisFile, stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) os.unlink(thisFile) except: # ToDo: make this more specific pass # ToDo: log this # catch any symlinks for thisFolder in [ os.path.join(root, internalName) for internalName in dirs ]: # make sure we can make it into all sub-folders and delete them: if not os.access(thisFolder, os.R_OK | os.X_OK): os.chmod( thisFolder, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) if os.path.islink(thisFolder): try: os.unlink(thisFolder) except: # ToDo: make this more specific pass # ToDo: log this # now that there are no mounted volumes, there should be no files, so delete the folders for root, dirs, files in os.walk(targetPath, topdown=False): try: os.rmdir(root) except Exception, error: # ToDo: make this more specific sys.stderr.write( 'Unable to delete folder: "%s" got error: %s' % (root, str(error))) # ToDo: logging