Repository navigation
Description
There is a divergence between Windows and other platforms on the behavior of .. when it comes to symlinks. On Windows, .. paths are resolved before resolving symlinks, but on other platforms they are resolved in the opposite order. This change in ordering yields different behavior as I'll demonstrate with an example:
Linux:
mkdir -p a/b
ln -s a/b mylink
touch a/file-in-a a/b/file-in-b
ls mylink/..The behavior in question is what directory does mylink/.. resolve to? Given no other knowledge, mylink/.. would seem to resolve to the current directory ., however, since mylink is a symlink to a/b, it first resolves to a/b/.. which then becomes a. This is indeed what happens on linux as the final command in this script, ls mylink/.., will print the contents of directory a rather than the current working directory ..
The following shows how to execute the same example on Windows:
NOTE: the mklink command requires running your command prompt as Admin
mkdir a\b
mklink /d mylink a\b
echo > a\file-in-a
echo > a\b\file-in-b
dir mylink\..In this case, the last command dir mylink\.. will not print the contents of directory a, but instead prints the contents of the current working directory .. This is because on Windows, .. directories are resolved without considering whether or not their parent directory is a symlink, which causes those symlinks to be removed from the path without resolving them.
So the question is, what should Zig's std.fs module do with .. directories? Here are the options I see:
- have
std.fsconform to the platform's conventional behavior regardless of whether it is common between all platforms - converge the behavior on each platform to a common design
- implement both a common behavior and the conventional platform behavior and provide an interface to select one or the other
The other question is if we provide a common behavior, do we select the Posix behavior or Windows? @andrewrk suggests the Posix behavior be selected because it is the more common behavior given that symlinks were added relatively recently on Windows and require Admin privileges to create. (see his comment on IRC: https://freenode.irclog.whitequark.org/zig/2021-01-09#28791306)
For now @andrewrk has suggested we implement the platform's conventional behavior and be sure to document this Windows divergence from other platforms. This issue will track any updates to this decision and serves as a placeholder to revisit this decision at a later date.
Activity
mikdusan commentedon Jan 12, 2021
why is this distinction of behaviour important? Is it for some kind of pathname normalizing/sanitizing function?
marler8997 commentedon Jan 12, 2021
@mikdusan, I'm not sure I understand your question exactly, but the reason we have to pick a behavior in the standard library is because we are calling
Nt*functions directly on windows, which do not resolve ".." paths. Furthermore, the function that does the ".." resolution requires allocation on the Windows process heap, and @andrewrk has stated he doesn't want to depend on heap allocation to resolve filenames. This means Zig has to implement the path resolution itself, so we have a choice of doing it the "Windows" way, or the "NonWindows" way. The question is, which method should we implement?marler8997 commentedon Jan 12, 2021
Anothing thing to consider is that if we want to adopt the Posix behavior on Windows, that will mean any function outside of
ntdllthat takes filenames will be "off limits" because they will be resolving symlinks throughntdllwhich will be using the Windows behavior. This could make the change unfeasible depending on how much of the win32 api this cuts us off from.EDIT: actually they won't be "off limits" because I think the standard library could sanitize filepaths by resolving ".." parts before passing them on to non
ntdllfunctionsmikdusan commentedon Jan 12, 2021
I can't speak to the details but it's best to preserve paths and just pass them to the operating system untouched.
If
Nt*functions need ".." resolved before hand, then I would think do it the Windows compatible way but only when being passed to that API that requires it. What confuses me is why would we ever effect linux/macos/etc with this?Sorry if I misunderstand...
marler8997 commentedon Jan 12, 2021
@mikdusan, the behavior you're describing is option 1. This issue was created to consider and discuss implementing the posix behavior on Windows as well (i.e. options 2 and 3).
mrakh commentedon Jan 12, 2021
I believe option 1 is best. Zig should treat file paths like any other string - nothing more than a bunch of bytes whose meaning is dependent on the application interpreting it. In this case, that means deferring file path resolution to the operating system's filesystem implementation. It's the simplest to implement, and it avoids subverting any programmer's understanding of the operating system they're working with.
But enough of me waxing philosophy. The practical problem with option 2 is that its implementation requires additional OS calls to check for and resolve symbolic links, which opens the unwitting programmer up to TOCTTOU race conditions:
What happens when a process silently updates
my_symlinkto point to some other folder between steps 1 and 2? Or likewise withmy_other_symlinkbetween steps 3 and 4?squeek502 commentedon Jan 15, 2021
Somewhat of a duplicate of #4658
Relevant comment:
matu3ba commentedon Mar 10, 2022
The use case of junctions is not included, which would however complicate the picture
https://superuser.com/questions/343074/directory-junction-vs-directory-symbolic-link
source
However, if Zig wants to support generating "windows links for folders without privileged access", one can only use Junctions.
Personally, I think there is no good default as the file system is not always under programmer control and the best one can do is to provide checking and sanitizing methods to ensure stuff happens only locally in programmer specified folders (with absolute paths).
The portable method would then simply not resolve symbolic links.
BTW: Plan9 position is that symlinks are a hack.