rpath.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:rdiff-backup 作者: sol1 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号