Skip to content

When possible, clone files on Linux and FreeBSD.#1736

Merged
jmschonfeld merged 7 commits intomainfrom
jgrynspan/1727-clone-on-Linux-and-FreeBSD
Feb 13, 2026
Merged

When possible, clone files on Linux and FreeBSD.#1736
jmschonfeld merged 7 commits intomainfrom
jgrynspan/1727-clone-on-Linux-and-FreeBSD

Conversation

@grynspan
Copy link
Contributor

Resolves #1727.

This change adds support for file cloning on Linux and FreeBSD where supported by the underlying OS and filesystem.

Motivation:

Cloning is generally faster than block-based or bit-by-bit copying, so if a platform supports it we should try to leverage it to improve overall I/O performance.

Modifications:

Added calls to the relevant API on Linux and FreeBSD. FreeBSD's API is new in 15.0, so there's a version check.

Result:

Ideally, faster file copies on these platforms!

Testing:

I don't have a great story for unit testing here as if either call fails, we'll fall back to block-based copying which, in the general case, will appear to the caller like a successful copy. Furthermore, our CI systems aren't configured with file systems that support file cloning anyway. I'm open to suggestions!

This change adds support for file cloning on Linux and FreeBSD where supported
by the underlying OS and filesystem.

I don't have a great story for unit testing here as if either call fails, we'll
fall back to block-based copying which, in the general case, will appear to the
caller like a successful copy. Furthermore, our CI systems aren't configured
with file systems that support file cloning anyway.

Resolves #1727.
@grynspan grynspan self-assigned this Feb 11, 2026
@grynspan grynspan requested a review from a team as a code owner February 11, 2026 21:34
@grynspan grynspan added enhancement New feature or request linux Issue regarding compiling/running on Linux freebsd Issue regarding compiling/running on FreeBSD labels Feb 11, 2026
@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan
Copy link
Contributor Author

@michael-yuji Mind taking a look?

#else
// Compiled against an older unistd.h, but presumably running on FreeBSD 15.0
// or newer. SEE: https://github.com/freebsd/freebsd-src/blob/main/sys/sys/unistd.h
return 0x00800000;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're building toolchains against FreeBSD 14.3's SDK right now, so the constant isn't defined yet.

@jmschonfeld
Copy link
Contributor

Re. testing - is it possible to easily check in a unit test whether or not cloning is supported and whether or not two files are clones? I suspect the latter might be feasible but the former is probably non trivial, and if none of our CI environments have file systems that actually support this then the point is probably moot.

Were you able to test locally on Linux and FreeBSD that it correctly creates a clone when running in an environment that supports it just to confirm we've tested it somewhere?

@grynspan
Copy link
Contributor Author

I've tested the equivalent code in Swift Testing on local VMs. I'm not certain how to go about doing that for Foundation but maybe it's straightforward to do it as a package dependency?

@grynspan
Copy link
Contributor Author

Verified in a VM that FileManagerTests.c clones files successfully:

% filefrag -e ../dir/foo
Filesystem type is: 58465342
File size of ../dir/foo is 10000 (3 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       2:      57613..     57615:      3:             last,shared,eof
../dir/foo: 1 extent found
% filefrag -e ../dir2/foo
Filesystem type is: 58465342
File size of ../dir2/foo is 10000 (3 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       2:      57613..     57615:      3:             last,shared,eof
../dir2/foo: 1 extent found

Note shared on both files indicating the extents are shared (not necessarily between each other but, yes, between each other).

@grynspan
Copy link
Contributor Author

I can't figure out how to check reflinked extents on FreeBSD but the call succeeds at least. It's documented to fail with EOPNOTSUPP if cloning isn't supported, so… good enough signal.

@grynspan
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@jmschonfeld jmschonfeld left a comment

Choose a reason for hiding this comment

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

Looks good overall, one organizational comment

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan requested a review from jmschonfeld February 12, 2026 00:07
@grynspan
Copy link
Contributor Author

@swift-ci test

Copy link
Member

@michael-yuji michael-yuji left a comment

Choose a reason for hiding this comment

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

lgtm, I'm yet able to test it locally, but things looks right to me

@grynspan grynspan requested a review from jmschonfeld February 13, 2026 19:45
@jmschonfeld jmschonfeld merged commit cdab606 into main Feb 13, 2026
18 checks passed
@jmschonfeld jmschonfeld deleted the jgrynspan/1727-clone-on-Linux-and-FreeBSD branch February 13, 2026 23:37
@github-project-automation github-project-automation bot moved this from Merge to Done in Swift on FreeBSD Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request freebsd Issue regarding compiling/running on FreeBSD linux Issue regarding compiling/running on Linux

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

FileManager does not support file cloning on Linux/FreeBSD

3 participants