From 40d9304d66edcab3925c75e9d8ad093562cf5d7b Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 21 Oct 1990 16:17:34 +0000 Subject: [PATCH] Use 'stat' module instead of hardcoding information from . --- Lib/cmpcache.py | 12 +++--------- Lib/dircmp.py | 20 +++++--------------- Lib/posixpath.py | 5 +++-- Lib/statcache.py | 16 ++++++---------- 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/Lib/cmpcache.py b/Lib/cmpcache.py index a47a4fd5aca..f85e0405752 100644 --- a/Lib/cmpcache.py +++ b/Lib/cmpcache.py @@ -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 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 + 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 - 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. # diff --git a/Lib/dircmp.py b/Lib/dircmp.py index 762a186095f..819f0bdf9f2 100644 --- a/Lib/dircmp.py +++ b/Lib/dircmp.py @@ -9,17 +9,7 @@ import path import dircache import cmpcache import statcache - - -# File type constants from . -# -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) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index e314cb3abfd..0c0d09f4b2f 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -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 = [] diff --git a/Lib/statcache.py b/Lib/statcache.py index eeb0ca41977..36ecc962d41 100644 --- a/Lib/statcache.py +++ b/Lib/statcache.py @@ -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])