Exemplo n.º 1
0
def getfilesec(filepath):

    header = ''

    if path.isfile(filepath):
        try:
            with open(filepath) as fileobj:
                header = fileobj.read(4096)
        except IOError:
            pass

    filestat = stat(filepath)
    owner, group, acls = getfileowneracls(filepath)
    streams = has_xattrs(filepath)
    link = None

    try:
        if islink(filepath):
            link = readlink(filepath)
    except (WindowsError, ValueError, OSError, IOError):
        pass

    mode = mode_to_letter(filestat.st_mode)

    extras = {
        'ACLs': [unicode(x) for x in acls] if acls else None,
        'Streams': streams,
        'Link': link
    }

    return int(filestat.st_ctime), int(filestat.st_atime), \
      int(filestat.st_mtime), filestat.st_size, owner, group, \
      header, mode, {k:v for k,v in extras.iteritems() if v}
Exemplo n.º 2
0
def _stat_to_ls_struct(path, name, _stat):
    if stat.S_ISLNK(_stat.st_mode):
        try:
            name += ' -> ' + readlink(path)
        except:
            pass

    return {
        T_NAME: name,
        T_TYPE: mode_to_letter(_stat.st_mode),
        T_SPEC: special_to_letter(_stat.st_mode),
        T_MODE: _stat.st_mode,
        T_UID: _stat.st_uid,
        T_GID: _stat.st_gid,
        T_SIZE: _stat.st_size,
        T_TIMESTAMP: int(_stat.st_mtime),
    }
Exemplo n.º 3
0
def getfilesec(filepath):
    header = ''

    filepath = path.expanduser(filepath)
    filepath = path.expandvars(filepath)

    if path.isfile(filepath):
        try:
            with open(filepath) as fileobj:
                header = fileobj.read(4096)
        except (OSError, IOError):
            pass

    try:
        filestat = stat(filepath)
        owner, group, acls = getfileowneracls(filepath)
        streams = has_xattrs(filepath)
        link = None
    except Exception as e:
        try_exc_utf8(e)
        raise

    try:
        if islink(filepath):
            link = readlink(filepath)
    except (WindowsError, ValueError, OSError, IOError):
        pass

    mode = mode_to_letter(filestat.st_mode)

    extras = {
        'ACLs': [unicode(x) for x in acls] if acls else None,
        'Streams': streams,
        'Link': link,
        'Version': getfilever(filepath),
    }

    certs = getfilecert(filepath)
    if certs:
        extras.update(certs)
        extras.update(getfiletrust(filepath))

    return int(filestat.st_ctime), int(filestat.st_atime), \
      int(filestat.st_mtime), filestat.st_size, owner, group, \
      header, mode, {k:v for k,v in extras.iteritems() if v}
Exemplo n.º 4
0
                try:
                    is_file = entry.is_file(
                        follow_symlinks=self.follow_symlinks)
                except OSError, e:
                    logger.debug('_walk_scandir:is_file: %s', e)
                    pass

            if not is_dir and not is_file and not is_symlink:
                try:
                    is_symlink = entry.is_symlink()
                except:
                    pass

            if is_symlink:
                try:
                    linked_to = readlink(name)
                    symlinks.append((entry, linked_to))
                except (IOError, OSError):
                    pass

            elif is_dir:
                dirs.append(entry)
            elif is_file:
                hardlinked = False
                try:
                    estat = entry.stat()
                    if estat.st_nlink > 1:
                        inode = estat.st_ino
                        if inode in dups:
                            hardlinks.append((entry, dups[inode]))
                            hardlinked = True
Exemplo n.º 5
0
    def _walk_scandir(self, top, dups={}):
        dirs = []
        files = []
        symlinks = []
        hardlinks = []
        special = []

        if self.single_device:
            try:
                topstat = stat(top)
            except OSError:
                return

            if self.single_device is True:
                self.single_device = topstat.st_dev
            elif self.single_device != topstat.st_dev:
                return

        try:
            scandir_it = scandir(top)
        except OSError as error:
            return

        while not self._terminate.is_set():
            try:
                try:
                    entry = next(scandir_it)

                except UnicodeDecodeError:
                    ## ???
                    continue

                except StopIteration:
                    break


            except OSError as error:
                return

            name = entry.path

            if self.exclude:
                if self.include and not self.include.match(name) and self.exclude.match(name):
                    continue
                elif self.exclude.match(name):
                    continue
            elif self.include and not self.include.match(name):
                continue

            is_dir = False
            is_file = False
            is_symlink = False
            is_special = False

            if not self.follow_symlinks:
                try:
                    is_symlink = entry.is_symlink()

                    if not is_symlink and sys.platform == 'win32' and entry.is_dir(follow_symlinks=False):
                        is_symlink = islink(entry.path)
                except OSError:
                    pass

            if not is_symlink:
                try:
                    is_dir = entry.is_dir(follow_symlinks=self.follow_symlinks)
                except OSError:
                    pass

            if not is_dir and not is_symlink:
                try:
                    is_file = entry.is_file(follow_symlinks=self.follow_symlinks)
                except OSError:
                    pass

            if not is_dir and not is_file and not is_symlink:
                try:
                    is_symlink = entry.is_symlink()
                except:
                    pass

            if is_symlink:
                try:
                    linked_to = readlink(name)
                    symlinks.append((entry, linked_to))
                except (IOError, OSError):
                    pass
            elif is_dir:
                dirs.append(entry)
            elif is_file:
                hardlinked = False
                try:
                    estat = entry.stat()
                    if estat.st_nlink > 1:
                        inode = estat.st_ino
                        if inode in dups:
                            hardlinks.append((entry, dups[inode]))
                            hardlinked = True
                        else:
                            dups[inode] = name

                    if not hardlinked:
                        files.append(entry)

                except OSError:
                    pass
            else:
                special.append(entry)

        yield top, dirs, files, symlinks, hardlinks, special

        dirpaths = [ entry.name for entry in dirs ]

        del dirs[:], files[:], symlinks[:], hardlinks[:], special[:]

        for direntry in dirpaths:
            new_path = path.join(top, direntry)
            if self.follow_symlinks or not islink(new_path):
                for entry in self._walk_scandir(new_path, dups):
                    yield entry
                    del entry