def abssymlink(path): """Return the absolute path of a symlink :param path: filepath to resolve :return: resolved path :raise: EnvironmentError, errno=ENINVAL if the requested path isn't a symlink """ mylink = os.readlink(path) if mylink[0] != '/': mydir = os.path.dirname(path) mylink = mydir + '/' + mylink return normpath(mylink)
def abssymlink(path): """ Return the absolute path of a symlink :param path: filepath to resolve :return: resolved path :raise: EnvironmentError, errno=ENINVAL if the requested path isn't a symlink """ mylink = os.readlink(path) if mylink[0] != '/': mydir = os.path.dirname(path) mylink = mydir + '/' + mylink return normpath(mylink)
if mode=0755, minimal=True, and a directory exists with mode 0707, this will restore the missing group perms resulting in 757. :return: True if the directory could be created/ensured to have those permissions, False if not. """ try: st = os.stat(path) except OSError: base = os.path.sep try: um = os.umask(0) # if the dir perms would lack +wx, we have to force it force_temp_perms = ((mode & 0300) != 0300) resets = [] apath = normpath(os.path.abspath(path)) sticky_parent = False for directory in apath.split(os.path.sep): base = join(base, directory) try: st = os.stat(base) if not stat.S_ISDIR(st.st_mode): return False # if it's a subdir, we need +wx at least if apath != base: if (st.st_mode & 0300) != 0300: try: os.chmod(base, (st.st_mode | 0300)) except OSError:
if mode=0755, minimal=True, and a directory exists with mode 0707, this will restore the missing group perms resulting in 757. :return: True if the directory could be created/ensured to have those permissions, False if not. """ try: st = os.stat(path) except OSError: base = os.path.sep try: um = os.umask(0) # if the dir perms would lack +wx, we have to force it force_temp_perms = ((mode & 0300) != 0300) resets = [] apath = normpath(os.path.abspath(path)) sticky_parent = False for directory in apath.split(os.path.sep): base = join(base, directory) try: st = os.stat(base) if not stat.S_ISDIR(st.st_mode): return False # if it's a subdir, we need +wx at least if apath != base: if ((st.st_mode & 0300) != 0300): try: os.chmod(base, (st.st_mode | 0300)) except OSError:
def ensure_dirs(path, gid=-1, uid=-1, mode=0o777, minimal=True): """ ensure dirs exist, creating as needed with (optional) gid, uid, and mode. be forewarned- if mode is specified to a mode that blocks the euid from accessing the dir, this code *will* try to create the dir. :param path: directory to ensure exists on disk :param gid: a valid GID to set any created directories to :param uid: a valid UID to set any created directories to :param mode: permissions to set any created directories to :param minimal: boolean controlling whether or not the specified mode must be enforced, or is the minimal permissions necessary. For example, if mode=0755, minimal=True, and a directory exists with mode 0707, this will restore the missing group perms resulting in 757. :return: True if the directory could be created/ensured to have those permissions, False if not. """ try: st = os.stat(path) except OSError: try: um = os.umask(0) # if the dir perms would lack +wx, we have to force it force_temp_perms = ((mode & 0o300) != 0o300) resets = [] apath = normpath(os.path.abspath(path)) sticky_parent = False base = os.path.sep # XXX: wonky way to get filesystem root... prev_path = None prev_mode = None for directory in apath.split(os.path.sep): base = join(base, directory) try: st = os.stat(base) if not stat.S_ISDIR(st.st_mode): return False prev_mode = st.st_mode # if it's a subdir, we need +x at least if apath != base: if (st.st_mode & 0o300) != 0o300: ''' try: os.chmod(base, (st.st_mode | 0o300)) except OSError: return False resets.append((base, st.st_mode)) ''' sticky_parent = (st.st_gid & stat.S_ISGID) except OSError: # nothing exists. if prev_mode & 0300 != 0300: # make sure parent allows for node creation try: os.chmod(prev_path, prev_mode | 0300) resets.append((prev_path, prev_mode)) except OSError: return False try: if force_temp_perms: if not _safe_mkdir(base, 0o700): return False resets.append((base, mode)) else: if not _safe_mkdir(base, mode): return False if base == apath and sticky_parent: resets.append((base, mode)) if gid != -1 or uid != -1: os.chown(base, uid, gid) except OSError: return False prev_path = base try: for base, m in reversed(resets): os.chmod(base, m) if uid != -1 or gid != -1: os.chown(base, uid, gid) except OSError: return False finally: os.umask(um) return True
def ensure_dirs(path, gid=-1, uid=-1, mode=0o777, minimal=True): """ ensure dirs exist, creating as needed with (optional) gid, uid, and mode. be forewarned- if mode is specified to a mode that blocks the euid from accessing the dir, this code *will* try to create the dir. :param path: directory to ensure exists on disk :param gid: a valid GID to set any created directories to :param uid: a valid UID to set any created directories to :param mode: permissions to set any created directories to :param minimal: boolean controlling whether or not the specified mode must be enforced, or is the minimal permissions necessary. For example, if mode=0755, minimal=True, and a directory exists with mode 0707, this will restore the missing group perms resulting in 757. :return: True if the directory could be created/ensured to have those permissions, False if not. """ try: st = os.stat(path) except OSError: base = os.path.sep try: um = os.umask(0) # if the dir perms would lack +wx, we have to force it force_temp_perms = ((mode & 0o300) != 0o300) resets = [] apath = normpath(os.path.abspath(path)) sticky_parent = False for directory in apath.split(os.path.sep): base = join(base, directory) try: st = os.stat(base) if not stat.S_ISDIR(st.st_mode): # one of the path components isn't a dir return False # if it's a subdir, we need +wx at least if apath != base: sticky_parent = (st.st_mode & stat.S_ISGID) except OSError: # nothing exists. try: if force_temp_perms: if not _safe_mkdir(base, 0o700): return False resets.append((base, mode)) else: if not _safe_mkdir(base, mode): return False if base == apath and sticky_parent: resets.append((base, mode)) if gid != -1 or uid != -1: os.chown(base, uid, gid) except OSError: return False try: for base, m in reversed(resets): os.chmod(base, m) if gid != -1 or uid != -1: os.chown(base, uid, gid) except OSError: return False finally: os.umask(um) return True else: if not os.path.isdir(path): # don't change perms for existing paths that aren't dirs return False try: if ((gid != -1 and gid != st.st_gid) or (uid != -1 and uid != st.st_uid)): os.chown(path, uid, gid) if minimal: if mode != (st.st_mode & mode): os.chmod(path, st.st_mode | mode) elif mode != (st.st_mode & 0o7777): os.chmod(path, mode) except OSError: return False return True