TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

PATH_MAX Is Tricky

67 pointsby eklitzkeabout 8 years ago

11 comments

problemsabout 8 years ago
Windows has a similarly disastrous situation where most tools and APIs follow MAX_PATH - which is defined to be 260 chars. But that doesn&#x27;t affect the actual filesystem or syscall interface, just common APIs and tools. This makes it impossible to delete files from windows explorer for example.<p>If you want to fix this you basically have to bypass it by using &quot;\\?\&quot; on the front of the full path. The situation gets messy when you&#x27;re trying to write an installer with node packages especially.<p><a href="https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;aa365247(v=vs.85).aspx#maxpath" rel="nofollow">https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;aa3...</a>
评论 #14200126 未加载
评论 #14202315 未加载
评论 #14196490 未加载
derefrabout 8 years ago
&gt; The problem is that you can’t meaningfully define a constant like this in a header file. The maximum path size is actually to be something like a filesystem limitation, or at the very least a kernel parameter.<p>AFAIK, &quot;paths&quot; aren&#x27;t a thing filesystems think about. As far as a filesystem driver is concerned, paths are either—for open&#x2F;read&#x2F;write&#x2F;etc.—plain inodes (a uint64), or—for directory-manipulation calls—an inode plus a ptrdiff_t to index into the dirent list†. The only things that care about NAME_MAX are lookup(2) [get inode given {inode, dirent}], and link(2) [put inode in {inode, dirent}].<p>So it&#x27;s really only the kernel, through its syscall interface, that cares about paths—and so PATH_MAX is just a representation of the maximum size of a path the kernel is willing to accept in those syscalls. As if they each had a statically-allocated path[PATH_MAX] buffer your path got copied into.<p>† Writing a FUSE filesystem is a great way to learn about what the kernel thinks a filesystem is. It&#x27;s very different from the userland perspective. For example, from a filesystem driver&#x27;s perspective, &quot;file descriptors&quot; don&#x27;t exist! Instead, read(2) and write(2) calls are stateless, each call getting passed a kernel-side io-handle struct that must get re-evaluated for matching permissions on each IO operation. (You can do <i>some</i> up-front evaluation during the open(2) call, but, given that a file&#x27;s permissions might <i>change</i> while you have a descriptor open to it, there&#x27;s not much point.)
评论 #14196143 未加载
评论 #14196135 未加载
Animatsabout 8 years ago
All the UNIX&#x2F;Linux&#x2F;POSIX functions which take output &quot;char *&quot; params without a length should have been moved to deprecated header files a long time ago. Like 1990 or so. It&#x27;s not too late.
评论 #14200152 未加载
loegabout 8 years ago
The GNU Hurd approach to PATH_MAX is to set it to something ridiculous like SIZE_MAX, something that cannot possible be allocated, to illustrate to programmers that it is a fiction.<p>I don&#x27;t think that&#x27;s necessarily the best approach, but it matches reality more closely than typical Linux&#x2F;BSD values (1024 or 4096).
评论 #14196443 未加载
评论 #14195973 未加载
TheAceOfHeartsabout 8 years ago
This post got me curious, so I did a quick search on macOS 10.12. I found the values defined in &quot;&#x2F;System&#x2F;Library&#x2F;Frameworks&#x2F;Kernel.framework&#x2F;Headers&#x2F;sys&#x2F;syslimits.h&quot;. PATH_MAX is 1024, and NAME_MAX is 255.<p>There&#x27;s also an amusing todo question that looks like it might&#x27;ve been there for at least close to 20 years now:<p><pre><code> #define OPEN_MAX 10240 &#x2F;* max open files per process - todo, make a config option? *&#x2F;</code></pre>
jstimpfleabout 8 years ago
The essence of the article: PATH_MAX applies to the syscall interface. It&#x27;s not related to file systems. Paths aren&#x27;t a file system thing, but simply a convenient means of addressing files. Basically they are URLs for local resources.<p>And that totally makes sense once you understand that files are basically &quot;objects&quot; (in the OO sense) identified by inodes instead of memory addresses. A file system implements the graph of these objects (linked by special file objects called <i>directories</i>). The fact that one can cross file system boundaries using file paths also indicates that file paths are none of a file system&#x27;s business.
评论 #14196867 未加载
teddyhabout 8 years ago
&gt; <i>This constant</i> [PATH_MAX] <i>is defined by POSIX</i>…<p>Well, it’s <i>allowed</i> by POSIX. A POSIX compatible system doesn’t <i>have</i> to define it if it has no such inherent restriction on path lengths. Indeed, the GNU Hurd does not have such a restriction, and consequently does not define it. This leads to many porting adventures for those trying to compile a program on GNU Hurd, believing their source code to be correct for any POSIX-compliant system.
recentdarknessabout 8 years ago
And to add to the confusion unix domain sockets have a maximal length of something between 92 a 108. That&#x27;s an implementation detail of platform it&#x27;s running on. This in particular has been biting me already.
leni536about 8 years ago
I played around with glibc&#x27;s getcwd() some time ago. With strace one can easily see how getcwd() works. If the current path is larger than PATH_MAX then the getcwd syscall fails. Then as I recall glibc uses &#x27;..&#x27;s recursively so it never has to call a syscall with a long relative path.<p>If there is a non-user-readable directory in the path then the fallback method fails but the getcwd syscall works if the path is short enough.<p>Bash also &quot;cheats&quot; by caching the working directory and updating it on &#x27;cd&#x27; commands.
manwe150about 8 years ago
If I understand the conclusion of the article right, it&#x27;s that we <i>should</i> actually just use PATH_MAX? In particular, he points to the glibc implementation of realpath as being very correct. But it (like the man page description of it says), appears to prefer to use the hard-coded value of PATH_MAX, unless that value is unavailable and it is forced to query for the kernel _PC_PATH_MAX value instead.<p>That&#x27;s not what I would have expected. Did I miss something obvious?
josteinkabout 8 years ago
That&#x27;s tricky indeed. And doing things properly seems quite involved.<p>For now I&#x27;ll keep my limits.h. At least until I get a real-world bug-reports telling me this is causing real-world issues :)