blob: a9c678ec03c9cb8c834ac9462bcb7a31cc4bf652 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Constants/functions for interpreting results of os.stat() and os.lstat().
2
3Suggested usage: from stat import *
4"""
5
Christian Heimese25f35e2008-03-20 10:49:03 +00006# Indices for stat struct members in the tuple returned by os.stat()
Guido van Rossum6b47ed11990-10-21 16:17:08 +00007
8ST_MODE = 0
9ST_INO = 1
10ST_DEV = 2
11ST_NLINK = 3
12ST_UID = 4
13ST_GID = 5
14ST_SIZE = 6
15ST_ATIME = 7
16ST_MTIME = 8
17ST_CTIME = 9
18
Guido van Rossum3bc034b1992-03-31 19:03:19 +000019# Extract bits from the mode
20
Guido van Rossum468c4481990-10-31 11:25:23 +000021def S_IMODE(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020022 """Return the portion of the file's mode that can be set by
23 os.chmod().
24 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +000025 return mode & 0o7777
Guido van Rossum3bc034b1992-03-31 19:03:19 +000026
Guido van Rossum6b47ed11990-10-21 16:17:08 +000027def S_IFMT(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020028 """Return the portion of the file's mode that describes the
29 file type.
30 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +000031 return mode & 0o170000
Guido van Rossum3bc034b1992-03-31 19:03:19 +000032
33# Constants used as S_IFMT() for various file types
34# (not all are implemented on all systems)
Guido van Rossum6b47ed11990-10-21 16:17:08 +000035
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020036S_IFDIR = 0o040000 # directory
37S_IFCHR = 0o020000 # character device
38S_IFBLK = 0o060000 # block device
39S_IFREG = 0o100000 # regular file
40S_IFIFO = 0o010000 # fifo (named pipe)
41S_IFLNK = 0o120000 # symbolic link
42S_IFSOCK = 0o140000 # socket file
Guido van Rossum6b47ed11990-10-21 16:17:08 +000043
Guido van Rossum3bc034b1992-03-31 19:03:19 +000044# Functions to test for each file type
45
Guido van Rossum6b47ed11990-10-21 16:17:08 +000046def S_ISDIR(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020047 """Return True if mode is from a directory."""
Tim Peters495ad3c2001-01-15 01:36:40 +000048 return S_IFMT(mode) == S_IFDIR
Guido van Rossum6b47ed11990-10-21 16:17:08 +000049
50def S_ISCHR(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020051 """Return True if mode is from a character special device file."""
Tim Peters495ad3c2001-01-15 01:36:40 +000052 return S_IFMT(mode) == S_IFCHR
Guido van Rossum6b47ed11990-10-21 16:17:08 +000053
54def S_ISBLK(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020055 """Return True if mode is from a block special device file."""
Tim Peters495ad3c2001-01-15 01:36:40 +000056 return S_IFMT(mode) == S_IFBLK
Guido van Rossum6b47ed11990-10-21 16:17:08 +000057
58def S_ISREG(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020059 """Return True if mode is from a regular file."""
Tim Peters495ad3c2001-01-15 01:36:40 +000060 return S_IFMT(mode) == S_IFREG
Guido van Rossum6b47ed11990-10-21 16:17:08 +000061
62def S_ISFIFO(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020063 """Return True if mode is from a FIFO (named pipe)."""
Tim Peters495ad3c2001-01-15 01:36:40 +000064 return S_IFMT(mode) == S_IFIFO
Guido van Rossum6b47ed11990-10-21 16:17:08 +000065
66def S_ISLNK(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020067 """Return True if mode is from a symbolic link."""
Tim Peters495ad3c2001-01-15 01:36:40 +000068 return S_IFMT(mode) == S_IFLNK
Guido van Rossum6b47ed11990-10-21 16:17:08 +000069
70def S_ISSOCK(mode):
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020071 """Return True if mode is from a socket."""
Tim Peters495ad3c2001-01-15 01:36:40 +000072 return S_IFMT(mode) == S_IFSOCK
Guido van Rossum8899a9c1992-05-06 11:38:27 +000073
74# Names for permission bits
75
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020076S_ISUID = 0o4000 # set UID bit
77S_ISGID = 0o2000 # set GID bit
78S_ENFMT = S_ISGID # file locking enforcement
79S_ISVTX = 0o1000 # sticky bit
80S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR
81S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
82S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR
83S_IRWXU = 0o0700 # mask for owner permissions
84S_IRUSR = 0o0400 # read by owner
85S_IWUSR = 0o0200 # write by owner
86S_IXUSR = 0o0100 # execute by owner
87S_IRWXG = 0o0070 # mask for group permissions
88S_IRGRP = 0o0040 # read by group
89S_IWGRP = 0o0020 # write by group
90S_IXGRP = 0o0010 # execute by group
91S_IRWXO = 0o0007 # mask for others (not in group) permissions
92S_IROTH = 0o0004 # read by others
93S_IWOTH = 0o0002 # write by others
94S_IXOTH = 0o0001 # execute by others
Thomas Wouterscf297e42007-02-23 15:07:44 +000095
96# Names for file flags
97
Giampaolo Rodola'a6bedde2012-05-14 14:53:33 +020098UF_NODUMP = 0x00000001 # do not dump file
99UF_IMMUTABLE = 0x00000002 # file may not be changed
100UF_APPEND = 0x00000004 # file may only be appended to
101UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack
102UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted
103UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
104UF_HIDDEN = 0x00008000 # OS X: file should not be displayed
105SF_ARCHIVED = 0x00010000 # file may be archived
106SF_IMMUTABLE = 0x00020000 # file may not be changed
107SF_APPEND = 0x00040000 # file may only be appended to
108SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted
109SF_SNAPSHOT = 0x00200000 # file is a snapshot file
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200110
111
112_filemode_table = (
113 ((S_IFLNK, "l"),
GPeryb92c5262018-08-10 08:12:08 +0300114 (S_IFSOCK, "s"), # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR
Giampaolo Rodola'ffa1d0b2012-05-15 15:30:25 +0200115 (S_IFREG, "-"),
116 (S_IFBLK, "b"),
117 (S_IFDIR, "d"),
118 (S_IFCHR, "c"),
119 (S_IFIFO, "p")),
120
121 ((S_IRUSR, "r"),),
122 ((S_IWUSR, "w"),),
123 ((S_IXUSR|S_ISUID, "s"),
124 (S_ISUID, "S"),
125 (S_IXUSR, "x")),
126
127 ((S_IRGRP, "r"),),
128 ((S_IWGRP, "w"),),
129 ((S_IXGRP|S_ISGID, "s"),
130 (S_ISGID, "S"),
131 (S_IXGRP, "x")),
132
133 ((S_IROTH, "r"),),
134 ((S_IWOTH, "w"),),
135 ((S_IXOTH|S_ISVTX, "t"),
136 (S_ISVTX, "T"),
137 (S_IXOTH, "x"))
138)
139
140def filemode(mode):
141 """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
142 perm = []
143 for table in _filemode_table:
144 for bit, char in table:
145 if mode & bit == bit:
146 perm.append(char)
147 break
148 else:
149 perm.append("-")
150 return "".join(perm)
Christian Heimesc77d9f32013-06-22 21:05:02 +0200151
Zachary Ware63f277b2014-06-19 09:46:37 -0500152
153# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
154# "st_file_attributes" member
155
156FILE_ATTRIBUTE_ARCHIVE = 32
157FILE_ATTRIBUTE_COMPRESSED = 2048
158FILE_ATTRIBUTE_DEVICE = 64
159FILE_ATTRIBUTE_DIRECTORY = 16
160FILE_ATTRIBUTE_ENCRYPTED = 16384
161FILE_ATTRIBUTE_HIDDEN = 2
162FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
163FILE_ATTRIBUTE_NORMAL = 128
164FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
165FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
166FILE_ATTRIBUTE_OFFLINE = 4096
167FILE_ATTRIBUTE_READONLY = 1
168FILE_ATTRIBUTE_REPARSE_POINT = 1024
169FILE_ATTRIBUTE_SPARSE_FILE = 512
170FILE_ATTRIBUTE_SYSTEM = 4
171FILE_ATTRIBUTE_TEMPORARY = 256
172FILE_ATTRIBUTE_VIRTUAL = 65536
173
174
Christian Heimesc77d9f32013-06-22 21:05:02 +0200175# If available, use C implementation
176try:
177 from _stat import *
Brett Cannon260fbe82013-07-04 18:16:15 -0400178except ImportError:
Christian Heimesc77d9f32013-06-22 21:05:02 +0200179 pass