Skip to content

symlinks and ".." directories and platforms oh my! #7751

@marler8997

Description

@marler8997

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:

  1. have std.fs conform to the platform's conventional behavior regardless of whether it is common between all platforms
  2. converge the behavior on each platform to a common design
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions