coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

mv hardlinks should be preserved when moving into a different directory

Open sylvestre opened this issue 3 years ago • 11 comments

Testcase:

export C=/usr/bin/
export C="target/debug/coreutils "
dir=hlink
other_partition_tmpdir=/dev/shm/tmp769306

rm -rf $dir $other_partition_tmpdir
mkdir $dir $other_partition_tmpdir
> $dir/a
ln $dir/a $dir/b

${C}mv $dir "$other_partition_tmpdir"

ls -1i "$other_partition_tmpdir/$dir"

They should be equal. They aren't currently

sylvestre avatar May 06 '23 09:05 sylvestre

I've try to reproduce this:

❯ mkdir test
❯ mkdir test2
❯ > test/a
❯ ln test/a test/b
❯ target/release-small/coreutils mv test test2
❯ ls -1i test2/test/
4219332 a
4219332 b

Does your test only work when moving the folder to another partition?

Skallwar avatar Jun 20 '23 12:06 Skallwar

That's very likely, mv can't do a simple rename across partitions, so it has some fallback procedure that's probably not working as expected here.

tertsdiepraam avatar Jun 20 '23 12:06 tertsdiepraam

Ok thanks for your input, I will try to reproduce this with an USB key.

EDIT: I got the test working, I will try to fix this then

Skallwar avatar Jun 20 '23 12:06 Skallwar

I've tracked down the issue to move_dir here. But this function comes from webdesus/fs_extra so I will have a look there

Skallwar avatar Jun 20 '23 14:06 Skallwar

There is a similar issue for symlink and a PR was proposed without reaction from the author. I wonder if we should "just" implement it directly on our side...

Skallwar avatar Jun 21 '23 14:06 Skallwar

I've try to reproduce this:

❯ mkdir test
❯ mkdir test2
❯ > test/a
❯ ln test/a test/b
❯ target/release-small/coreutils mv test test2
❯ ls -1i test2/test/
4219332 a
4219332 b

Does your test only work when moving the folder to another partition?

Why do I get an error when trying to move the test into test2, but it is OKAY when using GNU mv?

nemesis@nemesis:~/Documents/Github/test/bin_3$ /home/nemesis/Documents/Github/Focus/util/coreutils_master/target/release/coreutils mv test test2
mv: cannot move 'test' to a subdirectory of itself, 'test2/test'

allaboutevemirolive avatar Oct 31 '23 06:10 allaboutevemirolive

@allaboutevemirolive it's a bug, I opened a ticket for it: https://github.com/uutils/coreutils/issues/5481

cakebaker avatar Oct 31 '23 07:10 cakebaker

There is a similar issue for symlink and a PR was proposed without reaction from the author. I wonder if we should "just" implement it directly on our side...

I guess this is probably not a good first issue anymore then, as it involves a project management decision. Perhaps the label should be dropped or at least direction about how to proceed with this provided.

Also, fs_extra's maintainer seems to have little time for continuing the project. Could that become a problem as uutils keeps expanding?

mfontanaar avatar Nov 08 '25 20:11 mfontanaar

There is a similar issue for symlink and a PR was proposed without reaction from the author. I wonder if we should "just" implement it directly on our side...

That project is abandoned fully, so either someone has to fork and maintain it or uutils can just write an implementation.

MuntasirSZN avatar Aug 06 '26 11:08 MuntasirSZN

This looks fixed on main by #8296, which wasn't linked here so the issue stayed open.

Re-ran the testcase on current main (f2855f0), moving a directory from ext4 /tmp to tmpfs /dev/shm with a, b and sub/c hardlinked:

dir/a ino=1618 nlink=3
dir/b ino=1618 nlink=3
dir/sub/c ino=1618 nlink=3

Same result as GNU mv 9.7. Moving two hardlinked files in one call also keeps them on one inode. mv no longer goes through fs_extra for this; the cross-device path uses hardlink.rs, and test_mv_preserves_hardlinks_across_partitions and the related tests in inter_partition_copying cover it.

I think this can be closed, and the good first issue label is probably no longer accurate.

fixfon avatar Oct 29 '26 21:10 fixfon

Correction to my earlier comment: the cross-device fix on main only covers moving into an existing directory. Tested on Linux x86_64, main e0c703b0e, /tmp (ext4) to /dev/shm (tmpfs), with d/a, d/b and d/s/c hard-linked:

  • mv d /dev/shm/x/: links kept (one inode), same as GNU 9.12.
  • mv d /dev/shm/x/new: a, b and s/c end up as three files with nlink=1.
  • mv -T d /dev/shm/x/new: same split.

GNU 9.12 keeps one inode in all three cases. #16125 fixes the two renaming forms on the same setup.

fixfon avatar Nov 02 '26 21:11 fixfon