Skip to content

Fix FileHandle.truncateFile() #1297

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

Closed
wants to merge 1 commit into from
Closed

Fix FileHandle.truncateFile() #1297

wants to merge 1 commit into from

Conversation

DagAgren
Copy link

@DagAgren DagAgren commented Nov 4, 2017

FileHandle.truncateFile() currently does not work. It attempts to do a lseek() to the requested file size, which is not required before calling ftruncate. Furthermore, it expects this call to lseek() to return 0, which only happens if truncateFile() is called with a 0 size. Thus, the function will only succeed if called to truncate the file to zero bytes. In other cases, it will silently fail.

Remove useless lseek() which would cause the function to fail.
@ianpartridge
Copy link
Contributor

Thanks for your contribution! Can we improve the tests in this area at the same time?

@alblue
Copy link
Contributor

alblue commented Nov 6, 2017

@swift-ci please test

@alblue
Copy link
Contributor

alblue commented Nov 10, 2017

@DagAgren The change looks fine, but can you write some tests that exercise the new functionality, and verify it works as expected?

@alblue
Copy link
Contributor

alblue commented Nov 22, 2017

@DagAgren are you able to add the tests to verify your changes work as expected please?

@DagAgren
Copy link
Author

Sorry, I don't have an actual setup to build the code. I made this change quickly through github when I noticed the code was wrong while looking it up.

@alblue
Copy link
Contributor

alblue commented Nov 30, 2017

@ianpartridge would you be able to add a test for this so that we can get it merged in?

@ianpartridge
Copy link
Contributor

I'll have a go, yes.

@alblue
Copy link
Contributor

alblue commented Jan 25, 2018

@ianpartridge did you manage to get anywhere with tests for this, or should we just merge as is?

@nevil
Copy link
Contributor

nevil commented Apr 21, 2018

@alblue @ianpartridge @DagAgren

I created some basic tests for this change but noticed that the fix does not work the same as the Darwin implementation.

I had to change it like this:
https://github.com/nevil/swift-corelibs-foundation/blob/75b05c1d39d6fa395a47b12cb1bc9db254f158f0/Foundation/FileHandle.swift#L146-L149

Should I create a new PR with this implementation?

@parkera parkera requested a review from kperryua April 23, 2018 15:49
@kperryua
Copy link
Contributor

@nevil The behavior of your implementation more closely matches that of Darwin's, which is documented thusly:

Truncates or extends the file represented by the receiver to a specified offset within the file and puts the file pointer at that position.

The only real bug in the original implementation of this method is that it expects a return value of exactly 0 from lseek.

@nevil
Copy link
Contributor

nevil commented Apr 23, 2018

@kperryua Should I go ahead and create a new PR with the updated implementation?

@kperryua
Copy link
Contributor

@nevil Yes, please do.

@nevil nevil mentioned this pull request Apr 24, 2018
@nevil
Copy link
Contributor

nevil commented Apr 24, 2018

@kperryua
Created #1532

Thanks!

@nevil
Copy link
Contributor

nevil commented Apr 26, 2018

#1532 has been merged so I think this PR can be closed.

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.

6 participants