Use 'stat' module instead of hardcoding information from <sys/stat.h>.

This commit is contained in:
Guido van Rossum 1990-10-21 16:17:34 +00:00
parent 6b47ed1f9d
commit 40d9304d66
4 changed files with 17 additions and 36 deletions

View File

@ -8,10 +8,9 @@
# - Files with different type or size cannot be identical # - Files with different type or size cannot be identical
# - We keep a cache of outcomes of earlier comparisons # - We keep a cache of outcomes of earlier comparisons
# - We don't fork a process to run 'cmp' but read the files ourselves # - 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 posix
import stat
import statcache import statcache
@ -27,7 +26,7 @@ def cmp(f1, f2):
# Return 1 for identical files, 0 for different. # Return 1 for identical files, 0 for different.
# Raise exceptions if either file could not be statted, read, etc. # Raise exceptions if either file could not be statted, read, etc.
s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2)) 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 # Either is a not a plain file -- always report as different
return 0 return 0
if s1 = s2: if s1 = s2:
@ -53,12 +52,7 @@ def cmp(f1, f2):
# Return signature (i.e., type, size, mtime) from raw stat data. # Return signature (i.e., type, size, mtime) from raw stat data.
# #
def sig(st): def sig(st):
# 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid return stat.S_IFMT(st[ST_MODE]), st[stat.ST_SIZE], st[stat.ST_MTIME]
# 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
# Compare two files, really. # Compare two files, really.
# #

View File

@ -9,17 +9,7 @@ import path
import dircache import dircache
import cmpcache import cmpcache
import statcache import statcache
from stat import *
# 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
# Directory comparison class. # Directory comparison class.
# #
@ -79,13 +69,13 @@ class dircmp():
ok = 0 ok = 0
# #
if ok: if ok:
a_type = S_IFMT(a_stat) a_type = S_IFMT(a_stat[ST_MODE])
b_type = S_IFMT(b_stat) b_type = S_IFMT(b_stat[ST_MODE])
if a_type <> b_type: if a_type <> b_type:
dd.common_funny.append(x) dd.common_funny.append(x)
elif a_type = S_IFDIR: elif S_ISDIR(a_type):
dd.common_dirs.append(x) dd.common_dirs.append(x)
elif a_type = S_IFREG: elif S_ISREG(a_type):
dd.common_files.append(x) dd.common_files.append(x)
else: else:
dd.common_funny.append(x) dd.common_funny.append(x)

View File

@ -1,6 +1,7 @@
# Module 'path' -- common operations on POSIX pathnames # Module 'path' -- common operations on POSIX pathnames
import posix import posix
import stat
# Intelligent pathname concatenation. # Intelligent pathname concatenation.
@ -63,7 +64,7 @@ def isdir(path):
st = posix.stat(path) st = posix.stat(path)
except posix.error: except posix.error:
return 0 return 0
return st[0] / 4096 = 4 # S_IFDIR return stat.S_ISDIR(st[stat.ST_MODE])
# Is a path a symbolic link? # Is a path a symbolic link?
@ -74,7 +75,7 @@ def islink(path):
st = posix.lstat(path) st = posix.lstat(path)
except (posix.error, NameError): except (posix.error, NameError):
return 0 return 0
return st[0] / 4096 = 10 # S_IFLNK return stat.S_ISLNK(st[stat.ST_MODE])
_mounts = [] _mounts = []

View File

@ -4,7 +4,7 @@
# There are functions to reset the cache or to selectively remove items. # There are functions to reset the cache or to selectively remove items.
import posix import posix
from stat import *
# The cache. # The cache.
# Keys are pathnames, values are `posix.stat' outcomes. # Keys are pathnames, values are `posix.stat' outcomes.
@ -15,10 +15,8 @@ cache = {}
# Stat a file, possibly out of the cache. # Stat a file, possibly out of the cache.
# #
def stat(path): def stat(path):
try: if cache.has_key(path):
return cache[path] return cache[path]
except RuntimeError:
pass
cache[path] = ret = posix.stat(path) cache[path] = ret = posix.stat(path)
return ret return ret
@ -37,10 +35,8 @@ def reset():
# Remove a given item from the cache, if it exists. # Remove a given item from the cache, if it exists.
# #
def forget(path): def forget(path):
try: if cache.has_key(path):
del cache[path] del cache[path]
except RuntimeError:
pass
# Remove all pathnames with a given prefix. # Remove all pathnames with a given prefix.
@ -84,7 +80,7 @@ def forget_except_prefix(prefix):
# #
def isdir(path): def isdir(path):
try: try:
# mode is st[0]; type is mode/4096; S_IFDIR is 4 st = stat(path)
return stat(path)[0] / 4096 = 4 except posix.error:
except RuntimeError:
return 0 return 0
return S_ISDIR(st[ST_MODE])