1*cda5da8dSAndroid Build Coastguard Worker"""Constants/functions for interpreting results of os.stat() and os.lstat(). 2*cda5da8dSAndroid Build Coastguard Worker 3*cda5da8dSAndroid Build Coastguard WorkerSuggested usage: from stat import * 4*cda5da8dSAndroid Build Coastguard Worker""" 5*cda5da8dSAndroid Build Coastguard Worker 6*cda5da8dSAndroid Build Coastguard Worker# Indices for stat struct members in the tuple returned by os.stat() 7*cda5da8dSAndroid Build Coastguard Worker 8*cda5da8dSAndroid Build Coastguard WorkerST_MODE = 0 9*cda5da8dSAndroid Build Coastguard WorkerST_INO = 1 10*cda5da8dSAndroid Build Coastguard WorkerST_DEV = 2 11*cda5da8dSAndroid Build Coastguard WorkerST_NLINK = 3 12*cda5da8dSAndroid Build Coastguard WorkerST_UID = 4 13*cda5da8dSAndroid Build Coastguard WorkerST_GID = 5 14*cda5da8dSAndroid Build Coastguard WorkerST_SIZE = 6 15*cda5da8dSAndroid Build Coastguard WorkerST_ATIME = 7 16*cda5da8dSAndroid Build Coastguard WorkerST_MTIME = 8 17*cda5da8dSAndroid Build Coastguard WorkerST_CTIME = 9 18*cda5da8dSAndroid Build Coastguard Worker 19*cda5da8dSAndroid Build Coastguard Worker# Extract bits from the mode 20*cda5da8dSAndroid Build Coastguard Worker 21*cda5da8dSAndroid Build Coastguard Workerdef S_IMODE(mode): 22*cda5da8dSAndroid Build Coastguard Worker """Return the portion of the file's mode that can be set by 23*cda5da8dSAndroid Build Coastguard Worker os.chmod(). 24*cda5da8dSAndroid Build Coastguard Worker """ 25*cda5da8dSAndroid Build Coastguard Worker return mode & 0o7777 26*cda5da8dSAndroid Build Coastguard Worker 27*cda5da8dSAndroid Build Coastguard Workerdef S_IFMT(mode): 28*cda5da8dSAndroid Build Coastguard Worker """Return the portion of the file's mode that describes the 29*cda5da8dSAndroid Build Coastguard Worker file type. 30*cda5da8dSAndroid Build Coastguard Worker """ 31*cda5da8dSAndroid Build Coastguard Worker return mode & 0o170000 32*cda5da8dSAndroid Build Coastguard Worker 33*cda5da8dSAndroid Build Coastguard Worker# Constants used as S_IFMT() for various file types 34*cda5da8dSAndroid Build Coastguard Worker# (not all are implemented on all systems) 35*cda5da8dSAndroid Build Coastguard Worker 36*cda5da8dSAndroid Build Coastguard WorkerS_IFDIR = 0o040000 # directory 37*cda5da8dSAndroid Build Coastguard WorkerS_IFCHR = 0o020000 # character device 38*cda5da8dSAndroid Build Coastguard WorkerS_IFBLK = 0o060000 # block device 39*cda5da8dSAndroid Build Coastguard WorkerS_IFREG = 0o100000 # regular file 40*cda5da8dSAndroid Build Coastguard WorkerS_IFIFO = 0o010000 # fifo (named pipe) 41*cda5da8dSAndroid Build Coastguard WorkerS_IFLNK = 0o120000 # symbolic link 42*cda5da8dSAndroid Build Coastguard WorkerS_IFSOCK = 0o140000 # socket file 43*cda5da8dSAndroid Build Coastguard Worker# Fallbacks for uncommon platform-specific constants 44*cda5da8dSAndroid Build Coastguard WorkerS_IFDOOR = 0 45*cda5da8dSAndroid Build Coastguard WorkerS_IFPORT = 0 46*cda5da8dSAndroid Build Coastguard WorkerS_IFWHT = 0 47*cda5da8dSAndroid Build Coastguard Worker 48*cda5da8dSAndroid Build Coastguard Worker# Functions to test for each file type 49*cda5da8dSAndroid Build Coastguard Worker 50*cda5da8dSAndroid Build Coastguard Workerdef S_ISDIR(mode): 51*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a directory.""" 52*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFDIR 53*cda5da8dSAndroid Build Coastguard Worker 54*cda5da8dSAndroid Build Coastguard Workerdef S_ISCHR(mode): 55*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a character special device file.""" 56*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFCHR 57*cda5da8dSAndroid Build Coastguard Worker 58*cda5da8dSAndroid Build Coastguard Workerdef S_ISBLK(mode): 59*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a block special device file.""" 60*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFBLK 61*cda5da8dSAndroid Build Coastguard Worker 62*cda5da8dSAndroid Build Coastguard Workerdef S_ISREG(mode): 63*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a regular file.""" 64*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFREG 65*cda5da8dSAndroid Build Coastguard Worker 66*cda5da8dSAndroid Build Coastguard Workerdef S_ISFIFO(mode): 67*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a FIFO (named pipe).""" 68*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFIFO 69*cda5da8dSAndroid Build Coastguard Worker 70*cda5da8dSAndroid Build Coastguard Workerdef S_ISLNK(mode): 71*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a symbolic link.""" 72*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFLNK 73*cda5da8dSAndroid Build Coastguard Worker 74*cda5da8dSAndroid Build Coastguard Workerdef S_ISSOCK(mode): 75*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a socket.""" 76*cda5da8dSAndroid Build Coastguard Worker return S_IFMT(mode) == S_IFSOCK 77*cda5da8dSAndroid Build Coastguard Worker 78*cda5da8dSAndroid Build Coastguard Workerdef S_ISDOOR(mode): 79*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a door.""" 80*cda5da8dSAndroid Build Coastguard Worker return False 81*cda5da8dSAndroid Build Coastguard Worker 82*cda5da8dSAndroid Build Coastguard Workerdef S_ISPORT(mode): 83*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from an event port.""" 84*cda5da8dSAndroid Build Coastguard Worker return False 85*cda5da8dSAndroid Build Coastguard Worker 86*cda5da8dSAndroid Build Coastguard Workerdef S_ISWHT(mode): 87*cda5da8dSAndroid Build Coastguard Worker """Return True if mode is from a whiteout.""" 88*cda5da8dSAndroid Build Coastguard Worker return False 89*cda5da8dSAndroid Build Coastguard Worker 90*cda5da8dSAndroid Build Coastguard Worker# Names for permission bits 91*cda5da8dSAndroid Build Coastguard Worker 92*cda5da8dSAndroid Build Coastguard WorkerS_ISUID = 0o4000 # set UID bit 93*cda5da8dSAndroid Build Coastguard WorkerS_ISGID = 0o2000 # set GID bit 94*cda5da8dSAndroid Build Coastguard WorkerS_ENFMT = S_ISGID # file locking enforcement 95*cda5da8dSAndroid Build Coastguard WorkerS_ISVTX = 0o1000 # sticky bit 96*cda5da8dSAndroid Build Coastguard WorkerS_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR 97*cda5da8dSAndroid Build Coastguard WorkerS_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR 98*cda5da8dSAndroid Build Coastguard WorkerS_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR 99*cda5da8dSAndroid Build Coastguard WorkerS_IRWXU = 0o0700 # mask for owner permissions 100*cda5da8dSAndroid Build Coastguard WorkerS_IRUSR = 0o0400 # read by owner 101*cda5da8dSAndroid Build Coastguard WorkerS_IWUSR = 0o0200 # write by owner 102*cda5da8dSAndroid Build Coastguard WorkerS_IXUSR = 0o0100 # execute by owner 103*cda5da8dSAndroid Build Coastguard WorkerS_IRWXG = 0o0070 # mask for group permissions 104*cda5da8dSAndroid Build Coastguard WorkerS_IRGRP = 0o0040 # read by group 105*cda5da8dSAndroid Build Coastguard WorkerS_IWGRP = 0o0020 # write by group 106*cda5da8dSAndroid Build Coastguard WorkerS_IXGRP = 0o0010 # execute by group 107*cda5da8dSAndroid Build Coastguard WorkerS_IRWXO = 0o0007 # mask for others (not in group) permissions 108*cda5da8dSAndroid Build Coastguard WorkerS_IROTH = 0o0004 # read by others 109*cda5da8dSAndroid Build Coastguard WorkerS_IWOTH = 0o0002 # write by others 110*cda5da8dSAndroid Build Coastguard WorkerS_IXOTH = 0o0001 # execute by others 111*cda5da8dSAndroid Build Coastguard Worker 112*cda5da8dSAndroid Build Coastguard Worker# Names for file flags 113*cda5da8dSAndroid Build Coastguard Worker 114*cda5da8dSAndroid Build Coastguard WorkerUF_NODUMP = 0x00000001 # do not dump file 115*cda5da8dSAndroid Build Coastguard WorkerUF_IMMUTABLE = 0x00000002 # file may not be changed 116*cda5da8dSAndroid Build Coastguard WorkerUF_APPEND = 0x00000004 # file may only be appended to 117*cda5da8dSAndroid Build Coastguard WorkerUF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack 118*cda5da8dSAndroid Build Coastguard WorkerUF_NOUNLINK = 0x00000010 # file may not be renamed or deleted 119*cda5da8dSAndroid Build Coastguard WorkerUF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed 120*cda5da8dSAndroid Build Coastguard WorkerUF_HIDDEN = 0x00008000 # OS X: file should not be displayed 121*cda5da8dSAndroid Build Coastguard WorkerSF_ARCHIVED = 0x00010000 # file may be archived 122*cda5da8dSAndroid Build Coastguard WorkerSF_IMMUTABLE = 0x00020000 # file may not be changed 123*cda5da8dSAndroid Build Coastguard WorkerSF_APPEND = 0x00040000 # file may only be appended to 124*cda5da8dSAndroid Build Coastguard WorkerSF_NOUNLINK = 0x00100000 # file may not be renamed or deleted 125*cda5da8dSAndroid Build Coastguard WorkerSF_SNAPSHOT = 0x00200000 # file is a snapshot file 126*cda5da8dSAndroid Build Coastguard Worker 127*cda5da8dSAndroid Build Coastguard Worker 128*cda5da8dSAndroid Build Coastguard Worker_filemode_table = ( 129*cda5da8dSAndroid Build Coastguard Worker ((S_IFLNK, "l"), 130*cda5da8dSAndroid Build Coastguard Worker (S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR 131*cda5da8dSAndroid Build Coastguard Worker (S_IFREG, "-"), 132*cda5da8dSAndroid Build Coastguard Worker (S_IFBLK, "b"), 133*cda5da8dSAndroid Build Coastguard Worker (S_IFDIR, "d"), 134*cda5da8dSAndroid Build Coastguard Worker (S_IFCHR, "c"), 135*cda5da8dSAndroid Build Coastguard Worker (S_IFIFO, "p")), 136*cda5da8dSAndroid Build Coastguard Worker 137*cda5da8dSAndroid Build Coastguard Worker ((S_IRUSR, "r"),), 138*cda5da8dSAndroid Build Coastguard Worker ((S_IWUSR, "w"),), 139*cda5da8dSAndroid Build Coastguard Worker ((S_IXUSR|S_ISUID, "s"), 140*cda5da8dSAndroid Build Coastguard Worker (S_ISUID, "S"), 141*cda5da8dSAndroid Build Coastguard Worker (S_IXUSR, "x")), 142*cda5da8dSAndroid Build Coastguard Worker 143*cda5da8dSAndroid Build Coastguard Worker ((S_IRGRP, "r"),), 144*cda5da8dSAndroid Build Coastguard Worker ((S_IWGRP, "w"),), 145*cda5da8dSAndroid Build Coastguard Worker ((S_IXGRP|S_ISGID, "s"), 146*cda5da8dSAndroid Build Coastguard Worker (S_ISGID, "S"), 147*cda5da8dSAndroid Build Coastguard Worker (S_IXGRP, "x")), 148*cda5da8dSAndroid Build Coastguard Worker 149*cda5da8dSAndroid Build Coastguard Worker ((S_IROTH, "r"),), 150*cda5da8dSAndroid Build Coastguard Worker ((S_IWOTH, "w"),), 151*cda5da8dSAndroid Build Coastguard Worker ((S_IXOTH|S_ISVTX, "t"), 152*cda5da8dSAndroid Build Coastguard Worker (S_ISVTX, "T"), 153*cda5da8dSAndroid Build Coastguard Worker (S_IXOTH, "x")) 154*cda5da8dSAndroid Build Coastguard Worker) 155*cda5da8dSAndroid Build Coastguard Worker 156*cda5da8dSAndroid Build Coastguard Workerdef filemode(mode): 157*cda5da8dSAndroid Build Coastguard Worker """Convert a file's mode to a string of the form '-rwxrwxrwx'.""" 158*cda5da8dSAndroid Build Coastguard Worker perm = [] 159*cda5da8dSAndroid Build Coastguard Worker for table in _filemode_table: 160*cda5da8dSAndroid Build Coastguard Worker for bit, char in table: 161*cda5da8dSAndroid Build Coastguard Worker if mode & bit == bit: 162*cda5da8dSAndroid Build Coastguard Worker perm.append(char) 163*cda5da8dSAndroid Build Coastguard Worker break 164*cda5da8dSAndroid Build Coastguard Worker else: 165*cda5da8dSAndroid Build Coastguard Worker perm.append("-") 166*cda5da8dSAndroid Build Coastguard Worker return "".join(perm) 167*cda5da8dSAndroid Build Coastguard Worker 168*cda5da8dSAndroid Build Coastguard Worker 169*cda5da8dSAndroid Build Coastguard Worker# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s 170*cda5da8dSAndroid Build Coastguard Worker# "st_file_attributes" member 171*cda5da8dSAndroid Build Coastguard Worker 172*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_ARCHIVE = 32 173*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_COMPRESSED = 2048 174*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_DEVICE = 64 175*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_DIRECTORY = 16 176*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_ENCRYPTED = 16384 177*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_HIDDEN = 2 178*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 179*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_NORMAL = 128 180*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 181*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 182*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_OFFLINE = 4096 183*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_READONLY = 1 184*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_REPARSE_POINT = 1024 185*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_SPARSE_FILE = 512 186*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_SYSTEM = 4 187*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_TEMPORARY = 256 188*cda5da8dSAndroid Build Coastguard WorkerFILE_ATTRIBUTE_VIRTUAL = 65536 189*cda5da8dSAndroid Build Coastguard Worker 190*cda5da8dSAndroid Build Coastguard Worker 191*cda5da8dSAndroid Build Coastguard Worker# If available, use C implementation 192*cda5da8dSAndroid Build Coastguard Workertry: 193*cda5da8dSAndroid Build Coastguard Worker from _stat import * 194*cda5da8dSAndroid Build Coastguard Workerexcept ImportError: 195*cda5da8dSAndroid Build Coastguard Worker pass 196