1"""Constants/functions for interpreting results of os.stat() and os.lstat().
2
3Suggested usage: from stat import *
4"""
5
6# Indices for stat struct members in the tuple returned by os.stat()
7
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
19# Extract bits from the mode
20
21def S_IMODE(mode):
22    """Return the portion of the file's mode that can be set by
23    os.chmod().
24    """
25    return mode & 0o7777
26
27def S_IFMT(mode):
28    """Return the portion of the file's mode that describes the
29    file type.
30    """
31    return mode & 0o170000
32
33# Constants used as S_IFMT() for various file types
34# (not all are implemented on all systems)
35
36S_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
43# Fallbacks for uncommon platform-specific constants
44S_IFDOOR = 0
45S_IFPORT = 0
46S_IFWHT = 0
47
48# Functions to test for each file type
49
50def S_ISDIR(mode):
51    """Return True if mode is from a directory."""
52    return S_IFMT(mode) == S_IFDIR
53
54def S_ISCHR(mode):
55    """Return True if mode is from a character special device file."""
56    return S_IFMT(mode) == S_IFCHR
57
58def S_ISBLK(mode):
59    """Return True if mode is from a block special device file."""
60    return S_IFMT(mode) == S_IFBLK
61
62def S_ISREG(mode):
63    """Return True if mode is from a regular file."""
64    return S_IFMT(mode) == S_IFREG
65
66def S_ISFIFO(mode):
67    """Return True if mode is from a FIFO (named pipe)."""
68    return S_IFMT(mode) == S_IFIFO
69
70def S_ISLNK(mode):
71    """Return True if mode is from a symbolic link."""
72    return S_IFMT(mode) == S_IFLNK
73
74def S_ISSOCK(mode):
75    """Return True if mode is from a socket."""
76    return S_IFMT(mode) == S_IFSOCK
77
78def S_ISDOOR(mode):
79    """Return True if mode is from a door."""
80    return False
81
82def S_ISPORT(mode):
83    """Return True if mode is from an event port."""
84    return False
85
86def S_ISWHT(mode):
87    """Return True if mode is from a whiteout."""
88    return False
89
90# Names for permission bits
91
92S_ISUID = 0o4000  # set UID bit
93S_ISGID = 0o2000  # set GID bit
94S_ENFMT = S_ISGID # file locking enforcement
95S_ISVTX = 0o1000  # sticky bit
96S_IREAD = 0o0400  # Unix V7 synonym for S_IRUSR
97S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
98S_IEXEC = 0o0100  # Unix V7 synonym for S_IXUSR
99S_IRWXU = 0o0700  # mask for owner permissions
100S_IRUSR = 0o0400  # read by owner
101S_IWUSR = 0o0200  # write by owner
102S_IXUSR = 0o0100  # execute by owner
103S_IRWXG = 0o0070  # mask for group permissions
104S_IRGRP = 0o0040  # read by group
105S_IWGRP = 0o0020  # write by group
106S_IXGRP = 0o0010  # execute by group
107S_IRWXO = 0o0007  # mask for others (not in group) permissions
108S_IROTH = 0o0004  # read by others
109S_IWOTH = 0o0002  # write by others
110S_IXOTH = 0o0001  # execute by others
111
112# Names for file flags
113
114UF_NODUMP    = 0x00000001  # do not dump file
115UF_IMMUTABLE = 0x00000002  # file may not be changed
116UF_APPEND    = 0x00000004  # file may only be appended to
117UF_OPAQUE    = 0x00000008  # directory is opaque when viewed through a union stack
118UF_NOUNLINK  = 0x00000010  # file may not be renamed or deleted
119UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
120UF_HIDDEN    = 0x00008000  # OS X: file should not be displayed
121SF_ARCHIVED  = 0x00010000  # file may be archived
122SF_IMMUTABLE = 0x00020000  # file may not be changed
123SF_APPEND    = 0x00040000  # file may only be appended to
124SF_NOUNLINK  = 0x00100000  # file may not be renamed or deleted
125SF_SNAPSHOT  = 0x00200000  # file is a snapshot file
126
127
128_filemode_table = (
129    ((S_IFLNK,         "l"),
130     (S_IFSOCK,        "s"),  # Must appear before IFREG and IFDIR as IFSOCK == IFREG | IFDIR
131     (S_IFREG,         "-"),
132     (S_IFBLK,         "b"),
133     (S_IFDIR,         "d"),
134     (S_IFCHR,         "c"),
135     (S_IFIFO,         "p")),
136
137    ((S_IRUSR,         "r"),),
138    ((S_IWUSR,         "w"),),
139    ((S_IXUSR|S_ISUID, "s"),
140     (S_ISUID,         "S"),
141     (S_IXUSR,         "x")),
142
143    ((S_IRGRP,         "r"),),
144    ((S_IWGRP,         "w"),),
145    ((S_IXGRP|S_ISGID, "s"),
146     (S_ISGID,         "S"),
147     (S_IXGRP,         "x")),
148
149    ((S_IROTH,         "r"),),
150    ((S_IWOTH,         "w"),),
151    ((S_IXOTH|S_ISVTX, "t"),
152     (S_ISVTX,         "T"),
153     (S_IXOTH,         "x"))
154)
155
156def filemode(mode):
157    """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
158    perm = []
159    for table in _filemode_table:
160        for bit, char in table:
161            if mode & bit == bit:
162                perm.append(char)
163                break
164        else:
165            perm.append("-")
166    return "".join(perm)
167
168
169# Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
170# "st_file_attributes" member
171
172FILE_ATTRIBUTE_ARCHIVE = 32
173FILE_ATTRIBUTE_COMPRESSED = 2048
174FILE_ATTRIBUTE_DEVICE = 64
175FILE_ATTRIBUTE_DIRECTORY = 16
176FILE_ATTRIBUTE_ENCRYPTED = 16384
177FILE_ATTRIBUTE_HIDDEN = 2
178FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
179FILE_ATTRIBUTE_NORMAL = 128
180FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
181FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
182FILE_ATTRIBUTE_OFFLINE = 4096
183FILE_ATTRIBUTE_READONLY = 1
184FILE_ATTRIBUTE_REPARSE_POINT = 1024
185FILE_ATTRIBUTE_SPARSE_FILE = 512
186FILE_ATTRIBUTE_SYSTEM = 4
187FILE_ATTRIBUTE_TEMPORARY = 256
188FILE_ATTRIBUTE_VIRTUAL = 65536
189
190
191# If available, use C implementation
192try:
193    from _stat import *
194except ImportError:
195    pass
196