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;
}
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();
}
}
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);
}
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;
}
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;
}
Aggregations