Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Constants/functions for interpreting results of os.stat() and os.lstat(). |
| 2 | |
| 3 | Suggested usage: from stat import * |
| 4 | """ |
| 5 | |
Christian Heimes | e25f35e | 2008-03-20 10:49:03 +0000 | [diff] [blame] | 6 | # Indices for stat struct members in the tuple returned by os.stat() |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 19 | # Extract bits from the mode |
| 20 | |
Guido van Rossum | 468c448 | 1990-10-31 11:25:23 +0000 | [diff] [blame] | 21 | def S_IMODE(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 22 | """Return the portion of the file's mode that can be set by |
| 23 | os.chmod(). |
| 24 | """ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 25 | return mode & 0o7777 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 27 | def S_IFMT(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 28 | """Return the portion of the file's mode that describes the |
| 29 | file type. |
| 30 | """ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 31 | return mode & 0o170000 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 32 | |
| 33 | # Constants used as S_IFMT() for various file types |
| 34 | # (not all are implemented on all systems) |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 35 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 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 |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 44 | # Functions to test for each file type |
| 45 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 46 | def S_ISDIR(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 47 | """Return True if mode is from a directory.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 48 | return S_IFMT(mode) == S_IFDIR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 49 | |
| 50 | def S_ISCHR(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 51 | """Return True if mode is from a character special device file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 52 | return S_IFMT(mode) == S_IFCHR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 53 | |
| 54 | def S_ISBLK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 55 | """Return True if mode is from a block special device file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 56 | return S_IFMT(mode) == S_IFBLK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 57 | |
| 58 | def S_ISREG(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 59 | """Return True if mode is from a regular file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 60 | return S_IFMT(mode) == S_IFREG |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 61 | |
| 62 | def S_ISFIFO(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 63 | """Return True if mode is from a FIFO (named pipe).""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 64 | return S_IFMT(mode) == S_IFIFO |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 65 | |
| 66 | def S_ISLNK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 67 | """Return True if mode is from a symbolic link.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 68 | return S_IFMT(mode) == S_IFLNK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 69 | |
| 70 | def S_ISSOCK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 71 | """Return True if mode is from a socket.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 72 | return S_IFMT(mode) == S_IFSOCK |
Guido van Rossum | 8899a9c | 1992-05-06 11:38:27 +0000 | [diff] [blame] | 73 | |
| 74 | # Names for permission bits |
| 75 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 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 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 95 | |
| 96 | # Names for file flags |
| 97 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 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 |
Giampaolo Rodola' | ffa1d0b | 2012-05-15 15:30:25 +0200 | [diff] [blame] | 110 | |
| 111 | |
| 112 | _filemode_table = ( |
| 113 | ((S_IFLNK, "l"), |
GPery | b92c526 | 2018-08-10 08:12:08 +0300 | [diff] [blame] | 114 | (S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR |
Giampaolo Rodola' | ffa1d0b | 2012-05-15 15:30:25 +0200 | [diff] [blame] | 115 | (S_IFREG, "-"), |
| 116 | (S_IFBLK, "b"), |
| 117 | (S_IFDIR, "d"), |
| 118 | (S_IFCHR, "c"), |
| 119 | (S_IFIFO, "p")), |
| 120 | |
| 121 | ((S_IRUSR, "r"),), |
| 122 | ((S_IWUSR, "w"),), |
| 123 | ((S_IXUSR|S_ISUID, "s"), |
| 124 | (S_ISUID, "S"), |
| 125 | (S_IXUSR, "x")), |
| 126 | |
| 127 | ((S_IRGRP, "r"),), |
| 128 | ((S_IWGRP, "w"),), |
| 129 | ((S_IXGRP|S_ISGID, "s"), |
| 130 | (S_ISGID, "S"), |
| 131 | (S_IXGRP, "x")), |
| 132 | |
| 133 | ((S_IROTH, "r"),), |
| 134 | ((S_IWOTH, "w"),), |
| 135 | ((S_IXOTH|S_ISVTX, "t"), |
| 136 | (S_ISVTX, "T"), |
| 137 | (S_IXOTH, "x")) |
| 138 | ) |
| 139 | |
| 140 | def filemode(mode): |
| 141 | """Convert a file's mode to a string of the form '-rwxrwxrwx'.""" |
| 142 | perm = [] |
| 143 | for table in _filemode_table: |
| 144 | for bit, char in table: |
| 145 | if mode & bit == bit: |
| 146 | perm.append(char) |
| 147 | break |
| 148 | else: |
| 149 | perm.append("-") |
| 150 | return "".join(perm) |
Christian Heimes | c77d9f3 | 2013-06-22 21:05:02 +0200 | [diff] [blame] | 151 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 152 | |
| 153 | # Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s |
| 154 | # "st_file_attributes" member |
| 155 | |
| 156 | FILE_ATTRIBUTE_ARCHIVE = 32 |
| 157 | FILE_ATTRIBUTE_COMPRESSED = 2048 |
| 158 | FILE_ATTRIBUTE_DEVICE = 64 |
| 159 | FILE_ATTRIBUTE_DIRECTORY = 16 |
| 160 | FILE_ATTRIBUTE_ENCRYPTED = 16384 |
| 161 | FILE_ATTRIBUTE_HIDDEN = 2 |
| 162 | FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 |
| 163 | FILE_ATTRIBUTE_NORMAL = 128 |
| 164 | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 |
| 165 | FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 |
| 166 | FILE_ATTRIBUTE_OFFLINE = 4096 |
| 167 | FILE_ATTRIBUTE_READONLY = 1 |
| 168 | FILE_ATTRIBUTE_REPARSE_POINT = 1024 |
| 169 | FILE_ATTRIBUTE_SPARSE_FILE = 512 |
| 170 | FILE_ATTRIBUTE_SYSTEM = 4 |
| 171 | FILE_ATTRIBUTE_TEMPORARY = 256 |
| 172 | FILE_ATTRIBUTE_VIRTUAL = 65536 |
| 173 | |
| 174 | |
Christian Heimes | c77d9f3 | 2013-06-22 21:05:02 +0200 | [diff] [blame] | 175 | # If available, use C implementation |
| 176 | try: |
| 177 | from _stat import * |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 178 | except ImportError: |
Christian Heimes | c77d9f3 | 2013-06-22 21:05:02 +0200 | [diff] [blame] | 179 | pass |