-
Notifications
You must be signed in to change notification settings - Fork 114
FileOperations: Add Windows implementation for FileDescriptor.resize(to:) #89
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
Conversation
…to:) Signed-off-by: Si Beaumont <[email protected]>
@inline(__always) | ||
internal func ftruncate( | ||
_ fd: Int32, | ||
_ length: off_t | ||
) -> Int32 { | ||
_chsize(fd, numericCast(length)) | ||
} |
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.
@compnerd do you have any opinions on this? Do you think that _chsize
is the right choice for Windows or should we be using implementing this in terms of other APIs? You mentioned something about SetFilePointerEx
and SetEndOfFile
?
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.
In general, using the windows API is better. It tends to be more flexible and integrates better by covering more cases (e.g. file descriptors are really a lookup table in the C library to the underlying kernel handle, and that may be created bypassing the registration by using the Windows API).
At the very least please use _chsize_s
(which in fact I have a pending patch for NIO) as that uses a __int64
for the offset rather than a long
which is 32-bit. It effectively is the behavioral change of _FILE_OFFSET_BITS=64
on GNU. I don’t see a reason for modern code to use the 32-bit offset even under register pressure on 32-bit platforms in a library.
For keeping the signature the same, we can use _get_osf_handle
to get the kernel handle from the file descriptor (as +0! do not free, aka CloseHandle
). We should expose the handle variant of the API, but that can be done subsequently.
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.
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.
I do have some updated docs at https://github.com/compnerd/swift-build/blob/master/docs/WindowsQuickStart.md. I’m working on some more options for building.
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.
LGTM, +1 to _chsize_s
Signed-off-by: Si Beaumont <[email protected]>
@swift-ci please test |
@swift-ci please test |
Hmm, seems that this actually didn't build :-(:
|
I think there's this guard that needs removing... https://github.com/apple/swift-system/blob/main/Sources/System/Internals/Syscalls.swift#L128 |
#82 added
FileDescriptor.resize(to:)
. The original PR included addingftruncate
toWindowsSycallAdapters.swift
but it was removed during review, and so there is currently no implementation forresize(to:)
on Windows.The justification was that we didn't want to shoehorn Windows support into the POSIX code paths and that the Windows SDK was different enough to justify its own platform-specific implementation. However, looking at
chsize
(renamed_chsize
) it looks like it's supposed to be the equivalent to the POSIX-like functions in UCRT.In order to move discussion forward on an appropriate Windows implementation I have reinstated the syscall adapter as a strawman proposal.