13

does anyone know how to check if a file or directory is either a Symbolic Link, Junction Point, Mount Point or Hard Link?

As far as I know a symbolic links are detected by checking a file for its "ReparsePoint" attribute. Junction points are detected by checking a directory for the "ReparsePoint" attribute. So if the "ReparsePoint" attribute is set on a file it must be a symbolic link, otherwise if it's set on a directory it can only be a junction point...right?

Good so far, but I have still no idea how to detect "Mount Points" and "Hard Links". Can anyone tell me how to do this?

PythonJin
  • 4,034
  • 4
  • 32
  • 40
Alexander
  • 3,724
  • 8
  • 42
  • 50

3 Answers3

13

Symbolic Links, Junction Points, and Mount Points are all examples of different reparse points. Hard Links, however, are just regular files. On NTFS all files are hard links. You can detect that a file has multiple hard links pointing to it, but there's no "real file" that it points to. You can think of hard links as just different names for the same file.

Here's some information on accessing reparse points from C#: http://www.codeproject.com/KB/vista/ReparsePointID.aspx?display=Print

Here's some information on how to do it in C: http://blog.kalmbach-software.de/2008/02/

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Ok, this means if a have FileA.txt and a hard link FileB.txt pointing to FileA.txt there is no way to detect that FileB.txt is a hard Link? – Alexander Mar 21 '10 at 13:45
  • 7
    They BOTH are hard links. – akappa Mar 21 '10 at 13:51
  • Ok, so is there a way to detect Mount Points? – Alexander Mar 21 '10 at 13:58
  • I don't think there is a portable way to do it, since it's an OS-defined concept. On linux, you can just access to /proc/mounts and take the second field of each line. – akappa Mar 21 '10 at 14:04
  • Mount points are reparse points just like junctions. Are you asking how to tell the difference between different kinds of reparse points? Or do you want to know what they point to? – Gabe Mar 21 '10 at 16:47
  • In truth, every filename is a hard link to the file content. Hard links are always linking names to content, symbolic links (which are themselves a form of content) are links to other names, that's the difference. – Ben Voigt Mar 21 '10 at 17:02
  • @akappa: then if you delete FileA.txt what happens to FileB.txt ? – v.oddou Jun 03 '14 at 09:53
  • @v.oddou: nothing. Think of it as reference-counting garbage collection: the file is erased only when the number of hard-reference pointing to it is zero. When you delete FileA.txt, you are decrementing the reference count from 2 to 1, so nothing happens. – akappa Jun 03 '14 at 12:03
  • @v.oddou: The concept of "deleting" a file is a bit tricky. You're not deleting the file so much as removing one of its names. Once a file has no name, the file itself is removed. – Gabe Jun 03 '14 at 15:29
5

Hard links:

You can detect if multiple names are pointing to the same "data chunk" or "file content" by invoking the Win32 API function GetFileInformationByHandle. The nNumberOfLinks member of the returned BY_HANDLE_FILE_INFORMATION structure contains the total number of links

Mount Points:

You can iterate through all the mount points on a volume using FindFirstVolumeMountPoint and FindNextVolumeMountPoint. Also FindVolumeMountPointClose should be used to close the search handle.

From .NET

Doing this from .NET will require some P/Invoke magic

Hannes de Jager
  • 2,903
  • 6
  • 37
  • 57
  • See also this post for some related code and information: https://stackoverflow.com/a/39399232/1082063. In particular, the code there shows how to obtain the results from GetFileInformationByHandle and determine if two file handles point to the same file object. – David I. McIntosh Sep 26 '21 at 22:38
2

Please see my question NTFS Junctions, trouble understanding the API. It is kind of a duplicate of the question. But I explain all about how reparse points, mountpoints, junctions, and symbolic links are implemented, using C/C++. Instead of just giving links to API, blindly...

Community
  • 1
  • 1
unixman83
  • 9,421
  • 10
  • 68
  • 102