Home | History | Annotate | Download | only in Lib
      1 """Constants/functions for interpreting results of os.stat() and os.lstat().
      2 
      3 Suggested usage: from stat import *
      4 """
      5 
      6 # Indices for stat struct members in the tuple returned by os.stat()
      7 
      8 ST_MODE  = 0
      9 ST_INO   = 1
     10 ST_DEV   = 2
     11 ST_NLINK = 3
     12 ST_UID   = 4
     13 ST_GID   = 5
     14 ST_SIZE  = 6
     15 ST_ATIME = 7
     16 ST_MTIME = 8
     17 ST_CTIME = 9
     18 
     19 # Extract bits from the mode
     20 
     21 def S_IMODE(mode):
     22     """Return the portion of the file's mode that can be set by
     23     os.chmod().
     24     """
     25     return mode & 0o7777
     26 
     27 def S_IFMT(mode):
     28     """Return the portion of the file's mode that describes the
     29     file type.
     30     """
     31     return mode & 0o170000
     32 
     33 # Constants used as S_IFMT() for various file types
     34 # (not all are implemented on all systems)
     35 
     36 S_IFDIR  = 0o040000  # directory
     37 S_IFCHR  = 0o020000  # character device
     38 S_IFBLK  = 0o060000  # block device
     39 S_IFREG  = 0o100000  # regular file
     40 S_IFIFO  = 0o010000  # fifo (named pipe)
     41 S_IFLNK  = 0o120000  # symbolic link
     42 S_IFSOCK = 0o140000  # socket file
     43 
     44 # Functions to test for each file type
     45 
     46 def S_ISDIR(mode):
     47     """Return True if mode is from a directory."""
     48     return S_IFMT(mode) == S_IFDIR
     49 
     50 def S_ISCHR(mode):
     51     """Return True if mode is from a character special device file."""
     52     return S_IFMT(mode) == S_IFCHR
     53 
     54 def S_ISBLK(mode):
     55     """Return True if mode is from a block special device file."""
     56     return S_IFMT(mode) == S_IFBLK
     57 
     58 def S_ISREG(mode):
     59     """Return True if mode is from a regular file."""
     60     return S_IFMT(mode) == S_IFREG
     61 
     62 def S_ISFIFO(mode):
     63     """Return True if mode is from a FIFO (named pipe)."""
     64     return S_IFMT(mode) == S_IFIFO
     65 
     66 def S_ISLNK(mode):
     67     """Return True if mode is from a symbolic link."""
     68     return S_IFMT(mode) == S_IFLNK
     69 
     70 def S_ISSOCK(mode):
     71     """Return True if mode is from a socket."""
     72     return S_IFMT(mode) == S_IFSOCK
     73 
     74 # Names for permission bits
     75 
     76 S_ISUID = 0o4000  # set UID bit
     77 S_ISGID = 0o2000  # set GID bit
     78 S_ENFMT = S_ISGID # file locking enforcement
     79 S_ISVTX = 0o1000  # sticky bit
     80 S_IREAD = 0o0400  # Unix V7 synonym for S_IRUSR
     81 S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
     82 S_IEXEC = 0o0100  # Unix V7 synonym for S_IXUSR
     83 S_IRWXU = 0o0700  # mask for owner permissions
     84 S_IRUSR = 0o0400  # read by owner
     85 S_IWUSR = 0o0200  # write by owner
     86 S_IXUSR = 0o0100  # execute by owner
     87 S_IRWXG = 0o0070  # mask for group permissions
     88 S_IRGRP = 0o0040  # read by group
     89 S_IWGRP = 0o0020  # write by group
     90 S_IXGRP = 0o0010  # execute by group
     91 S_IRWXO = 0o0007  # mask for others (not in group) permissions
     92 S_IROTH = 0o0004  # read by others
     93 S_IWOTH = 0o0002  # write by others
     94 S_IXOTH = 0o0001  # execute by others
     95 
     96 # Names for file flags
     97 
     98 UF_NODUMP    = 0x00000001  # do not dump file
     99 UF_IMMUTABLE = 0x00000002  # file may not be changed
    100 UF_APPEND    = 0x00000004  # file may only be appended to
    101 UF_OPAQUE    = 0x00000008  # directory is opaque when viewed through a union stack
    102 UF_NOUNLINK  = 0x00000010  # file may not be renamed or deleted
    103 UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
    104 UF_HIDDEN    = 0x00008000  # OS X: file should not be displayed
    105 SF_ARCHIVED  = 0x00010000  # file may be archived
    106 SF_IMMUTABLE = 0x00020000  # file may not be changed
    107 SF_APPEND    = 0x00040000  # file may only be appended to
    108 SF_NOUNLINK  = 0x00100000  # file may not be renamed or deleted
    109 SF_SNAPSHOT  = 0x00200000  # file is a snapshot file
    110 
    111 
    112 _filemode_table = (
    113     ((S_IFLNK,         "l"),
    114      (S_IFREG,         "-"),
    115      (S_IFBLK,         "b"),
    116      (S_IFDIR,         "d"),
    117      (S_IFCHR,         "c"),
    118      (S_IFIFO,         "p")),
    119 
    120     ((S_IRUSR,         "r"),),
    121     ((S_IWUSR,         "w"),),
    122     ((S_IXUSR|S_ISUID, "s"),
    123      (S_ISUID,         "S"),
    124      (S_IXUSR,         "x")),
    125 
    126     ((S_IRGRP,         "r"),),
    127     ((S_IWGRP,         "w"),),
    128     ((S_IXGRP|S_ISGID, "s"),
    129      (S_ISGID,         "S"),
    130      (S_IXGRP,         "x")),
    131 
    132     ((S_IROTH,         "r"),),
    133     ((S_IWOTH,         "w"),),
    134     ((S_IXOTH|S_ISVTX, "t"),
    135      (S_ISVTX,         "T"),
    136      (S_IXOTH,         "x"))
    137 )
    138 
    139 def filemode(mode):
    140     """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
    141     perm = []
    142     for table in _filemode_table:
    143         for bit, char in table:
    144             if mode & bit == bit:
    145                 perm.append(char)
    146                 break
    147         else:
    148             perm.append("-")
    149     return "".join(perm)
    150 
    151 
    152 # Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
    153 # "st_file_attributes" member
    154 
    155 FILE_ATTRIBUTE_ARCHIVE = 32
    156 FILE_ATTRIBUTE_COMPRESSED = 2048
    157 FILE_ATTRIBUTE_DEVICE = 64
    158 FILE_ATTRIBUTE_DIRECTORY = 16
    159 FILE_ATTRIBUTE_ENCRYPTED = 16384
    160 FILE_ATTRIBUTE_HIDDEN = 2
    161 FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
    162 FILE_ATTRIBUTE_NORMAL = 128
    163 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
    164 FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
    165 FILE_ATTRIBUTE_OFFLINE = 4096
    166 FILE_ATTRIBUTE_READONLY = 1
    167 FILE_ATTRIBUTE_REPARSE_POINT = 1024
    168 FILE_ATTRIBUTE_SPARSE_FILE = 512
    169 FILE_ATTRIBUTE_SYSTEM = 4
    170 FILE_ATTRIBUTE_TEMPORARY = 256
    171 FILE_ATTRIBUTE_VIRTUAL = 65536
    172 
    173 
    174 # If available, use C implementation
    175 try:
    176     from _stat import *
    177 except ImportError:
    178     pass
    179