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
# - 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.
#

View file

@ -9,17 +9,7 @@
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 @@ def phase2(dd): # Distinguish files, directories, funnies
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)

View 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 = []

View 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 @@
# 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])