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 FromFS to safely convert a slash-separated path into an operating system path #57151
Comments
A concrete example of where this could be useful: |
I'm starting to get confused about which of these new filepath functions should be used when. |
This proposal has been added to the active column of the proposals project |
I am very sympathetic to that confusion. Counting
You should use You should use You should use |
It is unclear to me why the function is called The proposal says that it supports paths that Then there is fs.ValidPath that rejects absolute paths. If FromFS is supposed to support absolute paths and since FromFS is intended to replace |
I don't think it's a given that everyone who uses FromSlash today should use FromFS. Today, FromSlash converts slashes to the canonical form for the host OS but preserves the meaning of the existing path. So for example on Windows today, FromSlash("c:/foo") gives you c:\foo, which is the canonical form of its input. Similarly on a Mac you get FromSlash("c:/foo") is c:/foo, and in both cases the result of os.Open(p) and os.Open(FromSlash(p)) are the same. It sounds like FromFS would not do that. You should use FromFS when the input is meant to be a "portable" slash-separated path as opposed to a slash-separated path interpreted according to the local OS. Programs that accept a file name on the command line but want to convert to native conventions should probably keep using FromSlash. The compiler does this sometimes for arranging canonical outputs and then inverting them. It should keep using FromSlash and ToSlash. Maybe something reading from a zip file should use FromFS, but why not just have it use IsLocal+FromSlash instead of FromFS? Or is it just programs implementing an fs.FS that need to use FromFS? I don't think the exact scope is clearly defined yet. |
It seems like we are stuck on the name here. I noticed that internal/safepath.FromFS is called with paths beginning with / (like /foo) but those are not actually io/fs paths. I'm not sure if the proposed FromFS rejects those or not, but safepath.FromFS does not. Also it's probably too indirect a meaning to use "FS" here. There are fundamentally two kinds of FromSlash: ones that are canonicalizing the OS interpretation and ones that are converting from "portable" to "local OS". The current FromSlash does the former. We need a name for the latter. Maybe the From prefix is tripping us up and we should name this operation with some verb that can be the function name. |
|
The
filepath.FromSlash
function sounds like it converts a /-separated path into an operating system path. What it actually does is replace every / in its input withos.PathSeparator
.FromSlash
can map an input path into semantically different paths on different operating systems. For example,FromSlash("a\b")
returns the filenamea\b
on Unix and the fileb
in the directorya
on Windows.FromSlash("C:/foo")
returns a relative path on Unix and an absolute path on Windows.#56694 involves failures in the standard library to safely convert a non-platform-specific /-separated path into a semantically equivalent operating system path. The fix (https://go.dev/cl/455716) introduces an internal function to perform this operation. We should have a public API for this.
The proposal:
FromFS
rejects empty paths ("") and, on Windows, reserved device names such as NUL.FromFS
andIsLocal
(#56219) are similar in that both involve performing safety checks on a potentially-untrusted input path. They serve different roles, however:FromFS
takes a /-separated path in the form operated on by thepath
package and safely converts it to a semantically-equivalent operating system path.IsLocal
takes an operating system path and reports whether it refers to something surprising.The text was updated successfully, but these errors were encountered: