aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/ai/vespa/validation/PathValidator.java
blob: 0ae81e2315d91d41ade94d9494ae86a23f810795 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ai.vespa.validation;

import java.nio.file.Path;

/**
 * Path validations
 *
 * @author mortent
 */
public class PathValidator {

    /**
     * Validate that file is a child of basedir
     * @param root Root directory to use for validation
     * @param path Path to validate
     * @throws IllegalArgumentException if path is not a child of root
     */
    public static void validateChildOf(Path root, Path path) {
        if (!path.normalize().startsWith(root)) {
            throw new IllegalArgumentException("Invalid path %s".formatted(path));
        }
    }

    /**
     * Resolves a path under a root path
     * @param root root poth
     * @param path child to resolve
     * @return The resolved path
     * @throws IllegalArgumentException If the provided child path does not resolve as child of root
     */
    public static Path resolveChildOf(Path root, String path) {
        Path resolved = root.resolve(path);
        validateChildOf(root, resolved);
        return resolved;
    }
}