安装保险丝在 python 中给出无效参数错误
我已将 python 代码写入点安装上的安装保险丝。它总是给出无效参数错误。我已经在 C 语言中尝试过相同的程序,并且运行良好。任何 python 大师都可以帮我找出问题所在吗,我已将代码粘贴到这里。
#!/usr/bin/python
import stat
import os
import ctypes
from ctypes.util import find_library
libc = ctypes.CDLL (find_library ("c"))
def fuse_mount_sys (mountpoint, fsname):
fd = file.fileno (file ("/dev/fuse", 'w+'))
if fd < 0:
raise OSError("Could not open /dev/fuse")
mnt_param = "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i" \
% ("allow_other,default_permissions,max_read=131072", \
fd, stat.S_IFDIR, os.getuid(), os.getgid())
ret = libc.mount ("fuse", "/mount", "fuse", 0, mnt_param)
if ret < 0:
raise OSError("mount failed with code " + str(ret))
return fd
fds = fuse_mount_sys ("/mount", "fuse")
安装语法是:
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
我尝试使用 swig 并使用 C 编写程序,然后从中创建一个 .so,它们工作了。但我对用纯 python 编写很感兴趣。提前致谢。
strace 的输出:
$ strace -s 100 -v -e mount python fuse-mount.py
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = -1 EINVAL (Invalid argument)
$ strace -s 100 -v -e mount ./a.out
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = 0
I have written python code to a mount fuse on a point mount. It always gives invalid argument error. I have tried the same program in C and it works fine. Can any of python Guru's help me out in finding what the problem is, I have pasted the code here.
#!/usr/bin/python
import stat
import os
import ctypes
from ctypes.util import find_library
libc = ctypes.CDLL (find_library ("c"))
def fuse_mount_sys (mountpoint, fsname):
fd = file.fileno (file ("/dev/fuse", 'w+'))
if fd < 0:
raise OSError("Could not open /dev/fuse")
mnt_param = "%s,fd=%i,rootmode=%o,user_id=%i,group_id=%i" \
% ("allow_other,default_permissions,max_read=131072", \
fd, stat.S_IFDIR, os.getuid(), os.getgid())
ret = libc.mount ("fuse", "/mount", "fuse", 0, mnt_param)
if ret < 0:
raise OSError("mount failed with code " + str(ret))
return fd
fds = fuse_mount_sys ("/mount", "fuse")
mount syntax is:
int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const void *data);
I tried using swig and also writing the program using in C and then creating a .so from it and they worked. But I am interested in writing in pure python. Thanks in advance.
output of strace:
$ strace -s 100 -v -e mount python fuse-mount.py
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = -1 EINVAL (Invalid argument)
$ strace -s 100 -v -e mount ./a.out
mount("fuse", "/mount", "fuse", 0, "allow_other,default_permissions,max_read=131072,fd=3,rootmode=40000,user_id=0,group_id=0") = 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ctypes.c_void_p
无法使用细绳。相反,只需使用不带c_void_p
的字符串。然后您可以比较 和 的输出
,
直到它们匹配。
另外,请确保在调用 mount 时文件句柄
fd
仍然打开。file("/dev/fuse", 'w+')
将被某些 Python 实现(包括 cpython)自动进行垃圾收集并关闭。您可以通过将file("/dev/fuse")
的结果分配给变量来防止这种情况。ctypes.c_void_p
cannot be initialized with a string. Instead, just use a string withoutc_void_p
.You can then compare the output of
and
until they match.
Also, make sure the file handle
fd
is still open when you call mount.file("/dev/fuse", 'w+')
will be automatically garbage-collected and closed by some Python implementations, including cpython. You can prevent this by assigning the result offile("/dev/fuse")
to a variable.