-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: path/filepath: Add SameFile and InTree #42202
Comments
Go imho still has use for a (set of) function(s) that resolve links and a proper, safe and recommendable way when this is accepted. I hence don't see this as an alternative to #42201, but as a very useful addition. Also I think this API will perform poorly, which is fine if used sparingly, but will add up checking lots of files or directories in a very deep directory structure. For the high volume calling scenario, I would add the fundamentals on top of which these functions are built to the public API for that reason, such that the application has the perhaps at least twice faster alternative available if it prefers, although it would make the application code slightly more complex. |
It seems to me that |
Indeed. |
Even if it was just a thin wrapper (which I'm not 100% sure yet), I think it's important to have it as part of
|
This proposal is alternative to #42201.
When working with pathnames on filesystems, it is common to inspect the relations between two different paths. In prototypes, it is common to assume that pathnames are uniques and thus two pathnames refer to the same file iff the pathnames are equal. Since many applications operate on tree of files, it is also common to check whether a file is part of a specified tree given its root; this can naively be done with
strings.HasPrefix(path, root)
.In real world, filesystems offer a variety of different types of links which can make things harder. On top of this, Windows and UNIX have different filesystems with different concepts and incompatible APIs. So finding a common semantic (which is one of the goals of
path/filepath
) is complicated, and Go does not provide useful primitives for this.In other languages such as C, it became common under UNIX to use a function such as
realpath(3)
to obtain a so-called "canonical name" of a path. By using such primitive, it is technically possible to implement equality and containment tests in the following way:realpath(path1) == realpath(path2)
realpath(leaf)
starts withrealpath(root)
(Notice that there is a different kind of containment test, in which we don't want to follow
leaf
links, but this can be done with something likerealpath(dir(leaf)) starts with realpath(root)
so it's not really worth defining a different operation).There are a few problems with canonicalization:
realpath
does not follow bind mounts. On Windows, there is a syscall calledGetFinalPathNameByHandle
which provides 4 different canonicalization modes, and it's not immediately clear which one should be the one to use (moreover, to make things more complicated, not all modes work on all different kind of file objects).K:
, while users have absolute no idea what\\server\share
means.My proposal is to avoid the temptation to define a canonicalization function, and rather provide helpers to implement pathname tests for applications, to cover their needs. Thus, I propose to add the following functions:
It seems to me that these two functions should provide two primitives that can be used to solve canonicalization problems. I would be interested in hearing use cases which are not covered by these two functions but are solved by
realpath
orGetFinalPathNameByHandle
.The text was updated successfully, but these errors were encountered: