Search in sources :

Example 6 with FileChecksum

use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.

the class MemoryDatabaseCacheTest method testGetFileHistory.

@Test
public void testGetFileHistory() throws IOException {
    MemoryDatabase database = new MemoryDatabase();
    // Round 1: Add file history & version
    DatabaseVersion databaseVersion1 = TestDatabaseUtil.createDatabaseVersion();
    // - history 1, version 1
    FileVersion fileVersion1 = TestDatabaseUtil.createFileVersion("samechecksum1.jpg");
    fileVersion1.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    FileHistoryId idFile1 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory1 = new PartialFileHistory(idFile1);
    fileHistory1.addFileVersion(fileVersion1);
    databaseVersion1.addFileHistory(fileHistory1);
    database.addDatabaseVersion(databaseVersion1);
    FileHistoryId idFile1Get = FileHistoryId.parseFileId("1111111111111111");
    assertNotNull(database.getFileHistory(idFile1Get));
    assertEquals(fileHistory1, database.getFileHistory(idFile1Get));
    // Round 2: Add two other versions with same checksum to new database version
    DatabaseVersion databaseVersion2 = TestDatabaseUtil.createDatabaseVersion(databaseVersion1);
    // - history 1, version 2
    FileVersion fileVersion11 = TestDatabaseUtil.createFileVersion("samechecksum2-renamed.jpg", fileVersion1);
    // same checksum!
    fileVersion11.setChecksum(new FileChecksum(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
    fileVersion11.setStatus(FileStatus.RENAMED);
    // same ID
    FileHistoryId idFile11 = FileHistoryId.parseFileId("1111111111111111");
    PartialFileHistory fileHistory11 = new PartialFileHistory(idFile11);
    fileHistory11.addFileVersion(fileVersion11);
    databaseVersion2.addFileHistory(fileHistory11);
    database.addDatabaseVersion(databaseVersion2);
    // same ID
    FileHistoryId idFile111 = FileHistoryId.parseFileId("1111111111111111");
    assertNotNull(database.getFileHistory(idFile111));
    assertEquals(2, database.getFileHistory(idFile111).getFileVersions().size());
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) FileVersion(org.syncany.database.FileVersion) MemoryDatabase(org.syncany.database.MemoryDatabase) FileChecksum(org.syncany.database.FileContent.FileChecksum) PartialFileHistory(org.syncany.database.PartialFileHistory) DatabaseVersion(org.syncany.database.DatabaseVersion) Test(org.junit.Test)

Example 7 with FileChecksum

use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.

the class DatabaseVersionSqlDao method createDatabaseVersionFromRow.

protected DatabaseVersion createDatabaseVersionFromRow(ResultSet resultSet, boolean excludeChunkData, int fileHistoryMaxCount) throws SQLException {
    DatabaseVersionHeader databaseVersionHeader = createDatabaseVersionHeaderFromRow(resultSet);
    DatabaseVersion databaseVersion = new DatabaseVersion();
    databaseVersion.setHeader(databaseVersionHeader);
    // Add chunk/multichunk/filecontent data
    if (!excludeChunkData) {
        Map<ChunkChecksum, ChunkEntry> chunks = chunkDao.getChunks(databaseVersionHeader.getVectorClock());
        Map<MultiChunkId, MultiChunkEntry> multiChunks = multiChunkDao.getMultiChunks(databaseVersionHeader.getVectorClock());
        Map<FileChecksum, FileContent> fileContents = fileContentDao.getFileContents(databaseVersionHeader.getVectorClock());
        for (ChunkEntry chunk : chunks.values()) {
            databaseVersion.addChunk(chunk);
        }
        for (MultiChunkEntry multiChunk : multiChunks.values()) {
            databaseVersion.addMultiChunk(multiChunk);
        }
        for (FileContent fileContent : fileContents.values()) {
            databaseVersion.addFileContent(fileContent);
        }
    }
    // Add file histories
    Map<FileHistoryId, PartialFileHistory> fileHistories = fileHistoryDao.getFileHistoriesWithFileVersions(databaseVersionHeader.getVectorClock(), fileHistoryMaxCount);
    for (PartialFileHistory fileHistory : fileHistories.values()) {
        databaseVersion.addFileHistory(fileHistory);
    }
    return databaseVersion;
}
Also used : FileHistoryId(org.syncany.database.PartialFileHistory.FileHistoryId) MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileChecksum(org.syncany.database.FileContent.FileChecksum) PartialFileHistory(org.syncany.database.PartialFileHistory) FileContent(org.syncany.database.FileContent) ChunkEntry(org.syncany.database.ChunkEntry) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) MultiChunkEntry(org.syncany.database.MultiChunkEntry) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 8 with FileChecksum

use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.

the class FileVersionComparator method captureFileProperties.

public FileProperties captureFileProperties(File file, FileChecksum knownChecksum, boolean forceChecksum) {
    FileProperties fileProperties = new FileProperties();
    fileProperties.relativePath = FileUtil.getRelativeDatabasePath(rootFolder, file);
    Path filePath = null;
    try {
        filePath = Paths.get(file.getAbsolutePath());
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
    } catch (InvalidPathException e) {
        // This throws an exception if the filename is invalid,
        // e.g. colon in filename on windows "file:name"
        logger.log(Level.FINE, "InvalidPath", e);
        logger.log(Level.WARNING, "- Path '{0}' is invalid on this file system. It cannot exist. ", file.getAbsolutePath());
        fileProperties.exists = false;
        return fileProperties;
    }
    if (!fileProperties.exists) {
        return fileProperties;
    }
    try {
        // Read operating system dependent file attributes
        BasicFileAttributes fileAttributes = null;
        if (EnvironmentUtil.isWindows()) {
            DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);
            fileAttributes = dosAttrs;
        } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
            PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
            fileAttributes = posixAttrs;
        } else {
            fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        }
        fileProperties.lastModified = fileAttributes.lastModifiedTime().toMillis();
        fileProperties.size = fileAttributes.size();
        // Type
        if (fileAttributes.isSymbolicLink()) {
            fileProperties.type = FileType.SYMLINK;
            fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
        } else if (fileAttributes.isDirectory()) {
            fileProperties.type = FileType.FOLDER;
            fileProperties.linkTarget = null;
        } else {
            fileProperties.type = FileType.FILE;
            fileProperties.linkTarget = null;
        }
        // Checksum
        if (knownChecksum != null) {
            fileProperties.checksum = knownChecksum;
        } else {
            if (fileProperties.type == FileType.FILE && forceChecksum) {
                try {
                    if (fileProperties.size > 0) {
                        fileProperties.checksum = new FileChecksum(FileUtil.createChecksum(file, checksumAlgorithm));
                    } else {
                        fileProperties.checksum = null;
                    }
                } catch (NoSuchAlgorithmException | IOException e) {
                    logger.log(Level.FINE, "Failed create checksum", e);
                    logger.log(Level.SEVERE, "SEVERE: Unable to create checksum for file {0}", file);
                    fileProperties.checksum = null;
                }
            } else {
                fileProperties.checksum = null;
            }
        }
        // Must be last (!), used for vanish-test later
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
        fileProperties.locked = fileProperties.exists && FileUtil.isFileLocked(file);
        return fileProperties;
    } catch (IOException e) {
        logger.log(Level.FINE, "Failed to read file", e);
        logger.log(Level.SEVERE, "SEVERE: Cannot read file {0}. Assuming file is locked.", file);
        fileProperties.exists = true;
        fileProperties.locked = true;
        return fileProperties;
    }
}
Also used : Path(java.nio.file.Path) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) InvalidPathException(java.nio.file.InvalidPathException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) FileChecksum(org.syncany.database.FileContent.FileChecksum)

Example 9 with FileChecksum

use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.

the class MemoryDatabase method updateContentChecksumCache.

// TODO [medium] Very inefficient. Always updates whole cache
private void updateContentChecksumCache() {
    contentChecksumFileHistoriesCache.clear();
    for (PartialFileHistory fullFileHistory : fullDatabaseVersionCache.getFileHistories()) {
        FileChecksum lastVersionChecksum = fullFileHistory.getLastVersion().getChecksum();
        if (lastVersionChecksum != null) {
            List<PartialFileHistory> historiesWithVersionsWithSameChecksum = contentChecksumFileHistoriesCache.get(lastVersionChecksum);
            // Create if it does not exist
            if (historiesWithVersionsWithSameChecksum == null) {
                historiesWithVersionsWithSameChecksum = new ArrayList<PartialFileHistory>();
            }
            // Add to cache
            historiesWithVersionsWithSameChecksum.add(fullFileHistory);
            contentChecksumFileHistoriesCache.put(lastVersionChecksum, historiesWithVersionsWithSameChecksum);
        }
    }
}
Also used : FileChecksum(org.syncany.database.FileContent.FileChecksum)

Example 10 with FileChecksum

use of org.syncany.database.FileContent.FileChecksum in project syncany by syncany.

the class FileContentDaoTest method testGetFileContentsByVectorClock.

@Test
public void testGetFileContentsByVectorClock() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
    FileContentSqlDao fileContentDao = new FileContentSqlDao(databaseConnection);
    Map<FileChecksum, FileContent> fileContentsA1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A1"));
    Map<FileChecksum, FileContent> fileContentsA2 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A2"));
    Map<FileChecksum, FileContent> fileContentsA3 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A3"));
    Map<FileChecksum, FileContent> fileContentsA4 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A4"));
    Map<FileChecksum, FileContent> fileContentsA5 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A5"));
    Map<FileChecksum, FileContent> fileContentsA6 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A6"));
    Map<FileChecksum, FileContent> fileContentsA6B1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A6,B1"));
    Map<FileChecksum, FileContent> fileContentsA7B1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("A7,B1"));
    Map<FileChecksum, FileContent> fileContentsInvalid1 = fileContentDao.getFileContents(TestDatabaseUtil.createVectorClock("Invalid1"));
    // Test
    assertNotNull(fileContentsA1);
    assertEquals(0, fileContentsA1.size());
    assertNotNull(fileContentsA2);
    assertEquals(0, fileContentsA2.size());
    assertNotNull(fileContentsA3);
    assertEquals(0, fileContentsA3.size());
    assertNotNull(fileContentsA4);
    assertEquals(0, fileContentsA4.size());
    assertNotNull(fileContentsA5);
    assertEquals(1, fileContentsA5.size());
    assertNotNull(fileContentsA5.get(FileChecksum.parseFileChecksum("0fefb345b62b6c0b0e5212158a9aa7c1eeec2ca6")));
    assertEquals(12, fileContentsA5.get(FileChecksum.parseFileChecksum("0fefb345b62b6c0b0e5212158a9aa7c1eeec2ca6")).getSize());
    assertNotNull(fileContentsA6);
    assertEquals(1, fileContentsA6.size());
    assertNotNull(fileContentsA6.get(FileChecksum.parseFileChecksum("24a39e00d6156804e27f7c0987d00903da8e6682")));
    assertEquals(508, fileContentsA6.get(FileChecksum.parseFileChecksum("24a39e00d6156804e27f7c0987d00903da8e6682")).getSize());
    assertNotNull(fileContentsA6B1);
    assertEquals(1, fileContentsA6B1.size());
    assertNotNull(fileContentsA6B1.get(FileChecksum.parseFileChecksum("7666fd3b860c9d7588d9ca1807eebdf8cfaa8be3")));
    assertEquals(2029, fileContentsA6B1.get(FileChecksum.parseFileChecksum("7666fd3b860c9d7588d9ca1807eebdf8cfaa8be3")).getSize());
    assertNotNull(fileContentsA7B1);
    assertEquals(1, fileContentsA7B1.size());
    assertNotNull(fileContentsA7B1.get(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0")));
    assertEquals(512, fileContentsA7B1.get(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0")).getSize());
    assertNotNull(fileContentsInvalid1);
    assertEquals(0, fileContentsInvalid1.size());
    // Tear down
    databaseConnection.close();
    TestConfigUtil.deleteTestLocalConfigAndData(testConfig);
}
Also used : FileContent(org.syncany.database.FileContent) Config(org.syncany.config.Config) FileContentSqlDao(org.syncany.database.dao.FileContentSqlDao) Connection(java.sql.Connection) FileChecksum(org.syncany.database.FileContent.FileChecksum) Test(org.junit.Test)

Aggregations

FileChecksum (org.syncany.database.FileContent.FileChecksum)13 Test (org.junit.Test)6 FileVersion (org.syncany.database.FileVersion)6 Date (java.util.Date)4 DatabaseVersion (org.syncany.database.DatabaseVersion)4 FileContent (org.syncany.database.FileContent)4 Config (org.syncany.config.Config)3 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)3 MemoryDatabase (org.syncany.database.MemoryDatabase)3 PartialFileHistory (org.syncany.database.PartialFileHistory)3 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)3 Path (java.nio.file.Path)2 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)2 ChunkEntry (org.syncany.database.ChunkEntry)2 FileVersionComparator (org.syncany.database.FileVersionComparator)2 MultiChunkEntry (org.syncany.database.MultiChunkEntry)2 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)2 File (java.io.File)1 IOException (java.io.IOException)1 InvalidPathException (java.nio.file.InvalidPathException)1