Home » Linux » What is the best way of determining that two file paths are referring to the same file object?

# What is the best way of determining that two file paths are referring to the same file object?

Questions:

I want to write a unit test that checks that two file paths are equivalent, but I don’t want to assume that the string representation is the same.

For example, on Linux there could be symlinks in the path in one case and not in the other. On Windows, there could be drive notation on one (X:\foo) and network notation on the other (//serverX/foo). And most complicated, the file may have been written on Linux on an NFS share (in /path/to/file syntax) and verified on Windows using DOS syntax (X:\to\file) where X: is a NFS mount to /path.

Some ideas (found here on Stack Overflow, but not unified):

1. On Linux, compare the inode from stat
2. Use realpath (Is this Platform independent?)
3. On Windows, compare strings on GetFullPathName
4. On Windows, compare serial numbers and file index from GetFileInformationByHandle

What would be the cross-platform best solution? I’m writing this in C++, but I can drop down to C obviously.

You could check out the Boost.Filesystem library. Specifically, there is a method equivalent that seems to do exactly what you are looking for:
using namespace boost::filesystem;