-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version7Migrator will now perform migration _after_ visiting a direct…
…ory to avoid changes to elements while still iterating
- Loading branch information
1 parent
ca063e9
commit b266343
Showing
4 changed files
with
116 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/cryptomator/cryptofs/migration/v7/MigratingVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.cryptomator.cryptofs.migration.v7; | ||
|
||
import org.cryptomator.cryptofs.migration.api.MigrationProgressListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Path; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
class MigratingVisitor extends SimpleFileVisitor<Path> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MigratingVisitor.class); | ||
|
||
private final Path vaultRoot; | ||
private final MigrationProgressListener progressListener; | ||
private final long estimatedTotalFiles; | ||
|
||
public MigratingVisitor(Path vaultRoot, MigrationProgressListener progressListener, long estimatedTotalFiles) { | ||
this.vaultRoot = vaultRoot; | ||
this.progressListener = progressListener; | ||
this.estimatedTotalFiles = estimatedTotalFiles; | ||
} | ||
|
||
private Collection<FilePathMigration> migrationsInCurrentDir = new ArrayList<>(); | ||
private long migratedFiles = 0; | ||
|
||
// Step 1: Collect files to be migrated | ||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
final Optional<FilePathMigration> migration; | ||
try { | ||
migration = FilePathMigration.parse(vaultRoot, file); | ||
} catch (UninflatableFileException e) { | ||
LOG.warn("SKIP {} because inflation failed.", file); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
migration.ifPresent(migrationsInCurrentDir::add); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
// Step 2: Only after visiting this dir, we will perform any changes to avoid "ConcurrentModificationExceptions" | ||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | ||
for (FilePathMigration migration : migrationsInCurrentDir) { | ||
migratedFiles++; | ||
progressListener.update(MigrationProgressListener.ProgressState.MIGRATING, (double) migratedFiles / estimatedTotalFiles); | ||
try { | ||
Path migratedFile = migration.migrate(); | ||
LOG.info("MOVED {} to {}", migration.getOldPath(), migratedFile); | ||
} catch (FileAlreadyExistsException e) { | ||
LOG.error("Failed to migrate " + migration.getOldPath() + " due to FileAlreadyExistsException. Already migrated on a different machine?.", e); | ||
return FileVisitResult.TERMINATE; | ||
} | ||
} | ||
migrationsInCurrentDir.clear(); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters