stat.py 5.4 KB

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