Page MenuHomeFreeBSD

truncate: fix a minor nit + add a hole-punching test
Needs ReviewPublic

Authored by kevans on Jul 8 2025, 6:10 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Oct 5, 10:15 AM
Unknown Object (File)
Wed, Sep 24, 5:02 AM
Unknown Object (File)
Tue, Sep 16, 7:53 AM
Unknown Object (File)
Mon, Sep 15, 2:20 PM
Unknown Object (File)
Mon, Sep 15, 12:36 PM
Unknown Object (File)
Sep 6 2025, 11:34 AM
Unknown Object (File)
Sep 2 2025, 2:03 PM
Unknown Object (File)
Aug 25 2025, 9:06 PM
Subscribers

Details

Reviewers
khng
Group Reviewers
Klara
Summary

The struct spacectl_range we use is only really used in these three
lines of code, so re-scope it down to just the dealloc branch. This is
marginally easier to reason about what might be necessary to replace in
porting our truncate(1) to other platforms.

While we're here, add a test for the -d flag to be sure it really does
punch a hole in the file. The test also tries to confirm that it does
not disturb other segments of the file in the process, just to inspire
some confidence that it's not corrupting the file somehow.

Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 65276
Build 62159: arc lint + arc unit

Event Timeline

kevans requested review of this revision.Jul 8 2025, 6:10 PM

Fix a few nits upon post-review-creation-review

des added inline comments.
usr.bin/truncate/tests/lssparse.c
2

This largely duplicates bin/cp/tests/sparse.c.

usr.bin/truncate/tests/lssparse.c
2

I mean, they're both fairly trivial (and this particular one is an extract of the larger "mksparse" that I wrote for openrsync to additionally build out and sparsify files) - I need actual reporting of all the holes, not just whether there's any hole, and also the extra bits to decide the hole size. I could enhance that one, but where would you suggest placing it instead? Reaching into bin/cp/tests feels a bit wrong.

markj added inline comments.
usr.bin/truncate/tests/truncate_test.sh
454

Rather than filling a file using /dev/zero, can you use /dev/random to defeat the compression? In general it's nice to use the native filesystem when possible, so we get different test coverage depending on the filesystem in use. I run the test suite on both UFS and ZFS, for instance.

usr.bin/truncate/tests/lssparse.c
2

Perhaps we should just add this functionality to stat(1)...

kevans marked 3 inline comments as done.

Address review feedback; use stat -h and stop asserting on hole reporting

We'll still want stat -h to tell us the min hole size that we're working with,
but stop testing for hole reporting and just test that the segment has been
zeroed out. Many modern filesystems make this complicated as they have creative
license to deallocate as much or as little as they want; we can make the request
to deallocate a block and they will zero it out, but they may choose to keep the
block allocated for whatever reason.

Switch to just ensuring that the requested block is zeroed out without
collateral damage.