| 1 |
"""Constants/functions for interpreting results of os.stat() and os.lstat(). |
| 2 |
|
| 3 |
Suggested usage: from stat import * |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
def 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 |
|
| 27 |
def 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 |
|
| 34 |
|
| 35 |
|
| 36 |
S_IFDIR = 0o040000 |
| 37 |
S_IFCHR = 0o020000 |
| 38 |
S_IFBLK = 0o060000 |
| 39 |
S_IFREG = 0o100000 |
| 40 |
S_IFIFO = 0o010000 |
| 41 |
S_IFLNK = 0o120000 |
| 42 |
S_IFSOCK = 0o140000 |
| 43 |
|
| 44 |
S_IFDOOR = 0 |
| 45 |
S_IFPORT = 0 |
| 46 |
S_IFWHT = 0 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def S_ISDIR(mode): |
| 51 |
"""Return True if mode is from a directory.""" |
| 52 |
return S_IFMT(mode) == S_IFDIR |
| 53 |
|
| 54 |
def S_ISCHR(mode): |
| 55 |
"""Return True if mode is from a character special device file.""" |
| 56 |
return S_IFMT(mode) == S_IFCHR |
| 57 |
|
| 58 |
def S_ISBLK(mode): |
| 59 |
"""Return True if mode is from a block special device file.""" |
| 60 |
return S_IFMT(mode) == S_IFBLK |
| 61 |
|
| 62 |
def S_ISREG(mode): |
| 63 |
"""Return True if mode is from a regular file.""" |
| 64 |
return S_IFMT(mode) == S_IFREG |
| 65 |
|
| 66 |
def S_ISFIFO(mode): |
| 67 |
"""Return True if mode is from a FIFO (named pipe).""" |
| 68 |
return S_IFMT(mode) == S_IFIFO |
| 69 |
|
| 70 |
def S_ISLNK(mode): |
| 71 |
"""Return True if mode is from a symbolic link.""" |
| 72 |
return S_IFMT(mode) == S_IFLNK |
| 73 |
|
| 74 |
def S_ISSOCK(mode): |
| 75 |
"""Return True if mode is from a socket.""" |
| 76 |
return S_IFMT(mode) == S_IFSOCK |
| 77 |
|
| 78 |
def S_ISDOOR(mode): |
| 79 |
"""Return True if mode is from a door.""" |
| 80 |
return False |
| 81 |
|
| 82 |
def S_ISPORT(mode): |
| 83 |
"""Return True if mode is from an event port.""" |
| 84 |
return False |
| 85 |
|
| 86 |
def S_ISWHT(mode): |
| 87 |
"""Return True if mode is from a whiteout.""" |
| 88 |
return False |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
S_ISUID = 0o4000 |
| 93 |
S_ISGID = 0o2000 |
| 94 |
S_ENFMT = S_ISGID |
| 95 |
S_ISVTX = 0o1000 |
| 96 |
S_IREAD = 0o0400 |
| 97 |
S_IWRITE = 0o0200 |
| 98 |
S_IEXEC = 0o0100 |
| 99 |
S_IRWXU = 0o0700 |
| 100 |
S_IRUSR = 0o0400 |
| 101 |
S_IWUSR = 0o0200 |
| 102 |
S_IXUSR = 0o0100 |
| 103 |
S_IRWXG = 0o0070 |
| 104 |
S_IRGRP = 0o0040 |
| 105 |
S_IWGRP = 0o0020 |
| 106 |
S_IXGRP = 0o0010 |
| 107 |
S_IRWXO = 0o0007 |
| 108 |
S_IROTH = 0o0004 |
| 109 |
S_IWOTH = 0o0002 |
| 110 |
S_IXOTH = 0o0001 |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
UF_NODUMP = 0x00000001 |
| 115 |
UF_IMMUTABLE = 0x00000002 |
| 116 |
UF_APPEND = 0x00000004 |
| 117 |
UF_OPAQUE = 0x00000008 |
| 118 |
UF_NOUNLINK = 0x00000010 |
| 119 |
UF_COMPRESSED = 0x00000020 |
| 120 |
UF_HIDDEN = 0x00008000 |
| 121 |
SF_ARCHIVED = 0x00010000 |
| 122 |
SF_IMMUTABLE = 0x00020000 |
| 123 |
SF_APPEND = 0x00040000 |
| 124 |
SF_NOUNLINK = 0x00100000 |
| 125 |
SF_SNAPSHOT = 0x00200000 |
| 126 |
|
| 127 |
|
| 128 |
_filemode_table = ( |
| 129 |
((S_IFLNK, "l"), |
| 130 |
(S_IFSOCK, "s"), |
| 131 |
(S_IFREG, "-"), |
| 132 |
(S_IFBLK, "b"), |
| 133 |
(S_IFDIR, "d"), |
| 134 |
(S_IFCHR, "c"), |
| 135 |
(S_IFIFO, "p")), |
| 136 |
|
| 137 |
((S_IRUSR, "r"),), |
| 138 |
((S_IWUSR, "w"),), |
| 139 |
((S_IXUSR|S_ISUID, "s"), |
| 140 |
(S_ISUID, "S"), |
| 141 |
(S_IXUSR, "x")), |
| 142 |
|
| 143 |
((S_IRGRP, "r"),), |
| 144 |
((S_IWGRP, "w"),), |
| 145 |
((S_IXGRP|S_ISGID, "s"), |
| 146 |
(S_ISGID, "S"), |
| 147 |
(S_IXGRP, "x")), |
| 148 |
|
| 149 |
((S_IROTH, "r"),), |
| 150 |
((S_IWOTH, "w"),), |
| 151 |
((S_IXOTH|S_ISVTX, "t"), |
| 152 |
(S_ISVTX, "T"), |
| 153 |
(S_IXOTH, "x")) |
| 154 |
) |
| 155 |
|
| 156 |
def filemode(mode): |
| 157 |
"""Convert a file's mode to a string of the form '-rwxrwxrwx'.""" |
| 158 |
perm = [] |
| 159 |
for table in _filemode_table: |
| 160 |
for bit, char in table: |
| 161 |
if mode & bit == bit: |
| 162 |
perm.append(char) |
| 163 |
break |
| 164 |
else: |
| 165 |
if not perm: |
| 166 |
perm.append("?") |
| 167 |
else: |
| 168 |
perm.append("-") |
| 169 |
return "".join(perm) |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
FILE_ATTRIBUTE_ARCHIVE = 32 |
| 176 |
FILE_ATTRIBUTE_COMPRESSED = 2048 |
| 177 |
FILE_ATTRIBUTE_DEVICE = 64 |
| 178 |
FILE_ATTRIBUTE_DIRECTORY = 16 |
| 179 |
FILE_ATTRIBUTE_ENCRYPTED = 16384 |
| 180 |
FILE_ATTRIBUTE_HIDDEN = 2 |
| 181 |
FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 |
| 182 |
FILE_ATTRIBUTE_NORMAL = 128 |
| 183 |
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 |
| 184 |
FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 |
| 185 |
FILE_ATTRIBUTE_OFFLINE = 4096 |
| 186 |
FILE_ATTRIBUTE_READONLY = 1 |
| 187 |
FILE_ATTRIBUTE_REPARSE_POINT = 1024 |
| 188 |
FILE_ATTRIBUTE_SPARSE_FILE = 512 |
| 189 |
FILE_ATTRIBUTE_SYSTEM = 4 |
| 190 |
FILE_ATTRIBUTE_TEMPORARY = 256 |
| 191 |
FILE_ATTRIBUTE_VIRTUAL = 65536 |
| 192 |
|
| 193 |
IO_REPARSE_TAG_MOUNT_POINT = 0xA0000003 |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
try: |
| 198 |
from _stat import * |
| 199 |
except ImportError: |
| 200 |
pass |