MKDIR未正确设置权限

发布于 2025-01-25 16:58:20 字数 599 浏览 4 评论 0 原文

我正在尝试制作一个具有“执行中设置组ID”的目录 Python中

在Shell中的

$ mkdir --mode 2775 mail
$ ls -ld mail
drwxrwsr-x 1 john john 0 May  3 08:34 mail
      ^

:在Python中,我正在使用 pathlib.path.mkdir ,似乎没有考虑到一些模式的位, 即使清除了 umask

from pathlib import Path
import os

os.umask(0)
md = Path('mail')
md.mkdir(mode=0o2775)    

drwxrwxr-x 1 john john 0 May  3 08:35 mail
       ^

在九个位时是否有一些截止,不是documented

I am trying to make a directory that has the "set group ID on execution" set
in Python

In the shell:

$ mkdir --mode 2775 mail
$ ls -ld mail
drwxrwsr-x 1 john john 0 May  3 08:34 mail
      ^

In Python I am using pathlib.Path.mkdir, and it seems there some bits for mode are not taken into account,
even when the umask is cleared:

from pathlib import Path
import os

os.umask(0)
md = Path('mail')
md.mkdir(mode=0o2775)    

drwxrwxr-x 1 john john 0 May  3 08:35 mail
       ^

Is there some cut-off at nine bits that is not documented

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2025-02-01 16:58:20

最终,在非Windows Systems, path.mkdir 呼叫 os.mkdir 又调用C系统调用 mkdirat 。该呼叫的行为是根据模式

参数模式指定要使用的权限。它以通常的方式通过过程的umask修改:创建目录的权限为(mode& 〜umask& 0777)

请注意,在 0777的上方,权限明确掩盖了所有位;这就是 mkdir 系统调用在某些OS上的行为。事实之后,您只需要 chmod 即可。

Ultimately, on non-Windows systems, Path.mkdir calls os.mkdir which in turn invokes the C system call mkdirat. That call's behavior is defined in terms of mkdir, which, on Linux, documents the mode with:

The argument mode specifies the permissions to use. It is modified by the process's umask in the usual way: the permissions of the created directory are (mode & ~umask & 0777)

Note that the permissions explicitly mask off all bits above 0777; this is just how mkdir system calls behave on some OSes. You'll just have to chmod it after the fact.

痴骨ら 2025-02-01 16:58:20

pathlib 's mkdir 基本上只调用 os.mkdir() 。那里的文档包括:

在某些系统上,模式被忽略。在使用它的地方,首先将当前的umask值掩盖。如果设置了最后9个以外的位(即模式的八分位表示的最后3位数字),则其含义取决于平台。在某些平台上,它们被忽略,您应该调用 chmod()明确设置它们。

因此,仅保证使用最后9位。
您必须执行(无需设置Umask):

md.mkdir()
md.chmod(0o2775)

如果 Pathlib 不会默默地忽略额外的位,而是实际上抛出了一个值,那就更好了。

pathlib's mkdir essentially just calls os.mkdir(). The documentation there includes the section:

On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call chmod() explicitly to set them.

So only the last 9 bits are guaranteed to be used.
You'll have to do (no need to set umask):

md.mkdir()
md.chmod(0o2775)

It would be better if pathlib would not silently ignore extra bits, but actually threw a ValueError.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文