blob: 0f29498b48ddd9af34164c11838b38006b37fe17 [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