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 44# Functions to test for each file type 45 46def S_ISDIR(mode): 47 """Return True if mode is from a directory.""" 48 return S_IFMT(mode) == S_IFDIR 49 50def S_ISCHR(mode): 51 """Return True if mode is from a character special device file.""" 52 return S_IFMT(mode) == S_IFCHR 53 54def S_ISBLK(mode): 55 """Return True if mode is from a block special device file.""" 56 return S_IFMT(mode) == S_IFBLK 57 58def S_ISREG(mode): 59 """Return True if mode is from a regular file.""" 60 return S_IFMT(mode) == S_IFREG 61 62def S_ISFIFO(mode): 63 """Return True if mode is from a FIFO (named pipe).""" 64 return S_IFMT(mode) == S_IFIFO 65 66def S_ISLNK(mode): 67 """Return True if mode is from a symbolic link.""" 68 return S_IFMT(mode) == S_IFLNK 69 70def 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 76S_ISUID = 0o4000 # set UID bit 77S_ISGID = 0o2000 # set GID bit 78S_ENFMT = S_ISGID # file locking enforcement 79S_ISVTX = 0o1000 # sticky bit 80S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR 81S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR 82S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR 83S_IRWXU = 0o0700 # mask for owner permissions 84S_IRUSR = 0o0400 # read by owner 85S_IWUSR = 0o0200 # write by owner 86S_IXUSR = 0o0100 # execute by owner 87S_IRWXG = 0o0070 # mask for group permissions 88S_IRGRP = 0o0040 # read by group 89S_IWGRP = 0o0020 # write by group 90S_IXGRP = 0o0010 # execute by group 91S_IRWXO = 0o0007 # mask for others (not in group) permissions 92S_IROTH = 0o0004 # read by others 93S_IWOTH = 0o0002 # write by others 94S_IXOTH = 0o0001 # execute by others 95 96# Names for file flags 97 98UF_NODUMP = 0x00000001 # do not dump file 99UF_IMMUTABLE = 0x00000002 # file may not be changed 100UF_APPEND = 0x00000004 # file may only be appended to 101UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack 102UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted 103UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed 104UF_HIDDEN = 0x00008000 # OS X: file should not be displayed 105SF_ARCHIVED = 0x00010000 # file may be archived 106SF_IMMUTABLE = 0x00020000 # file may not be changed 107SF_APPEND = 0x00040000 # file may only be appended to 108SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted 109SF_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 139def 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 155FILE_ATTRIBUTE_ARCHIVE = 32 156FILE_ATTRIBUTE_COMPRESSED = 2048 157FILE_ATTRIBUTE_DEVICE = 64 158FILE_ATTRIBUTE_DIRECTORY = 16 159FILE_ATTRIBUTE_ENCRYPTED = 16384 160FILE_ATTRIBUTE_HIDDEN = 2 161FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 162FILE_ATTRIBUTE_NORMAL = 128 163FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 164FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 165FILE_ATTRIBUTE_OFFLINE = 4096 166FILE_ATTRIBUTE_READONLY = 1 167FILE_ATTRIBUTE_REPARSE_POINT = 1024 168FILE_ATTRIBUTE_SPARSE_FILE = 512 169FILE_ATTRIBUTE_SYSTEM = 4 170FILE_ATTRIBUTE_TEMPORARY = 256 171FILE_ATTRIBUTE_VIRTUAL = 65536 172 173 174# If available, use C implementation 175try: 176 from _stat import * 177except ImportError: 178 pass 179