Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Sources/System/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ extension FileDescriptor {
}
#endif

#if !os(Windows)
/*System 1.2.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
extension FileDescriptor {
/// Truncate or extend the file referenced by this file descriptor.
Expand Down Expand Up @@ -447,4 +446,3 @@ extension FileDescriptor {
}
}
}
#endif
7 changes: 7 additions & 0 deletions Sources/System/Internals/WindowsSyscallAdapters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ internal func pwrite(
return Int(nNumberOfBytesWritten)
}

@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.

#endif