1 | n/a | """Constants/functions for interpreting results of os.stat() and os.lstat(). |
---|
2 | n/a | |
---|
3 | n/a | Suggested usage: from stat import * |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | # Indices for stat struct members in the tuple returned by os.stat() |
---|
7 | n/a | |
---|
8 | n/a | ST_MODE = 0 |
---|
9 | n/a | ST_INO = 1 |
---|
10 | n/a | ST_DEV = 2 |
---|
11 | n/a | ST_NLINK = 3 |
---|
12 | n/a | ST_UID = 4 |
---|
13 | n/a | ST_GID = 5 |
---|
14 | n/a | ST_SIZE = 6 |
---|
15 | n/a | ST_ATIME = 7 |
---|
16 | n/a | ST_MTIME = 8 |
---|
17 | n/a | ST_CTIME = 9 |
---|
18 | n/a | |
---|
19 | n/a | # Extract bits from the mode |
---|
20 | n/a | |
---|
21 | n/a | def S_IMODE(mode): |
---|
22 | n/a | """Return the portion of the file's mode that can be set by |
---|
23 | n/a | os.chmod(). |
---|
24 | n/a | """ |
---|
25 | n/a | return mode & 0o7777 |
---|
26 | n/a | |
---|
27 | n/a | def S_IFMT(mode): |
---|
28 | n/a | """Return the portion of the file's mode that describes the |
---|
29 | n/a | file type. |
---|
30 | n/a | """ |
---|
31 | n/a | return mode & 0o170000 |
---|
32 | n/a | |
---|
33 | n/a | # Constants used as S_IFMT() for various file types |
---|
34 | n/a | # (not all are implemented on all systems) |
---|
35 | n/a | |
---|
36 | n/a | S_IFDIR = 0o040000 # directory |
---|
37 | n/a | S_IFCHR = 0o020000 # character device |
---|
38 | n/a | S_IFBLK = 0o060000 # block device |
---|
39 | n/a | S_IFREG = 0o100000 # regular file |
---|
40 | n/a | S_IFIFO = 0o010000 # fifo (named pipe) |
---|
41 | n/a | S_IFLNK = 0o120000 # symbolic link |
---|
42 | n/a | S_IFSOCK = 0o140000 # socket file |
---|
43 | n/a | |
---|
44 | n/a | # Functions to test for each file type |
---|
45 | n/a | |
---|
46 | n/a | def S_ISDIR(mode): |
---|
47 | n/a | """Return True if mode is from a directory.""" |
---|
48 | n/a | return S_IFMT(mode) == S_IFDIR |
---|
49 | n/a | |
---|
50 | n/a | def S_ISCHR(mode): |
---|
51 | n/a | """Return True if mode is from a character special device file.""" |
---|
52 | n/a | return S_IFMT(mode) == S_IFCHR |
---|
53 | n/a | |
---|
54 | n/a | def S_ISBLK(mode): |
---|
55 | n/a | """Return True if mode is from a block special device file.""" |
---|
56 | n/a | return S_IFMT(mode) == S_IFBLK |
---|
57 | n/a | |
---|
58 | n/a | def S_ISREG(mode): |
---|
59 | n/a | """Return True if mode is from a regular file.""" |
---|
60 | n/a | return S_IFMT(mode) == S_IFREG |
---|
61 | n/a | |
---|
62 | n/a | def S_ISFIFO(mode): |
---|
63 | n/a | """Return True if mode is from a FIFO (named pipe).""" |
---|
64 | n/a | return S_IFMT(mode) == S_IFIFO |
---|
65 | n/a | |
---|
66 | n/a | def S_ISLNK(mode): |
---|
67 | n/a | """Return True if mode is from a symbolic link.""" |
---|
68 | n/a | return S_IFMT(mode) == S_IFLNK |
---|
69 | n/a | |
---|
70 | n/a | def S_ISSOCK(mode): |
---|
71 | n/a | """Return True if mode is from a socket.""" |
---|
72 | n/a | return S_IFMT(mode) == S_IFSOCK |
---|
73 | n/a | |
---|
74 | n/a | # Names for permission bits |
---|
75 | n/a | |
---|
76 | n/a | S_ISUID = 0o4000 # set UID bit |
---|
77 | n/a | S_ISGID = 0o2000 # set GID bit |
---|
78 | n/a | S_ENFMT = S_ISGID # file locking enforcement |
---|
79 | n/a | S_ISVTX = 0o1000 # sticky bit |
---|
80 | n/a | S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR |
---|
81 | n/a | S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR |
---|
82 | n/a | S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR |
---|
83 | n/a | S_IRWXU = 0o0700 # mask for owner permissions |
---|
84 | n/a | S_IRUSR = 0o0400 # read by owner |
---|
85 | n/a | S_IWUSR = 0o0200 # write by owner |
---|
86 | n/a | S_IXUSR = 0o0100 # execute by owner |
---|
87 | n/a | S_IRWXG = 0o0070 # mask for group permissions |
---|
88 | n/a | S_IRGRP = 0o0040 # read by group |
---|
89 | n/a | S_IWGRP = 0o0020 # write by group |
---|
90 | n/a | S_IXGRP = 0o0010 # execute by group |
---|
91 | n/a | S_IRWXO = 0o0007 # mask for others (not in group) permissions |
---|
92 | n/a | S_IROTH = 0o0004 # read by others |
---|
93 | n/a | S_IWOTH = 0o0002 # write by others |
---|
94 | n/a | S_IXOTH = 0o0001 # execute by others |
---|
95 | n/a | |
---|
96 | n/a | # Names for file flags |
---|
97 | n/a | |
---|
98 | n/a | UF_NODUMP = 0x00000001 # do not dump file |
---|
99 | n/a | UF_IMMUTABLE = 0x00000002 # file may not be changed |
---|
100 | n/a | UF_APPEND = 0x00000004 # file may only be appended to |
---|
101 | n/a | UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack |
---|
102 | n/a | UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted |
---|
103 | n/a | UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed |
---|
104 | n/a | UF_HIDDEN = 0x00008000 # OS X: file should not be displayed |
---|
105 | n/a | SF_ARCHIVED = 0x00010000 # file may be archived |
---|
106 | n/a | SF_IMMUTABLE = 0x00020000 # file may not be changed |
---|
107 | n/a | SF_APPEND = 0x00040000 # file may only be appended to |
---|
108 | n/a | SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted |
---|
109 | n/a | SF_SNAPSHOT = 0x00200000 # file is a snapshot file |
---|
110 | n/a | |
---|
111 | n/a | |
---|
112 | n/a | _filemode_table = ( |
---|
113 | n/a | ((S_IFLNK, "l"), |
---|
114 | n/a | (S_IFREG, "-"), |
---|
115 | n/a | (S_IFBLK, "b"), |
---|
116 | n/a | (S_IFDIR, "d"), |
---|
117 | n/a | (S_IFCHR, "c"), |
---|
118 | n/a | (S_IFIFO, "p")), |
---|
119 | n/a | |
---|
120 | n/a | ((S_IRUSR, "r"),), |
---|
121 | n/a | ((S_IWUSR, "w"),), |
---|
122 | n/a | ((S_IXUSR|S_ISUID, "s"), |
---|
123 | n/a | (S_ISUID, "S"), |
---|
124 | n/a | (S_IXUSR, "x")), |
---|
125 | n/a | |
---|
126 | n/a | ((S_IRGRP, "r"),), |
---|
127 | n/a | ((S_IWGRP, "w"),), |
---|
128 | n/a | ((S_IXGRP|S_ISGID, "s"), |
---|
129 | n/a | (S_ISGID, "S"), |
---|
130 | n/a | (S_IXGRP, "x")), |
---|
131 | n/a | |
---|
132 | n/a | ((S_IROTH, "r"),), |
---|
133 | n/a | ((S_IWOTH, "w"),), |
---|
134 | n/a | ((S_IXOTH|S_ISVTX, "t"), |
---|
135 | n/a | (S_ISVTX, "T"), |
---|
136 | n/a | (S_IXOTH, "x")) |
---|
137 | n/a | ) |
---|
138 | n/a | |
---|
139 | n/a | def filemode(mode): |
---|
140 | n/a | """Convert a file's mode to a string of the form '-rwxrwxrwx'.""" |
---|
141 | n/a | perm = [] |
---|
142 | n/a | for table in _filemode_table: |
---|
143 | n/a | for bit, char in table: |
---|
144 | n/a | if mode & bit == bit: |
---|
145 | n/a | perm.append(char) |
---|
146 | n/a | break |
---|
147 | n/a | else: |
---|
148 | n/a | perm.append("-") |
---|
149 | n/a | return "".join(perm) |
---|
150 | n/a | |
---|
151 | n/a | |
---|
152 | n/a | # Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s |
---|
153 | n/a | # "st_file_attributes" member |
---|
154 | n/a | |
---|
155 | n/a | FILE_ATTRIBUTE_ARCHIVE = 32 |
---|
156 | n/a | FILE_ATTRIBUTE_COMPRESSED = 2048 |
---|
157 | n/a | FILE_ATTRIBUTE_DEVICE = 64 |
---|
158 | n/a | FILE_ATTRIBUTE_DIRECTORY = 16 |
---|
159 | n/a | FILE_ATTRIBUTE_ENCRYPTED = 16384 |
---|
160 | n/a | FILE_ATTRIBUTE_HIDDEN = 2 |
---|
161 | n/a | FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768 |
---|
162 | n/a | FILE_ATTRIBUTE_NORMAL = 128 |
---|
163 | n/a | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 |
---|
164 | n/a | FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072 |
---|
165 | n/a | FILE_ATTRIBUTE_OFFLINE = 4096 |
---|
166 | n/a | FILE_ATTRIBUTE_READONLY = 1 |
---|
167 | n/a | FILE_ATTRIBUTE_REPARSE_POINT = 1024 |
---|
168 | n/a | FILE_ATTRIBUTE_SPARSE_FILE = 512 |
---|
169 | n/a | FILE_ATTRIBUTE_SYSTEM = 4 |
---|
170 | n/a | FILE_ATTRIBUTE_TEMPORARY = 256 |
---|
171 | n/a | FILE_ATTRIBUTE_VIRTUAL = 65536 |
---|
172 | n/a | |
---|
173 | n/a | |
---|
174 | n/a | # If available, use C implementation |
---|
175 | n/a | try: |
---|
176 | n/a | from _stat import * |
---|
177 | n/a | except ImportError: |
---|
178 | n/a | pass |
---|