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