为什么os.chflags()在Linux下不起作用? [英] Why doesn't os.chflags() work under Linux?

查看:64
本文介绍了为什么os.chflags()在Linux下不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在64位Debian GNU/Linux 8(jessie)下使用Python 2.7.9.我只是尝试通过调用 os.chflags(path,mode) 来更改文件属性.在Python文档中,有一个有关 os的文章接口,该接口表示该方法在 Unix 中可用,但不适用于Linux.Python总是抛出:

  Traceback(最近一次通话最近):在< module>中的文件"/home/lexer/py/epam/tests/main.py"的第43行.os.chflags(path_to_file(file_name),stat.SF_NOUNLINK)AttributeError:模块"对象没有属性"chflags" 

有一个问题已经存在在很早以前就提出来了,但是我仍然不明白为什么 os.chflags()不能执行 chattr 命令的工作.有人可以详细说明吗?

解决方案

Linux不提供 chflags 系统调用,因此Python不提供包装器 os.chflags().

chattr 命令使用以下代码( e2fsprogs-1.42.13 lib/e2p/fsetflags.c ):

  fd =打开(名称,OPEN_FLAGS);如果(fd == -1)返回-1;f =(int)标志;r = ioctl(fd,EXT2_IOC_SETFLAGS,& f);如果(r == -1)save_errno = errno;关闭(fd); 

设置文件的扩展属性,因此如果将其移植到Python(并使用一些C从 ext2fs/ext2_fs.h 中提取 EXT2_IOC_SETFLAGS 的值)),您可以执行以下操作:

 #!/usr/bin/python2导入fcntl导入操作系统导入结构#取自ext2fs/ext2_fs.h.EXT2_IMMUTABLE_FL = 0x00000010EXT2_IOC_SETFLAGS = 0x40086602fd = os.open('/var/tmp/testfile',os.O_RDWR)f = struct.pack('i',EXT2_IMMUTABLE_FL)fcntl.ioctl(fd,EXT2_IOC_SETFLAGS,f);os.close(FD) 

Etvoilà:

  [tim @ passepartout〜] $ lsattr/var/tmp/testfile---- i -----------/var/tmp/testfile[tim @ passepartout〜] $ 

但是,出于所有实际目的,将 chattr(1)作为子进程执行比将上面的概念验证转化为无需维护即可可靠运行的东西更为谨慎./p>

I'm using Python 2.7.9 under Debian GNU/Linux 8 (jessie) 64-bit. I just tried to change file attributes by calling os.chflags(path, mode). In the Python docs there is an article about the os interface which says that this method is available in Unix, but it doesn't work for Linux. Python always throws:

Traceback (most recent call last):
File "/home/lexer/py/epam/tests/main.py", line 43, in <module>
os.chflags(path_to_file(file_name), stat.SF_NOUNLINK)
AttributeError: 'module' object has no attribute 'chflags'

There is an issue which was already raised for that a long time ago, but I still can't understand why os.chflags() doesn't do the chattr command's job. Could anybody elaborate it?

解决方案

Linux does not provide the chflags syscall, so Python does not provide the wrapper os.chflags().

The chattr command uses the code (e2fsprogs-1.42.13's lib/e2p/fsetflags.c):

        fd = open (name, OPEN_FLAGS);
        if (fd == -1)
                return -1;
        f = (int) flags;
        r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
        if (r == -1)
                save_errno = errno;
        close (fd);

to set the extended attributes for a file, so if you port that to Python (and use some C to extract the value for EXT2_IOC_SETFLAGS from ext2fs/ext2_fs.h), you can do something like:

#!/usr/bin/python2

import fcntl
import os
import struct

# Taken from ext2fs/ext2_fs.h.
EXT2_IMMUTABLE_FL = 0x00000010
EXT2_IOC_SETFLAGS = 0x40086602

fd = os.open('/var/tmp/testfile', os.O_RDWR)
f = struct.pack('i', EXT2_IMMUTABLE_FL)
fcntl.ioctl(fd, EXT2_IOC_SETFLAGS, f);
os.close(fd)

Et voilà:

[tim@passepartout ~]$ lsattr /var/tmp/testfile
----i----------- /var/tmp/testfile
[tim@passepartout ~]$

But for all practical purposes it is probably much more prudent to execute chattr(1) as a child process than to turn the proof-of-concept above into something that runs reliably without maintenance.

这篇关于为什么os.chflags()在Linux下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆