]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
authorGuido van Rossum <guido@python.org>
Sun, 21 Oct 1990 16:17:34 +0000 (16:17 +0000)
committerGuido van Rossum <guido@python.org>
Sun, 21 Oct 1990 16:17:34 +0000 (16:17 +0000)
Lib/cmpcache.py
Lib/dircmp.py
Lib/posixpath.py
Lib/statcache.py

index a47a4fd5aca8d3c4aaff1752ff0d68e0b35f470c..f85e04057529c23ccfa5cb6f99a5d7e72a193139 100644 (file)
@@ -8,10 +8,9 @@
 #      - Files with different type or size cannot be identical
 #      - We keep a cache of outcomes of earlier comparisons
 #      - We don't fork a process to run 'cmp' but read the files ourselves
-#
-# XXX There is a dependency on constants in <sys/stat.h> here.
 
 import posix
+import stat
 import statcache
 
 
@@ -27,7 +26,7 @@ def cmp(f1, f2):
        # Return 1 for identical files, 0 for different.
        # Raise exceptions if either file could not be statted, read, etc.
        s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2))
-       if s1[0] <> 8 or s2[0] <> 8: # XXX 8 is S_IFREG in <sys/stat.h>
+       if not stat.S_ISREG(s1[0]) or not stat.S_ISREG(s2[0]):
                # Either is a not a plain file -- always report as different
                return 0
        if s1 = s2:
@@ -53,12 +52,7 @@ def cmp(f1, f2):
 # Return signature (i.e., type, size, mtime) from raw stat data.
 #
 def sig(st):
-       # 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
-       # 6-9: st_size, st_atime, st_mtime, st_ctime
-       type = st[0] / 4096 # XXX dependent on S_IFMT in <sys/stat.h>
-       size = st[6]
-       mtime = st[8]
-       return type, size, mtime
+       return stat.S_IFMT(st[ST_MODE]), st[stat.ST_SIZE], st[stat.ST_MTIME]
 
 # Compare two files, really.
 #
index 762a186095f71a3e05d5ac9b55000d3d7b3af5e8..819f0bdf9f28c085aea53fcf7071966626f5f2a1 100644 (file)
@@ -9,17 +9,7 @@ import path
 import dircache
 import cmpcache
 import statcache
-
-
-# File type constants from <sys/stat.h>.
-#
-S_IFDIR = 4
-S_IFREG = 8
-
-# Extract the file type from a stat buffer.
-#
-def S_IFMT(st): return st[0] / 4096
-
+from stat import *
 
 # Directory comparison class.
 #
@@ -79,13 +69,13 @@ class dircmp():
                                ok = 0
                        #
                        if ok:
-                               a_type = S_IFMT(a_stat)
-                               b_type = S_IFMT(b_stat)
+                               a_type = S_IFMT(a_stat[ST_MODE])
+                               b_type = S_IFMT(b_stat[ST_MODE])
                                if a_type <> b_type:
                                        dd.common_funny.append(x)
-                               elif a_type = S_IFDIR:
+                               elif S_ISDIR(a_type):
                                        dd.common_dirs.append(x)
-                               elif a_type = S_IFREG:
+                               elif S_ISREG(a_type):
                                        dd.common_files.append(x)
                                else:
                                        dd.common_funny.append(x)
index e314cb3abfd9108a1f40b951db421615addebb14..0c0d09f4b2f03a1950aa9d3d89d790dc9295734d 100644 (file)
@@ -1,6 +1,7 @@
 # Module 'path' -- common operations on POSIX pathnames
 
 import posix
+import stat
 
 
 # Intelligent pathname concatenation.
@@ -63,7 +64,7 @@ def isdir(path):
                st = posix.stat(path)
        except posix.error:
                return 0
-       return st[0] / 4096 = 4 # S_IFDIR
+       return stat.S_ISDIR(st[stat.ST_MODE])
 
 
 # Is a path a symbolic link?
@@ -74,7 +75,7 @@ def islink(path):
                st = posix.lstat(path)
        except (posix.error, NameError):
                return 0
-       return st[0] / 4096 = 10 # S_IFLNK
+       return stat.S_ISLNK(st[stat.ST_MODE])
 
 
 _mounts = []
index eeb0ca419775712b2a1abda66886dff08dc8b77c..36ecc962d413699656a4dad820a19434fcc98bbd 100644 (file)
@@ -4,7 +4,7 @@
 # There are functions to reset the cache or to selectively remove items.
 
 import posix
-
+from stat import *
 
 # The cache.
 # Keys are pathnames, values are `posix.stat' outcomes.
@@ -15,10 +15,8 @@ cache = {}
 # Stat a file, possibly out of the cache.
 #
 def stat(path):
-       try:
+       if cache.has_key(path):
                return cache[path]
-       except RuntimeError:
-               pass
        cache[path] = ret = posix.stat(path)
        return ret
 
@@ -37,10 +35,8 @@ def reset():
 # Remove a given item from the cache, if it exists.
 #
 def forget(path):
-       try:
+       if cache.has_key(path):
                del cache[path]
-       except RuntimeError:
-               pass
 
 
 # Remove all pathnames with a given prefix.
@@ -84,7 +80,7 @@ def forget_except_prefix(prefix):
 #
 def isdir(path):
        try:
-               # mode is st[0]; type is mode/4096; S_IFDIR is 4
-               return stat(path)[0] / 4096 = 4
-       except RuntimeError:
+               st = stat(path)
+       except posix.error:
                return 0
+       return S_ISDIR(st[ST_MODE])