|
11 | 11 | #include <errno.h> |
12 | 12 | #include <endian.h> |
13 | 13 | #include <sys/stat.h> |
| 14 | +#include <sys/sysmacros.h> |
| 15 | +#include <fcntl.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <limits.h> |
14 | 18 |
|
15 | 19 | static inline void lcfs_node_unrefp(struct lcfs_node_s **nodep) |
16 | 20 | { |
@@ -227,6 +231,150 @@ static void test_hardlinked_whiteout_load(void) |
227 | 231 | assert(errsv == EINVAL); |
228 | 232 | } |
229 | 233 |
|
| 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 | + |
230 | 378 | // Verifies that lcfs_fd_measure_fsverity fails on a fd without fsverity |
231 | 379 | static void test_no_verity(void) |
232 | 380 | { |
@@ -255,4 +403,5 @@ int main(int argc, char **argv) |
255 | 403 | test_xattr_addremove(); |
256 | 404 | test_xattr_doubleadd(); |
257 | 405 | test_hardlinked_whiteout_load(); |
| 406 | + test_build_hardlinks(); |
258 | 407 | } |
0 commit comments