SAMEPATH is path the same

optional: java

true if inputs resolve to same path. Both paths must exist.

NOTE: in general on Windows same(".", "not-exist/..") is true, but on Unix it is false. In C/C++ access() or stat() the same behavior is observed Windows vs Unix.

Contents

Inputs

function y = samepath(path1, path2)
arguments
  path1 {mustBeTextScalar}
  path2 {mustBeTextScalar}
end

y = stdlib.exists(path1) && stdlib.exists(path2);

if ~y, return; end

if ~ispc() && stdlib.isoctave()
  [r1, e1] = stat(path1);
  [r2, e2] = stat(path2);

  y = e1 == 0 && e2 == 0 && ...
      r1.ino == r2.ino && r1.dev == r2.dev;

elseif stdlib.has_java() && stdlib.java_api() >= 11
% https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/nio/file/Files.html#isSameFile(java.nio.file.Path,java.nio.file.Path)
% Java 1.8 is buggy in some corner cases, so we require at least 11.
  y = java.nio.file.Files.isSameFile(javaPathObject(path1), javaPathObject(path2));

else

  y = strcmp(stdlib.canonical(path1), stdlib.canonical(path2));

end

%!assert(samepath(".", "."))
%!assert(samepath(".", "./"))
%!assert(!samepath("not-exist", "not-exist/.."))