blob: 5f6f522dc20040a6f35c6457afae83eacf714fd0 [file] [log] [blame]
Guido van Rossum6b47ed11990-10-21 16:17:08 +00001# Module 'stat'
Guido van Rossum3bc034b1992-03-31 19:03:19 +00002#
Guido van Rossum6b47ed11990-10-21 16:17:08 +00003# Defines constants and functions for interpreting stat/lstat struct
Guido van Rossum3bc034b1992-03-31 19:03:19 +00004# as returned by os.stat() and os.lstat() (if it exists).
5#
Guido van Rossum6b47ed11990-10-21 16:17:08 +00006# Suggested usage: from stat import *
Guido van Rossum3bc034b1992-03-31 19:03:19 +00007#
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 Rossum7aeb4b91994-08-23 13:32:20 +000011# systems like MS-DOS).
Guido van Rossum6b47ed11990-10-21 16:17:08 +000012
Guido van Rossum3bc034b1992-03-31 19:03:19 +000013# Indices for stat struct members in tuple returned by os.stat()
Guido van Rossum6b47ed11990-10-21 16:17:08 +000014
15ST_MODE = 0
16ST_INO = 1
17ST_DEV = 2
18ST_NLINK = 3
19ST_UID = 4
20ST_GID = 5
21ST_SIZE = 6
22ST_ATIME = 7
23ST_MTIME = 8
24ST_CTIME = 9
25
Guido van Rossum3bc034b1992-03-31 19:03:19 +000026# Extract bits from the mode
27
Guido van Rossum468c4481990-10-31 11:25:23 +000028def S_IMODE(mode):
Guido van Rossum3bc034b1992-03-31 19:03:19 +000029 return mode & 07777
30
Guido van Rossum6b47ed11990-10-21 16:17:08 +000031def S_IFMT(mode):
Guido van Rossum8899a9c1992-05-06 11:38:27 +000032 return mode & 0170000
Guido van Rossum3bc034b1992-03-31 19:03:19 +000033
34# Constants used as S_IFMT() for various file types
35# (not all are implemented on all systems)
Guido van Rossum6b47ed11990-10-21 16:17:08 +000036
37S_IFDIR = 0040000
38S_IFCHR = 0020000
39S_IFBLK = 0060000
40S_IFREG = 0100000
41S_IFIFO = 0010000
42S_IFLNK = 0120000
43S_IFSOCK = 0140000
44
Guido van Rossum3bc034b1992-03-31 19:03:19 +000045# Functions to test for each file type
46
Guido van Rossum6b47ed11990-10-21 16:17:08 +000047def S_ISDIR(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000048 return S_IFMT(mode) == S_IFDIR
Guido van Rossum6b47ed11990-10-21 16:17:08 +000049
50def S_ISCHR(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000051 return S_IFMT(mode) == S_IFCHR
Guido van Rossum6b47ed11990-10-21 16:17:08 +000052
53def S_ISBLK(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000054 return S_IFMT(mode) == S_IFBLK
Guido van Rossum6b47ed11990-10-21 16:17:08 +000055
56def S_ISREG(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000057 return S_IFMT(mode) == S_IFREG
Guido van Rossum6b47ed11990-10-21 16:17:08 +000058
59def S_ISFIFO(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000060 return S_IFMT(mode) == S_IFIFO
Guido van Rossum6b47ed11990-10-21 16:17:08 +000061
62def S_ISLNK(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000063 return S_IFMT(mode) == S_IFLNK
Guido van Rossum6b47ed11990-10-21 16:17:08 +000064
65def S_ISSOCK(mode):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000066 return S_IFMT(mode) == S_IFSOCK
Guido van Rossum8899a9c1992-05-06 11:38:27 +000067
68# Names for permission bits
69
70S_ISUID = 04000
71S_ISGID = 02000
72S_ENFMT = S_ISGID
73S_ISVTX = 01000
74S_IREAD = 00400
75S_IWRITE = 00200
76S_IEXEC = 00100
77S_IRWXU = 00700
78S_IRUSR = 00400
79S_IWUSR = 00200
80S_IXUSR = 00100
81S_IRWXG = 00070
82S_IRGRP = 00040
83S_IWGRP = 00020
84S_IXGRP = 00010
85S_IRWXO = 00007
86S_IROTH = 00004
87S_IWOTH = 00002
88S_IXOTH = 00001