-
Notifications
You must be signed in to change notification settings - Fork 286
[1/3] Proper path resolution: add openat functionality #2254
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea28b4d
[1/2] Add openat functionality
yagehu 1c64d11
Fix windows
yagehu 107be03
Fix windows
yagehu 6d7c2f7
Remove unused constants and types
yagehu cbeb0da
Remove unused constants and types
yagehu 2dd5cda
Remove unused constants and types
yagehu 900ef79
Merge branch 'path-res/1/openat' of github.com:yagehu/wazero into pat…
yagehu 00f22bb
Emulate O_NOFOLLOW behavior on Windows openat
yagehu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package sysfs | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
// NTUnicodeString is a UTF-16 string for NT native APIs, corresponding to UNICODE_STRING. | ||
type NTUnicodeString struct { | ||
Length uint16 | ||
MaximumLength uint16 | ||
Buffer *uint16 | ||
} | ||
|
||
type OBJECT_ATTRIBUTES struct { | ||
Length uint32 | ||
RootDirectory syscall.Handle | ||
ObjectName *NTUnicodeString | ||
Attributes uint32 | ||
|
||
// Always nil. | ||
SecurityDescriptor uintptr | ||
|
||
// Always nil. | ||
SecurityQoS uintptr | ||
} | ||
|
||
type IO_STATUS_BLOCK struct { | ||
Status NTStatus | ||
Information uintptr | ||
} | ||
|
||
// https://learn.microsoft.com/en-us/windows/win32/secauthz/access-mask | ||
type ACCESS_MASK uint32 | ||
|
||
// Constants for type ACCESS_MASK | ||
const ( | ||
READ_CONTROL = 0x00020000 | ||
SYNCHRONIZE = 0x00100000 | ||
STANDARD_RIGHTS_READ = READ_CONTROL | ||
STANDARD_RIGHTS_WRITE = READ_CONTROL | ||
STANDARD_RIGHTS_EXECUTE = READ_CONTROL | ||
) | ||
|
||
// File access rights constants. | ||
// https://learn.microsoft.com/en-us/windows/win32/fileio/file-access-rights-constants | ||
const ( | ||
FILE_READ_DATA = 0x00000001 | ||
FILE_READ_ATTRIBUTES = 0x00000080 | ||
FILE_READ_EA = 0x00000008 | ||
FILE_WRITE_DATA = 0x00000002 | ||
FILE_WRITE_ATTRIBUTES = 0x00000100 | ||
FILE_WRITE_EA = 0x00000010 | ||
FILE_APPEND_DATA = 0x00000004 | ||
FILE_EXECUTE = 0x00000020 | ||
) | ||
|
||
const ( | ||
FILE_GENERIC_READ = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE | ||
FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE | ||
FILE_GENERIC_EXECUTE = STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE | ||
) | ||
|
||
// NtCreateFile CreateDisposition | ||
const ( | ||
FILE_SUPERSEDE = 0x00000000 | ||
FILE_OPEN = 0x00000001 | ||
FILE_CREATE = 0x00000002 | ||
FILE_OPEN_IF = 0x00000003 | ||
FILE_OVERWRITE = 0x00000004 | ||
) | ||
|
||
const ( | ||
// NtCreateFile CreateOptions | ||
FILE_OPEN_FOR_BACKUP_INTENT = 0x00004000 | ||
|
||
// https://learn.microsoft.com/en-us/windows/win32/api/ntdef/nf-ntdef-initializeobjectattributes | ||
OBJ_CASE_INSENSITIVE = 0x00000040 | ||
) | ||
|
||
func NtCreateFile( | ||
handle *syscall.Handle, | ||
access uint32, | ||
oa *OBJECT_ATTRIBUTES, | ||
iosb *IO_STATUS_BLOCK, | ||
allocationSize *int64, | ||
attributes uint32, | ||
share uint32, | ||
disposition uint32, | ||
options uint32, | ||
eabuffer uintptr, | ||
ealength uint32, | ||
) syscall.Errno { | ||
r0, _, _ := syscall.SyscallN( | ||
procNtCreateFile.Addr(), | ||
uintptr(unsafe.Pointer(handle)), | ||
uintptr(access), | ||
uintptr(unsafe.Pointer(oa)), | ||
uintptr(unsafe.Pointer(iosb)), | ||
uintptr(unsafe.Pointer(allocationSize)), | ||
uintptr(attributes), | ||
uintptr(share), | ||
uintptr(disposition), | ||
uintptr(options), | ||
uintptr(eabuffer), | ||
uintptr(ealength), | ||
) | ||
|
||
return NTStatus(r0).Errno() | ||
} | ||
|
||
// NewNTUnicodeString returns a new NTUnicodeString structure for use with native | ||
// NT APIs that work over the NTUnicodeString type. Note that most Windows APIs | ||
// do not use NTUnicodeString, and instead UTF16PtrFromString should be used for | ||
// the more common *uint16 string type. | ||
func NewNTUnicodeString(s string) (*NTUnicodeString, error) { | ||
var u NTUnicodeString | ||
|
||
s16, err := syscall.UTF16PtrFromString(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
RtlInitUnicodeString(&u, s16) | ||
|
||
return &u, nil | ||
} | ||
|
||
func RtlInitUnicodeString(destinationString *NTUnicodeString, sourceString *uint16) { | ||
syscall.SyscallN( | ||
procRtlInitUnicodeString.Addr(), | ||
uintptr(unsafe.Pointer(destinationString)), | ||
uintptr(unsafe.Pointer(sourceString)), | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to expose this at this level?
the reason I am asking is that this obviously only applies to hierarchical FSs, but it won't work (as you can see later) on other types of file descriptors such as sockets or stdin/err/out. Could it be possible to keep this internal and limited to tree-like file systems?
Essentially, the final goal of this (series of) PR is to introduce an API that will reproduce the correct behavior when resolving paths, so it would great if we can scope it only to files for which "paths" make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It definitely doesn't need to be exposed at this level. I misunderstood your last comment and thought you wanted the method moved from the public interface to a private one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, that was a fair assumption, but I was wondering if we could keep it as an implementation detail of Open for those FSs that need hierarchical file access 🤔