Search in sources :

Example 1 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class FileSystemActionReconciliator method determineActionWithLocalVersionAndLocalFileAsExpected.

private void determineActionWithLocalVersionAndLocalFileAsExpected(FileVersion winningLastVersion, File winningLastFile, FileVersion localLastVersion, File localLastFile, MemoryDatabase winnersDatabase, List<FileSystemAction> fileSystemActions) {
    FileVersionComparison winningVersionToLocalVersionComparison = fileVersionComparator.compare(winningLastVersion, localLastVersion);
    boolean contentChanged = winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_CHECKSUM) || winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_SIZE);
    if (winningVersionToLocalVersionComparison.areEqual()) {
        // Local file = local version = winning version!
        logger.log(Level.INFO, "     -> (8) Equals: Nothing to do, local file equals local version equals winning version: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
    } else if (winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.DELETED)) {
        FileSystemAction action = new ChangeFileSystemAction(config, winnersDatabase, assembler, localLastVersion, winningLastVersion);
        fileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (9) Content changed: Local file does not exist, but it should: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getChangedFiles().add(winningLastVersion.getPath());
    } else if (winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.NEW)) {
        FileSystemAction action = new DeleteFileSystemAction(config, localLastVersion, winningLastVersion, winnersDatabase);
        fileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (10) Local file exists, but should not: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getDeletedFiles().add(winningLastVersion.getPath());
    } else if (winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_LINK_TARGET)) {
        FileSystemAction action = new NewSymlinkFileSystemAction(config, winningLastVersion, winnersDatabase);
        fileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (11) Changed link target: local file has a different link target: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getNewFiles().add(winningLastVersion.getPath());
    } else if (!contentChanged && (winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_LAST_MOD_DATE) || winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_ATTRIBUTES) || winningVersionToLocalVersionComparison.getFileChanges().contains(FileChange.CHANGED_PATH))) {
        FileSystemAction action = new RenameFileSystemAction(config, localLastVersion, winningLastVersion, winnersDatabase);
        fileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (12) Rename / Changed file attributes: Local file has different file attributes: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getChangedFiles().add(winningLastVersion.getPath());
    } else {
        // Content changed
        FileSystemAction action = new ChangeFileSystemAction(config, winnersDatabase, assembler, localLastVersion, winningLastVersion);
        fileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (13) Content changed: Local file differs from winning version: local file = " + localLastFile + ", local version = " + localLastVersion + ", winning version = " + winningLastVersion);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getChangedFiles().add(winningLastVersion.getPath());
    }
}
Also used : RenameFileSystemAction(org.syncany.operations.down.actions.RenameFileSystemAction) NewFileSystemAction(org.syncany.operations.down.actions.NewFileSystemAction) ChangeFileSystemAction(org.syncany.operations.down.actions.ChangeFileSystemAction) SetAttributesFileSystemAction(org.syncany.operations.down.actions.SetAttributesFileSystemAction) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) ChangeFileSystemAction(org.syncany.operations.down.actions.ChangeFileSystemAction) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) RenameFileSystemAction(org.syncany.operations.down.actions.RenameFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction)

Example 2 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class FileSystemAction method fileChanges.

protected FileVersionComparison fileChanges(FileVersion expectedLocalFileVersion) {
    File actualLocalFile = getAbsolutePathFile(expectedLocalFileVersion.getPath());
    FileVersionComparison fileVersionComparison = fileVersionHelper.compare(expectedLocalFileVersion, actualLocalFile, true);
    return fileVersionComparison;
}
Also used : FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) File(java.io.File)

Example 3 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class FileVersionComparatorTest method testCompareFileVersionToFile.

@Test
public void testCompareFileVersionToFile() throws Exception {
    // Setup
    Config config = TestConfigUtil.createTestLocalConfig();
    FileVersionComparator versionComparator = new FileVersionComparator(config.getLocalDir(), config.getChunker().getChecksumAlgorithm());
    // Create File
    File somefile = new File(config.getLocalDir() + "/file1");
    Path somefilePath = Paths.get(somefile.getAbsolutePath());
    TestFileUtil.createRandomFile(somefile, 130 * 1024);
    somefile.setLastModified(1182196000);
    if (EnvironmentUtil.isWindows()) {
        Files.setAttribute(somefilePath, "dos:readonly", true);
        Files.setAttribute(somefilePath, "dos:hidden", false);
        Files.setAttribute(somefilePath, "dos:archive", true);
        Files.setAttribute(somefilePath, "dos:system", false);
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        Files.setPosixFilePermissions(somefilePath, PosixFilePermissions.fromString("r--rwxrw-"));
    }
    // Create FileVersion
    FileVersion fileVersion = new FileVersion();
    fileVersion.setVersion(100L);
    // << definitely differs
    fileVersion.setChecksum(new FileChecksum(new byte[] { 0x11, 0x22, 0x33 }));
    fileVersion.setLastModified(new Date(1182196000));
    fileVersion.setPath("file1");
    fileVersion.setSize(130 * 1024L);
    fileVersion.setLinkTarget(null);
    fileVersion.setStatus(FileStatus.NEW);
    fileVersion.setType(FileType.FILE);
    if (EnvironmentUtil.isWindows()) {
        fileVersion.setDosAttributes("r-a-");
    } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
        fileVersion.setPosixPermissions("r--rwxrw-");
    }
    // Run
    FileVersionComparison fileComparison = versionComparator.compare(fileVersion, somefile, null, true);
    // Test
    assertFalse(fileComparison.areEqual());
    assertTrue(CollectionUtil.containsExactly(fileComparison.getFileChanges(), FileChange.CHANGED_CHECKSUM));
    // Tear down
    TestConfigUtil.deleteTestLocalConfigAndData(config);
}
Also used : Path(java.nio.file.Path) Config(org.syncany.config.Config) FileVersion(org.syncany.database.FileVersion) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) FileVersionComparator(org.syncany.database.FileVersionComparator) File(java.io.File) FileChecksum(org.syncany.database.FileContent.FileChecksum) Date(java.util.Date) Test(org.junit.Test)

Example 4 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class FileSystemActionReconciliator method determineFileSystemActions.

public List<FileSystemAction> determineFileSystemActions(MemoryDatabase winnersDatabase, boolean cleanupOccurred, List<PartialFileHistory> localFileHistoriesWithLastVersion) throws Exception {
    this.assembler = new Assembler(config, localDatabase, winnersDatabase);
    List<FileSystemAction> fileSystemActions = new ArrayList<FileSystemAction>();
    // Load file history cache
    logger.log(Level.INFO, "- Loading current file tree...");
    Map<FileHistoryId, FileVersion> localFileHistoryIdCache = fillFileHistoryIdCache(localFileHistoriesWithLastVersion);
    logger.log(Level.INFO, "- Determine filesystem actions ...");
    for (PartialFileHistory winningFileHistory : winnersDatabase.getFileHistories()) {
        // Get remote file version and content
        FileVersion winningLastVersion = winningFileHistory.getLastVersion();
        File winningLastFile = new File(config.getLocalDir(), winningLastVersion.getPath());
        // Get local file version and content
        FileVersion localLastVersion = localFileHistoryIdCache.get(winningFileHistory.getFileHistoryId());
        File localLastFile = (localLastVersion != null) ? new File(config.getLocalDir(), localLastVersion.getPath()) : null;
        logger.log(Level.INFO, "  + Comparing local version: " + localLastVersion);
        logger.log(Level.INFO, "    with winning version   : " + winningLastVersion);
        // No local file version in local database
        if (localLastVersion == null) {
            determineActionNoLocalLastVersion(winningLastVersion, winningLastFile, winnersDatabase, fileSystemActions);
        } else // Local version found in local database
        {
            FileVersionComparison localFileToVersionComparison = fileVersionComparator.compare(localLastVersion, localLastFile, true);
            // Local file on disk as expected
            if (localFileToVersionComparison.areEqual()) {
                determineActionWithLocalVersionAndLocalFileAsExpected(winningLastVersion, winningLastFile, localLastVersion, localLastFile, winnersDatabase, fileSystemActions);
            } else // Local file NOT what was expected
            {
                determineActionWithLocalVersionAndLocalFileDiffers(winningLastVersion, winningLastFile, localLastVersion, localLastFile, winnersDatabase, fileSystemActions, localFileToVersionComparison);
            }
        }
    }
    if (cleanupOccurred) {
        logger.log(Level.INFO, "- Determine filesystem actions (for deleted histories in winner's branch)...");
        Map<FileHistoryId, FileVersion> winnerFileHistoryIdCache = fillFileHistoryIdCache(winnersDatabase.getFileHistories());
        for (PartialFileHistory localFileHistoryWithLastVersion : localFileHistoriesWithLastVersion) {
            boolean localFileHistoryInWinnersDatabase = winnerFileHistoryIdCache.get(localFileHistoryWithLastVersion.getFileHistoryId()) != null;
            if (!localFileHistoryInWinnersDatabase) {
                FileVersion localLastVersion = localFileHistoryWithLastVersion.getLastVersion();
                File localLastFile = (localLastVersion != null) ? new File(config.getLocalDir(), localLastVersion.getPath()) : null;
                determineActionFileHistoryNotInWinnerBranch(localLastVersion, localLastFile, fileSystemActions);
            }
        }
    }
    return fileSystemActions;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) RenameFileSystemAction(org.syncany.operations.down.actions.RenameFileSystemAction) NewFileSystemAction(org.syncany.operations.down.actions.NewFileSystemAction) ChangeFileSystemAction(org.syncany.operations.down.actions.ChangeFileSystemAction) SetAttributesFileSystemAction(org.syncany.operations.down.actions.SetAttributesFileSystemAction) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) Assembler(org.syncany.operations.Assembler) File(java.io.File) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 5 with FileVersionComparison

use of org.syncany.database.FileVersionComparator.FileVersionComparison in project syncany by syncany.

the class FileSystemActionReconciliator method determineActionNoLocalLastVersion.

private void determineActionNoLocalLastVersion(FileVersion winningLastVersion, File winningLastFile, MemoryDatabase winnersDatabase, List<FileSystemAction> outFileSystemActions) throws Exception {
    FileVersionComparison winningFileToVersionComparison = fileVersionComparator.compare(winningLastVersion, winningLastFile, true);
    boolean contentChanged = winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_CHECKSUM) || winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_SIZE);
    if (winningFileToVersionComparison.areEqual()) {
        logger.log(Level.INFO, "     -> (1) Equals: Nothing to do, winning version equals winning file: " + winningLastVersion + " AND " + winningLastFile);
    } else if (winningFileToVersionComparison.getFileChanges().contains(FileChange.DELETED)) {
        FileSystemAction action = new NewFileSystemAction(config, winnersDatabase, assembler, winningLastVersion);
        outFileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (2) Deleted: Local file does NOT exist, but it should, winning version not known: " + winningLastVersion + " AND " + winningLastFile);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getNewFiles().add(winningLastVersion.getPath());
    } else if (winningFileToVersionComparison.getFileChanges().contains(FileChange.NEW)) {
        FileSystemAction action = new DeleteFileSystemAction(config, null, winningLastVersion, winnersDatabase);
        outFileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (3) New: winning version was deleted, but local exists, winning version = " + winningLastVersion + " at " + winningLastFile);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getDeletedFiles().add(winningLastVersion.getPath());
    } else if (winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_LINK_TARGET)) {
        FileSystemAction action = new NewSymlinkFileSystemAction(config, winningLastVersion, winnersDatabase);
        outFileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (4) Changed link target: winning file has a different link target: " + winningLastVersion + " AND " + winningLastFile);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getNewFiles().add(winningLastVersion.getPath());
    } else if (!contentChanged && (winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_LAST_MOD_DATE) || winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_ATTRIBUTES))) {
        FileSystemAction action = new SetAttributesFileSystemAction(config, winningLastVersion, winnersDatabase);
        outFileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (5) Changed file attributes: winning file has different file attributes: " + winningLastVersion + " AND " + winningLastFile);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getNewFiles().add(winningLastVersion.getPath());
    } else if (winningFileToVersionComparison.getFileChanges().contains(FileChange.CHANGED_PATH)) {
        logger.log(Level.INFO, "     -> (6) Changed path: winning file has a different path: " + winningLastVersion + " AND " + winningLastFile);
        throw new Exception("What happend here?");
    } else {
        // Content changed
        FileSystemAction action = new NewFileSystemAction(config, winnersDatabase, assembler, winningLastVersion);
        outFileSystemActions.add(action);
        logger.log(Level.INFO, "     -> (7) Content changed: Winning file differs from winning version: " + winningLastVersion + " AND " + winningLastFile);
        logger.log(Level.INFO, "     -> " + action);
        changeSet.getNewFiles().add(winningLastVersion.getPath());
    }
}
Also used : NewFileSystemAction(org.syncany.operations.down.actions.NewFileSystemAction) RenameFileSystemAction(org.syncany.operations.down.actions.RenameFileSystemAction) NewFileSystemAction(org.syncany.operations.down.actions.NewFileSystemAction) ChangeFileSystemAction(org.syncany.operations.down.actions.ChangeFileSystemAction) SetAttributesFileSystemAction(org.syncany.operations.down.actions.SetAttributesFileSystemAction) FileSystemAction(org.syncany.operations.down.actions.FileSystemAction) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction) SetAttributesFileSystemAction(org.syncany.operations.down.actions.SetAttributesFileSystemAction) FileVersionComparison(org.syncany.database.FileVersionComparator.FileVersionComparison) NewSymlinkFileSystemAction(org.syncany.operations.down.actions.NewSymlinkFileSystemAction) DeleteFileSystemAction(org.syncany.operations.down.actions.DeleteFileSystemAction)

Aggregations

FileVersionComparison (org.syncany.database.FileVersionComparator.FileVersionComparison)6 File (java.io.File)3 ChangeFileSystemAction (org.syncany.operations.down.actions.ChangeFileSystemAction)3 DeleteFileSystemAction (org.syncany.operations.down.actions.DeleteFileSystemAction)3 FileSystemAction (org.syncany.operations.down.actions.FileSystemAction)3 NewFileSystemAction (org.syncany.operations.down.actions.NewFileSystemAction)3 NewSymlinkFileSystemAction (org.syncany.operations.down.actions.NewSymlinkFileSystemAction)3 RenameFileSystemAction (org.syncany.operations.down.actions.RenameFileSystemAction)3 SetAttributesFileSystemAction (org.syncany.operations.down.actions.SetAttributesFileSystemAction)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 FileVersion (org.syncany.database.FileVersion)2 FileVersionComparator (org.syncany.database.FileVersionComparator)2 Date (java.util.Date)1 Test (org.junit.Test)1 Config (org.syncany.config.Config)1 FileChecksum (org.syncany.database.FileContent.FileChecksum)1 FileChange (org.syncany.database.FileVersionComparator.FileChange)1 FileProperties (org.syncany.database.FileVersionComparator.FileProperties)1 PartialFileHistory (org.syncany.database.PartialFileHistory)1