Skip to content

Add fs.ResolvePath to resolve symbolic links - #275

Merged
Hamza El-Saawy (helsaawy) merged 4 commits into
microsoft:mainfrom
helsaawy:fs-resolve
Apr 14, 2023
Merged

Add fs.ResolvePath to resolve symbolic links#275
Hamza El-Saawy (helsaawy) merged 4 commits into
microsoft:mainfrom
helsaawy:fs-resolve

Conversation

@helsaawy

Copy link
Copy Markdown
Contributor

filepath.EvalSymlinks does not work well on Windows, and can loop indefinitely in certain situations and error out.

Add ResolvePath, which uses the Win32 GetFinalPathNameByHandle to resolve the final path of a file by first opening it.
Add "internal/stringbuffer".WString struct to maintain a pool of []uint16 buffers for use with interacting with the Win32APIs
Add "internal/fs" pkg with Win32 CreateFile, GetFinalPathNameByHandle, and associated flags and access masks

Core implementation is based off of: containerd/containerd#5411

Signed-off-by: Hamza El-Saawy hamzaelsaawy@microsoft.com

@helsaawy
Hamza El-Saawy (helsaawy) requested a review from a team as a code owner February 6, 2023 19:52
`filepath.EvalSymlinks` does not work well on Windows, and can enter
infinite loops in certain situations and error out.
Use Win32 API GetFinalPathNameByHandle to handle path resolution.

Implementation based off on: containerd/containerd#5411

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
@kevpar

Copy link
Copy Markdown
Member

@helsaawy

Copy link
Copy Markdown
Contributor Author

Can we carry over the tests from https://github.com/containerd/containerd/blob/main/pkg/os/os_windows_test.go?

I wanted to avoid that since that would hcsshim as a dependency for this package, which could cause weird cyclic import issues.
The alternative would be add computestorage bindings here, but I would rather save that for a separate PR

@helsaawy

Copy link
Copy Markdown
Contributor Author

Can we carry over the tests from https://github.com/containerd/containerd/blob/main/pkg/os/os_windows_test.go?

I wanted to avoid that since that would hcsshim as a dependency for this package, which could cause weird cyclic import issues. The alternative would be add computestorage bindings here, but I would rather save that for a separate PR

Kevin Parsons (@kevpar)
I added tests and needed APIs to this PR: #276

Comment thread pkg/fs/resolve.go Outdated
Comment thread internal/fs/security.go Outdated
Comment thread internal/fs/security.go Outdated
Comment thread internal/fs/fs.go Outdated
Comment thread internal/fs/fs.go
// Specific Object Access
// from ntioapi.h

FILE_READ_DATA AccessMask = (0x0001) // file & pipe

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with constants, if it's in golang.org/x/sys/windows, let's use those instead.

@helsaawy Hamza El-Saawy (helsaawy) Feb 13, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, create values that just reference windows.* for their definition?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to define a FILE_READ_DATA const at all? I would think we can just use the const from the other package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sys/windows constants are not typed, so we lose the ability to type-check and catch bugs that way
and the general constants were all copied in from the corresponding headers, since it was the same amount of work to copy in the ones we needed vs all of them

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the general philosophy has been to carry as little as we can here, and rely on golang.org/x/sys/windows for the bulk of Windows support. I'd like to not get in the habit of re-creating functionality that lives in windows as well.

Can we get away without using typed constants here? Alternately, can we just add the minimum set of typed constants here that we need? And then we could try to get windows to type theirs (though that might be tough, as it is a breaking change).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt they would, since the majority of that code seems auto-generated.
I just copied-pasted the constants here from the relevant header files, so it was trivial to add of them, but ill whittle the number down to just what we use here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do away with the types all together, but I am hesitant to do so, since the function signatures tend to be pretty crowded (eg, CreateFile(*uint16, uint32, uint32, *windows.SecurityAttributes, uint32, uint32, wiindows.Handle)) and having types prevents mixing up parameters

@kevpar Kevin Parsons (kevpar) Apr 13, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interest of not blocking the PR I think this is okay as is. I am interested in longer term how we can push things upstream to stdlib and x/sys/windows rather than accumulating more bits specific to this repo, so that would be a good thing to think about.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Update logic to try querying for normalized path initially, then use
opened path if access is denied.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

@kevpar Kevin Parsons (kevpar) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolve logic looks fine to me. It would be good to have someone else specifically look at the string buffer parts since I didn't focus on that.

@msscotb Scott Brender (msscotb) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@helsaawy
Hamza El-Saawy (helsaawy) merged commit b884eb7 into microsoft:main Apr 14, 2023
@helsaawy
Hamza El-Saawy (helsaawy) deleted the fs-resolve branch April 14, 2023 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants