def get_file_info(system, path, verbose=False):
fullpath = get_fullpath(system, path)
if fullpath == IO_ERROR:
return IO_ERROR
result = {}
if system.startswith('/'): # local or sub
if can_read(system, path):
# TODO platform-specific
stats = os.stat(fullpath)
stm = stats.st_mode
result['type'] = get_file_type_char(stat.S_IFMT(stm))
result['permissions'] = '%o' % (stat.S_IMODE(stm))
result['UID'] = stats[stat.ST_UID]
result['GID'] = stats[stat.ST_GID]
result['ATIME'] = stats[stat.ST_ATIME]
result['MTIME'] = stats[stat.ST_MTIME]
result['CTIME'] = stats[stat.ST_CTIME] # actually mtime on UNIX, TODO
else:
if verbose:
log.info('File \'%s\' is not accessible.' % (fullpath))
result['type'] = None
result['permissions'] = None
result['UID'] = None
result['GID'] = None
result['ATIME'] = None
result['MTIME'] = None
result['CTIME'] = None
return result
else: # SSH/FTP/TFTP/HTTP
# TODO NOT IMPLEMENTED
return IO_ERROR
python类ST_UID的实例源码
def sleep(st, repository, delay):
if delay <= 0:
raise Locked(st)
uid = st[stat.ST_UID]
try:
pwent = pwd.getpwuid(uid)
user = pwent[0]
except KeyError:
user = "uid %d" % uid
print "[%s]" % time.ctime(time.time())[11:19],
print "Waiting for %s's lock in" % user, repository
time.sleep(delay)
def unix_longify(file, stat_info):
# for now, only pay attention to the lower bits
mode = ('%o' % stat_info[stat.ST_MODE])[-3:]
mode = string.join(map(lambda x: mode_table[x], mode), '')
if stat.S_ISDIR(stat_info[stat.ST_MODE]):
dirchar = 'd'
else:
dirchar = '-'
date = ls_date(int(time.time()), stat_info[stat.ST_MTIME])
user = str(stat_info[stat.ST_UID]).replace(' ', '_')
group = str(stat_info[stat.ST_GID]).replace(' ', '_')
if user == 'System_Processes':
user = 'Sysproc'
if group == 'System_Processes':
group = 'Sysproc'
return '%s%s %3d %-8s %-8s %8d %s %s' % (
dirchar,
mode,
stat_info[stat.ST_NLINK],
user,
group,
stat_info[stat.ST_SIZE],
date,
file
)
# Emulate the unix 'ls' command's date field.
# it has two formats - if the date is more than 180
# days in the past, then it's like this:
# Oct 19 1995
# otherwise, it looks like this:
# Oct 19 17:33
def make_file_dict_python(filename):
"""Create the data dictionary using a Python call to os.lstat
We do this on Windows since Python's implementation is much better
than the one in cmodule.c Eventually, we will move to using
this on all platforms since CPUs have gotten much faster than
they were when it was necessary to write cmodule.c
"""
try:
statblock = os.lstat(filename)
except os.error:
return {'type':None}
data = {}
mode = statblock[stat.ST_MODE]
if stat.S_ISREG(mode): type_ = 'reg'
elif stat.S_ISDIR(mode): type_ = 'dir'
elif stat.S_ISCHR(mode):
type_ = 'dev'
s = statblock.st_rdev
data['devnums'] = ('c',) + (s >> 8, s & 0xff)
elif stat.S_ISBLK(mode):
type_ = 'dev'
s = statblock.st_rdev
data['devnums'] = ('b',) + (s >> 8, s & 0xff)
elif stat.S_ISFIFO(mode): type_ = 'fifo'
elif stat.S_ISLNK(mode):
type_ = 'sym'
data['linkname'] = os.readlink(filename)
elif stat.S_ISSOCK(mode): type_ = 'sock'
else: raise C.UnknownFileError(filename)
data['type'] = type_
data['size'] = statblock[stat.ST_SIZE]
data['perms'] = stat.S_IMODE(mode)
data['uid'] = statblock[stat.ST_UID]
data['gid'] = statblock[stat.ST_GID]
data['inode'] = statblock[stat.ST_INO]
data['devloc'] = statblock[stat.ST_DEV]
data['nlink'] = statblock[stat.ST_NLINK]
if os.name == 'nt':
global type
if type(filename) == unicode:
attribs = win32file.GetFileAttributesW(filename)
else:
attribs = win32file.GetFileAttributes(filename)
if attribs & winnt.FILE_ATTRIBUTE_REPARSE_POINT:
data['type'] = 'sym'
data['linkname'] = None
if not (type_ == 'sym' or type_ == 'dev'):
# mtimes on symlinks and dev files don't work consistently
data['mtime'] = long(statblock[stat.ST_MTIME])
data['atime'] = long(statblock[stat.ST_ATIME])
data['ctime'] = long(statblock[stat.ST_CTIME])
return data
def _check_path(pathname):
try:
info = os.stat(pathname)
except os.error, e:
return None, ["stat error: %s" % e]
kind = None
errors = []
mode = info[stat.ST_MODE]
isdir = stat.S_ISDIR(mode)
isreg = stat.S_ISREG(mode)
if isreg or isdir:
#
# Quick version of access() where we use existing stat() data.
#
# This might not be perfect -- the OS may return slightly different
# results for some bizarre reason. However, we make a good show of
# "can I read this file/dir?" by checking the various perm bits.
#
# NOTE: if the UID matches, then we must match the user bits -- we
# cannot defer to group or other bits. Similarly, if the GID matches,
# then we must have read access in the group bits.
#
# If the UID or GID don't match, we need to check the
# results of an os.access() call, in case the web server process
# is in the group that owns the directory.
#
if isdir:
mask = stat.S_IROTH | stat.S_IXOTH
else:
mask = stat.S_IROTH
if info[stat.ST_UID] == _uid:
if ((mode >> 6) & mask) != mask:
errors.append("error: path is not accessible to user %i" % _uid)
elif info[stat.ST_GID] == _gid:
if ((mode >> 3) & mask) != mask:
errors.append("error: path is not accessible to group %i" % _gid)
# If the process running the web server is a member of
# the group stat.ST_GID access may be granted.
# so the fall back to os.access is needed to figure this out.
elif (mode & mask) != mask:
if not os.access(pathname, isdir and (os.R_OK | os.X_OK) or os.R_OK):
errors.append("error: path is not accessible")
if isdir:
kind = vclib.DIR
else:
kind = vclib.FILE
else:
errors.append("error: path is not a file or directory")
return kind, errors