From 3490be26efc6cfce869bf53998587ec37154c163 Mon Sep 17 00:00:00 2001 From: lars Date: Fri, 24 Nov 2006 11:02:02 +0000 Subject: [PATCH] mark created mountpoints to remove them safely afterwards --- src/cryptobox/core/container.py | 48 ++++++++++++++++++++++----------- src/cryptobox/core/main.py | 8 ++---- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/cryptobox/core/container.py b/src/cryptobox/core/container.py index ced4197..d0cb76c 100644 --- a/src/cryptobox/core/container.py +++ b/src/cryptobox/core/container.py @@ -11,16 +11,6 @@ import re import logging from cryptobox.core.exceptions import * -"""exceptions: - CBVolumeIsActive - CBNameActivelyUsed - CBInvalidName - CBInvalidPassword - CBInvalidType - CBCreateError - CBMountError - CBChangePasswordError - """ ContainerTypes = { "unused":0, @@ -30,6 +20,11 @@ ContainerTypes = { } +## we use this marker to make sure, that we do not remove a non-cryptobox directory +## below the mount directory +MOUNT_DIR_MARKER = '_cryptobox_mount_dir_' + + class CryptoBoxContainer: __fsTypes = { @@ -377,7 +372,7 @@ class CryptoBoxContainer: self.log.warn("Could not open %s" % (os.devnull, )) self.__cleanMountDirs() if not os.path.exists(self.__getMountPoint()): - os.mkdir(self.__getMountPoint()) + self.__createMountDirectory(self.__getMountPoint()) if not os.path.exists(self.__getMountPoint()): errorMsg = "Could not create mountpoint (%s)" % (self.__getMountPoint(), ) self.log.error(errorMsg) @@ -478,7 +473,7 @@ class CryptoBoxContainer: self.log.warn("Could not open %s" % (os.devnull, )) self.__cleanMountDirs() if not os.path.exists(self.__getMountPoint()): - os.mkdir(self.__getMountPoint()) + self.__createMountDirectory(self.__getMountPoint()) if not os.path.exists(self.__getMountPoint()): errorMsg = "Could not create mountpoint (%s)" % (self.__getMountPoint(), ) self.log.error(errorMsg) @@ -637,11 +632,32 @@ class CryptoBoxContainer: """ remove all unnecessary subdirs of the mount parent directory this should be called for every (u)mount """ subdirs = os.listdir(self.cbox.prefs["Locations"]["MountParentDir"]) - for dir in subdirs: - abs_dir = os.path.join(self.cbox.prefs["Locations"]["MountParentDir"], dir) - if (not os.path.islink(abs_dir)) and os.path.isdir(abs_dir) and (not os.path.ismount(abs_dir)): - os.rmdir(abs_dir) + for d in subdirs: + abs_dir = os.path.join(self.cbox.prefs["Locations"]["MountParentDir"], d) + if (not os.path.islink(abs_dir)) \ + and os.path.isdir(abs_dir) \ + and (not os.path.ismount(abs_dir)) \ + and (os.path.isfile(os.path.join(abs_dir,MOUNT_DIR_MARKER))) \ + and (len(os.listdir(abs_dir)) == 1): + try: + os.remove(os.path.join(abs_dir,MOUNT_DIR_MARKER)) + os.rmdir(abs_dir) + except OSError,errMsg: + ## we do not care too much about unclean cleaning ... + self.log.info("failed to clean a mountpoint (%s): %s" % (abs_dir,str(errMsg))) + + def __createMountDirectory(self,dirname): + """create and mark a mount directory + this marking helps to remove old mountdirs safely""" + os.mkdir(dirname) + try: + f = file(os.path.join(dirname,MOUNT_DIR_MARKER),"w") + f.close() + except OSError,errMsg: + ## we do not care too much about the marking + self.log.info("failed to mark a mountpoint (%s): %s" % (dirname,str(errMsg))) + def __getEventArgs(self): """return an array of arguments for event scripts handling pre/post-mount/umount diff --git a/src/cryptobox/core/main.py b/src/cryptobox/core/main.py index 4212a0e..58fcb84 100644 --- a/src/cryptobox/core/main.py +++ b/src/cryptobox/core/main.py @@ -1,9 +1,5 @@ ''' This is the web interface for a fileserver managing encrypted filesystems. - -It was originally written in bash/perl. Now a complete rewrite is in -progress. So things might be confusing here. Hopefully not for long. -:) ''' # check python version @@ -26,7 +22,7 @@ VERSION = "0.3~1" class CryptoBox: '''this class rules them all! - put things like logging, conf and oter stuff in here, + put things like logging, conf and other stuff in here, that might be used by more classes, it will be passed on to them''' @@ -186,7 +182,7 @@ class CryptoBoxProps(CryptoBox): try: result = self.containers[:] if filterType != None: - if filterType in range(len(cbxContainer.Types)): + if filterType in range(len(cbxContainer.ContainerTypes)): return [e for e in self.containers if e.getType() == filterType] else: self.log.info("invalid filterType (%d)" % filterType)