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

Prevent BufferedWriter from producing empty files #2677

Merged
merged 6 commits into from Mar 12, 2024

Conversation

stefanadranca
Copy link
Contributor

@stefanadranca stefanadranca commented Mar 11, 2024

Preventing BufferedWriter from producing empty files

Motivation:

In the current implementation, if the users forget to call 'flush()' on the writer when creating a new file with 'withFileHandle(forWritingAt:options:execute)' the newly created file is empty.

Modifications:

Implemented the 'withBufferedWriter()' method for the 'WriteFileHandle'. The method calls flush on the bufferedWriter after executing the closure that writes the contents to the buffer as indicated by the user.

Result:

Users can now use the 'withBufferedWriter' method instead of instantiating the bufferedWriter themselves, reducing the possibility of omitting 'flush()'.

Motivation:

In the current implementation, if the users forget to call 'flush()' on
the writer when creating a new file with 'withFileHandle(forWritingAt:options:execute)' the newly created file is empty.

Modifications:

- Added the 'withBufferedWriter()' method for the 'WritableFileHandleProtocol' and implemented it for
the WriteFileHandle and 'ReadWriteFilehandle'. The method calls flush on the bufferedWriter after
executing the closure that writes the contents to the file as indicated by the user.

Result:

Users can now use the 'withBufferedWriter' method instead of instantiating the bufferedWriter themselves,
reducing the possibility of omitting 'flush()'.

TO DO:
- documentthe method
- implement tests
@glbrntt
Copy link
Contributor

glbrntt commented Mar 11, 2024

@swift-server-bot add to allowlist

@stefanadranca stefanadranca marked this pull request as ready for review March 11, 2024 19:05
Copy link
Contributor

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

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

Couple of doc nits but looks good otherwise!

///
/// - Parameters:
/// - body: The closure that writes the contents to the buffer created in this method.
/// - Returns: The result of the executed closure.
Copy link
Contributor

Choose a reason for hiding this comment

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

Returns: shouldn't be indented here, otherwise it's treated as documentation for a parameter

Sources/NIOFileSystem/BufferedWriter.swift Show resolved Hide resolved
@glbrntt glbrntt added the 🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0 label Mar 12, 2024
@glbrntt glbrntt enabled auto-merge (squash) March 12, 2024 10:36
@glbrntt glbrntt merged commit a40ad7c into apple:main Mar 12, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NIOFileSystem's BufferedWriter produces empty files when they shouldn't be empty
2 participants