Search in sources :

Example 1 with FileVersion

use of org.syncany.database.FileVersion in project syncany by syncany.

the class RenameFileWithDiffModifiedDateScenarioTest method testChangedModifiedDate.

@Test
public void testChangedModifiedDate() throws Exception {
    // Setup 
    TransferSettings testConnection = TestConfigUtil.createTestLocalConnection();
    TestClient clientA = new TestClient("A", testConnection);
    TestClient clientB = new TestClient("B", testConnection);
    // Run 
    // A, create two files with identical content and change mod. date of one of them
    clientA.createNewFile("A-file1.jpg", 50 * 1024);
    clientA.copyFile("A-file1.jpg", "A-file1-with-different-modified-date.jpg");
    clientA.getLocalFile("A-file1.jpg").setLastModified(0);
    clientA.up();
    // B, down, then move BOTH files
    clientB.down();
    assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead());
    assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile());
    clientB.moveFile("A-file1.jpg", "A-file1-moved.jpg");
    clientB.moveFile("A-file1-with-different-modified-date.jpg", "A-file1-with-different-modified-date-moved.jpg");
    clientB.up();
    TestSqlDatabase clientDatabaseB = clientB.loadLocalDatabase();
    PartialFileHistory file1Orig = clientDatabaseB.getFileHistoryWithFileVersions("A-file1-moved.jpg");
    PartialFileHistory file1WithDiffLastModDate = clientDatabaseB.getFileHistoryWithFileVersions("A-file1-with-different-modified-date-moved.jpg");
    assertNotNull(file1Orig);
    assertNotNull(file1WithDiffLastModDate);
    FileVersion fileVersion1OrigV1 = file1Orig.getFileVersion(1);
    FileVersion fileVersion1OrigV2 = file1Orig.getFileVersion(2);
    FileVersion fileVersion1WithDiffLastModDateV1 = file1WithDiffLastModDate.getFileVersion(1);
    FileVersion fileVersion1WithDiffLastModDateV2 = file1WithDiffLastModDate.getFileVersion(2);
    assertNotNull(fileVersion1OrigV1);
    assertNotNull(fileVersion1OrigV2);
    assertNotNull(fileVersion1WithDiffLastModDateV1);
    assertNotNull(fileVersion1WithDiffLastModDateV2);
    assertEquals("A-file1.jpg", fileVersion1OrigV1.getName());
    assertEquals("A-file1-moved.jpg", fileVersion1OrigV2.getName());
    assertEquals("A-file1-with-different-modified-date.jpg", fileVersion1WithDiffLastModDateV1.getName());
    assertEquals("A-file1-with-different-modified-date-moved.jpg", fileVersion1WithDiffLastModDateV2.getName());
    // Tear down
    clientA.deleteTestData();
    clientB.deleteTestData();
}
Also used : TestClient(org.syncany.tests.util.TestClient) FileVersion(org.syncany.database.FileVersion) TestSqlDatabase(org.syncany.tests.util.TestSqlDatabase) TransferSettings(org.syncany.plugins.transfer.TransferSettings) PartialFileHistory(org.syncany.database.PartialFileHistory) Test(org.junit.Test)

Example 2 with FileVersion

use of org.syncany.database.FileVersion in project syncany by syncany.

the class DeleteFileSystemAction method execute.

/**
	 * Deletes a file locally and/or creates a conflicting file if the
	 * file does not match the expectations. There are two major cases:
	 * 
	 * <p>Normal case: The file version of the to-be-deleted file is known. If the file is 
	 * as expected, it is deleted; if not and the file exists, a conflict file is created.
	 * 
	 * <p>Special case: The file version of the to-be-deleted file in unknown. 
	 * In case to-be-deleted-file exists locally, we need to compare it to
	 * the local file (especially its checksum!). The {@link FileVersionComparator}
	 * does, however, perform a cancelling test in which {@link FileVersion}s marked as
	 * 'DELETED' are not compared in detail (no checksum/attribute/etc. comparisons). To
	 * circumvent this behavior, we pretend the file has just changed and do the comparison.
	 * If the to-be-deleted file and file version are equal, the local file is deleted. 
	 * Otherwise, a conflict file is created. 
	 */
@Override
public FileSystemActionResult execute() throws Exception {
    // Special case: locally unknown file to be deleted		
    if (fileVersion1 == null) {
        if (fileExists(fileVersion2)) {
            FileVersion pretendChangedFileVersion = fileVersion2.clone();
            pretendChangedFileVersion.setStatus(FileStatus.CHANGED);
            if (fileAsExpected(pretendChangedFileVersion)) {
                deleteFile(fileVersion2);
            } else {
                moveToConflictFile(fileVersion2);
            }
        }
    } else // Normal case: locally known file to be deleted
    {
        if (!fileAsExpected(fileVersion1)) {
            if (fileExists(fileVersion1)) {
                moveToConflictFile(fileVersion2);
            }
        } else {
            deleteFile(fileVersion1);
        }
    }
    return new FileSystemActionResult();
}
Also used : FileVersion(org.syncany.database.FileVersion)

Example 3 with FileVersion

use of org.syncany.database.FileVersion in project syncany by syncany.

the class FileHistorySqlDao method createFileHistoriesFromResult.

protected Map<FileHistoryId, PartialFileHistory> createFileHistoriesFromResult(ResultSet resultSet) throws SQLException {
    Map<FileHistoryId, PartialFileHistory> fileHistories = new HashMap<FileHistoryId, PartialFileHistory>();
    PartialFileHistory fileHistory = null;
    while (resultSet.next()) {
        FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
        FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
        // Old history (= same filehistory identifier)
        if (fileHistory != null && fileHistory.getFileHistoryId().equals(fileHistoryId)) {
            // Same history!
            fileHistory.addFileVersion(lastFileVersion);
        } else // New history!
        {
            // Add the old history
            if (fileHistory != null) {
                fileHistories.put(fileHistory.getFileHistoryId(), fileHistory);
            }
            // Create a new one
            fileHistory = new PartialFileHistory(fileHistoryId);
            fileHistory.addFileVersion(lastFileVersion);
        }
    }
    // Add the last history
    if (fileHistory != null) {
        fileHistories.put(fileHistory.getFileHistoryId(), fileHistory);
    }
    return fileHistories;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) HashMap(java.util.HashMap) FileVersion(org.syncany.database.FileVersion) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 4 with FileVersion

use of org.syncany.database.FileVersion in project syncany by syncany.

the class FileHistorySqlDao method getFileHistoriesWithLastVersion.

public List<PartialFileHistory> getFileHistoriesWithLastVersion() {
    List<PartialFileHistory> fileHistories = new ArrayList<PartialFileHistory>();
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoriesWithLastVersion.sql")) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
                FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
                PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryId);
                fileHistory.addFileVersion(lastFileVersion);
                fileHistories.add(fileHistory);
            }
        }
        return fileHistories;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Example 5 with FileVersion

use of org.syncany.database.FileVersion in project syncany by syncany.

the class FileHistorySqlDao method getLastVersionByFileHistoryId.

private PartialFileHistory getLastVersionByFileHistoryId(String fileHistoryId) {
    try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getLastVersionByFileHistoryId.sql")) {
        preparedStatement.setString(1, fileHistoryId);
        preparedStatement.setString(2, fileHistoryId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet);
                FileHistoryId fileHistoryIdData = FileHistoryId.parseFileId(resultSet.getString("filehistory_id"));
                PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryIdData);
                fileHistory.addFileVersion(lastFileVersion);
                return fileHistory;
            } else {
                return null;
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) SQLException(java.sql.SQLException) FileVersion(org.syncany.database.FileVersion) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PartialFileHistory(org.syncany.database.PartialFileHistory)

Aggregations

FileVersion (org.syncany.database.FileVersion)50 PartialFileHistory (org.syncany.database.PartialFileHistory)23 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)21 Test (org.junit.Test)17 DatabaseVersion (org.syncany.database.DatabaseVersion)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 MemoryDatabase (org.syncany.database.MemoryDatabase)8 PreparedStatement (java.sql.PreparedStatement)7 File (java.io.File)6 ResultSet (java.sql.ResultSet)6 FileChecksum (org.syncany.database.FileContent.FileChecksum)6 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 Config (org.syncany.config.Config)5 TreeMap (java.util.TreeMap)4 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)4 List (java.util.List)3 FileContent (org.syncany.database.FileContent)3 MultiChunkEntry (org.syncany.database.MultiChunkEntry)3