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

Add FileHandle::truncate and ftruncate #7162

Merged
merged 4 commits into from
Oct 31, 2018
Merged

Conversation

kjbracey
Copy link
Contributor

@kjbracey kjbracey commented Jun 7, 2018

Description

Add support for file truncation (or extension) to the abstract API.

Unit tests added.

Implementation added for:

  • File::truncate
  • FileSystem::file_truncate
  • FATFileSystem::file_truncate
  • LittleFileSystem::file_truncate

Pull request type

[ ] Fix
[ ] Refactor
[ ] New target
[X] Feature
[ ] Breaking change

@kjbracey kjbracey requested a review from geky June 7, 2018 11:31
@kjbracey
Copy link
Contributor Author

kjbracey commented Jun 7, 2018

Relevant to #7091 and #7010

@0xc0170 0xc0170 requested a review from a team June 7, 2018 13:36
@dannybenor
Copy link

I am not sure we want the side effects of this feature on flash based file systems

@kjbracey
Copy link
Contributor Author

kjbracey commented Jun 8, 2018

I am not sure we want the side effects of this feature on flash based file systems

If a particular filing system cannot or does not want to implement the call, then that's fine. Specialised filing systems can have basically arbitrary limitations.

Only fully general-purpose random-access filing systems would be expected to implement every call and usage pattern and thus be usable by every application. If you are one of those, then truncating is trivial, and extension can be no worse than the application writing all the zeroes manually, which you must support. If you're not general purpose, maybe you don't support changing file lengths after creation at all. That would be fine.

In a specialised system, one potential use of this is for efficient buffer pre-allocation. Eg a system that supports the sequence "open(), write(), write(), write() close()" to create a file might have a fixed-size buffer pre-allocation and thus limit on the total.

But you could have a contract such that if "truncate" was issued before the first write, you set the write buffer to that size - can be used as an API to specify the total amount you're intending to write.

@cmonr
Copy link
Contributor

cmonr commented Jun 18, 2018

@ARMmbed/mbed-os-storage Would y'all mind posting a response to @kjbracey-arm explanation/rationale ?

@geky
Copy link
Contributor

geky commented Jun 18, 2018

Sorry about delay, this looked reasonable to me, but I want to toss in the implementations that came out of the discussions with @OscarGarciaF:

Next chance I get I can bring them in if no one beats me to it (on top of this pr hopefully).

Copy link
Contributor

@davidsaada davidsaada left a comment

Choose a reason for hiding this comment

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

Other than the minor Doxy issue, looks good to me.

platform/FileHandle.h Show resolved Hide resolved
@dannybenor
Copy link

We need to have a test for this new feature

@dannybenor
Copy link

Please do not merge

@kjbracey
Copy link
Contributor Author

kjbracey commented Jul 2, 2018

Added unit test covering the FileHandle/retarget bits being added here.

Actual functional tests would be a matter for any invidual filing systems implementing it at the minute - we've no general filing system-agnostic test suite afaik.

@geky
Copy link
Contributor

geky commented Jul 2, 2018

Hey @kjbracey-arm, I tacked on the filesystem system implementations of truncate. Feel free to remove if they're just getting the in way.

Thanks for writing up the documentation and other glue layers.

@dannybenor
Copy link

My general approach is that I do not want to add new functionality to mbedos that is not clearly required. In addition, we need good testing at the file system level.
Will not merge until both conditions are achieved: We know why we need it and we have tests

@kjbracey
Copy link
Contributor Author

kjbracey commented Jul 3, 2018

For this particular call, I find it hard to think of it as new functionality - I think this just covers an odd omission in the existing "support general-purpose filesystems" functionality. My first thought on seeing a request to add it was "whoops, why wasn't that already there?". If we can extend files, and seek, we should be able to truncate them.

ftruncate is a pretty core part of the general POSIX filing system API we try to follow, and is already implemented in the two main general-purpose filing systems in the system - it's just not been exposed through the generic mbed OS C++ API.

We've had a couple of user requests to access this for the general-purposes filing systems, and it also has potential utility as a pre-write size hint for key/value-type filing systems as described above.

Agree that it should be added to FAT and LFS tests when we hook it up to them, so @geky's added commit now requires that.

@cmonr
Copy link
Contributor

cmonr commented Aug 13, 2018

@kjbracey-arm @davidsaada @geky @dannybenor
Considering that this PR has sat for over a month, is it safe to say that this will not be going in and should be clsoed?

@geky
Copy link
Contributor

geky commented Aug 14, 2018

Truncate is a fundamental function for a POSIX filesystem. If it's not going in now, it should go in eventually. It's just seems to be a low priority item.

@cmonr, could be move this to a feature branch for now to get it out of the pr pool?

@cmonr
Copy link
Contributor

cmonr commented Aug 14, 2018

@geky Yeah, that works for me, unless anyone else opposes.

I can look into it tomorrow.

@cmonr
Copy link
Contributor

cmonr commented Aug 18, 2018

Branch feature-posix has been created. Feel free to close this PR and reopen against that branch.

@geky geky changed the base branch from master to feature-posix August 18, 2018 17:21
@geky
Copy link
Contributor

geky commented Aug 18, 2018

@cmonr, turns out GitHub supports reassigning branches

@cmonr
Copy link
Contributor

cmonr commented Aug 18, 2018

@geky Take a look at the last Travis CI run. GitHub may work with it just bine, but from what I've observed, Travis tends to not like it.

We can see is that's still the case after a rebase ;)

@cmonr
Copy link
Contributor

cmonr commented Aug 24, 2018

Closing and re-opening to get Ttravis to recognize the updated base branch.

@cmonr
Copy link
Contributor

cmonr commented Oct 26, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

Build : SUCCESS

Build number : 3473
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7162/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 26, 2018

@cmonr
Copy link
Contributor

cmonr commented Oct 29, 2018

@kjbracey-arm When you get a chance, the test failures were quite consistent across devices.

kjbracey and others added 3 commits October 30, 2018 17:19
ARM C library is really good at optimising out calls to underlying
seek. The only ones we were actually detecting in the empty file case
were the ones that the default FileHandle::size() made itself during
the SEEK_END case.

When we implement TestFile::size() directly, we will no longer see
a single seek call from the ARM C library in the empty file case, so
remove those tests.

Beef up the non-empty file case, adding checks that we are making
underlying read+write calls in the correct position, as a proxy for
direct checks for underlying seek being called.
- File::truncate
- FileSystem::file_truncate
- FATFileSystem::file_truncate
- LittleFileSystem::file_truncate
@kjbracey
Copy link
Contributor Author

Okay, that was a pain. As @dannybenor suggested, the ARM C library is really good at optimising out underlying seek calls. In fact, the only ones the test was detecting in the ARM C empty file case were the ones that the default FileHandle::size() made itself. As soon as the TestFile was upgraded to have its own size for the truncate test there were no remaining seeks to detect in the empty file test. Reworked the test to cover everything in the non-empty file test.

I've run the modified tests locally on ARM C and GCC.

@cmonr
Copy link
Contributor

cmonr commented Oct 30, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 30, 2018

Build : SUCCESS

Build number : 3507
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7162/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 30, 2018

Test : SUCCESS

Build number : 3292
Test logs :http://mbed-os-logs.s3-website-us-west-1.amazonaws.com/?prefix=logs/7162/3292

@mbed-ci
Copy link

mbed-ci commented Oct 31, 2018

@cmonr cmonr merged commit eea338c into ARMmbed:feature-posix Oct 31, 2018
@kjbracey
Copy link
Contributor Author

Why don't we just merge this in 5.11?

Should I go ahead and make a merging PR for the feature branch? Could there be anything else that would be aiming for the feature branch in 5.11 timeframe?

@cmonr
Copy link
Contributor

cmonr commented Oct 31, 2018

Not that I'm aware of. @ARMmbed/mbed-os-storage ^^^

@geky
Copy link
Contributor

geky commented Oct 31, 2018

Probably better to aim for 5.12 at this point, but is there any reason not to move this into master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants