Correct way to compare file paths when one is canonical and other absolute

33 views Asked by At

In my testing, I find myfile.getCanonicalPath() produces paths such as:

/data/data/<pkg>/files/...

and myfile.getAbsolutePath() produces paths such as:

/data/user/0/<pkg>/files/...

Given a method such as:

public boolean pathsAreEqual(String p1, String p2) {
  // return true if both paths represent the same location in filesystem
}

or

public String getRelativePath(String p1, String p2) {
  // return a reduced p2 to a relative path from p1

  // was hoping to use Path.relativize() here but that fails due to prefix diffs.
}

the leading path difference is problematic.

The only approach I can think of is to traverse the paths (right-to-left) until the pkg name is found and use that as the root.

Thanks for any ideas.

0

There are 0 answers