Skip to content

Commit 73f2e41

Browse files
uvf: type-aware, failure-safe rename replacement
1 parent cc9cf05 commit 73f2e41

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

‎vfs-middleware-uvf/src/main/java/org/cryptomator/vfs/middleware/uvf/UvfFileSystem.java‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,26 +289,48 @@ private void deleteSymbolicLink(ResolvedPath resolved) throws IOException {
289289
delegate.deleteDirectory(resolved.entryPath());
290290
}
291291

292+
/**
293+
* {@inheritDoc}
294+
*
295+
* @implNote Replacing a target is only atomic for a file replacing a file. Directories and symbolic links are directories in the ciphertext, which
296+
* no delegate can replace in one step, so in every other combination the target is deleted before the move: a concurrent lookup may see it missing,
297+
* and a failing move leaves it deleted.
298+
*/
292299
@Override
293300
public void rename(String sourcePath, String targetPath, boolean replaceExisting) throws IOException {
294301
if (targetPath.startsWith(sourcePath + "/")) {
295302
// the ciphertext layout is flat, so the delegate cannot notice that the move would orphan the whole subtree
296303
throw new FileSystemException(sourcePath, targetPath, "Cannot move a directory into itself");
297304
}
298305
var source = pathResolver.resolve(sourcePath);
306+
var sourceType = nodeType(source, delegate.readNodeMetadata(source.entryPath())); // fails before anything is touched if the source is missing
307+
if (sourcePath.equals(targetPath)) {
308+
return;
309+
}
299310
var target = pathResolver.resolve(targetPath);
300-
var targetMetadata = replaceExisting ? metadataOrNull(target.entryPath()) : null;
301-
if (targetMetadata != null && targetMetadata.type() == VfsNodeType.DIRECTORY) {
302-
// directory and symlink entries are ciphertext directories with content, which the delegate can never replace; remove them first
311+
var targetMetadata = replaceExisting ? metadataOrNull(target.entryPath()) : null; // without the flag the delegate's own no-replace check stays atomic
312+
if (targetMetadata != null) {
303313
switch (nodeType(target, targetMetadata)) {
304314
case DIRECTORY -> {
305-
if (nodeType(source, delegate.readNodeMetadata(source.entryPath())) != VfsNodeType.DIRECTORY) {
315+
if (sourceType != VfsNodeType.DIRECTORY) {
306316
throw new FileSystemException(sourcePath, targetPath, "Is a directory");
307317
}
308-
deleteDirectory(targetPath);
318+
deleteDirectory(targetPath); // refuses non-empty directories
319+
}
320+
case SYMBOLIC_LINK -> {
321+
if (sourceType == VfsNodeType.DIRECTORY) {
322+
throw new FileSystemException(sourcePath, targetPath, "Not a directory");
323+
}
324+
deleteSymbolicLink(target);
325+
}
326+
case FILE -> {
327+
if (sourceType == VfsNodeType.DIRECTORY) {
328+
throw new FileSystemException(sourcePath, targetPath, "Not a directory");
329+
}
330+
if (sourceType == VfsNodeType.SYMBOLIC_LINK) {
331+
delegate.deleteFile(target.contentsPath()); // file over file is left to the delegate's atomic replace
332+
}
309333
}
310-
case SYMBOLIC_LINK -> deleteSymbolicLink(target);
311-
case FILE -> throw new AssertionError();
312334
}
313335
}
314336
try {

‎vfs-middleware-uvf/src/test/java/org/cryptomator/vfs/middleware/uvf/UvfFileSystemTest.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,63 @@ void replaceNonEmptyDirectory() throws IOException {
365365
assertThrows(DirectoryNotEmptyException.class, () -> fs.rename("/src", "/dst", true));
366366
}
367367

368+
@Test
369+
@DisplayName("a missing source leaves the target untouched")
370+
void missingSource() throws IOException {
371+
fs.createDirectory("/dst", Set.of());
372+
fs.createSymbolicLink("/link", "t");
373+
374+
assertThrows(NoSuchFileException.class, () -> fs.rename("/missing", "/dst", true));
375+
assertThrows(NoSuchFileException.class, () -> fs.rename("/missing", "/link", true));
376+
377+
assertEquals(VfsNodeType.DIRECTORY, fs.readNodeMetadata("/dst").type());
378+
assertEquals("t", fs.readSymbolicLink("/link"));
379+
}
380+
381+
@Test
382+
@DisplayName("renaming onto itself keeps the node")
383+
void ontoItself() throws IOException {
384+
fs.createDirectory("/d", Set.of());
385+
writeFile("/d/f", "x");
386+
fs.createSymbolicLink("/link", "t");
387+
388+
fs.rename("/d", "/d", true);
389+
fs.rename("/link", "/link", true);
390+
391+
assertEquals("x", readFile("/d/f"));
392+
assertEquals("t", fs.readSymbolicLink("/link"));
393+
}
394+
395+
@Test
396+
@DisplayName("a symbolic link replaces a file and vice versa")
397+
void symlinkAndFileReplaceEachOther() throws IOException {
398+
fs.createSymbolicLink("/link", "t");
399+
writeFile("/f", "x");
400+
fs.createSymbolicLink("/link2", "u");
401+
writeFile("/g", "y");
402+
403+
fs.rename("/link", "/f", true);
404+
fs.rename("/g", "/link2", true);
405+
406+
assertEquals("t", fs.readSymbolicLink("/f"));
407+
assertEquals("y", readFile("/link2"));
408+
assertThrows(NoSuchFileException.class, () -> fs.readNodeMetadata("/link"));
409+
assertThrows(NoSuchFileException.class, () -> fs.readNodeMetadata("/g"));
410+
}
411+
412+
@Test
413+
@DisplayName("directories and symbolic links never replace each other")
414+
void directoryAndSymlink() throws IOException {
415+
fs.createDirectory("/d", Set.of());
416+
fs.createSymbolicLink("/link", "t");
417+
418+
assertThrows(FileSystemException.class, () -> fs.rename("/d", "/link", true));
419+
assertThrows(FileSystemException.class, () -> fs.rename("/link", "/d", true));
420+
421+
assertEquals(VfsNodeType.DIRECTORY, fs.readNodeMetadata("/d").type());
422+
assertEquals("t", fs.readSymbolicLink("/link"));
423+
}
424+
368425
@Test
369426
@DisplayName("refuses to move a directory into its own subtree")
370427
void intoItself() throws IOException {

0 commit comments

Comments
 (0)