1"""Constants/functions for interpreting results of os.stat() and os.lstat(). 2 3Suggested usage: from stat import * 4""" 5 6# Indices for stat struct members in the tuple returned by os.stat() 7 8ST_MODE = 0 9ST_INO = 1 10ST_DEV = 2 11ST_NLINK = 3 12ST_UID = 4 13ST_GID = 5 14ST_SIZE = 6 15ST_ATIME = 7 16ST_MTIME = 8 17ST_CTIME = 9 18 19# Extract bits from the mode 20 21def 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 27def 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 36S_IFDIR = 0o040000 # directory 37S_IFCHR = 0o020000 # character device 38S_IFBLK = 0o060000 # block device 39S_IFREG = 0o100000 # regular file 40S_IFIFO = 0o010000 # fifo (named pipe) 41S_IFLNK = 0o120000 # symbolic link 42S_IFSOCK = 0o140000 # socket file 43# Fallbacks for uncommon platform-specific constants 44S_IFDOOR = 0 45S_IFPORT = 0 46S_IFWHT = 0 47 48# Functions to test for each file type 49 50def S_ISDIR(mode): 51 """Return True if mode is from a directory.""" 52 return S_IFMT(mode) == S_IFDIR 53 54def S_ISCHR(mode): 55 """Return True if mode is from a character special device file.""" 56 return S_IFMT(mode) == S_IFCHR 57 58def S_ISBLK(mode): 59 """Return True if mode is from a block special device file.""" 60 return S_IFMT(mode) == S_IFBLK 61 62def S_ISREG(mode): 63 """Return True if mode is from a regular file.""" 64 return S_IFMT(mode) == S_IFREG 65 66def S_ISFIFO(mode): 67 """Return True if mode is from a FIFO (named pipe).""" 68 return S_IFMT(mode) == S_IFIFO 69 70def S_ISLNK(mode): 71 """Return True if mode is from a symbolic link.""" 72 return S_IFMT(mode) == S_IFLNK 73 74def S_ISSOCK(mode): 75 """Return True if mode is from a socket.""" 76 return S_IFMT(mode) == S_IFSOCK 77 78def S_ISDOOR(mode): 79 """Return True if mode is from a door.""" 80 return False 81 82def S_ISPORT(mode): 83 """Return True if mode is from an event port.""" 84 return False 85 86def S_ISWHT(mode): 87 """Return True if mode is from a whiteout.""" 88 return False 89 90# Names for permission bits 91 92S_ISUID = 0o4000 # set UID bit 93S_ISGID = 0o2000 # set GID bit 94S_ENFMT = S_ISGID # file locking enforcement 95S_ISVTX = 0o1000 # sticky bit 96S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR 97S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR 98S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR 99S_IRWXU = 0o0700 # mask for owner permissions 100S_IRUSR = 0o0400 # read by owner 101S_IWUSR = 0o0200 # write by owner 102S_IXUSR = 0o0100 # execute by owner 103S_IRWXG = 0o0070 # mask for group permissions 104S_IRGRP = 0o0040 # read by group 105S_IWGRP = 0o0020 # write by group 106S_IXGRP = 0o0010 # execute by group 107S_IRWXO = 0o0007 # mask for others (not in group) permissions 108S_IROTH = 0o0004 # read by others 109S_IWOTH = 0o0002 # write by others 110S_IXOTH = 0o0001 # execute by others 111 112# Names for file flags 113UF_SETTABLE = 0x0000ffff # owner settable flags 114UF_NODUMP = 0x00000001 # do not dump file 115UF_IMMUTABLE = 0x00000002 # file may not be changed 116UF_APPEND = 0x00000004 # file may only be appended to 117UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack 118UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted 119UF_COMPRESSED = 0x00000020 # macOS: file is compressed 120UF_TRACKED = 0x00000040 # macOS: used for handling document IDs 121UF_DATAVAULT = 0x00000080 # macOS: entitlement needed for I/O 122UF_HIDDEN = 0x00008000 # macOS: file should not be displayed 123SF_SETTABLE = 0xffff0000 # superuser settable flags 124SF_ARCHIVED = 0x00010000 # file may be archived 125SF_IMMUTABLE = 0x00020000 # file may not be changed 126SF_APPEND = 0x00040000 # file may only be appended to 127SF_RESTRICTED = 0x00080000 # macOS: entitlement needed for writing 128SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted 129SF_SNAPSHOT = 0x00200000 # file is a snapshot file 130SF_FIRMLINK = 0x00800000 # macOS: file is a firmlink 131SF_DATALESS = 0x40000000 # macOS: file is a dataless object 132 133 134_filemode_table = ( 135 # File type chars according to: 136 # http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h 137 ((S_IFLNK, "l"), 138 (S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR 139 (S_IFREG, "-"), 140 (S_IFBLK, "b"), 141 (S_IFDIR, "d"), 142 (S_IFCHR, "c"), 143 (S_IFIFO, "p")), 144 145 ((S_IRUSR, "r"),), 146 ((S_IWUSR, "w"),), 147 ((S_IXUSR|S_ISUID, "s"), 148 (S_ISUID, "S"), 149 (S_IXUSR, "x")), 150 151 ((S_IRGRP, "r"),), 152 ((S_IWGRP, "w"),), 153 ((S_IXGRP|S_ISGID, "s"), 154 (S_ISGID, "S"), 155 (S_IXGRP, "x")), 156 157 ((S_IROTH, "r"),), 158 ((S_IWOTH, "w"),), 159 ((S_IXOTH|S_ISVTX, "t"), 160 (S_ISVTX, "T"), 161 (S_IXOTH, "x")) 162) 163 164def filemode(mode): 165 """Convert a file's mode to a string of the form '-rwxrwxrwx'.""" 166 perm = [] 167 for index, table in enumerate(_filemode_table): 168 for bit, char in table: 169 if mode & bit == bit: 170 perm.append(char) 171 break 172 else: 173 if index == 0: 174 # Unknown filetype 175 perm.append("?") 176 else: 177 perm.append("-") 178 return "".join(perm) 179 180 181# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s 182# "st_file_attributes" member 183 184FILE_ATTRIBUTE_ARCHIVE = 32 185FILE_ATTRIBUTE_COMPRESSED = 2048 186FILE_ATTRIBUTE_DEVICE = 64 187FILE_ATTRIBUTE_DIRECTORY = 16 188FILE_ATTRIBUTE_ENCRYPTED = 16384 189FILE_ATTRIBUTE_HIDDEN = 2 190FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 191FILE_ATTRIBUTE_NORMAL = 128 192FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 193FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 194FILE_ATTRIBUTE_OFFLINE = 4096 195FILE_ATTRIBUTE_READONLY = 1 196FILE_ATTRIBUTE_REPARSE_POINT = 1024 197FILE_ATTRIBUTE_SPARSE_FILE = 512 198FILE_ATTRIBUTE_SYSTEM = 4 199FILE_ATTRIBUTE_TEMPORARY = 256 200FILE_ATTRIBUTE_VIRTUAL = 65536 201 202 203# If available, use C implementation 204try: 205 from _stat import * 206except ImportError: 207 pass 208