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

Commit 2e2c7d23 authored by Narayan Kamath's avatar Narayan Kamath Committed by Android Git Automerger
Browse files

am 6af42aea: am 9f34234f: am b916d8ad: Merge "Implement FileUtils#contains."

* commit '6af42aea':
  Implement FileUtils#contains.
parents 5c5f4b70 6af42aea
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -382,4 +382,26 @@ public class FileUtils {
        }
        return filePath.startsWith(dirPath);
    }

    /**
     * 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);
    }
}