From 778b75bbcd3ee0906bd7b1a7af1fd984f1c4608b Mon Sep 17 00:00:00 2001 From: Manuel Franceschini <manuel.franceschini@gmail.com> Date: Sat, 5 Apr 2008 12:07:10 +0000 Subject: [PATCH] Backend directory functions for file backend Add _[Create,Remove,Rename]FileStorageDir function which are needed for file-based instance management. These function check whether the given directory to operate on is under the cluster-wide defined default file storage dir. If this is not the case the won't do anything and return False. This is to prevent cluster manipulation or damage. Reviewed-by: ultrotter --- lib/backend.py | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/lib/backend.py b/lib/backend.py index 9df5ddac9..b9e8a3319 100644 --- a/lib/backend.py +++ b/lib/backend.py @@ -1417,6 +1417,130 @@ def RenameBlockDevices(devlist): return result +def _TransformFileStorageDir(file_storage_dir): + """Checks whether given file_storage_dir is valid. + + Checks wheter the given file_storage_dir is within the cluster-wide + default file_storage_dir stored in SimpleStore. Only paths under that + directory are allowed. + + Args: + file_storage_dir: string with path + + Returns: + normalized file_storage_dir (string) if valid, None otherwise + + """ + file_storage_dir = os.path.normpath(file_storage_dir) + base_file_storage_dir = ssconf.SimpleStore().GetFileStorageDir() + if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) == + base_file_storage_dir): + logger.Error("file storage directory '%s' is not under base file" + " storage directory '%s'" % + (file_storage_dir, base_file_storage_dir)) + return None + return file_storage_dir + + +def CreateFileStorageDir(file_storage_dir): + """Create file storage directory. + + Args: + file_storage_dir: string containing the path + + Returns: + tuple with first element a boolean indicating wheter dir + creation was successful or not + + """ + file_storage_dir = _TransformFileStorageDir(file_storage_dir) + result = True, + if not file_storage_dir: + result = False, + else: + if os.path.exists(file_storage_dir): + if not os.path.isdir(file_storage_dir): + logger.Error("'%s' is not a directory" % file_storage_dir) + result = False, + else: + try: + os.makedirs(file_storage_dir, 0750) + except OSError, err: + logger.Error("Cannot create file storage directory '%s': %s" % + (file_storage_dir, err)) + result = False, + return result + + +def RemoveFileStorageDir(file_storage_dir): + """Remove file storage directory. + + Remove it only if it's empty. If not log an error and return. + + Args: + file_storage_dir: string containing the path + + Returns: + tuple with first element a boolean indicating wheter dir + removal was successful or not + + """ + file_storage_dir = _TransformFileStorageDir(file_storage_dir) + result = True, + if not file_storage_dir: + result = False, + else: + if os.path.exists(file_storage_dir): + if not os.path.isdir(file_storage_dir): + logger.Error("'%s' is not a directory" % file_storage_dir) + result = False, + # deletes dir only if empty, otherwise we want to return False + try: + os.rmdir(file_storage_dir) + except OSError, err: + logger.Error("Cannot remove file storage directory '%s': %s" % + (file_storage_dir, err)) + result = False, + return result + + +def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir): + """Rename the file storage directory. + + Args: + old_file_storage_dir: string containing the old path + new_file_storage_dir: string containing the new path + + Returns: + tuple with first element a boolean indicating wheter dir + rename was successful or not + + """ + old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir) + new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir) + result = True, + if not old_file_storage_dir or not new_file_storage_dir: + result = False, + else: + if not os.path.exists(new_file_storage_dir): + if os.path.isdir(old_file_storage_dir): + try: + os.rename(old_file_storage_dir, new_file_storage_dir) + except OSError, err: + logger.Error("Cannot rename '%s' to '%s': %s" + % (old_file_storage_dir, new_file_storage_dir, err)) + result = False, + else: + logger.Error("'%s' is not a directory" % old_file_storage_dir) + result = False, + else: + if os.path.exists(old_file_storage_dir): + logger.Error("Cannot rename '%s' to '%s'. Both locations exist." % + old_file_storage_dir, new_file_storage_dir) + result = False, + return result + + class HooksRunner(object): """Hook runner. -- GitLab