Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d6d1dbac authored by Narayan Kamath's avatar Narayan Kamath
Browse files

Implement FileUtils#contains.

Partial cherry-pick of changes 4ca728c0 and 21de56a9, which
can't be cherry-picked due to their large surface area.

Change-Id: Ife46e150d360cd5241dea93863141749233c1805
parent 41f40581
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -357,4 +357,26 @@ public class FileUtils {
            }
        }
    }

    /**
     * Test if a file lives under the given directory, either as a direct child
     * or a distant grandchild.
     * <p>
     * Both files <em>must</em> have been resolved using
     * {@link File#getCanonicalFile()} to avoid symlink or path traversal
     * attacks.
     */
    public static boolean contains(File dir, File file) {
        String dirPath = dir.getAbsolutePath();
        String filePath = file.getAbsolutePath();

        if (dirPath.equals(filePath)) {
            return true;
        }

        if (!dirPath.endsWith("/")) {
            dirPath += "/";
        }
        return filePath.startsWith(dirPath);
    }
}