2

I have file paths in one side and their new paths where I want to duplicate them.

How to test whether they can be simply hardlinked or they should be copied?

Zebooka
  • 380
  • 2
  • 10

2 Answers2

4

Step 1: Call link.

Step 2: If it returns -1 with errno set to EXDEV, then make a copy.

[update]

In general, there is no portable (and reliable) way to answer this question. The closest you can get, I think, is to call statvfs for the source and destination and then compare the f_fsid fields of the statvfs structures.

Unfortunately, POSIX does not guarantee anything in particular about the f_fsid field. This test should work on Linux unless maybe you are asking about NFS-mounted filesystems...

Nemo
  • 70,042
  • 10
  • 116
  • 153
  • But it will create hardlink, and I do not want to do this - I need only to know whether it can be done. Also I'm talking about shell commands :) – Zebooka Feb 03 '12 at 06:03
1

Nemo's answer is indeed the simplest solution: try the link(2) syscall.

If you only want to know if the path are hard linkable without even attempting the link(2) syscall, you could find their filesystem e.g. using statfs(2), check that they are on the same filesystem, and check that the filesystem's type is good enough.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547