Blame Lib/stat.py

rpm-build 2bd099
"""Constants/functions for interpreting results of os.stat() and os.lstat().
rpm-build 2bd099
rpm-build 2bd099
Suggested usage: from stat import *
rpm-build 2bd099
"""
rpm-build 2bd099
rpm-build 2bd099
# Indices for stat struct members in the tuple returned by os.stat()
rpm-build 2bd099
rpm-build 2bd099
ST_MODE  = 0
rpm-build 2bd099
ST_INO   = 1
rpm-build 2bd099
ST_DEV   = 2
rpm-build 2bd099
ST_NLINK = 3
rpm-build 2bd099
ST_UID   = 4
rpm-build 2bd099
ST_GID   = 5
rpm-build 2bd099
ST_SIZE  = 6
rpm-build 2bd099
ST_ATIME = 7
rpm-build 2bd099
ST_MTIME = 8
rpm-build 2bd099
ST_CTIME = 9
rpm-build 2bd099
rpm-build 2bd099
# Extract bits from the mode
rpm-build 2bd099
rpm-build 2bd099
def S_IMODE(mode):
rpm-build 2bd099
    """Return the portion of the file's mode that can be set by
rpm-build 2bd099
    os.chmod().
rpm-build 2bd099
    """
rpm-build 2bd099
    return mode & 0o7777
rpm-build 2bd099
rpm-build 2bd099
def S_IFMT(mode):
rpm-build 2bd099
    """Return the portion of the file's mode that describes the
rpm-build 2bd099
    file type.
rpm-build 2bd099
    """
rpm-build 2bd099
    return mode & 0o170000
rpm-build 2bd099
rpm-build 2bd099
# Constants used as S_IFMT() for various file types
rpm-build 2bd099
# (not all are implemented on all systems)
rpm-build 2bd099
rpm-build 2bd099
S_IFDIR  = 0o040000  # directory
rpm-build 2bd099
S_IFCHR  = 0o020000  # character device
rpm-build 2bd099
S_IFBLK  = 0o060000  # block device
rpm-build 2bd099
S_IFREG  = 0o100000  # regular file
rpm-build 2bd099
S_IFIFO  = 0o010000  # fifo (named pipe)
rpm-build 2bd099
S_IFLNK  = 0o120000  # symbolic link
rpm-build 2bd099
S_IFSOCK = 0o140000  # socket file
rpm-build 2bd099
rpm-build 2bd099
# Functions to test for each file type
rpm-build 2bd099
rpm-build 2bd099
def S_ISDIR(mode):
rpm-build 2bd099
    """Return True if mode is from a directory."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFDIR
rpm-build 2bd099
rpm-build 2bd099
def S_ISCHR(mode):
rpm-build 2bd099
    """Return True if mode is from a character special device file."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFCHR
rpm-build 2bd099
rpm-build 2bd099
def S_ISBLK(mode):
rpm-build 2bd099
    """Return True if mode is from a block special device file."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFBLK
rpm-build 2bd099
rpm-build 2bd099
def S_ISREG(mode):
rpm-build 2bd099
    """Return True if mode is from a regular file."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFREG
rpm-build 2bd099
rpm-build 2bd099
def S_ISFIFO(mode):
rpm-build 2bd099
    """Return True if mode is from a FIFO (named pipe)."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFIFO
rpm-build 2bd099
rpm-build 2bd099
def S_ISLNK(mode):
rpm-build 2bd099
    """Return True if mode is from a symbolic link."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFLNK
rpm-build 2bd099
rpm-build 2bd099
def S_ISSOCK(mode):
rpm-build 2bd099
    """Return True if mode is from a socket."""
rpm-build 2bd099
    return S_IFMT(mode) == S_IFSOCK
rpm-build 2bd099
rpm-build 2bd099
# Names for permission bits
rpm-build 2bd099
rpm-build 2bd099
S_ISUID = 0o4000  # set UID bit
rpm-build 2bd099
S_ISGID = 0o2000  # set GID bit
rpm-build 2bd099
S_ENFMT = S_ISGID # file locking enforcement
rpm-build 2bd099
S_ISVTX = 0o1000  # sticky bit
rpm-build 2bd099
S_IREAD = 0o0400  # Unix V7 synonym for S_IRUSR
rpm-build 2bd099
S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
rpm-build 2bd099
S_IEXEC = 0o0100  # Unix V7 synonym for S_IXUSR
rpm-build 2bd099
S_IRWXU = 0o0700  # mask for owner permissions
rpm-build 2bd099
S_IRUSR = 0o0400  # read by owner
rpm-build 2bd099
S_IWUSR = 0o0200  # write by owner
rpm-build 2bd099
S_IXUSR = 0o0100  # execute by owner
rpm-build 2bd099
S_IRWXG = 0o0070  # mask for group permissions
rpm-build 2bd099
S_IRGRP = 0o0040  # read by group
rpm-build 2bd099
S_IWGRP = 0o0020  # write by group
rpm-build 2bd099
S_IXGRP = 0o0010  # execute by group
rpm-build 2bd099
S_IRWXO = 0o0007  # mask for others (not in group) permissions
rpm-build 2bd099
S_IROTH = 0o0004  # read by others
rpm-build 2bd099
S_IWOTH = 0o0002  # write by others
rpm-build 2bd099
S_IXOTH = 0o0001  # execute by others
rpm-build 2bd099
rpm-build 2bd099
# Names for file flags
rpm-build 2bd099
rpm-build 2bd099
UF_NODUMP    = 0x00000001  # do not dump file
rpm-build 2bd099
UF_IMMUTABLE = 0x00000002  # file may not be changed
rpm-build 2bd099
UF_APPEND    = 0x00000004  # file may only be appended to
rpm-build 2bd099
UF_OPAQUE    = 0x00000008  # directory is opaque when viewed through a union stack
rpm-build 2bd099
UF_NOUNLINK  = 0x00000010  # file may not be renamed or deleted
rpm-build 2bd099
UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
rpm-build 2bd099
UF_HIDDEN    = 0x00008000  # OS X: file should not be displayed
rpm-build 2bd099
SF_ARCHIVED  = 0x00010000  # file may be archived
rpm-build 2bd099
SF_IMMUTABLE = 0x00020000  # file may not be changed
rpm-build 2bd099
SF_APPEND    = 0x00040000  # file may only be appended to
rpm-build 2bd099
SF_NOUNLINK  = 0x00100000  # file may not be renamed or deleted
rpm-build 2bd099
SF_SNAPSHOT  = 0x00200000  # file is a snapshot file
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
_filemode_table = (
rpm-build 2bd099
    ((S_IFLNK,         "l"),
rpm-build 2bd099
     (S_IFREG,         "-"),
rpm-build 2bd099
     (S_IFBLK,         "b"),
rpm-build 2bd099
     (S_IFDIR,         "d"),
rpm-build 2bd099
     (S_IFCHR,         "c"),
rpm-build 2bd099
     (S_IFIFO,         "p")),
rpm-build 2bd099
rpm-build 2bd099
    ((S_IRUSR,         "r"),),
rpm-build 2bd099
    ((S_IWUSR,         "w"),),
rpm-build 2bd099
    ((S_IXUSR|S_ISUID, "s"),
rpm-build 2bd099
     (S_ISUID,         "S"),
rpm-build 2bd099
     (S_IXUSR,         "x")),
rpm-build 2bd099
rpm-build 2bd099
    ((S_IRGRP,         "r"),),
rpm-build 2bd099
    ((S_IWGRP,         "w"),),
rpm-build 2bd099
    ((S_IXGRP|S_ISGID, "s"),
rpm-build 2bd099
     (S_ISGID,         "S"),
rpm-build 2bd099
     (S_IXGRP,         "x")),
rpm-build 2bd099
rpm-build 2bd099
    ((S_IROTH,         "r"),),
rpm-build 2bd099
    ((S_IWOTH,         "w"),),
rpm-build 2bd099
    ((S_IXOTH|S_ISVTX, "t"),
rpm-build 2bd099
     (S_ISVTX,         "T"),
rpm-build 2bd099
     (S_IXOTH,         "x"))
rpm-build 2bd099
)
rpm-build 2bd099
rpm-build 2bd099
def filemode(mode):
rpm-build 2bd099
    """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
rpm-build 2bd099
    perm = []
rpm-build 2bd099
    for table in _filemode_table:
rpm-build 2bd099
        for bit, char in table:
rpm-build 2bd099
            if mode & bit == bit:
rpm-build 2bd099
                perm.append(char)
rpm-build 2bd099
                break
rpm-build 2bd099
        else:
rpm-build 2bd099
            perm.append("-")
rpm-build 2bd099
    return "".join(perm)
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
rpm-build 2bd099
# "st_file_attributes" member
rpm-build 2bd099
rpm-build 2bd099
FILE_ATTRIBUTE_ARCHIVE = 32
rpm-build 2bd099
FILE_ATTRIBUTE_COMPRESSED = 2048
rpm-build 2bd099
FILE_ATTRIBUTE_DEVICE = 64
rpm-build 2bd099
FILE_ATTRIBUTE_DIRECTORY = 16
rpm-build 2bd099
FILE_ATTRIBUTE_ENCRYPTED = 16384
rpm-build 2bd099
FILE_ATTRIBUTE_HIDDEN = 2
rpm-build 2bd099
FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
rpm-build 2bd099
FILE_ATTRIBUTE_NORMAL = 128
rpm-build 2bd099
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
rpm-build 2bd099
FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
rpm-build 2bd099
FILE_ATTRIBUTE_OFFLINE = 4096
rpm-build 2bd099
FILE_ATTRIBUTE_READONLY = 1
rpm-build 2bd099
FILE_ATTRIBUTE_REPARSE_POINT = 1024
rpm-build 2bd099
FILE_ATTRIBUTE_SPARSE_FILE = 512
rpm-build 2bd099
FILE_ATTRIBUTE_SYSTEM = 4
rpm-build 2bd099
FILE_ATTRIBUTE_TEMPORARY = 256
rpm-build 2bd099
FILE_ATTRIBUTE_VIRTUAL = 65536
rpm-build 2bd099
rpm-build 2bd099
rpm-build 2bd099
# If available, use C implementation
rpm-build 2bd099
try:
rpm-build 2bd099
    from _stat import *
rpm-build 2bd099
except ImportError:
rpm-build 2bd099
    pass