Search in sources :

Example 1 with FileContent

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

the class Assembler method assembleToCache.

/**
 * Assembles the given file version to the local cache and returns a reference
 * to the cached file after successfully assembling the file.
 */
public File assembleToCache(FileVersion fileVersion) throws Exception {
    File reconstructedFileInCache = config.getCache().createTempFile("reconstructedFileVersion");
    logger.log(Level.INFO, "     - Creating file " + fileVersion.getPath() + " to " + reconstructedFileInCache + " ...");
    FileContent fileContent = localDatabase.getFileContent(fileVersion.getChecksum(), true);
    if (fileContent == null && memoryDatabase != null) {
        fileContent = memoryDatabase.getContent(fileVersion.getChecksum());
    }
    // Check consistency!
    if (fileContent == null && fileVersion.getChecksum() != null) {
        throw new Exception("Cannot determine file content for checksum " + fileVersion.getChecksum());
    }
    // Create empty file
    if (fileContent == null) {
        FileUtils.touch(reconstructedFileInCache);
        return reconstructedFileInCache;
    }
    // Create non-empty file
    Chunker chunker = config.getChunker();
    MultiChunker multiChunker = config.getMultiChunker();
    FileOutputStream reconstructedFileOutputStream = new FileOutputStream(reconstructedFileInCache);
    MessageDigest reconstructedFileChecksum = MessageDigest.getInstance(chunker.getChecksumAlgorithm());
    if (fileContent != null) {
        // File can be empty!
        Collection<ChunkChecksum> fileChunks = fileContent.getChunks();
        for (ChunkChecksum chunkChecksum : fileChunks) {
            MultiChunkId multiChunkIdForChunk = localDatabase.getMultiChunkId(chunkChecksum);
            if (multiChunkIdForChunk == null && memoryDatabase != null) {
                multiChunkIdForChunk = memoryDatabase.getMultiChunkIdForChunk(chunkChecksum);
            }
            File decryptedMultiChunkFile = config.getCache().getDecryptedMultiChunkFile(multiChunkIdForChunk);
            MultiChunk multiChunk = multiChunker.createMultiChunk(decryptedMultiChunkFile);
            InputStream chunkInputStream = multiChunk.getChunkInputStream(chunkChecksum.getBytes());
            byte[] buffer = new byte[4096];
            int read = 0;
            while (-1 != (read = chunkInputStream.read(buffer))) {
                reconstructedFileChecksum.update(buffer, 0, read);
                reconstructedFileOutputStream.write(buffer, 0, read);
            }
            chunkInputStream.close();
            multiChunk.close();
        }
    }
    reconstructedFileOutputStream.close();
    // Validate checksum
    byte[] reconstructedFileExpectedChecksum = fileContent.getChecksum().getBytes();
    byte[] reconstructedFileActualChecksum = reconstructedFileChecksum.digest();
    if (!Arrays.equals(reconstructedFileActualChecksum, reconstructedFileExpectedChecksum)) {
        throw new Exception("Checksums do not match: expected " + StringUtil.toHex(reconstructedFileExpectedChecksum) + " != actual " + StringUtil.toHex(reconstructedFileActualChecksum));
    }
    return reconstructedFileInCache;
}
Also used : MultiChunkId(org.syncany.database.MultiChunkEntry.MultiChunkId) InputStream(java.io.InputStream) Chunker(org.syncany.chunk.Chunker) MultiChunker(org.syncany.chunk.MultiChunker) MultiChunk(org.syncany.chunk.MultiChunk) MultiChunker(org.syncany.chunk.MultiChunker) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileContent(org.syncany.database.FileContent) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 2 with FileContent

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

the class DatabaseXmlWriter method writeFileContents.

private void writeFileContents(IndentXmlStreamWriter xmlOut, Collection<FileContent> fileContents) throws XMLStreamException {
    if (fileContents.size() > 0) {
        xmlOut.writeStartElement("fileContents");
        for (FileContent fileContent : fileContents) {
            xmlOut.writeStartElement("fileContent");
            xmlOut.writeAttribute("checksum", fileContent.getChecksum().toString());
            xmlOut.writeAttribute("size", fileContent.getSize());
            xmlOut.writeStartElement("chunkRefs");
            Collection<ChunkChecksum> fileContentChunkChunks = fileContent.getChunks();
            for (ChunkChecksum chunkChecksum : fileContentChunkChunks) {
                xmlOut.writeEmptyElement("chunkRef");
                xmlOut.writeAttribute("ref", chunkChecksum.toString());
            }
            // </chunkRefs>
            xmlOut.writeEndElement();
            // </fileContent>
            xmlOut.writeEndElement();
        }
        // </fileContents>
        xmlOut.writeEndElement();
    }
}
Also used : FileContent(org.syncany.database.FileContent) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum)

Example 3 with FileContent

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

the class FileContentDaoTest method testGetFileContentByChecksum.

@Test
public void testGetFileContentByChecksum() throws Exception {
    // Setup
    Config testConfig = TestConfigUtil.createTestLocalConfig();
    Connection databaseConnection = testConfig.createDatabaseConnection();
    // Run
    TestSqlUtil.runSqlFromResource(databaseConnection, "test.insert.set3.sql");
    FileContentSqlDao fileContentDao = new FileContentSqlDao(databaseConnection);
    FileContent fileContent1WithChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0"), true);
    FileContent fileContent1WithoutChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("eba69a8e359ce3258520138a50ed9860127ab6e0"), false);
    FileContent fileContent2WithChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("254416e71ae50431fc6ced6751075b3366db7cc8"), true);
    FileContent fileContent2WithoutChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("254416e71ae50431fc6ced6751075b3366db7cc8"), false);
    FileContent fileContentNonExistingWithChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"), true);
    FileContent fileContentNonExistingWithoutChunks = fileContentDao.getFileContent(FileChecksum.parseFileChecksum("beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"), false);
    FileContent fileContentNullChecksum = fileContentDao.getFileContent(null, false);
    // Test
    assertNotNull(fileContent1WithChunks);
    assertEquals(1, fileContent1WithChunks.getChunks().size());
    assertEquals("eba69a8e359ce3258520138a50ed9860127ab6e0", fileContent1WithChunks.getChecksum().toString());
    assertEquals("eba69a8e359ce3258520138a50ed9860127ab6e0", fileContent1WithChunks.getChunks().get(0).toString());
    assertNotNull(fileContent1WithoutChunks);
    assertEquals("eba69a8e359ce3258520138a50ed9860127ab6e0", fileContent1WithoutChunks.getChecksum().toString());
    assertEquals(0, fileContent1WithoutChunks.getChunks().size());
    assertNotNull(fileContent2WithChunks);
    assertEquals(3, fileContent2WithChunks.getChunks().size());
    assertEquals("254416e71ae50431fc6ced6751075b3366db7cc8", fileContent2WithChunks.getChecksum().toString());
    assertEquals("b310c0eedcd03238888c6abb3e3398633139ecc5", fileContent2WithChunks.getChunks().get(0).toString());
    assertEquals("8ed8d50a6e9da3197bd665bc3a1f229ebcde9b42", fileContent2WithChunks.getChunks().get(1).toString());
    assertEquals("ab85720d3f31bd08ca1cd25dcd8a490e5f00783b", fileContent2WithChunks.getChunks().get(2).toString());
    assertNotNull(fileContent2WithoutChunks);
    assertEquals("254416e71ae50431fc6ced6751075b3366db7cc8", fileContent2WithoutChunks.getChecksum().toString());
    assertEquals(0, fileContent2WithoutChunks.getChunks().size());
    assertNull(fileContentNonExistingWithChunks);
    assertNull(fileContentNonExistingWithoutChunks);
    assertNull(fileContentNullChecksum);
    // 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) Test(org.junit.Test)

Example 4 with FileContent

use of org.syncany.database.FileContent 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 5 with FileContent

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

the class FileContentSqlDao method createFileContents.

private Map<FileChecksum, FileContent> createFileContents(ResultSet resultSet) throws SQLException {
    Map<FileChecksum, FileContent> fileContents = new HashMap<FileChecksum, FileContent>();
    FileChecksum currentFileChecksum = null;
    while (resultSet.next()) {
        FileChecksum fileChecksum = FileChecksum.parseFileChecksum(resultSet.getString("checksum"));
        FileContent fileContent = null;
        if (currentFileChecksum != null && currentFileChecksum.equals(fileChecksum)) {
            fileContent = fileContents.get(fileChecksum);
        } else {
            fileContent = new FileContent();
            fileContent.setChecksum(fileChecksum);
            fileContent.setSize(resultSet.getLong("size"));
        }
        ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(resultSet.getString("chunk_checksum"));
        fileContent.addChunk(chunkChecksum);
        fileContents.put(fileChecksum, fileContent);
        currentFileChecksum = fileChecksum;
    }
    return fileContents;
}
Also used : FileContent(org.syncany.database.FileContent) HashMap(java.util.HashMap) ChunkChecksum(org.syncany.database.ChunkEntry.ChunkChecksum) FileChecksum(org.syncany.database.FileContent.FileChecksum)

Aggregations

FileContent (org.syncany.database.FileContent)15 ChunkChecksum (org.syncany.database.ChunkEntry.ChunkChecksum)9 MultiChunkId (org.syncany.database.MultiChunkEntry.MultiChunkId)6 ChunkEntry (org.syncany.database.ChunkEntry)5 DatabaseVersion (org.syncany.database.DatabaseVersion)5 MultiChunkEntry (org.syncany.database.MultiChunkEntry)5 Test (org.junit.Test)4 FileChecksum (org.syncany.database.FileContent.FileChecksum)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 Config (org.syncany.config.Config)3 FileVersion (org.syncany.database.FileVersion)3 PartialFileHistory (org.syncany.database.PartialFileHistory)3 FileHistoryId (org.syncany.database.PartialFileHistory.FileHistoryId)3 FileContentSqlDao (org.syncany.database.dao.FileContentSqlDao)3 File (java.io.File)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 DatabaseVersionHeader (org.syncany.database.DatabaseVersionHeader)2