Skip to content
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

FileOperations: Add Windows implementation for FileDescriptor.resize(to:) #89

Merged
merged 3 commits into from
Jun 9, 2022

Conversation

simonjbeaumont
Copy link
Contributor

#82 added FileDescriptor.resize(to:). The original PR included adding ftruncate to WindowsSycallAdapters.swift but it was removed during review, and so there is currently no implementation for resize(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.

…to:)

Signed-off-by: Si Beaumont <beaumont@apple.com>
Comment on lines 108 to 114
@inline(__always)
internal func ftruncate(
_ fd: Int32,
_ length: off_t
) -> Int32 {
_chsize(fd, numericCast(length))
}
Copy link
Contributor Author

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?

Copy link
Collaborator

@compnerd compnerd Jun 6, 2022

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@compnerd for now I replaced the use of _chsize with _chsize_s in 91d61b2.

I'd hate to break the Windows build again. Are there any useful docs for getting cross-compilation up and running?

Copy link
Collaborator

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.

Copy link
Contributor

@milseman milseman left a 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 <beaumont@apple.com>
@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

@milseman
Copy link
Contributor

milseman commented Jun 9, 2022

@swift-ci please test

@milseman milseman merged commit 94fa6bd into apple:main Jun 9, 2022
@compnerd
Copy link
Collaborator

compnerd commented Jun 9, 2022

Hmm, seems that this actually didn't build :-(:

"C:\Program Files\CMake\bin\cmake.exe" --build S:\92\b/swift-system
[1/1] Linking Swift shared library bin\SystemPackage.dll
FAILED: bin/SystemPackage.dll Sources/System/CMakeFiles/SystemPackage.dir/Errno.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FileDescriptor.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FileHelpers.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FileOperations.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePermissions.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/PlatformString.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/SystemString.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Util.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/UtilConsumers.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePath.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathComponents.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathComponentView.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathParsing.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathString.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathSyntax.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/FilePath/FilePathWindows.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/CInterop.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/Constants.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/Exports.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/Mocking.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/Syscalls.swift.obj Sources/System/CMakeFiles/SystemPackage.dir/Internals/WindowsSyscallAdapters.swift.obj swift/SystemPackage.swiftmodule lib/SystemPackage.lib 
cmd.exe /C "cd . && S:\92\toolchain-windows-x64\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -target x86_64-unknown-windows-msvc -output-file-map Sources\System\CMakeFiles\SystemPackage.dir\Release\output-file-map.json -incremental -j 54 -emit-library -o bin\SystemPackage.dll -module-name SystemPackage -module-link-name SystemPackage -emit-module -emit-module-path swift\SystemPackage.swiftmodule -emit-dependencies -DSystemPackage_EXPORTS -resource-dir "S:\92/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/usr/lib/swift" -LS:\92/sdk-windows-x64/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/usr/lib/swift/windows -O -libc MD -I S:\92\s\swift-system\Sources\CSystem\include S:\92\s\swift-system\Sources\System\Errno.swift S:\92\s\swift-system\Sources\System\FileDescriptor.swift S:\92\s\swift-system\Sources\System\FileHelpers.swift S:\92\s\swift-system\Sources\System\FileOperations.swift S:\92\s\swift-system\Sources\System\FilePermissions.swift S:\92\s\swift-system\Sources\System\PlatformString.swift S:\92\s\swift-system\Sources\System\SystemString.swift S:\92\s\swift-system\Sources\System\Util.swift S:\92\s\swift-system\Sources\System\UtilConsumers.swift S:\92\s\swift-system\Sources\System\FilePath\FilePath.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathComponents.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathComponentView.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathParsing.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathString.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathSyntax.swift S:\92\s\swift-system\Sources\System\FilePath\FilePathWindows.swift S:\92\s\swift-system\Sources\System\Internals\CInterop.swift S:\92\s\swift-system\Sources\System\Internals\Constants.swift S:\92\s\swift-system\Sources\System\Internals\Exports.swift S:\92\s\swift-system\Sources\System\Internals\Mocking.swift S:\92\s\swift-system\Sources\System\Internals\Syscalls.swift S:\92\s\swift-system\Sources\System\Internals\WindowsSyscallAdapters.swift    -Xlinker -implib:lib\SystemPackage.lib   && cd ."
S:\92\s\swift-system\Sources\System\Internals\Syscalls.swift:82:10: warning: 'lseek' is deprecated: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _lseek. See online help for details.
  return lseek(fd, off, whence)
         ^
S:\92\s\swift-system\Sources\System\Internals\Syscalls.swift:109:10: warning: 'dup' is deprecated: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _dup. See online help for details.
  return dup(fd)
         ^
S:\92\s\swift-system\Sources\System\Internals\Syscalls.swift:116:10: warning: 'dup2' is deprecated: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _dup2. See online help for details.
  return dup2(fd, fd2)
         ^
S:\92\s\swift-system\Sources\System\FileOperations.swift:445:7: error: cannot find 'system_ftruncate' in scope
      system_ftruncate(self.rawValue, _COffT(newSize))
      ^~~~~~~~~~~~~~~~
S:\92\s\swift-system\Sources\System\Internals\Exports.swift:60:3: warning: 'strerror' is deprecated: This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  strerror(__errnum)
  ^

@simonjbeaumont
Copy link
Contributor Author

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

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.

None yet

3 participants