def file_mode_detector(path): mode = path.stat().st_mode if stat.S_ISDIR(mode): return 'directory' elif stat.S_ISCHR(mode): return 'special file' elif stat.S_ISBLK(mode): return 'block special' elif stat.S_ISREG(mode): if path.stat().st_size == 0: return 'regular empty file' else: return 'regular file' elif stat.S_ISFIFO(mode): return 'FIFO/pipe' elif stat.S_ISLNK(mode): return 'symbolic link' elif stat.S_ISSOCK(mode): return 'socket' elif stat.S_ISDOOR(mode): return 'door' elif stat.S_ISPORT(mode): return 'event port' elif stat.S_ISWHT(mode): return 'whiteout'
def _get_file_info(info): st = info.stat(follow_symlinks=False) if stat.S_ISSOCK(st.st_mode): what = "socket" elif stat.S_ISLNK(st.st_mode): what = "symbolic link" if os.path.exists(info.path) else "broken symbolic link" elif stat.S_ISREG(st.st_mode): what = "regular file" elif stat.S_ISBLK(st.st_mode): what = "block special device" elif stat.S_ISDIR(st.st_mode): what = "directory" elif stat.S_ISCHR(st.st_mode): what = "character special device" elif stat.S_ISIFO(st.st_mode): what = "FIFO" elif stat.S_ISDOOR(st.st_mode): what = "door" elif stat.S_ISPORT(st.st_mode): what = "event port" elif stat.S_ISWHT(st.st_mode): what = "whiteout" else: what = "unknown" return (what, st.st_size)
def __init__(self, start_directory, relpath, status, ignore_paths=None): # pylint:disable=too-many-branches self.start_directory = start_directory self.relpath = relpath self.status = status self.ignore_paths = ignore_paths self.root = self.start_directory.root self.path = join(self.root, self.relpath) if self.root != "." else self.relpath self.abspath = join(self.start_directory.absroot, self.relpath) self.dir, self.name = split(self.path) # Collect information early to avoid having OSErrors later on. if stat.S_ISLNK(self.status.st_mode): self.link = os.readlink(self.path) self.target = os.path.realpath(join(self.dir, self.link)) self.broken = not os.path.exists(self.target) else: # Don't set link and target so that KeyError is raised in get_attribute(). self.broken = False self.hide = self.name[0] == "." self.mode = self.status.st_mode if stat.S_ISDIR(self.mode): self.type = "directory" elif stat.S_ISREG(self.mode): self.type = "file" elif stat.S_ISLNK(self.mode): self.type = "symlink" elif stat.S_ISSOCK(self.mode): self.type = "socket" elif stat.S_ISFIFO(self.mode): self.type = "fifo" elif stat.S_ISCHR(self.mode): self.type = "char" elif stat.S_ISBLK(self.mode): self.type = "block" elif stat.S_ISDOOR(self.mode): self.type = "door" elif stat.S_ISPORT(self.mode): self.type = "port" elif stat.S_ISWHT(self.mode): self.type = "whiteout" else: self.type = "other"
def classify_direntry(path): """Annotate a directory entry with type information, akin to what GNU ls -F does.""" st = os.lstat(path) mode = st.st_mode perms = stat.S_IMODE(mode) if stat.S_ISREG(mode): if perms & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH): suffix = "*" else: suffix = "" if perms & stat.S_ISUID: suffix += " !suid!" elif perms & stat.S_ISGID: suffix += " !sgid!" elif stat.S_ISDIR(mode): suffix = "/" elif stat.S_ISLNK(mode): dest = os.readlink(path) try: dest = classify_direntry(os.path.join(os.path.dirname(path), dest)) suffix = " => " + dest except OSError: # broken symlink suffix = " =/> " + shlex.quote(dest) else: suffix = " % " if stat.S_ISBLK(mode): suffix += "blockdev" elif stat.S_ISCHR(mode): suffix += "chardev" elif stat.S_ISDOOR(mode): suffix += "door" elif stat.S_ISFIFO(mode): suffix += "fifo" elif stat.S_ISPORT(mode): suffix += "evport" elif stat.S_ISSOCK(mode): suffix += "sock" elif stat.S_ISWHT(mode): suffix += "whiteout" else: suffix += "unknown" return shlex.quote(path) + suffix
def get_attributes(path): st = os.stat(path) return Attributes( st.st_size, os.access(path, os.X_OK), stat.filemode(st.st_mode), os.path.islink(path), stat.S_ISFIFO(st.st_mode), stat.S_ISDOOR(st.st_mode), stat.S_ISSOCK(st.st_mode), st.st_nlink, st.st_uid, st.st_gid, st.st_mtime, )
def filetypeToString(mode): if stat.S_ISDIR(mode): return "directory" elif stat.S_ISCHR(mode): return "character device" elif stat.S_ISBLK(mode): return "block device" elif stat.S_ISREG(mode): return "regular file" elif stat.S_ISFIFO(mode): return "FIFO (named pipe)" elif stat.S_ISLNK(mode): return "symbolic link" elif stat.S_ISDOOR(mode): return "door" elif stat.S_ISPORT(mode): return "event port" else: return "unknown"
def fileType(file): if stat.S_ISDIR(file) != 0: return 'directory' elif stat.S_ISCHR(file) != 0: return 'special file' elif stat.S_ISBLK(file) != 0: return 'block special file' elif stat.S_ISREG(file) != 0: return 'regular file' elif stat.S_ISFIFO(file) != 0: return 'pipe' elif stat.S_ISLNK(file) != 0: return 'symbolic link' elif stat.S_ISSOCK(file) != 0: return 'socket' elif stat.S_ISDOOR(file) != 0: return 'door' elif stat.S_ISPORT(file) != 0: return 'event port' elif stat.S_ISWHT(file) != 0: return 'whiteout'
file descriptors on stdout. Usage: fd_stats.py: check all file descriptors fd_status.py fd1 fd2 ...: check only specified file descriptors """ import errno import os import stat import sys if __name__ == '__main__': fds = [] if len(sys.argv) == 1: try: _MAXFD = os.sysconf('SC_OPEN_MAX') except: _MAXFD = 256 test_fds = range(0, _MAXFD) else: test_fds = map(int, sys.argv[1:]) for fd in test_fds: try: st = os.fstat(fd) except OSError as e: if e.errno == errno.EBADF: continue raise if not stat.S_ISDOOR(st.st_mode): fds.append(fd) print(','.join(map(str, fds)))
def color_file(file_path: str, path_stat: os.stat_result) -> (Color, str): """Determine color to use for file *approximately* as ls --color would, given lstat() results and its path. Parameters ---------- file_path: relative path of file (as user typed it). path_stat: lstat() results for file_path. Returns ------- color token, color_key Notes ----- * implementation follows one authority: https://github.com/coreutils/coreutils/blob/master/src/ls.c#L4879 * except: 1. does not return 'mi'. That's the color ls uses to show the (missing) *target* of a symlink (in ls -l, not ls). 2. in dircolors, setting type code to '0 or '00' bypasses that test and proceeds to others. In our implementation, setting code to '00' paints the file with no color. This is arguably a bug. """ lsc = builtins.__xonsh__.env["LS_COLORS"] color_key = "fi" # if symlink, get info on (final) target if stat.S_ISLNK(path_stat.st_mode): try: tar_path_stat = os.stat(file_path) # and work with its properties if lsc.is_target("ln"): # if ln=target path_stat = tar_path_stat except FileNotFoundError: # bug always color broken link 'or' color_key = "or" # early exit ret_color_token = file_color_tokens.get(color_key, Text) return ret_color_token, color_key mode = path_stat.st_mode if stat.S_ISREG(mode): if mode & stat.S_ISUID: color_key = "su" elif mode & stat.S_ISGID: color_key = "sg" else: cap = os_listxattr(file_path, follow_symlinks=False) if cap and "security.capability" in cap: # protect None return on some OS? color_key = "ca" elif stat.S_IMODE(mode) & (stat.S_IXUSR + stat.S_IXGRP + stat.S_IXOTH): color_key = "ex" elif path_stat.st_nlink > 1: color_key = "mh" else: color_key = "fi" elif stat.S_ISDIR( mode): # ls --color doesn't colorize sticky or ow if not dirs... color_key = "di" if not (ON_WINDOWS ): # on Windows, these do not mean what you think they mean. if (mode & stat.S_ISVTX) and (mode & stat.S_IWOTH): color_key = "tw" elif mode & stat.S_IWOTH: color_key = "ow" elif mode & stat.S_ISVTX: color_key = "st" elif stat.S_ISLNK(mode): color_key = "ln" elif stat.S_ISFIFO(mode): color_key = "pi" elif stat.S_ISSOCK(mode): color_key = "so" elif stat.S_ISBLK(mode): color_key = "bd" elif stat.S_ISCHR(mode): color_key = "cd" elif stat.S_ISDOOR(mode): color_key = "do" else: color_key = "or" # any other type --> orphan # if still normal file -- try color by file extension. # note: symlink to *.<ext> will be colored 'fi' unless the symlink itself # ends with .<ext>. `ls` does the same. Bug-for-bug compatibility! if color_key == "fi": match = color_file_extension_RE.match(file_path) if match: ext = "*" + match.group(1) # look for *.<fileExtension> coloring if ext in lsc: color_key = ext ret_color_token = file_color_tokens.get(color_key, Text) return ret_color_token, color_key
def color_file(file_path: str, mode: int) -> (Color, str): """Determine color to use for file as ls -c would, given stat() results and its name. Parameters ---------- file_path : string relative path of file (as user typed it). mode : int stat() results for file_path. Returns ------- color token, color_key Notes ----- * doesn't handle CA (capability) * doesn't handle LS TARGET mapping """ lsc = builtins.__xonsh__.env["LS_COLORS"] color_key = "rs" if stat.S_ISLNK(mode): # must test link before S_ISREG (esp execute) color_key = "ln" try: os.stat(file_path) except FileNotFoundError: color_key = "or" elif stat.S_ISREG(mode): if stat.S_IMODE(mode) & (stat.S_IXUSR + stat.S_IXGRP + stat.S_IXOTH): color_key = "ex" elif ( mode & stat.S_ISUID ): # too many tests before we get to the common case -- restructure? color_key = "su" elif mode & stat.S_ISGID: color_key = "sg" else: match = color_file_extension_RE.match(file_path) if match: ext = "*" + match.group( 1) # look for *.<fileExtension> coloring if ext in lsc: color_key = ext else: color_key = "rs" else: color_key = "rs" elif stat.S_ISDIR( mode): # ls -c doesn't colorize sticky or ow if not dirs... color_key = ("di", "ow", "st", "tw")[(mode & stat.S_ISVTX == stat.S_ISVTX) * 2 + (mode & stat.S_IWOTH == stat.S_IWOTH)] elif stat.S_ISCHR(mode): color_key = "cd" elif stat.S_ISBLK(mode): color_key = "bd" elif stat.S_ISFIFO(mode): color_key = "pi" elif stat.S_ISSOCK(mode): color_key = "so" elif stat.S_ISDOOR(mode): color_key = "do" # bug missing mapping for FMT based PORT and WHITEOUT ?? ret_color_token = file_color_tokens.get(color_key, Text) return ret_color_token, color_key
fd_stats.py: check all file descriptors fd_status.py fd1 fd2 ...: check only specified file descriptors """ import errno import os import stat import sys if __name__ == "__main__": fds = [] if len(sys.argv) == 1: try: _MAXFD = os.sysconf("SC_OPEN_MAX") except: _MAXFD = 256 test_fds = range(0, _MAXFD) else: test_fds = map(int, sys.argv[1:]) for fd in test_fds: try: st = os.fstat(fd) except OSError, e: if e.errno == errno.EBADF: continue raise # Ignore Solaris door files if not hasattr(stat, 'S_ISDOOR') or not stat.S_ISDOOR(st.st_mode): fds.append(fd) print ','.join(map(str, fds))