Search in sources :

Example 1 with ResolvedSymlink

use of com.google.copybara.util.FileUtil.ResolvedSymlink in project copybara by google.

the class CheckoutPath method readSymbolicLink.

@StarlarkMethod(name = "read_symlink", doc = "Read the symlink")
public CheckoutPath readSymbolicLink() throws EvalException {
    try {
        Path symlinkPath = checkoutDir.resolve(path);
        if (!Files.isSymbolicLink(symlinkPath)) {
            throw Starlark.errorf("%s is not a symlink", path);
        }
        ResolvedSymlink resolvedSymlink = FileUtil.resolveSymlink(Glob.ALL_FILES.relativeTo(checkoutDir), symlinkPath);
        if (!resolvedSymlink.isAllUnderRoot()) {
            throw Starlark.errorf("Symlink %s points to a file outside the checkout dir: %s", symlinkPath, resolvedSymlink.getRegularFile());
        }
        return create(checkoutDir.relativize(resolvedSymlink.getRegularFile()));
    } catch (IOException e) {
        String msg = String.format("Cannot resolve symlink %s: %s", path, e);
        logger.atSevere().withCause(e).log("%s", msg);
        throw Starlark.errorf("%s", msg);
    }
}
Also used : Path(java.nio.file.Path) ResolvedSymlink(com.google.copybara.util.FileUtil.ResolvedSymlink) IOException(java.io.IOException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod)

Aggregations

ResolvedSymlink (com.google.copybara.util.FileUtil.ResolvedSymlink)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)1