codekingpro/portable-devtools
114k
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 = 09ST_INO = 110ST_DEV = 211ST_NLINK = 312ST_UID = 413ST_GID = 514ST_SIZE = 615ST_ATIME = 716ST_MTIME = 817ST_CTIME = 918 19# Extract bits from the mode20 21def S_IMODE(mode):22 """Return the portion of the file's mode that can be set by23 os.chmod().24 """25 return mode & 0o777726 27def S_IFMT(mode):28 """Return the portion of the file's mode that describes the29 file type.30 """31 return mode & 0o17000032 33# Constants used as S_IFMT() for various file types34# (not all are implemented on all systems)35 36S_IFDIR = 0o040000 # directory37S_IFCHR = 0o020000 # character device38S_IFBLK = 0o060000 # block device39S_IFREG = 0o100000 # regular file40S_IFIFO = 0o010000 # fifo (named pipe)41S_IFLNK = 0o120000 # symbolic link42S_IFSOCK = 0o140000 # socket file43# Fallbacks for uncommon platform-specific constants44S_IFDOOR = 045S_IFPORT = 046S_IFWHT = 047 48# Functions to test for each file type49 50def S_ISDIR(mode):51 """Return True if mode is from a directory."""52 return S_IFMT(mode) == S_IFDIR53 54def S_ISCHR(mode):55 """Return True if mode is from a character special device file."""56 return S_IFMT(mode) == S_IFCHR57 58def S_ISBLK(mode):59 """Return True if mode is from a block special device file."""60 return S_IFMT(mode) == S_IFBLK61 62def S_ISREG(mode):63 """Return True if mode is from a regular file."""64 return S_IFMT(mode) == S_IFREG65 66def S_ISFIFO(mode):67 """Return True if mode is from a FIFO (named pipe)."""68 return S_IFMT(mode) == S_IFIFO69 70def S_ISLNK(mode):71 """Return True if mode is from a symbolic link."""72 return S_IFMT(mode) == S_IFLNK73 74def S_ISSOCK(mode):75 """Return True if mode is from a socket."""76 return S_IFMT(mode) == S_IFSOCK77 78def S_ISDOOR(mode):79 """Return True if mode is from a door."""80 return False81 82def S_ISPORT(mode):83 """Return True if mode is from an event port."""84 return False85 86def S_ISWHT(mode):87 """Return True if mode is from a whiteout."""88 return False89 90# Names for permission bits91 92S_ISUID = 0o4000 # set UID bit93S_ISGID = 0o2000 # set GID bit94S_ENFMT = S_ISGID # file locking enforcement95S_ISVTX = 0o1000 # sticky bit96S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR97S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR98S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR99S_IRWXU = 0o0700 # mask for owner permissions100S_IRUSR = 0o0400 # read by owner101S_IWUSR = 0o0200 # write by owner102S_IXUSR = 0o0100 # execute by owner103S_IRWXG = 0o0070 # mask for group permissions104S_IRGRP = 0o0040 # read by group105S_IWGRP = 0o0020 # write by group106S_IXGRP = 0o0010 # execute by group107S_IRWXO = 0o0007 # mask for others (not in group) permissions108S_IROTH = 0o0004 # read by others109S_IWOTH = 0o0002 # write by others110S_IXOTH = 0o0001 # execute by others111 112# Names for file flags113UF_SETTABLE = 0x0000ffff # owner settable flags114UF_NODUMP = 0x00000001 # do not dump file115UF_IMMUTABLE = 0x00000002 # file may not be changed116UF_APPEND = 0x00000004 # file may only be appended to117UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack118UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted119UF_COMPRESSED = 0x00000020 # macOS: file is compressed120UF_TRACKED = 0x00000040 # macOS: used for handling document IDs121UF_DATAVAULT = 0x00000080 # macOS: entitlement needed for I/O122UF_HIDDEN = 0x00008000 # macOS: file should not be displayed123SF_SETTABLE = 0xffff0000 # superuser settable flags124SF_ARCHIVED = 0x00010000 # file may be archived125SF_IMMUTABLE = 0x00020000 # file may not be changed126SF_APPEND = 0x00040000 # file may only be appended to127SF_RESTRICTED = 0x00080000 # macOS: entitlement needed for writing128SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted129SF_SNAPSHOT = 0x00200000 # file is a snapshot file130SF_FIRMLINK = 0x00800000 # macOS: file is a firmlink131SF_DATALESS = 0x40000000 # macOS: file is a dataless object132 133 134_filemode_table = (135 # File type chars according to:136 # http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h137 ((S_IFLNK, "l"),138 (S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR139 (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 index == 0:170 if S_IFMT(mode) == bit:171 perm.append(char)172 break173 else:174 if mode & bit == bit:175 perm.append(char)176 break177 else:178 if index == 0:179 # Unknown filetype180 perm.append("?")181 else:182 perm.append("-")183 return "".join(perm)184 185 186# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s187# "st_file_attributes" member188 189FILE_ATTRIBUTE_ARCHIVE = 32190FILE_ATTRIBUTE_COMPRESSED = 2048191FILE_ATTRIBUTE_DEVICE = 64192FILE_ATTRIBUTE_DIRECTORY = 16193FILE_ATTRIBUTE_ENCRYPTED = 16384194FILE_ATTRIBUTE_HIDDEN = 2195FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768196FILE_ATTRIBUTE_NORMAL = 128197FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192198FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072199FILE_ATTRIBUTE_OFFLINE = 4096200FILE_ATTRIBUTE_READONLY = 1201FILE_ATTRIBUTE_REPARSE_POINT = 1024202FILE_ATTRIBUTE_SPARSE_FILE = 512203FILE_ATTRIBUTE_SYSTEM = 4204FILE_ATTRIBUTE_TEMPORARY = 256205FILE_ATTRIBUTE_VIRTUAL = 65536206 207 208# If available, use C implementation209try:210 from _stat import *211except ImportError:212 pass213 