Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.0] Canonicalize the parent path in RemoteActionFileSystem#delete. #21282

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,24 @@ public String getFileSystemType(PathFragment path) {
return "remoteActionFS";
}

// Like resolveSymbolicLinks(), except that only the parent path is canonicalized.
private PathFragment resolveSymbolicLinksForParent(PathFragment path) throws IOException {
PathFragment parentPath = path.getParentDirectory();
if (parentPath != null) {
return resolveSymbolicLinks(parentPath).asFragment().getChild(path.getBaseName());
}
return path;
}

@Override
protected boolean delete(PathFragment path) throws IOException {
try {
path = resolveSymbolicLinksForParent(path);
} catch (FileNotFoundException ignored) {
// Failure to delete a nonexistent path is not an error.
return false;
}

boolean deleted = localFs.getPath(path).delete();
if (isOutput(path)) {
deleted = remoteOutputTree.getPath(path).delete() || deleted;
Expand Down Expand Up @@ -462,10 +478,7 @@ protected void chmod(PathFragment path, int mode) throws IOException {

@Override
protected PathFragment readSymbolicLink(PathFragment path) throws IOException {
PathFragment parentPath = path.getParentDirectory();
if (parentPath != null) {
path = resolveSymbolicLinks(parentPath).asFragment().getChild(path.getBaseName());
}
path = resolveSymbolicLinksForParent(path);

if (path.startsWith(execRoot)) {
var execPath = path.relativeTo(execRoot);
Expand Down Expand Up @@ -497,10 +510,7 @@ protected PathFragment readSymbolicLink(PathFragment path) throws IOException {
@Override
protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFragment)
throws IOException {
PathFragment parentPath = linkPath.getParentDirectory();
if (parentPath != null) {
linkPath = resolveSymbolicLinks(parentPath).asFragment().getChild(linkPath.getBaseName());
}
linkPath = resolveSymbolicLinksForParent(linkPath);

if (isOutput(linkPath)) {
remoteOutputTree.getPath(linkPath).createSymbolicLink(targetFragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,49 @@ public void statAndExists_notFound() throws Exception {
FileNotFoundException.class, () -> actionFs.stat(path, /* followSymlinks= */ true));
}

@Test
public void delete_deleteSymlink() throws Exception {
RemoteActionFileSystem actionFs = (RemoteActionFileSystem) createActionFileSystem();

PathFragment linkPath = getOutputPath("link");
PathFragment targetPath = getOutputPath("target");
actionFs.getPath(linkPath).createSymbolicLink(execRoot.getRelative(targetPath).asFragment());
writeLocalFile(actionFs, targetPath, "content");

assertThat(actionFs.delete(linkPath)).isTrue();
assertThat(actionFs.exists(linkPath, /* followSymlinks= */ false)).isFalse();
assertThat(actionFs.exists(targetPath, /* followSymlinks= */ false)).isTrue();
}

@Test
public void delete_followSymlinks(
@TestParameter FilesystemTestParam from, @TestParameter FilesystemTestParam to)
throws Exception {
RemoteActionFileSystem actionFs = (RemoteActionFileSystem) createActionFileSystem();
FileSystem fromFs = from.getFilesystem(actionFs);
FileSystem toFs = to.getFilesystem(actionFs);

PathFragment dirLinkPath = getOutputPath("dirLink");
PathFragment dirTargetPath = getOutputPath("dirTarget");
fromFs
.getPath(dirLinkPath)
.createSymbolicLink(execRoot.getRelative(dirTargetPath).asFragment());
actionFs.getPath(dirTargetPath).createDirectoryAndParents();

PathFragment naivePath = dirLinkPath.getChild("file");
PathFragment canonicalPath = dirTargetPath.getChild("file");

if (toFs.equals(actionFs.getLocalFileSystem())) {
writeLocalFile(actionFs, canonicalPath, "content");
} else {
injectRemoteFile(actionFs, canonicalPath, "content");
}

assertThat(actionFs.delete(naivePath)).isTrue();
assertThat(actionFs.exists(naivePath, /* followSymlinks= */ false)).isFalse();
assertThat(actionFs.exists(canonicalPath, /* followSymlinks= */ false)).isFalse();
}

@Test
public void setLastModifiedTime_forRemoteOutputTree() throws Exception {
RemoteActionFileSystem actionFs = (RemoteActionFileSystem) createActionFileSystem();
Expand Down
Loading