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): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 22 | return mode & 0o7777 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 24 | def S_IFMT(mode): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 25 | return mode & 0o170000 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 26 | |
| 27 | # Constants used as S_IFMT() for various file types |
| 28 | # (not all are implemented on all systems) |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 30 | S_IFDIR = 0o040000 |
| 31 | S_IFCHR = 0o020000 |
| 32 | S_IFBLK = 0o060000 |
| 33 | S_IFREG = 0o100000 |
| 34 | S_IFIFO = 0o010000 |
| 35 | S_IFLNK = 0o120000 |
| 36 | S_IFSOCK = 0o140000 |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 38 | # Functions to test for each file type |
| 39 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 40 | def S_ISDIR(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 41 | return S_IFMT(mode) == S_IFDIR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 42 | |
| 43 | def S_ISCHR(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 44 | return S_IFMT(mode) == S_IFCHR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 45 | |
| 46 | def S_ISBLK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 47 | return S_IFMT(mode) == S_IFBLK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 48 | |
| 49 | def S_ISREG(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 50 | return S_IFMT(mode) == S_IFREG |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 51 | |
| 52 | def S_ISFIFO(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 53 | return S_IFMT(mode) == S_IFIFO |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 54 | |
| 55 | def S_ISLNK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 56 | return S_IFMT(mode) == S_IFLNK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 57 | |
| 58 | def S_ISSOCK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 59 | return S_IFMT(mode) == S_IFSOCK |
Guido van Rossum | 8899a9c | 1992-05-06 11:38:27 +0000 | [diff] [blame] | 60 | |
| 61 | # Names for permission bits |
| 62 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 63 | S_ISUID = 0o4000 |
| 64 | S_ISGID = 0o2000 |
Guido van Rossum | 8899a9c | 1992-05-06 11:38:27 +0000 | [diff] [blame] | 65 | S_ENFMT = S_ISGID |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 66 | S_ISVTX = 0o1000 |
| 67 | S_IREAD = 0o0400 |
| 68 | S_IWRITE = 0o0200 |
| 69 | S_IEXEC = 0o0100 |
| 70 | S_IRWXU = 0o0700 |
| 71 | S_IRUSR = 0o0400 |
| 72 | S_IWUSR = 0o0200 |
| 73 | S_IXUSR = 0o0100 |
| 74 | S_IRWXG = 0o0070 |
| 75 | S_IRGRP = 0o0040 |
| 76 | S_IWGRP = 0o0020 |
| 77 | S_IXGRP = 0o0010 |
| 78 | S_IRWXO = 0o0007 |
| 79 | S_IROTH = 0o0004 |
| 80 | S_IWOTH = 0o0002 |
| 81 | S_IXOTH = 0o0001 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 82 | |
| 83 | # Names for file flags |
| 84 | |
| 85 | UF_NODUMP = 0x00000001 |
| 86 | UF_IMMUTABLE = 0x00000002 |
| 87 | UF_APPEND = 0x00000004 |
| 88 | UF_OPAQUE = 0x00000008 |
| 89 | UF_NOUNLINK = 0x00000010 |
| 90 | SF_ARCHIVED = 0x00010000 |
| 91 | SF_IMMUTABLE = 0x00020000 |
| 92 | SF_APPEND = 0x00040000 |
| 93 | SF_NOUNLINK = 0x00100000 |
| 94 | SF_SNAPSHOT = 0x00200000 |