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): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 22 | """Return the portion of the file's mode that can be set by |
| 23 | os.chmod(). |
| 24 | """ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 25 | return mode & 0o7777 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 27 | def S_IFMT(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 28 | """Return the portion of the file's mode that describes the |
| 29 | file type. |
| 30 | """ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 31 | return mode & 0o170000 |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 32 | |
| 33 | # Constants used as S_IFMT() for various file types |
| 34 | # (not all are implemented on all systems) |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 35 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 36 | S_IFDIR = 0o040000 # directory |
| 37 | S_IFCHR = 0o020000 # character device |
| 38 | S_IFBLK = 0o060000 # block device |
| 39 | S_IFREG = 0o100000 # regular file |
| 40 | S_IFIFO = 0o010000 # fifo (named pipe) |
| 41 | S_IFLNK = 0o120000 # symbolic link |
| 42 | S_IFSOCK = 0o140000 # socket file |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 3bc034b | 1992-03-31 19:03:19 +0000 | [diff] [blame] | 44 | # Functions to test for each file type |
| 45 | |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 46 | def S_ISDIR(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 47 | """Return True if mode is from a directory.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +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): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 51 | """Return True if mode is from a character special device file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 52 | return S_IFMT(mode) == S_IFCHR |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 53 | |
| 54 | def S_ISBLK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 55 | """Return True if mode is from a block special device file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 56 | return S_IFMT(mode) == S_IFBLK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 57 | |
| 58 | def S_ISREG(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 59 | """Return True if mode is from a regular file.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 60 | return S_IFMT(mode) == S_IFREG |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 61 | |
| 62 | def S_ISFIFO(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 63 | """Return True if mode is from a FIFO (named pipe).""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 64 | return S_IFMT(mode) == S_IFIFO |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 65 | |
| 66 | def S_ISLNK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 67 | """Return True if mode is from a symbolic link.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 68 | return S_IFMT(mode) == S_IFLNK |
Guido van Rossum | 6b47ed1 | 1990-10-21 16:17:08 +0000 | [diff] [blame] | 69 | |
| 70 | def S_ISSOCK(mode): |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 71 | """Return True if mode is from a socket.""" |
Tim Peters | 495ad3c | 2001-01-15 01:36:40 +0000 | [diff] [blame] | 72 | return S_IFMT(mode) == S_IFSOCK |
Guido van Rossum | 8899a9c | 1992-05-06 11:38:27 +0000 | [diff] [blame] | 73 | |
| 74 | # Names for permission bits |
| 75 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 76 | S_ISUID = 0o4000 # set UID bit |
| 77 | S_ISGID = 0o2000 # set GID bit |
| 78 | S_ENFMT = S_ISGID # file locking enforcement |
| 79 | S_ISVTX = 0o1000 # sticky bit |
| 80 | S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR |
| 81 | S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR |
| 82 | S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR |
| 83 | S_IRWXU = 0o0700 # mask for owner permissions |
| 84 | S_IRUSR = 0o0400 # read by owner |
| 85 | S_IWUSR = 0o0200 # write by owner |
| 86 | S_IXUSR = 0o0100 # execute by owner |
| 87 | S_IRWXG = 0o0070 # mask for group permissions |
| 88 | S_IRGRP = 0o0040 # read by group |
| 89 | S_IWGRP = 0o0020 # write by group |
| 90 | S_IXGRP = 0o0010 # execute by group |
| 91 | S_IRWXO = 0o0007 # mask for others (not in group) permissions |
| 92 | S_IROTH = 0o0004 # read by others |
| 93 | S_IWOTH = 0o0002 # write by others |
| 94 | S_IXOTH = 0o0001 # execute by others |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 95 | |
| 96 | # Names for file flags |
| 97 | |
Giampaolo Rodola' | a6bedde | 2012-05-14 14:53:33 +0200 | [diff] [blame] | 98 | UF_NODUMP = 0x00000001 # do not dump file |
| 99 | UF_IMMUTABLE = 0x00000002 # file may not be changed |
| 100 | UF_APPEND = 0x00000004 # file may only be appended to |
| 101 | UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack |
| 102 | UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted |
| 103 | UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed |
| 104 | UF_HIDDEN = 0x00008000 # OS X: file should not be displayed |
| 105 | SF_ARCHIVED = 0x00010000 # file may be archived |
| 106 | SF_IMMUTABLE = 0x00020000 # file may not be changed |
| 107 | SF_APPEND = 0x00040000 # file may only be appended to |
| 108 | SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted |
| 109 | SF_SNAPSHOT = 0x00200000 # file is a snapshot file |
Giampaolo Rodola' | ffa1d0b | 2012-05-15 15:30:25 +0200 | [diff] [blame] | 110 | |
| 111 | |
| 112 | _filemode_table = ( |
| 113 | ((S_IFLNK, "l"), |
| 114 | (S_IFREG, "-"), |
| 115 | (S_IFBLK, "b"), |
| 116 | (S_IFDIR, "d"), |
| 117 | (S_IFCHR, "c"), |
| 118 | (S_IFIFO, "p")), |
| 119 | |
| 120 | ((S_IRUSR, "r"),), |
| 121 | ((S_IWUSR, "w"),), |
| 122 | ((S_IXUSR|S_ISUID, "s"), |
| 123 | (S_ISUID, "S"), |
| 124 | (S_IXUSR, "x")), |
| 125 | |
| 126 | ((S_IRGRP, "r"),), |
| 127 | ((S_IWGRP, "w"),), |
| 128 | ((S_IXGRP|S_ISGID, "s"), |
| 129 | (S_ISGID, "S"), |
| 130 | (S_IXGRP, "x")), |
| 131 | |
| 132 | ((S_IROTH, "r"),), |
| 133 | ((S_IWOTH, "w"),), |
| 134 | ((S_IXOTH|S_ISVTX, "t"), |
| 135 | (S_ISVTX, "T"), |
| 136 | (S_IXOTH, "x")) |
| 137 | ) |
| 138 | |
| 139 | def filemode(mode): |
| 140 | """Convert a file's mode to a string of the form '-rwxrwxrwx'.""" |
| 141 | perm = [] |
| 142 | for table in _filemode_table: |
| 143 | for bit, char in table: |
| 144 | if mode & bit == bit: |
| 145 | perm.append(char) |
| 146 | break |
| 147 | else: |
| 148 | perm.append("-") |
| 149 | return "".join(perm) |