stat.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Functions to test for each file type
  36. def S_ISDIR(mode):
  37. """Return True if mode is from a directory."""
  38. return S_IFMT(mode) == S_IFDIR
  39. def S_ISCHR(mode):
  40. """Return True if mode is from a character special device file."""
  41. return S_IFMT(mode) == S_IFCHR
  42. def S_ISBLK(mode):
  43. """Return True if mode is from a block special device file."""
  44. return S_IFMT(mode) == S_IFBLK
  45. def S_ISREG(mode):
  46. """Return True if mode is from a regular file."""
  47. return S_IFMT(mode) == S_IFREG
  48. def S_ISFIFO(mode):
  49. """Return True if mode is from a FIFO (named pipe)."""
  50. return S_IFMT(mode) == S_IFIFO
  51. def S_ISLNK(mode):
  52. """Return True if mode is from a symbolic link."""
  53. return S_IFMT(mode) == S_IFLNK
  54. def S_ISSOCK(mode):
  55. """Return True if mode is from a socket."""
  56. return S_IFMT(mode) == S_IFSOCK
  57. # Names for permission bits
  58. S_ISUID = 0o4000 # set UID bit
  59. S_ISGID = 0o2000 # set GID bit
  60. S_ENFMT = S_ISGID # file locking enforcement
  61. S_ISVTX = 0o1000 # sticky bit
  62. S_IREAD = 0o0400 # Unix V7 synonym for S_IRUSR
  63. S_IWRITE = 0o0200 # Unix V7 synonym for S_IWUSR
  64. S_IEXEC = 0o0100 # Unix V7 synonym for S_IXUSR
  65. S_IRWXU = 0o0700 # mask for owner permissions
  66. S_IRUSR = 0o0400 # read by owner
  67. S_IWUSR = 0o0200 # write by owner
  68. S_IXUSR = 0o0100 # execute by owner
  69. S_IRWXG = 0o0070 # mask for group permissions
  70. S_IRGRP = 0o0040 # read by group
  71. S_IWGRP = 0o0020 # write by group
  72. S_IXGRP = 0o0010 # execute by group
  73. S_IRWXO = 0o0007 # mask for others (not in group) permissions
  74. S_IROTH = 0o0004 # read by others
  75. S_IWOTH = 0o0002 # write by others
  76. S_IXOTH = 0o0001 # execute by others
  77. # Names for file flags
  78. UF_NODUMP = 0x00000001 # do not dump file
  79. UF_IMMUTABLE = 0x00000002 # file may not be changed
  80. UF_APPEND = 0x00000004 # file may only be appended to
  81. UF_OPAQUE = 0x00000008 # directory is opaque when viewed through a union stack
  82. UF_NOUNLINK = 0x00000010 # file may not be renamed or deleted
  83. UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed
  84. UF_HIDDEN = 0x00008000 # OS X: file should not be displayed
  85. SF_ARCHIVED = 0x00010000 # file may be archived
  86. SF_IMMUTABLE = 0x00020000 # file may not be changed
  87. SF_APPEND = 0x00040000 # file may only be appended to
  88. SF_NOUNLINK = 0x00100000 # file may not be renamed or deleted
  89. SF_SNAPSHOT = 0x00200000 # file is a snapshot file
  90. _filemode_table = (
  91. ((S_IFLNK, "l"),
  92. (S_IFREG, "-"),
  93. (S_IFBLK, "b"),
  94. (S_IFDIR, "d"),
  95. (S_IFCHR, "c"),
  96. (S_IFIFO, "p")),
  97. ((S_IRUSR, "r"),),
  98. ((S_IWUSR, "w"),),
  99. ((S_IXUSR|S_ISUID, "s"),
  100. (S_ISUID, "S"),
  101. (S_IXUSR, "x")),
  102. ((S_IRGRP, "r"),),
  103. ((S_IWGRP, "w"),),
  104. ((S_IXGRP|S_ISGID, "s"),
  105. (S_ISGID, "S"),
  106. (S_IXGRP, "x")),
  107. ((S_IROTH, "r"),),
  108. ((S_IWOTH, "w"),),
  109. ((S_IXOTH|S_ISVTX, "t"),
  110. (S_ISVTX, "T"),
  111. (S_IXOTH, "x"))
  112. )
  113. def filemode(mode):
  114. """Convert a file's mode to a string of the form '-rwxrwxrwx'."""
  115. perm = []
  116. for table in _filemode_table:
  117. for bit, char in table:
  118. if mode & bit == bit:
  119. perm.append(char)
  120. break
  121. else:
  122. perm.append("-")
  123. return "".join(perm)
  124. # Windows FILE_ATTRIBUTE constants for interpreting os.stat()'s
  125. # "st_file_attributes" member
  126. FILE_ATTRIBUTE_ARCHIVE = 32
  127. FILE_ATTRIBUTE_COMPRESSED = 2048
  128. FILE_ATTRIBUTE_DEVICE = 64
  129. FILE_ATTRIBUTE_DIRECTORY = 16
  130. FILE_ATTRIBUTE_ENCRYPTED = 16384
  131. FILE_ATTRIBUTE_HIDDEN = 2
  132. FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
  133. FILE_ATTRIBUTE_NORMAL = 128
  134. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
  135. FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
  136. FILE_ATTRIBUTE_OFFLINE = 4096
  137. FILE_ATTRIBUTE_READONLY = 1
  138. FILE_ATTRIBUTE_REPARSE_POINT = 1024
  139. FILE_ATTRIBUTE_SPARSE_FILE = 512
  140. FILE_ATTRIBUTE_SYSTEM = 4
  141. FILE_ATTRIBUTE_TEMPORARY = 256
  142. FILE_ATTRIBUTE_VIRTUAL = 65536
  143. # If available, use C implementation
  144. try:
  145. from _stat import *
  146. except ImportError:
  147. pass