Esempio n. 1
0
    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'
Esempio n. 2
0
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)
Esempio n. 3
0
    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"
Esempio n. 4
0
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
Esempio n. 5
0
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'