Exemplo 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'
Exemplo 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)
Exemplo 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"
Exemplo 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
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'
Exemplo n.º 6
0
def filepath_to_fileobject(filepath, args):
    fobj = Objects.FileObject()

    #Determine type - done in three steps.
    if os.path.islink(filepath):
        fobj.name_type = "l"
    elif os.path.isdir(filepath):
        fobj.name_type = "d"
    elif os.path.isfile(filepath):
        fobj.name_type = "r"
    else:
        #Need to finish type determinations with stat structure.
        pass

    #Prime fileobjects from Stat data (lstat for soft links).
    if fobj.name_type == "l":
        sobj = os.lstat(filepath)
    else:
        sobj = os.stat(filepath)
    #_logger.debug(sobj)
    fobj.populate_from_stat(sobj)

    if fobj.name_type is None:
        if stat.S_ISCHR(fobj.mode):
            fobj.name_type = "c"
        elif stat.S_ISBLK(fobj.mode):
            fobj.name_type = "b"
        elif stat.S_ISFIFO(fobj.mode):
            fobj.name_type = "p"
        elif stat.S_ISSOCK(fobj.mode):
            fobj.name_type = "s"
        elif stat.S_ISWHT(fobj.mode):
            fobj.name_type = "w"
        else:
            raise NotImplementedError(
                "No reporting check written for file type of %r." % filepath)

    #Hard-coded information: Name, and assumed allocation status.
    fobj.filename = filepath
    fobj.alloc = True

    if fobj.name_type == "l":
        fobj.link_target = os.readlink(filepath)
    if not args.n:
        #Add hashes for regular files.
        if fobj.name_type == "r":
            try:
                with open(filepath, "rb") as in_fh:
                    chunk_size = 2**22
                    md5obj = hashlib.md5()
                    sha512obj = hashlib.sha512()
                    any_error = False
                    while True:
                        buf = b""
                        try:
                            buf = in_fh.read(chunk_size)
                        except Exception as e:
                            any_error = True
                            fobj.error = "".join(traceback.format_stack())
                            if e.args:
                                fobj.error += "\n" + str(e.args)
                            buf = b""
                        if buf == b"":
                            break

                        md5obj.update(buf)
                        sha512obj.update(buf)

                    if not any_error:
                        fobj.md5 = md5obj.hexdigest()
                        fobj.sha512 = sha512obj.hexdigest()
            except Exception as e:
                if fobj.error is None:
                    fobj.error = ""
                else:
                    fobj.error += "\n"
                fobj.error += "".join(traceback.format_stack())
                if e.args:
                    fobj.error += "\n" + str(e.args)
    return fobj
def filepath_to_fileobject(filepath, **kwargs):
    """
    Optional arguments:
    * ignore_properties - dictionary of property names to exclude from FileObject.
    """
    global walk_default_hashes
    fobj = Objects.FileObject()

    ignore_properties = kwargs.get("ignore_properties", dict())
    #_logger.debug("ignore_properties = %r." % ignore_properties)

    #Determine type - done in three steps.
    if os.path.islink(filepath):
        name_type = "l"
    elif os.path.isdir(filepath):
        name_type = "d"
    elif os.path.isfile(filepath):
        name_type = "r"
    else:
        #Nop. Need to finish type determinations with stat structure.
        name_type = None

    # Retrieve stat struct for file to finish determining name type, and later to populate properties.
    if name_type == "l":
        sobj = os.lstat(filepath)
    else:
        sobj = os.stat(filepath)
    #_logger.debug(sobj)

    if name_type is None:
        if stat.S_ISCHR(sobj.st_mode):
            name_type = "c"
        elif stat.S_ISBLK(sobj.st_mode):
            name_type = "b"
        elif stat.S_ISFIFO(sobj.st_mode):
            name_type = "p"
        elif stat.S_ISSOCK(sobj.st_mode):
            name_type = "s"
        elif stat.S_ISWHT(sobj.st_mode):
            name_type = "w"
        else:
            raise NotImplementedError(
                "No reporting check written for file type of %r." % filepath)

    _should_ignore = lambda x: Objects.FileObject._should_ignore_property(
        ignore_properties, name_type, x)

    if not _should_ignore("name_type"):
        fobj.name_type = name_type

    #Prime fileobjects from Stat data (lstat for soft links).
    fobj.populate_from_stat(sobj,
                            ignore_properties=ignore_properties,
                            name_type=name_type)

    #Hard-coded information: Name, and assumed allocation status.
    if not _should_ignore("filename"):
        fobj.filename = filepath
    if not _should_ignore("alloc"):
        fobj.alloc = True

    if not _should_ignore("link_target"):
        if name_type == "l":
            fobj.link_target = os.readlink(filepath)

    #Add hashes for (mostly regular) files.
    if name_type in ["-", "r", "v"]:
        # Check total OR
        if functools.reduce(
                lambda y, z: y or z,
                map(lambda x: not _should_ignore(x), walk_default_hashes)):
            try:
                with open(filepath, "rb") as in_fh:
                    chunk_size = 2**22
                    md5obj = hashlib.md5()
                    sha1obj = hashlib.sha1()
                    sha224obj = hashlib.sha224()
                    sha256obj = hashlib.sha256()
                    sha384obj = hashlib.sha384()
                    sha512obj = hashlib.sha512()
                    any_error = False
                    while True:
                        buf = b""
                        try:
                            buf = in_fh.read(chunk_size)
                        except Exception as e:
                            any_error = True
                            if not _should_ignore("error"):
                                fobj.error = "".join(traceback.format_stack())
                                if e.args:
                                    fobj.error += "\n" + str(e.args)
                            buf = b""
                        if buf == b"":
                            break

                        if not _should_ignore("md5"):
                            md5obj.update(buf)
                        if not _should_ignore("sha1"):
                            sha1obj.update(buf)
                        if not _should_ignore("sha224"):
                            sha224obj.update(buf)
                        if not _should_ignore("sha256"):
                            sha256obj.update(buf)
                        if not _should_ignore("sha384"):
                            sha384obj.update(buf)
                        if not _should_ignore("sha512"):
                            sha512obj.update(buf)

                    if not any_error:
                        if not _should_ignore("md5"):
                            fobj.md5 = md5obj.hexdigest()
                        if not _should_ignore("sha1"):
                            fobj.sha1 = sha1obj.hexdigest()
                        if not _should_ignore("sha224"):
                            fobj.sha224 = sha224obj.hexdigest()
                        if not _should_ignore("sha256"):
                            fobj.sha256 = sha256obj.hexdigest()
                        if not _should_ignore("sha384"):
                            fobj.sha384 = sha384obj.hexdigest()
                        if not _should_ignore("sha512"):
                            fobj.sha512 = sha512obj.hexdigest()
            except Exception as e:
                if not _should_ignore("error"):
                    if fobj.error is None:
                        fobj.error = ""
                    else:
                        fobj.error += "\n"
                    fobj.error += "".join(traceback.format_stack())
                    if e.args:
                        fobj.error += "\n" + str(e.args)
    return fobj