It appears that tar doesn't always recognize (and treat as hard links) files
that are the same file, but appear in two different directories from bind
mounts.

$ sudo mount -o bind /tmp/foo/bar /tmp/foo/baz
$ echo "test data" >/tmp/foo/bar/bleh
$ stat --printf "%d %i %n\n" /tmp/foo/bar/bleh /tmp/foo/baz/bleh
51 97105 /tmp/foo/bar/bleh
51 97105 /tmp/foo/baz/bleh

Notice the same device and inode number

Test 1 -- using tar with a single file path:

$ tar -cf /tmp/testing.tar /tmp/foo
$ tar -tvf /tmp/testing.tar
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/baz/
-rw-rw-r-- derekp/derekp    10 2021-12-29 19:00 tmp/foo/baz/bleh
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/bar/
-rw-rw-r-- derekp/derekp    10 2021-12-29 19:00 tmp/foo/bar/bleh

Notice that "bleh" is repeated twice as regular files.
Test 2 -- adding a second path to the command line:

$ tar -cf /tmp/testing.tar /tmp/foo /tmp/test
$ tar -tvf /tmp/testing.tar
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/baz/
-rw-rw-r-- derekp/derekp    10 2021-12-29 19:00 tmp/foo/baz/bleh
drwxrwxr-x derekp/derekp     0 2021-12-29 18:59 tmp/foo/bar/
hrw-rw-r-- derekp/derekp     0 2021-12-29 19:00 tmp/foo/bar/bleh link
to tmp/foo/baz/bleh
-rw-rw-r-- derekp/derekp     0 2021-12-29 19:01 tmp/test

In this case it successfully recognizes /tmp/foo/bar/bleh as a hard link to
/tmp/foo/baz/bleh.

Reply via email to