Docker卷挂载NFS共享的权限测试问题



我有一个问题与以下python代码

import os
print("Can I write into the tv folder?",os.access("/tv", os.W_OK))
print("Does the /tv/test file exists?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "w") as f:
f.write("toto")
print("Does the /tv/test file exists now?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "r") as f:
print("Content of the /tv/test file:")
print(f.read())

打印出

Can I write into the tv folder? False
Does the /tv/test file exists? False
Does the /tv/test file exists now? True
Content of the /tv/test file;
toto

但是根据对os.access的第一次调用,我不应该能够编写测试文件…

我的问题不是它已经能够创建文件,我想要的。但是测试应该产生一个相关的结果。(我使用的一个软件因此发生故障)

我在树莓派上的docker容器中运行Python 3.8.10 (docker版本20.10.7),tv文件夹是由docker挂载的远程nfs共享。

root@7f0a44aad8a9:/> ls -la /tv
total 8
drwxrwxrwx 1 abc  abc   3826 Jul 27 14:18  .
drwxr-xr-x 1 root root  4096 Jul 27 14:12  ..
drwxrwxrwx 1 abc  abc    988 May 13 07:30 DARK
... (a lot of other folders)
-rwxrwxrwx 1 abc  abc      4 Jul 27 14:18  test

docker-compose文件:

version: "3.4"
services:
bazarr:
image: ghcr.io/linuxserver/bazarr
container_name: bazarr
volumes:
- shows:/tv
ports:
- 6767:6767
dns: 1.1.1.1
restart: unless-stopped
volumes:
shows:
driver_opts:
type: "nfs"
o: "nfsvers=3,addr=xxx,soft,rw"
device: ":xxx"

我知道os.access可以给出假阳性,但是假阴性?
有人已经看到了吗?
这是意料之中的吗?

编辑:这个问题不是python特有的

root@6c5dd99ee211:/> if [ -w /tv ]; then echo "WRITABLE"; else echo "NOT WRITABLE"; fi
NOT WRITABLE

但是为什么呢?

这与POSIX文件系统(包括*nix文件系统)的关系比与Python的关系要大得多。

但是根据对os的第一次调用。访问,我不应该能够编写测试文件…

不,不幸的是这是错误的(但不可否认这有点不直观)。

在POSIX文件系统中,您可能没有向目录写入的权限(这是您所检查的),但是这个并不意味着您不能向该目录中的文件写入。它的意思是你不能写新的文件到该目录。

的例子:

(base) mmessersmith@HP-ZBK-0425:~$ mkdir test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 777 test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 500 test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/new_file.txt
touch: cannot touch 'test_dir/new_file.txt': Permission denied
(base) mmessersmith@HP-ZBK-0425:~$ echo "123" > test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
123
(base) mmessersmith@HP-ZBK-0425:~$ ls -al test_dir/
total 12
dr-x------  2 mmessersmith mmessersmith 4096 Jul 27 09:32 .
drwxr-xr-x 24 mmessersmith mmessersmith 4096 Jul 27 09:31 ..
-rwxrwxrwx  1 mmessersmith mmessersmith    4 Jul 27 09:32 abc.txt

注意,我不能写一个新的文件到目录test_dir,但我仍然可以写到test_dir中的abc.txt。在上面的示例中,您需要检查os.access("/tv/test", os.W_OK)。在上面示例的REPL中:

(base) mmessersmith@HP-ZBK-0425:~$ python
Python 3.8.3 (default, May 19 2020, 18:47:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.access('test_dir', os.W_OK)
False
>>> os.access('test_dir/abc.txt', os.R_OK)
True
>>> os.access('test_dir/abc.txt', os.W_OK)
True

最后,注意os.access是linux/unix访问的简单包装器:https://manpages.debian.org/buster/manpages-dev/access.2.en.html.

所以,不,这不是假阳性或假阴性,这只是文件系统碰巧工作的方式。

最新更新