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):
stat
realpath
(Is this Platform independent?)GetFullPathName
GetFileInformationByHandle
What would be the cross-platform best solution? I'm writing this in C++, but I can drop down to C obviously.
Two file paths can be compared lexicographically in Java using the method java. io. File. compareTo().
path. samefile() method in Python is used to check whether the given two pathnames refer to the same file or directory or not. This is determined by comparing device number and i-node number of the given paths.
The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object. For Example: if we create a file object using the path as “program.
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; path p("/path/to/file/one"); path q("/sym_link/to/one"); assert(equivalent(p, q));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With