diff options
Diffstat (limited to 'ufo/gluster/swift/common/fs_utils.py')
-rw-r--r-- | ufo/gluster/swift/common/fs_utils.py | 114 |
1 files changed, 67 insertions, 47 deletions
diff --git a/ufo/gluster/swift/common/fs_utils.py b/ufo/gluster/swift/common/fs_utils.py index 88368c78..0613a26d 100644 --- a/ufo/gluster/swift/common/fs_utils.py +++ b/ufo/gluster/swift/common/fs_utils.py @@ -16,38 +16,53 @@ import logging import os import errno +import os.path as os_path +from eventlet import tpool +from gluster.swift.common.exceptions import FileOrDirNotFoundError, \ + NotDirectoryError + +def do_walk(*args, **kwargs): + return os.walk(*args, **kwargs) + +def do_write(fd, msg): + try: + cnt = os.write(fd, msg) + except OSError as err: + logging.exception("Write failed, err: %s", str(err)) + raise + return cnt def do_mkdir(path): try: os.mkdir(path) - except Exception, err: - logging.exception("Mkdir failed on %s err: %s", path, str(err)) + except OSError as err: if err.errno != errno.EEXIST: + logging.exception("Mkdir failed on %s err: %s", path, err.strerror) raise return True def do_makedirs(path): try: os.makedirs(path) - except Exception, err: - logging.exception("Makedirs failed on %s err: %s", path, str(err)) + except OSError as err: if err.errno != errno.EEXIST: + logging.exception("Makedirs failed on %s err: %s", path, err.strerror) raise return True def do_listdir(path): try: buf = os.listdir(path) - except Exception, err: - logging.exception("Listdir failed on %s err: %s", path, str(err)) + except OSError as err: + logging.exception("Listdir failed on %s err: %s", path, err.strerror) raise return buf def do_chown(path, uid, gid): try: os.chown(path, uid, gid) - except Exception, err: - logging.exception("Chown failed on %s err: %s", path, str(err)) + except OSError as err: + logging.exception("Chown failed on %s err: %s", path, err.strerror) raise return True @@ -58,18 +73,24 @@ def do_stat(path): buf = os.fstat(path) else: buf = os.stat(path) - except Exception, err: - logging.exception("Stat failed on %s err: %s", path, str(err)) + except OSError as err: + logging.exception("Stat failed on %s err: %s", path, err.strerror) raise - return buf def do_open(path, mode): - try: - fd = open(path, mode) - except Exception, err: - logging.exception("Open failed on %s err: %s", path, str(err)) - raise + if isinstance(mode, int): + try: + fd = os.open(path, mode) + except OSError as err: + logging.exception("Open failed on %s err: %s", path, str(err)) + raise + else: + try: + fd = open(path, mode) + except IOError as err: + logging.exception("Open failed on %s err: %s", path, str(err)) + raise return fd def do_close(fd): @@ -79,27 +100,27 @@ def do_close(fd): os.close(fd) else: fd.close() - except Exception, err: - logging.exception("Close failed on %s err: %s", fd, str(err)) + except OSError as err: + logging.exception("Close failed on %s err: %s", fd, err.strerror) raise return True def do_unlink(path, log = True): try: os.unlink(path) - except Exception, err: - if log: - logging.exception("Unlink failed on %s err: %s", path, str(err)) + except OSError as err: if err.errno != errno.ENOENT: + if log: + logging.exception("Unlink failed on %s err: %s", path, err.strerror) raise return True def do_rmdir(path): try: os.rmdir(path) - except Exception, err: - logging.exception("Rmdir failed on %s err: %s", path, str(err)) + except OSError as err: if err.errno != errno.ENOENT: + logging.exception("Rmdir failed on %s err: %s", path, err.strerror) raise res = False else: @@ -109,9 +130,9 @@ def do_rmdir(path): def do_rename(old_path, new_path): try: os.rename(old_path, new_path) - except Exception, err: + except OSError as err: logging.exception("Rename failed on %s to %s err: %s", old_path, new_path, \ - str(err)) + err.strerror) raise return True @@ -123,13 +144,7 @@ def mkdirs(path): :param path: path to create """ if not os.path.isdir(path): - try: - do_makedirs(path) - except OSError, err: - #TODO: check, isdir will fail if mounted and volume stopped. - #if err.errno != errno.EEXIST or not os.path.isdir(path) - if err.errno != errno.EEXIST: - raise + do_makedirs(path) def dir_empty(path): """ @@ -138,22 +153,27 @@ def dir_empty(path): :returns: True/False. """ if os.path.isdir(path): - try: - files = do_listdir(path) - except Exception, err: - logging.exception("listdir failed on %s err: %s", path, str(err)) - raise - if not files: - return True - else: - return False - else: - if not os.path.exists(path): - return True + files = do_listdir(path) + return not files + elif not os.path.exists(path): + raise FileOrDirNotFoundError() + raise NotDirectoryError() def rmdirs(path): - if not os.path.isdir(path) or not dir_empty(path): - logging.error("rmdirs failed: %s may not be empty or not valid dir", path) + if not os.path.isdir(path): return False + try: + os.rmdir(path) + except OSError as err: + if err.errno != errno.ENOENT: + logging.error("rmdirs failed on %s, err: %s", path, err.strerror) + return False + return True - return do_rmdir(path) +def do_fsync(fd): + try: + tpool.execute(os.fsync, fd) + except OSError as err: + logging.exception("fsync failed with err: %s", err.strerror) + raise + return True |