为文件系统定义 PATH_MAX?
我目前正在编写一个文件系统。 statvfs
(甚至 statfs) 结构包含一个字段,指定该路径中名称的最大长度。由于
PATH_MAX
在 pathconf
< 中定义/a> 手册页(getconf
),这意味着它是在每个目录的基础上定义的(因此,由底层文件系统确定)。如何指定这个值?
I'm presently writing a filesystem. The statvfs
(and even the statfs
) structs contain a field specifying the maximum length of a name in that path. As PATH_MAX
is defined in the pathconf
manpage (getconf
), this means it is defined on a per-directory basis (and thus, determined by the underlying filesystem). How does one specify this value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PATH_MAX
主要表现为文件系统函数调用接口的属性,因此我认为让它在目录之间变化没有多大意义。例如,重命名或移动其中包含大型目录树的目录可能会使最长的绝对路径名更长,并且限制它会很复杂且效率低下。
相反,PATH_MAX 允许内核将传递的路径名复制到临时未分页内存,然后可以对其进行处理,而无需在每次访问时允许页面错误。分配大量此类内存可能会阻止内核正在执行的大多数其他操作,甚至导致内核恐慌。
PATH_MAX
mostly behaves as a property of the file system function call interface, so I don't think it makes much sense to have it vary across directories.For example, renaming or moving a directory with large directory trees in it may make the longest absolute pathname longer and it would be complicated and inefficient to limit that.
Instead,
PATH_MAX
serves to allow the kernel to copy passed pathnames to temporary unpaged memory, which can then be processed without needing to allow for a page fault at each access. Allocating huge amounts of such memory may block most other things the kernel is doing or even cause kernel panics.由于这个问题被标记为“FUSE”...
我刚刚在处理 FUSE 文件系统时遇到了这个问题。我给 FUSE 开发人员写了一封电子邮件,寻求澄清。当前
libfuse
维护者的回复(2018 年 1 月):没有办法指定 FUSE 文件系统 [驱动程序] 中的最大路径长度。供参考:完整电子邮件线程
Since this question is tagged "FUSE" ...
I just ran into this issue while working on a FUSE filesystem. I wrote an e-mail to the FUSE developers, seeking clarification. Reply from the current
libfuse
maintainer (January 2018): There is not a way to specify the maximum path length in a FUSE filesystem [driver].For reference: Full e-mail thread
在 Linux 上,glibc 的
pathconf
实现返回 PATH_MAX 的编译时常量值,因此没有运行时魔法 FUSE 或任何其他人可以执行调整它。 (请参阅 sysdeps/unix/sysv/linux/pathconf.c,它会进入 sysdeps/posix/pathconf.c。)您的问题“如何指定文件系统的 PATH_MAX?”的答案是“你不能。glibc 不允许你,FUSE 只是信使。”最终的结果是陷入困境。 这是一篇博客文章,讨论了关心和不关心 PATH_MAX 的代码。 依赖不长于 PATH_MAX 的路径的软件很久以前就被其他文件系统破坏了,因此您可以安全地忽略 PATH_MAX。
在 MacOS X(可能还有其他 BSD)上:pathconf 的实现完全在内核中,并且可以根据文件系统进行交换。 OSXFUSE 包含一个 NOOP 版本的 pathconf,它应该返回通常的编译时常量。然而,在我的测试中,它似乎捕获了另一个返回 ENXIO 的 NOOP 函数,并且我无法让 pathconf 工作。
额外奖励:对于 NAME_MAX,实现 statfs 并设置 f_namemax。
On Linux, glibc's implementation of
pathconf
returns a compile-time constant value of PATH_MAX so there is no runtime magic FUSE or anyone else can perform to adjust it. (See sysdeps/unix/sysv/linux/pathconf.c which falls through to sysdeps/posix/pathconf.c.) The answer to your question "How do I specify my filesystem's PATH_MAX?" is "You can't. glibc doesn't let you and FUSE is just the messenger."The end result is a sticky situation. Here's a blog post that discusses the code that does and does not care about PATH_MAX. Software that relies on paths no longer than PATH_MAX was broken long ago by other filesystems so it's safe for you to ignore PATH_MAX.
On MacOS X (and probably other BSDs): The implementation of
pathconf
is entirely in the kernel and can be swapped out per filesystem. OSXFUSE includes a NOOP version of pathconf which should return the usual compile-time constants. However, in my tests it seems to be catching another NOOP function along the way which returns an ENXIO and I can't get pathconf to work.Bonus: for NAME_MAX, implement statfs and set f_namemax.
POSIX 允许
_PC_PATH_MAX
根据当前目录而变化,但这并不意味着不改变它的系统不兼容。PATH_MAX
存在的真正原因是内核在对其进行任何实际工作之前将路径名复制到内核空间中。您关于
statvfs
中存在PATH_MAX
相关字段的断言是错误的。这与NAME_MAX
有关,这是不同的事情。POSIX allows
_PC_PATH_MAX
to vary based on the current directory, but that doesn't mean that systems which don't vary it aren't compliant.The real reason for
PATH_MAX
existing is that the kernel copies the pathname into kernelspace before doing any actual work with it.Your assertion that there is a
PATH_MAX
-related field instatvfs
is just wrong. That's related toNAME_MAX
, which is a different thing.我对其他操作系统了解不够,但恕我直言,这是至少在 FreeBSD 5.2.1 中的系统范围设置
PATH_MAX 位于 #62
sys/syslimits.h
因为
static int ufs_pathconf()
返回 UFS FS 的 PATHCONF 信息,以您指定的方式使用此变量。I do not enough about other OSes but imho this is a system-wide setting in at least FreeBSD 5.2.1
PATH_MAX is found in #62
sys/syslimits.h
Because
static int ufs_pathconf()
which returns thePATHCONF
information for UFS FS, uses this variable in the manner you specified.PATH_MAX 是系统范围的设置,通常在 pathmax.h 中定义为:
PATH_MAX is a system wide setting and is usually defined in pathmax.h as: