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 | |
Georg Brandl | 402b270 | 2008-03-20 07:25:55 +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): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 22 | return mode & 07777 |
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): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 25 | return mode & 0170000 |
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 | |
| 30 | S_IFDIR = 0040000 |
| 31 | S_IFCHR = 0020000 |
| 32 | S_IFBLK = 0060000 |
| 33 | S_IFREG = 0100000 |
| 34 | S_IFIFO = 0010000 |
| 35 | S_IFLNK = 0120000 |
| 36 | S_IFSOCK = 0140000 |
| 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 | |
| 63 | S_ISUID = 04000 |
| 64 | S_ISGID = 02000 |
| 65 | S_ENFMT = S_ISGID |
| 66 | S_ISVTX = 01000 |
| 67 | S_IREAD = 00400 |
| 68 | S_IWRITE = 00200 |
| 69 | S_IEXEC = 00100 |
| 70 | S_IRWXU = 00700 |
| 71 | S_IRUSR = 00400 |
| 72 | S_IWUSR = 00200 |
| 73 | S_IXUSR = 00100 |
| 74 | S_IRWXG = 00070 |
| 75 | S_IRGRP = 00040 |
| 76 | S_IWGRP = 00020 |
| 77 | S_IXGRP = 00010 |
| 78 | S_IRWXO = 00007 |
| 79 | S_IROTH = 00004 |
| 80 | S_IWOTH = 00002 |
| 81 | S_IXOTH = 00001 |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +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 |
Ned Deily | 43e1054 | 2011-06-27 23:41:53 -0700 | [diff] [blame] | 90 | UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed |
| 91 | UF_HIDDEN = 0x00008000 # OS X: file should not be displayed |
Martin v. Löwis | 382abef | 2007-02-19 10:55:19 +0000 | [diff] [blame] | 92 | SF_ARCHIVED = 0x00010000 |
| 93 | SF_IMMUTABLE = 0x00020000 |
| 94 | SF_APPEND = 0x00040000 |
| 95 | SF_NOUNLINK = 0x00100000 |
| 96 | SF_SNAPSHOT = 0x00200000 |