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 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 6 | # XXX Strictly spoken, this module may have to be adapted for each POSIX |
| 7 | # implementation; in practice, however, the numeric constants used by |
| 8 | # stat() are almost universal (even for stat() emulations on non-UNIX |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 9 | # systems like MS-DOS). |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 11 | # Indices for stat struct members in tuple returned by os.stat() |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 12 | |
| 13 | ST_MODE = 0 |
| 14 | ST_INO = 1 |
| 15 | ST_DEV = 2 |
| 16 | ST_NLINK = 3 |
| 17 | ST_UID = 4 |
| 18 | ST_GID = 5 |
| 19 | ST_SIZE = 6 |
| 20 | ST_ATIME = 7 |
| 21 | ST_MTIME = 8 |
| 22 | ST_CTIME = 9 |
| 23 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 24 | # Extract bits from the mode |
| 25 | |
Guido van Rossum | 468c448 | 1990-10-31 11:25:23 +0000 | [diff] [blame] | 26 | def S_IMODE(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 27 | return mode & 07777 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 29 | def S_IFMT(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 30 | return mode & 0170000 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 31 | |
| 32 | # Constants used as S_IFMT() for various file types |
| 33 | # (not all are implemented on all systems) |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 34 | |
| 35 | S_IFDIR = 0040000 |
| 36 | S_IFCHR = 0020000 |
| 37 | S_IFBLK = 0060000 |
| 38 | S_IFREG = 0100000 |
| 39 | S_IFIFO = 0010000 |
| 40 | S_IFLNK = 0120000 |
| 41 | S_IFSOCK = 0140000 |
| 42 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 43 | # Functions to test for each file type |
| 44 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 45 | def S_ISDIR(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 46 | return S_IFMT(mode) == S_IFDIR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 47 | |
| 48 | def S_ISCHR(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 49 | return S_IFMT(mode) == S_IFCHR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 50 | |
| 51 | def S_ISBLK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 52 | return S_IFMT(mode) == S_IFBLK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 53 | |
| 54 | def S_ISREG(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 55 | return S_IFMT(mode) == S_IFREG |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 56 | |
| 57 | def S_ISFIFO(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 58 | return S_IFMT(mode) == S_IFIFO |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 59 | |
| 60 | def S_ISLNK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 61 | return S_IFMT(mode) == S_IFLNK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 62 | |
| 63 | def S_ISSOCK(mode): |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 64 | return S_IFMT(mode) == S_IFSOCK |
Guido van Rossum | 8899a9c | 1992-05-06 11:38:27 +0000 | [diff] [blame] | 65 | |
| 66 | # Names for permission bits |
| 67 | |
| 68 | S_ISUID = 04000 |
| 69 | S_ISGID = 02000 |
| 70 | S_ENFMT = S_ISGID |
| 71 | S_ISVTX = 01000 |
| 72 | S_IREAD = 00400 |
| 73 | S_IWRITE = 00200 |
| 74 | S_IEXEC = 00100 |
| 75 | S_IRWXU = 00700 |
| 76 | S_IRUSR = 00400 |
| 77 | S_IWUSR = 00200 |
| 78 | S_IXUSR = 00100 |
| 79 | S_IRWXG = 00070 |
| 80 | S_IRGRP = 00040 |
| 81 | S_IWGRP = 00020 |
| 82 | S_IXGRP = 00010 |
| 83 | S_IRWXO = 00007 |
| 84 | S_IROTH = 00004 |
| 85 | S_IWOTH = 00002 |
| 86 | S_IXOTH = 00001 |