我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用stat.S_ISVTX。
def find_ext_volume_global_trash(volume_root): # from [2] Trash directories (1) check for a .Trash dir with the right # permissions set. trash_dir = op.join(volume_root, TOPDIR_TRASH) if not op.exists(trash_dir): return None mode = os.lstat(trash_dir).st_mode # vol/.Trash must be a directory, cannot be a symlink, and must have the # sticky bit set. if not op.isdir(trash_dir) or op.islink(trash_dir) or not (mode & stat.S_ISVTX): return None trash_dir = op.join(trash_dir, str(uid)) try: check_create(trash_dir) except OSError: return None return trash_dir
def _apply_operation_to_mode(user, operator, mode_to_apply, current_mode): if operator == '=': if user == 'u': mask = stat.S_IRWXU | stat.S_ISUID elif user == 'g': mask = stat.S_IRWXG | stat.S_ISGID elif user == 'o': mask = stat.S_IRWXO | stat.S_ISVTX # mask out u, g, or o permissions from current_mode and apply new permissions inverse_mask = mask ^ PERM_BITS new_mode = (current_mode & inverse_mask) | mode_to_apply elif operator == '+': new_mode = current_mode | mode_to_apply elif operator == '-': new_mode = current_mode - (current_mode & mode_to_apply) return new_mode
def insecure_inode(path): """This particular inode can be altered by someone other than the owner""" pathstat = os.stat(path).st_mode # Directories with a sticky bit are always acceptable. if os.path.isdir(path) and pathstat & stat.S_ISVTX: return False # The path is writable by someone who is not us. elif pathstat & (stat.S_IWGRP | stat.S_IWOTH): return True else: return False
def _apply_operation_to_mode(self, user, operator, mode_to_apply, current_mode): if operator == '=': if user == 'u': mask = stat.S_IRWXU | stat.S_ISUID elif user == 'g': mask = stat.S_IRWXG | stat.S_ISGID elif user == 'o': mask = stat.S_IRWXO | stat.S_ISVTX # mask out u, g, or o permissions from current_mode and apply new permissions inverse_mask = mask ^ PERM_BITS new_mode = (current_mode & inverse_mask) | mode_to_apply elif operator == '+': new_mode = current_mode | mode_to_apply elif operator == '-': new_mode = current_mode - (current_mode & mode_to_apply) return new_mode
def get_tree(self, path): path = path.lstrip(os.sep.encode('utf-8')) directory = os.path.join(self.root, path) tree = Tree() directory_items = self._sftp.listdir_attr_b(directory) for fattr in directory_items: filename = fattr.filename item = {} if stat.S_ISREG(fattr.st_mode): item['type'] = 'blob' item['filetype'] = 'regular' elif stat.S_ISDIR(fattr.st_mode): item['type'] = 'tree' item['filetype'] = 'directory' elif stat.S_ISLNK(fattr.st_mode): item['filetype'] = 'link' item['link'] = self._sftp.readlink(os.path.join(directory, filename)).encode('utf8') elif stat.S_ISFIFO(fattr.st_mode): item['filetype'] = 'fifo' else: continue # FIXME: Warn fmode = fattr.st_mode & (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO | stat.S_ISVTX) item['uid'] = fattr.st_uid item['gid'] = fattr.st_gid item['mode'] = fmode item['mtime'] = int(fattr.st_mtime) item['size'] = fattr.st_size tree.add(filename, item) return tree
def get_tree(self, path): path = path.lstrip(os.sep.encode('utf-8')) directory = os.path.join(self.root, path) tree = Tree() try: directory_items = os.listdir(directory) except OSError as err: raise RemoteOperationError(err.strerror) for filename in directory_items: assert isinstance(filename, bytes) item = {} fullname = os.path.join(directory, filename) fstat = os.lstat(fullname) if stat.S_ISREG(fstat.st_mode): item['type'] = 'blob' item['filetype'] = 'regular' elif stat.S_ISDIR(fstat.st_mode): item['type'] = 'tree' item['filetype'] = 'directory' elif stat.S_ISLNK(fstat.st_mode): item['filetype'] = 'link' item['link'] = os.readlink(os.path.join(directory, filename)) elif stat.S_ISFIFO(fstat.st_mode): item['filetype'] = 'fifo' else: continue # FIXME: Warn fmode = fstat.st_mode & (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO | stat.S_ISVTX) item['uid'] = fstat.st_uid item['gid'] = fstat.st_gid item['mode'] = fmode item['atime'] = int(fstat.st_atime) item['mtime'] = int(fstat.st_mtime) item['ctime'] = int(fstat.st_ctime) item['size'] = fstat.st_size tree.add(filename, item) return tree
def test_make_fsroot(self): """Validates directory layout in chrooted environment.""" native.make_fsroot(self.root, 'myproid') def isdir(path): """Checks directory presence in chrooted environment.""" return os.path.isdir(os.path.join(self.root, path)) def issticky(path): """Checks directory mode in chrooted environment.""" statinfo = os.stat(os.path.join(self.root, path)) return statinfo.st_mode & stat.S_ISVTX self.assertTrue(isdir('tmp')) self.assertTrue(isdir('opt')) self.assertTrue(isdir('var/spool/keytabs')) self.assertTrue(isdir('var/spool/tickets')) self.assertTrue(isdir('var/spool/tokens')) self.assertTrue(isdir('var/tmp')) self.assertTrue(isdir('var/tmp/cores')) # self.assertTrue(isdir('home')) self.assertTrue(issticky('tmp')) self.assertTrue(issticky('opt')) self.assertTrue(issticky('var/tmp')) self.assertTrue(issticky('var/tmp/cores')) self.assertTrue(issticky('var/spool/tickets')) treadmill.fs.mount_tmpfs.assert_has_calls([ mock.call(mock.ANY, '/var/spool/tickets', mock.ANY), mock.call(mock.ANY, '/var/spool/keytabs', mock.ANY) ]) treadmill.fs.mount_bind.assert_has_calls([ mock.call(mock.ANY, '/bin') ])
def configure(self, container_dir, app): root_dir = os.path.join(container_dir, 'root') newroot_norm = fs.norm_safe(root_dir) mounts = [ ] emptydirs = [ '/u', '/var/account', '/var/empty', '/var/lock', '/var/log', '/var/run', ] stickydirs = [ '/opt', ] for mount in mounts: if os.path.exists(mount): fs.mount_bind(newroot_norm, mount) for directory in emptydirs: fs.mkdir_safe(newroot_norm + directory) for directory in stickydirs: os.chmod(newroot_norm + directory, 0o777 | stat.S_ISVTX)
def init(self): self.block_id_references = collections.defaultdict(weakref.WeakSet) self.fds = Allocator() self.inodes = inode.INodeAllocator(self, self.root_inode) self.dirty_node_set = set() self.dirty_file_set = set() block_id = self.storage.get_block_id_by_name(self.content_name) tn = self.directory_node_class(forest=self, block_id=block_id) self.root = self.inodes.add_inode(tn, value=self.root_inode) self.root.direntry.set_data('st_mode', self.root.direntry.mode | stat.S_ISVTX) # Root dir is sticky by default
def humanUnixAttributes(mode): """ Convert a Unix file attributes (or "file mode") to an unicode string. Original source code: http://cvs.savannah.gnu.org/viewcvs/coreutils/lib/filemode.c?root=coreutils >>> humanUnixAttributes(0o644) '-rw-r--r-- (644)' >>> humanUnixAttributes(0o2755) '-rwxr-sr-x (2755)' """ chars = [_ftypelet(mode), 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x'] for i in range(1, 10): if not mode & 1 << 9 - i: chars[i] = '-' if mode & stat.S_ISUID: if chars[3] != 'x': chars[3] = 'S' else: chars[3] = 's' if mode & stat.S_ISGID: if chars[6] != 'x': chars[6] = 'S' else: chars[6] = 's' if mode & stat.S_ISVTX: if chars[9] != 'x': chars[9] = 'T' else: chars[9] = 't' return "%s (%o)" % (''.join(chars), mode)
def __str__(self): "create a unix-style long description of the file (like ls -l)" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & 0700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & 070) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == 0xffffffffL): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid if uid is None: uid = 0 if gid is None: gid = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, self.st_size, datestr, filename)
def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms): prev_mode = stat.S_IMODE(path_stat.st_mode) is_directory = stat.S_ISDIR(path_stat.st_mode) has_x_permissions = (prev_mode & EXEC_PERM_BITS) > 0 apply_X_permission = is_directory or has_x_permissions # Permission bits constants documented at: # http://docs.python.org/2/library/stat.html#stat.S_ISUID if apply_X_permission: X_perms = { 'u': {'X': stat.S_IXUSR}, 'g': {'X': stat.S_IXGRP}, 'o': {'X': stat.S_IXOTH} } else: X_perms = { 'u': {'X': 0}, 'g': {'X': 0}, 'o': {'X': 0} } user_perms_to_modes = { 'u': { 'r': stat.S_IRUSR, 'w': stat.S_IWUSR, 'x': stat.S_IXUSR, 's': stat.S_ISUID, 't': 0, 'u': prev_mode & stat.S_IRWXU, 'g': (prev_mode & stat.S_IRWXG) << 3, 'o': (prev_mode & stat.S_IRWXO) << 6 }, 'g': { 'r': stat.S_IRGRP, 'w': stat.S_IWGRP, 'x': stat.S_IXGRP, 's': stat.S_ISGID, 't': 0, 'u': (prev_mode & stat.S_IRWXU) >> 3, 'g': prev_mode & stat.S_IRWXG, 'o': (prev_mode & stat.S_IRWXO) << 3 }, 'o': { 'r': stat.S_IROTH, 'w': stat.S_IWOTH, 'x': stat.S_IXOTH, 's': 0, 't': stat.S_ISVTX, 'u': (prev_mode & stat.S_IRWXU) >> 6, 'g': (prev_mode & stat.S_IRWXG) >> 3, 'o': prev_mode & stat.S_IRWXO } } # Insert X_perms into user_perms_to_modes for key, value in X_perms.items(): user_perms_to_modes[key].update(value) or_reduce = lambda mode, perm: mode | user_perms_to_modes[user][perm] return reduce(or_reduce, perms, 0)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid if uid is None: uid = 0 if gid is None: gid = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, self.st_size, datestr, filename)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx((self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx((self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx(self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime('%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime('%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid size = self.st_size if uid is None: uid = 0 if gid is None: gid = 0 if size is None: size = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % (ks, uid, gid, size, datestr, filename)
def __str__(self): """create a unix-style long description of the file (like ls -l)""" if self.st_mode is not None: kind = stat.S_IFMT(self.st_mode) if kind == stat.S_IFIFO: ks = 'p' elif kind == stat.S_IFCHR: ks = 'c' elif kind == stat.S_IFDIR: ks = 'd' elif kind == stat.S_IFBLK: ks = 'b' elif kind == stat.S_IFREG: ks = '-' elif kind == stat.S_IFLNK: ks = 'l' elif kind == stat.S_IFSOCK: ks = 's' else: ks = '?' ks += self._rwx( (self.st_mode & o700) >> 6, self.st_mode & stat.S_ISUID) ks += self._rwx( (self.st_mode & o70) >> 3, self.st_mode & stat.S_ISGID) ks += self._rwx( self.st_mode & 7, self.st_mode & stat.S_ISVTX, True) else: ks = '?---------' # compute display date if (self.st_mtime is None) or (self.st_mtime == xffffffff): # shouldn't really happen datestr = '(unknown date)' else: if abs(time.time() - self.st_mtime) > 15552000: # (15552000 = 6 months) datestr = time.strftime( '%d %b %Y', time.localtime(self.st_mtime)) else: datestr = time.strftime( '%d %b %H:%M', time.localtime(self.st_mtime)) filename = getattr(self, 'filename', '?') # not all servers support uid/gid uid = self.st_uid gid = self.st_gid size = self.st_size if uid is None: uid = 0 if gid is None: gid = 0 if size is None: size = 0 return '%s 1 %-8d %-8d %8d %-12s %s' % ( ks, uid, gid, size, datestr, filename)
def humanUnixAttributes(mode): """ Convert a Unix file attributes (or "file mode") to an unicode string. Original source code: http://cvs.savannah.gnu.org/viewcvs/coreutils/lib/filemode.c?root=coreutils >>> humanUnixAttributes(0644) u'-rw-r--r-- (644)' >>> humanUnixAttributes(02755) u'-rwxr-sr-x (2755)' """ def ftypelet(mode): if stat.S_ISREG (mode) or not stat.S_IFMT(mode): return '-' if stat.S_ISBLK (mode): return 'b' if stat.S_ISCHR (mode): return 'c' if stat.S_ISDIR (mode): return 'd' if stat.S_ISFIFO(mode): return 'p' if stat.S_ISLNK (mode): return 'l' if stat.S_ISSOCK(mode): return 's' return '?' chars = [ ftypelet(mode), 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x' ] for i in xrange(1, 10): if not mode & 1 << 9 - i: chars[i] = '-' if mode & stat.S_ISUID: if chars[3] != 'x': chars[3] = 'S' else: chars[3] = 's' if mode & stat.S_ISGID: if chars[6] != 'x': chars[6] = 'S' else: chars[6] = 's' if mode & stat.S_ISVTX: if chars[9] != 'x': chars[9] = 'T' else: chars[9] = 't' return u"%s (%o)" % (''.join(chars), mode)