You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have code which opens both files and directories as files to perform various actions such as fetching and comparing generations (I need to upstream to linux once I write tests) or other metadata with SyscallConn.
Trying to use #67002 to make it less conservative with some TOCTOU edge cases prove challenging since *os.Root does not provide needed SyscallConn to fetch the generation of directories and more architecturally the current codepath is to open everything as a *os.File and then use statx to decide what to do next.
package os
// AsRoot open the File as a Root if it is a directory otherwise it errors.// The Root is returned with a new lifetime such that each need to be closed independently.func (*File) AsRoot() (*Root, error)
// AsFile opens the Root as a File.// The File is returned with a new lifetime such that each need to be closed independently.func (*Root) AsFile() (*File, error)
The text was updated successfully, but these errors were encountered:
I discussed this with @neild already and:
I think it might be possible to convert *os.File → *os.Root by using SyscallConn then calling os.OpenRoot with fmt.Sprintf("/proc/self/fd/%d") on Linux.
Not portable tho.
He pointed out *os.Root → *os.File should be possible by calling (*os.Root).Open(root, ".").
I am not sure if lifetimes should be independent or not.
I guess from a point of implementing in the std this is the easiest, but as a consumer I would sometime want the opposite.
I would not want to try to have a Root and a File as two views of the same file descriptor. (In some cases, Roots don't even have a file descriptor.)
You can go from a Root to a File with root.Open("."), at the expense of creating a new FD.
Going from a File to a Root is not currently possible. I can imagine a method on File which lets you do so:
// Root returns a root rooted at the location of f, which must be a directory file.func (f*File) Root() (*Root, error)
I don't know if there's a need for that operation; motivating use cases would be useful.
It might be reasonable to add a Root.SyscallConn method to give access to a root's FD, with the caveat that this method would return an error on platforms where a root doesn't have an FD (GOOS=js, GOOS=plan9). But perhaps that isn't necessary, and root.Open(".") is sufficient.
Proposal Details
I have code which opens both files and directories as files to perform various actions such as fetching and comparing generations (I need to upstream to linux once I write tests) or other metadata with
SyscallConn
.Trying to use #67002 to make it less conservative with some TOCTOU edge cases prove challenging since
*os.Root
does not provide neededSyscallConn
to fetch the generation of directories and more architecturally the current codepath is to open everything as a*os.File
and then usestatx
to decide what to do next.The text was updated successfully, but these errors were encountered: