Skip to content

Commit 49dfcd3

Browse files
committed
mkcomposefs: Add --hardlinks flag, matching composefs-rs
Wire up LCFS_BUILD_TRACK_HARDLINKS behind a new --hardlinks CLI option. The name matches composefs-rs's own drop-in mkcomposefs reimplementation, which likewise defaults to not tracking hardlinks (bit-for-bit compatible with this tool) and requires --hardlinks to opt into deduplication. The flag only affects the directory-scan path; combined with --from-file it is a harmless no-op since dumpfiles already encode hardlinks explicitly via the '@' prefix. Assisted-by: AI Closes: composefs#444 Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 2dbff17 commit 49dfcd3

4 files changed

Lines changed: 210 additions & 0 deletions

File tree

‎man/mkcomposefs.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ will be a mountable composefs image.
5858
**\-\-user-xattrs**
5959
: Only add xattrs with the "user." prefix to files in the image.
6060

61+
**\-\-hardlinks**
62+
: When building from a directory (i.e. without *--from-file*), detect
63+
files that share the same device and inode number and store them
64+
as hardlinks in the image, matching composefs-rs's *--hardlinks*
65+
option. This is not the default: without this option every file
66+
is treated as an independent inode with *nlink* of 1, which
67+
matches the historical behavior of this tool.
68+
6169
**\-\-from-file**
6270
: The source is a file in the **composefs-dump(5)** format. If
6371
the specified file is "-", the data is read from stdin.

‎tests/integration.sh‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,50 @@ run_test() {
5656

5757
run_test /usr/bin "--no-nlink"
5858

59+
# Regression test for https://github.com/composefs/composefs/issues/444:
60+
# building directly from a directory (as opposed to --from-file) used to
61+
# silently drop hardlinks, giving every file nlink=1 even if two paths
62+
# were hardlinked to each other. This is opt-in via --hardlinks, so also
63+
# confirm the (unchanged) default behavior. Runs for both regular files and
64+
# symlinks, since any non-directory type can be hardlinked (`ln -P`), not
65+
# just regular files.
66+
test_hardlinks() {
67+
local dir=hardlink-test
68+
local create=$1
69+
70+
rm -rf "${dir}"
71+
mkdir -p "${dir}/src"
72+
"${create}" "${dir}/src/file1"
73+
ln "${dir}/src/file1" "${dir}/src/file2"
74+
75+
# Default (no --hardlinks): every path gets its own independent inode
76+
# with nlink=1, matching this tool's historical behavior.
77+
mkcomposefs "${dir}/src" "${dir}/default.cfs"
78+
default_dump=$(composefs-info dump "${dir}/default.cfs")
79+
test "$(echo "${default_dump}" | awk '$1=="/file1"{print $4}')" = "1"
80+
test "$(echo "${default_dump}" | awk '$1=="/file2"{print $4}')" = "1"
81+
! echo "${default_dump}" | grep -q '^/file[12] [0-9]* @'
82+
83+
# --hardlinks: file1 and file2 are the same inode (nlink=2), with
84+
# exactly one of the two recorded as a hardlink (the "@" marker from
85+
# composefs-dump(5)) pointing at the other.
86+
mkcomposefs --hardlinks "${dir}/src" "${dir}/hardlinks.cfs"
87+
hl_dump=$(composefs-info dump "${dir}/hardlinks.cfs")
88+
test "$(echo "${hl_dump}" | awk '$1=="/file1"{print $4}')" = "2"
89+
test "$(echo "${hl_dump}" | awk '$1=="/file2"{print $4}')" = "2"
90+
test "$(echo "${hl_dump}" | grep -c '^/file[12] [0-9]* @')" = "1"
91+
92+
rm -rf "${dir}"
93+
}
94+
create_regular_file() {
95+
dd if=/dev/urandom "of=$1" bs=1024 count=4 status=none
96+
}
97+
create_symlink() {
98+
ln -s target-does-not-need-to-exist "$1"
99+
}
100+
test_hardlinks create_regular_file
101+
test_hardlinks create_symlink
102+
59103
check_fsverity () {
60104
fsverity --version >/dev/null 2>&1 || return 1
61105
tmpfile=$(mktemp --tmpdir lcfs-fsverity.XXXXXX)

‎tests/test-lcfs.c‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <errno.h>
1212
#include <endian.h>
1313
#include <sys/stat.h>
14+
#include <sys/sysmacros.h>
15+
#include <fcntl.h>
16+
#include <stdlib.h>
17+
#include <limits.h>
1418

1519
static inline void lcfs_node_unrefp(struct lcfs_node_s **nodep)
1620
{
@@ -227,6 +231,150 @@ static void test_hardlinked_whiteout_load(void)
227231
assert(errsv == EINVAL);
228232
}
229233

234+
static void create_regular_file(const char *path)
235+
{
236+
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
237+
assert(fd >= 0);
238+
assert(write(fd, "hello", 5) == 5);
239+
close(fd);
240+
}
241+
242+
static void create_symlink(const char *path)
243+
{
244+
assert(symlink("target-does-not-need-to-exist", path) == 0);
245+
}
246+
247+
// Verifies that lcfs_build() only detects hardlinked files of a given type
248+
// when scanning a directory tree if LCFS_BUILD_TRACK_HARDLINKS is passed; by
249+
// default (matching historical behavior) every path gets its own
250+
// independent inode with nlink == 1. Parameterized over the node type being
251+
// hardlinked, since any non-directory type (regular files, symlinks, FIFOs,
252+
// device nodes, sockets) can be hardlinked, not just regular files.
253+
static void test_build_hardlinks_for(void (*create)(const char *path))
254+
{
255+
char tmpdir[] = "/tmp/test-build-hardlinks.XXXXXX";
256+
assert(mkdtemp(tmpdir) != NULL);
257+
258+
char path1[PATH_MAX], path2[PATH_MAX];
259+
snprintf(path1, sizeof(path1), "%s/file1", tmpdir);
260+
snprintf(path2, sizeof(path2), "%s/file2", tmpdir);
261+
262+
create(path1);
263+
264+
assert(link(path1, path2) == 0);
265+
266+
int dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
267+
assert(dirfd >= 0);
268+
269+
/* Without LCFS_BUILD_TRACK_HARDLINKS: today's default behavior is
270+
* that every path becomes an independent inode with nlink == 1. */
271+
{
272+
char *failed_path = NULL;
273+
cleanup_node struct lcfs_node_s *root =
274+
lcfs_build(dirfd, ".", 0, &failed_path);
275+
assert(root != NULL);
276+
277+
struct lcfs_node_s *n1 = lcfs_node_lookup_child(root, "file1");
278+
struct lcfs_node_s *n2 = lcfs_node_lookup_child(root, "file2");
279+
assert(n1 != NULL);
280+
assert(n2 != NULL);
281+
assert(lcfs_node_get_hardlink_target(n1) == NULL);
282+
assert(lcfs_node_get_hardlink_target(n2) == NULL);
283+
assert(lcfs_node_get_nlink(n1) == 1);
284+
assert(lcfs_node_get_nlink(n2) == 1);
285+
286+
assert(testwrite_node(root) == 0);
287+
}
288+
289+
/* With LCFS_BUILD_TRACK_HARDLINKS: one of the two paths becomes a
290+
* hardlink to the other, and the target's nlink is bumped to 2. */
291+
{
292+
char *failed_path = NULL;
293+
cleanup_node struct lcfs_node_s *root = lcfs_build(
294+
dirfd, ".", LCFS_BUILD_TRACK_HARDLINKS, &failed_path);
295+
assert(root != NULL);
296+
297+
struct lcfs_node_s *n1 = lcfs_node_lookup_child(root, "file1");
298+
struct lcfs_node_s *n2 = lcfs_node_lookup_child(root, "file2");
299+
assert(n1 != NULL);
300+
assert(n2 != NULL);
301+
302+
/* Whichever of the two is visited first by readdir() becomes
303+
* the canonical inode; the other becomes a hardlink to it. */
304+
struct lcfs_node_s *target1 = lcfs_node_get_hardlink_target(n1);
305+
struct lcfs_node_s *target2 = lcfs_node_get_hardlink_target(n2);
306+
assert((target1 == NULL) != (target2 == NULL));
307+
308+
struct lcfs_node_s *link_node = target1 != NULL ? n1 : n2;
309+
struct lcfs_node_s *target_node = target1 != NULL ? n2 : n1;
310+
assert(lcfs_node_get_hardlink_target(link_node) == target_node);
311+
assert(lcfs_node_get_nlink(target_node) == 2);
312+
313+
assert(testwrite_node(root) == 0);
314+
}
315+
316+
close(dirfd);
317+
unlink(path1);
318+
unlink(path2);
319+
rmdir(tmpdir);
320+
}
321+
322+
// A chardev with rdev=0 is how composefs represents an overlayfs whiteout;
323+
// nlink>1 on one is semantically invalid (a whiteout marks the absence of a
324+
// file) and rejected outright by the EROFS writer. Verify that
325+
// LCFS_BUILD_TRACK_HARDLINKS deliberately leaves such inodes untracked, so
326+
// two hardlinked rdev=0 chardevs on disk still build (each as its own
327+
// independent inode) instead of failing.
328+
static void test_build_hardlinks_whiteout_exception(void)
329+
{
330+
char tmpdir[] = "/tmp/test-build-hardlinks.XXXXXX";
331+
assert(mkdtemp(tmpdir) != NULL);
332+
333+
char path1[PATH_MAX], path2[PATH_MAX];
334+
snprintf(path1, sizeof(path1), "%s/wh1", tmpdir);
335+
snprintf(path2, sizeof(path2), "%s/wh2", tmpdir);
336+
337+
if (mknod(path1, S_IFCHR | 0644, makedev(0, 0)) < 0) {
338+
/* Creating device nodes may be disallowed in some sandboxed
339+
* or unprivileged test environments; skip rather than fail. */
340+
assert(errno == EPERM || errno == EACCES);
341+
rmdir(tmpdir);
342+
return;
343+
}
344+
assert(link(path1, path2) == 0);
345+
346+
int dirfd = open(tmpdir, O_RDONLY | O_DIRECTORY);
347+
assert(dirfd >= 0);
348+
349+
char *failed_path = NULL;
350+
cleanup_node struct lcfs_node_s *root =
351+
lcfs_build(dirfd, ".", LCFS_BUILD_TRACK_HARDLINKS, &failed_path);
352+
assert(root != NULL);
353+
354+
struct lcfs_node_s *n1 = lcfs_node_lookup_child(root, "wh1");
355+
struct lcfs_node_s *n2 = lcfs_node_lookup_child(root, "wh2");
356+
assert(n1 != NULL);
357+
assert(n2 != NULL);
358+
assert(lcfs_node_get_hardlink_target(n1) == NULL);
359+
assert(lcfs_node_get_hardlink_target(n2) == NULL);
360+
assert(lcfs_node_get_nlink(n1) == 1);
361+
assert(lcfs_node_get_nlink(n2) == 1);
362+
363+
assert(testwrite_node(root) == 0);
364+
365+
close(dirfd);
366+
unlink(path1);
367+
unlink(path2);
368+
rmdir(tmpdir);
369+
}
370+
371+
static void test_build_hardlinks(void)
372+
{
373+
test_build_hardlinks_for(create_regular_file);
374+
test_build_hardlinks_for(create_symlink);
375+
test_build_hardlinks_whiteout_exception();
376+
}
377+
230378
// Verifies that lcfs_fd_measure_fsverity fails on a fd without fsverity
231379
static void test_no_verity(void)
232380
{
@@ -255,4 +403,5 @@ int main(int argc, char **argv)
255403
test_xattr_addremove();
256404
test_xattr_doubleadd();
257405
test_hardlinked_whiteout_load();
406+
test_build_hardlinks();
258407
}

‎tools/mkcomposefs.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static __attribute__((format(printf, 1, 2))) char *make_error(const char *fmt, .
5959
#define OPT_MIN_VERSION 114
6060
#define OPT_THREADS 115
6161
#define OPT_MAX_VERSION 116
62+
#define OPT_HARDLINKS 117
6263

6364
static size_t split_at(const char **start, size_t *length, char split_char,
6465
bool *partial)
@@ -1463,6 +1464,10 @@ static void usage(const char *argv0)
14631464
" --skip-devices Don't store device nodes\n"
14641465
" --skip-xattrs Don't store file xattrs\n"
14651466
" --user-xattrs Only store user.* xattrs\n"
1467+
" --hardlinks Detect and deduplicate hard-linked files when building\n"
1468+
" from a directory (matches composefs-rs's --hardlinks;\n"
1469+
" default is to treat every file as an independent\n"
1470+
" inode for compatibility)\n"
14661471
" --print-digest Print the digest of the image\n"
14671472
" --print-digest-only Print the digest of the image, don't write image\n"
14681473
" --from-file The source is a dump file, not a directory\n"
@@ -1488,6 +1493,7 @@ int main(int argc, char **argv)
14881493
.has_arg = no_argument,
14891494
.flag = NULL,
14901495
.val = OPT_SKIP_DEVICES },
1496+
{ .name = "hardlinks", .has_arg = no_argument, .flag = NULL, .val = OPT_HARDLINKS },
14911497
{ .name = "use-epoch", .has_arg = no_argument, .flag = NULL, .val = OPT_USE_EPOCH },
14921498
{ .name = "digest-store",
14931499
.has_arg = required_argument,
@@ -1557,6 +1563,9 @@ int main(int argc, char **argv)
15571563
case OPT_SKIP_DEVICES:
15581564
buildflags |= LCFS_BUILD_SKIP_DEVICES;
15591565
break;
1566+
case OPT_HARDLINKS:
1567+
buildflags |= LCFS_BUILD_TRACK_HARDLINKS;
1568+
break;
15601569
case OPT_DIGEST_STORE:
15611570
digest_store_path = optarg;
15621571
break;

0 commit comments

Comments
 (0)