def __init__(self, is_dir=False, is_file=False, size=0, ctime=time(), mtime=time(), atime=time()):
self.is_dir = is_dir
self.is_file = is_file
if self.is_dir:
self.attr = dict(st_mode=(S_IFDIR | 0o755), st_nlink=2)
if self.is_file:
self.attr = dict(st_mode=(S_IFREG | 0o755), st_nlink=1, st_size=size,
st_ctime=ctime, st_mtime=mtime, st_atime=atime)
self.attr["attrs"] = {}
python类S_IFREG的实例源码
def test_mknod_c(oc):
a = oc.ops.mknod(llfuse.ROOT_INODE, b'cdev',
stat.S_IFCHR, 42, oc.rctx_user)
assert a.st_rdev == 42
oc.ops.forget1(a.st_ino)
a = oc.ops.mknod(llfuse.ROOT_INODE, b'bdev',
stat.S_IFBLK, 43, oc.rctx_user)
assert a.st_rdev == 43
oc.ops.forget1(a.st_ino)
a = oc.ops.mknod(llfuse.ROOT_INODE, b'regfile',
stat.S_IFREG, 44, oc.rctx_user)
assert not a.st_rdev
oc.ops.forget1(a.st_ino)
def create_file(self, dir_inode, name, *, mode=0):
if not stat.S_IFMT(mode):
mode |= stat.S_IFREG
dir_inode.change_times()
_debug('create_file %s 0x%x', name, mode)
return self._create(mode, dir_inode, name)
def test_mode(self):
with open(TESTFN, 'w'):
pass
if os.name == 'posix':
os.chmod(TESTFN, 0o700)
st_mode = self.get_mode()
self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IMODE(st_mode),
stat.S_IRWXU)
os.chmod(TESTFN, 0o070)
st_mode = self.get_mode()
self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IMODE(st_mode),
stat.S_IRWXG)
os.chmod(TESTFN, 0o007)
st_mode = self.get_mode()
self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IMODE(st_mode),
stat.S_IRWXO)
os.chmod(TESTFN, 0o444)
st_mode = self.get_mode()
self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IMODE(st_mode), 0o444)
else:
os.chmod(TESTFN, 0o700)
st_mode = self.get_mode()
self.assertS_IS("REG", st_mode)
self.assertEqual(stat.S_IFMT(st_mode),
stat.S_IFREG)
def cmp(f1, f2, shallow=1):
"""Compare two files.
Arguments:
f1 -- First file name
f2 -- Second file name
shallow -- Just check stat signature (do not read the files).
defaults to 1.
Return value:
True if the files are the same, False otherwise.
This function uses a cache for past comparisons and the results,
with a cache invalidation mechanism relying on stale signatures.
"""
s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
return False
if shallow and s1 == s2:
return True
if s1[1] != s2[1]:
return False
outcome = _cache.get((f1, f2, s1, s2))
if outcome is None:
outcome = _do_cmp(f1, f2)
if len(_cache) > 100: # limit the maximum size of the cache
_cache.clear()
_cache[f1, f2, s1, s2] = outcome
return outcome
def cmp(f1, f2, shallow=True):
"""Compare two files.
Arguments:
f1 -- First file name
f2 -- Second file name
shallow -- Just check stat signature (do not read the files).
defaults to True.
Return value:
True if the files are the same, False otherwise.
This function uses a cache for past comparisons and the results,
with cache entries invalidated if their stat information
changes. The cache may be cleared by calling clear_cache().
"""
s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
return False
if shallow and s1 == s2:
return True
if s1[1] != s2[1]:
return False
outcome = _cache.get((f1, f2, s1, s2))
if outcome is None:
outcome = _do_cmp(f1, f2)
if len(_cache) > 100: # limit the maximum size of the cache
clear_cache()
_cache[f1, f2, s1, s2] = outcome
return outcome
def getattr(self, path, fh=None):
first_dir = common.get_first_dir(path)
if first_dir in self.dirs:
return self.dirs[first_dir].getattr(common.remove_first_dir(path), fh)
uid, gid, pid = fuse_get_context()
if path == '/' or path in self.dirs:
st = {'st_mode': (stat.S_IFDIR | 0o555), 'st_nlink': 2}
elif path.lower() in self.files:
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': self.files[path.lower()]['size'], 'st_nlink': 1}
else:
raise FuseOSError(errno.ENOENT)
return {**st, **self.g_stat, 'st_uid': uid, 'st_gid': gid}
def getattr(self, path, fh=None):
first_dir = common.get_first_dir(path)
if first_dir in self.dirs:
return self.dirs[first_dir].getattr(common.remove_first_dir(path), fh)
uid, gid, pid = fuse_get_context()
if path == '/':
st = {'st_mode': (stat.S_IFDIR | 0o555), 'st_nlink': 2}
elif path.lower() in self.files:
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': self.files[path.lower()]['size'], 'st_nlink': 1}
else:
raise FuseOSError(errno.ENOENT)
return {**st, **self.g_stat, 'st_uid': uid, 'st_gid': gid}
def getattr(self, path, fh=None):
uid, gid, pid = fuse_get_context()
if path == '/':
st = {'st_mode': (stat.S_IFDIR | (0o555 if self.readonly else 0o777)), 'st_nlink': 2}
elif path.lower() in self.files:
st = {'st_mode': (stat.S_IFREG | (0o444 if (self.readonly or path.lower() == '/_nandinfo.txt') else 0o666)),
'st_size': self.files[path.lower()]['size'], 'st_nlink': 1}
else:
raise FuseOSError(errno.ENOENT)
return {**st, **self.g_stat, 'st_uid': uid, 'st_gid': gid}
def getattr(self, path, fh=None):
first_dir = common.get_first_dir(path)
if first_dir in self.dirs:
return self.dirs[first_dir].getattr(common.remove_first_dir(path), fh)
uid, gid, pid = fuse_get_context()
if path == '/' or path in self.dirs:
st = {'st_mode': (stat.S_IFDIR | 0o555), 'st_nlink': 2}
elif path.lower() in self.files:
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': self.files[path.lower()]['size'], 'st_nlink': 1}
else:
raise FuseOSError(errno.ENOENT)
return {**st, **self.g_stat, 'st_uid': uid, 'st_gid': gid}
def getattr(self, path, fh=None):
uid, gid, pid = fuse_get_context()
try:
item = self.romfs_reader.get_info_from_path(path)
except romfs.RomFSFileNotFoundException:
raise FuseOSError(errno.ENOENT)
if item.type == 'dir':
st = {'st_mode': (stat.S_IFDIR | 0o555), 'st_nlink': 2}
elif item.type == 'file':
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': item.size, 'st_nlink': 1}
else:
# this won't happen unless I fucked up
raise FuseOSError(errno.ENOENT)
return {**st, **self.g_stat, 'st_uid': uid, 'st_gid': gid}
def cmp(f1, f2, shallow=1):
"""Compare two files.
Arguments:
f1 -- First file name
f2 -- Second file name
shallow -- Just check stat signature (do not read the files).
defaults to 1.
Return value:
True if the files are the same, False otherwise.
This function uses a cache for past comparisons and the results,
with a cache invalidation mechanism relying on stale signatures.
"""
s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
return False
if shallow and s1 == s2:
return True
if s1[1] != s2[1]:
return False
outcome = _cache.get((f1, f2, s1, s2))
if outcome is None:
outcome = _do_cmp(f1, f2)
if len(_cache) > 100: # limit the maximum size of the cache
_cache.clear()
_cache[f1, f2, s1, s2] = outcome
return outcome
def cmp(f1, f2, shallow=1):
"""Compare two files.
Arguments:
f1 -- First file name
f2 -- Second file name
shallow -- Just check stat signature (do not read the files).
defaults to 1.
Return value:
True if the files are the same, False otherwise.
This function uses a cache for past comparisons and the results,
with a cache invalidation mechanism relying on stale signatures.
"""
s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
return False
if shallow and s1 == s2:
return True
if s1[1] != s2[1]:
return False
outcome = _cache.get((f1, f2, s1, s2))
if outcome is None:
outcome = _do_cmp(f1, f2)
if len(_cache) > 100: # limit the maximum size of the cache
_cache.clear()
_cache[f1, f2, s1, s2] = outcome
return outcome
def getattr(self):
return {
'st_size': len(self.get_data()),
'st_atime': self.get_st_atime(),
'st_mtime': self.get_st_mtime(),
'st_ctime': self.get_st_ctime(),
'st_uid': os.getuid(),
'st_gid': os.getegid(),
'st_mode': S_IFREG | self.mode,
'st_nlink': 1,
}
def stat(self):
st = os.lstat(self.full_path)
stat = {
key: getattr(st, key)
for key in (
'st_atime', 'st_ctime', 'st_gid', 'st_mtime', 'st_nlink',
'st_size', 'st_uid'
)
}
stat['st_mode'] = S_IFREG | 0o770
return stat
def getattr(self, submission=None, path=None):
if self.stat is None:
super(File, self).getattr(submission, path)
self.stat['st_mode'] = S_IFREG | 0o770
self.stat['st_nlink'] = 1
if self.stat['st_size'] is None:
self.stat['st_size'] = len(self.data)
return self.stat
def cmp(f1, f2, shallow=True):
"""Compare two files.
Arguments:
f1 -- First file name
f2 -- Second file name
shallow -- Just check stat signature (do not read the files).
defaults to True.
Return value:
True if the files are the same, False otherwise.
This function uses a cache for past comparisons and the results,
with cache entries invalidated if their stat information
changes. The cache may be cleared by calling clear_cache().
"""
s1 = _sig(os.stat(f1))
s2 = _sig(os.stat(f2))
if s1[0] != stat.S_IFREG or s2[0] != stat.S_IFREG:
return False
if shallow and s1 == s2:
return True
if s1[1] != s2[1]:
return False
outcome = _cache.get((f1, f2, s1, s2))
if outcome is None:
outcome = _do_cmp(f1, f2)
if len(_cache) > 100: # limit the maximum size of the cache
clear_cache()
_cache[f1, f2, s1, s2] = outcome
return outcome
def _to_mode(attr):
m = 0
if (attr & FILE_ATTRIBUTE_DIRECTORY):
m |= stdstat.S_IFDIR | 0o111
else:
m |= stdstat.S_IFREG
if (attr & FILE_ATTRIBUTE_READONLY):
m |= 0o444
else:
m |= 0o666
return m
def _read_value(self, path_components):
self.key_name = "\\".join(path_components[1:-1])
self.value_name = path_components[-1]
with OpenKey(self._hive_handle, self.key_name) as key:
# We are a value - we can be read but we can not be listed.
self.value, value_type = QueryValueEx(key, self.value_name)
self.st_mode = stat.S_IFREG
self.value_type = self.registry_map[value_type]
self.st_size = len(utils.SmartStr(self.value))
def readdir(self, path, offset):
if path == "/":
for pid in self.tasks:
result = fuse.Direntry(str(pid))
result.type = stat.S_IFREG
result.st_size = self.address_space_size
yield result