-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
symlinks and ".." directories and platforms oh my! #7751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
why is this distinction of behaviour important? Is it for some kind of pathname normalizing/sanitizing function? |
@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 |
Anothing thing to consider is that if we want to adopt the Posix behavior on Windows, that will mean any function outside of 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 |
I can't speak to the details but it's best to preserve paths and just pass them to the operating system untouched. If Sorry if I misunderstand... |
@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). |
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 |
Somewhat of a duplicate of #4658 Relevant comment:
|
The use case of junctions is not included, which would however complicate the picture
source 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. |
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:
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, sincemylink
is a symlink toa/b
, it first resolves toa/b/..
which then becomesa
. This is indeed what happens on linux as the final command in this script,ls mylink/..
, will print the contents of directorya
rather than the current working directory.
.The following shows how to execute the same example on Windows:
In this case, the last command
dir mylink\..
will not print the contents of directorya
, 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:std.fs
conform to the platform's conventional behavior regardless of whether it is common between all platformsThe 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.
The text was updated successfully, but these errors were encountered: