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

FileDescriptor: Add resize(to newSize:) and fileSize() APIs #82

Merged
merged 8 commits into from
May 20, 2022

Conversation

simonjbeaumont
Copy link
Contributor

@simonjbeaumont simonjbeaumont commented May 4, 2022

The FileDescriptor API currently doesn't expose anything that resembles ftruncate(2) for truncating or extending the file associated with a file descriptor.

This PR adds a new API FileDescriptor.resize(to newSize:) which wraps ftruncate, available on non-Windows platforms.

Signed-off-by: Si Beaumont <beaumont@apple.com>
Comment on lines 108 to 113
@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.

Full disclosure: I have not tested the Windows path. AFAICT there isn't a Windows CI pipeline?

Copy link
Member

Choose a reason for hiding this comment

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

I believe it's best to surround the addition of resize with #if !os(Windows).

Generally, I don't think it's a good idea to try to shoehorn Windows support into POSIX code paths here, or anywhere else in this code base. Swift System is not intended to provide a portable API layer, and the Windows SDK is different enough that it deserves its own, standalone, implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, apologies for that, I was just following the pattern I saw with the existing WindowsSyscallAdapters. I added some fixups which move this API to only non-Windows platforms:

  • 02daf4b fixup: Move FileDescriptor.resize to extension with #if !os(Windows)
  • 7fab65f fixup: Remove Windows syscall adaptor for ftruncate

@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

Signed-off-by: Si Beaumont <beaumont@apple.com>
@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

@milseman milseman requested a review from lorentey May 5, 2022 14:56
@milseman
Copy link
Contributor

milseman commented May 5, 2022

@lorentey can you take an initial look?

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

Big thumbs up on resize! I'm less convinced we need fileSize.

Let's keep the Windows implementation out for now.

) throws -> Int64 {
let current = try seek(offset: 0, from: .current)
let size = try seek(offset: 0, from: .end)
try seek(offset: current, from: .start)
Copy link
Member

Choose a reason for hiding this comment

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

This is temporarily mutating the file position during what looks like a read-only operation; I feel the potential pitfalls of this (alongside the fact that many file descriptors do not have a file size) probably makes this entry point less suitable for this package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's reasonable. I added the following fixup to address this:

  • d90bf85 fixup: Move FileDescriptor.fileSize() to an internal extension in SystemTests

Comment on lines 108 to 113
@inline(__always)
internal func ftruncate(
_ fd: Int32, _ length: off_t
) -> Int32 {
_chsize(fd, numericCast(length))
}
Copy link
Member

Choose a reason for hiding this comment

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

I believe it's best to surround the addition of resize with #if !os(Windows).

Generally, I don't think it's a good idea to try to shoehorn Windows support into POSIX code paths here, or anywhere else in this code base. Swift System is not intended to provide a portable API layer, and the Windows SDK is different enough that it deserves its own, standalone, implementations.

Copy link
Contributor Author

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

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

Thanks @lorentey for the review!

Big thumbs up on resize! I'm less convinced we need fileSize.

No problem, I moved fileSize to be an implementation detail of the tests for resize.

Let's keep the Windows implementation out for now.

Done.

I also updated the PR description to reflect the changes.

) throws -> Int64 {
let current = try seek(offset: 0, from: .current)
let size = try seek(offset: 0, from: .end)
try seek(offset: current, from: .start)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's reasonable. I added the following fixup to address this:

  • d90bf85 fixup: Move FileDescriptor.fileSize() to an internal extension in SystemTests

Comment on lines 108 to 113
@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.

OK, apologies for that, I was just following the pattern I saw with the existing WindowsSyscallAdapters. I added some fixups which move this API to only non-Windows platforms:

  • 02daf4b fixup: Move FileDescriptor.resize to extension with #if !os(Windows)
  • 7fab65f fixup: Remove Windows syscall adaptor for ftruncate

Sources/System/FileHelpers.swift Outdated Show resolved Hide resolved
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

@milseman milseman mentioned this pull request May 6, 2022
@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

Signed-off-by: Si Beaumont <beaumont@apple.com>
@simonjbeaumont
Copy link
Contributor Author

@swift-ci please test

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