本文整理汇总了Python中os.fchown方法的典型用法代码示例。如果您正苦于以下问题:Python os.fchown方法的具体用法?Python os.fchown怎么用?Python os.fchown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.fchown方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_file_owner
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def set_file_owner(path, owner, group=None, fd=None):
if owner:
uid = pwd.getpwnam(owner).pw_uid
else:
uid = os.geteuid()
if group:
gid = grp.getgrnam(group).gr_gid
else:
gid = os.getegid()
if fd is not None and hasattr(os, 'fchown'):
os.fchown(fd, (uid, gid))
else:
# Python<2.6
os.chown(path, (uid, gid))
示例2: _test_all_chown_common
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _test_all_chown_common(self, chown_func, first_param):
"""Common code for chown, fchown and lchown tests."""
if os.getuid() == 0:
try:
# Many linux distros have a nfsnobody user as MAX_UID-2
# that makes a good test case for signedness issues.
# http://bugs.python.org/issue1747858
# This part of the test only runs when run as root.
# Only scary people run their tests as root.
ent = pwd.getpwnam('nfsnobody')
chown_func(first_param, ent.pw_uid, ent.pw_gid)
except KeyError:
pass
else:
# non-root cannot chown to root, raises OSError
self.assertRaises(OSError, chown_func,
first_param, 0, 0)
# test a successful chown call
chown_func(first_param, os.getuid(), os.getgid())
示例3: test_fchown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
self.check(os.fchown, -1, -1)
示例4: test_fchown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
os.unlink(test_support.TESTFN)
# re-create the file
test_file = open(test_support.TESTFN, 'w')
try:
fd = test_file.fileno()
self._test_all_chown_common(posix.fchown, fd,
getattr(posix, 'fstat', None))
finally:
test_file.close()
示例5: test_fchown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
if hasattr(os, "fchown"):
self.check(os.fchown, -1, -1)
示例6: setstat
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def setstat(self, filename, attrs, fsetstat=False):
"""setstat and fsetstat requests.
Filename is an handle in the fstat variant.
If you're using Python < 3.3,
you could find useful the futimes file / function.
"""
if not fsetstat:
f = os.open(filename, os.O_WRONLY)
chown = os.chown
chmod = os.chmod
else: # filename is a fd
f = filename
chown = os.fchown
chmod = os.fchmod
if b'size' in attrs:
os.ftruncate(f, attrs[b'size'])
if all(k in attrs for k in (b'uid', b'gid')):
chown(filename, attrs[b'uid'], attrs[b'gid'])
if b'perm' in attrs:
chmod(filename, attrs[b'perm'])
if all(k in attrs for k in (b'atime', b'mtime')):
if not fsetstat:
os.utime(filename, (attrs[b'atime'], attrs[b'mtime']))
else:
futimes(filename, (attrs[b'atime'], attrs[b'mtime']))
示例7: test_fchown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
os.unlink(support.TESTFN)
# re-create the file
test_file = open(support.TESTFN, 'w')
try:
fd = test_file.fileno()
self._test_all_chown_common(posix.fchown, fd,
getattr(posix, 'fstat', None))
finally:
test_file.close()
示例8: _chown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _chown(self):
if self.uid >= 0 or self.gid >= 0:
os.fchown(self.fh.fileno(), self.uid, self.gid)
示例9: copy
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def copy(self, dst, src=None):
"""Atomically copy tickets to destination."""
if src is None:
src = self.tkt_path
dst_dir = os.path.dirname(dst)
with io.open(src, 'rb') as tkt_src_file:
# TODO; rewrite as fs.write_safe.
with tempfile.NamedTemporaryFile(dir=dst_dir,
prefix='.tmp' + self.princ,
delete=False,
mode='wb') as tkt_dst_file:
try:
# Copy binary from source to dest
shutil.copyfileobj(tkt_src_file, tkt_dst_file)
# Set the owner
if self.uid is not None:
os.fchown(tkt_dst_file.fileno(), self.uid, -1)
# Copy the mode
src_stat = os.fstat(tkt_src_file.fileno())
os.fchmod(tkt_dst_file.fileno(),
stat.S_IMODE(src_stat.st_mode))
tkt_dst_file.flush()
os.rename(tkt_dst_file.name, dst)
except (IOError, OSError):
_LOGGER.exception('Error copying ticket from %s to %s',
src, dst)
finally:
fs.rm_safe(tkt_dst_file.name)
示例10: test_fchown
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
os.unlink(test_support.TESTFN)
# re-create the file
test_file = open(test_support.TESTFN, 'w')
try:
fd = test_file.fileno()
self._test_all_chown_common(posix.fchown, fd)
finally:
test_file.close()
示例11: _test_all_chown_common
# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _test_all_chown_common(self, chown_func, first_param, stat_func):
"""Common code for chown, fchown and lchown tests."""
def check_stat(uid, gid):
if stat_func is not None:
stat = stat_func(first_param)
self.assertEqual(stat.st_uid, uid)
self.assertEqual(stat.st_gid, gid)
uid = os.getuid()
gid = os.getgid()
# test a successful chown call
chown_func(first_param, uid, gid)
check_stat(uid, gid)
chown_func(first_param, -1, gid)
check_stat(uid, gid)
chown_func(first_param, uid, -1)
check_stat(uid, gid)
if uid == 0:
# Try an amusingly large uid/gid to make sure we handle
# large unsigned values. (chown lets you use any
# uid/gid you like, even if they aren't defined.)
#
# This problem keeps coming up:
# http://bugs.python.org/issue1747858
# http://bugs.python.org/issue4591
# http://bugs.python.org/issue15301
# Hopefully the fix in 4591 fixes it for good!
#
# This part of the test only runs when run as root.
# Only scary people run their tests as root.
big_value = 2**31
chown_func(first_param, big_value, big_value)
check_stat(big_value, big_value)
chown_func(first_param, -1, -1)
check_stat(big_value, big_value)
chown_func(first_param, uid, gid)
check_stat(uid, gid)
elif platform.system() in ('HP-UX', 'SunOS'):
# HP-UX and Solaris can allow a non-root user to chown() to root
# (issue #5113)
raise unittest.SkipTest("Skipping because of non-standard chown() "
"behavior")
else:
# non-root cannot chown to root, raises OSError
self.assertRaises(OSError, chown_func, first_param, 0, 0)
check_stat(uid, gid)
self.assertRaises(OSError, chown_func, first_param, 0, -1)
check_stat(uid, gid)
if 0 not in os.getgroups():
self.assertRaises(OSError, chown_func, first_param, -1, 0)
check_stat(uid, gid)
# test illegal types
for t in str, float:
self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
check_stat(uid, gid)
self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
check_stat(uid, gid)