Add storage api for importing files

This commit is contained in:
Jeremy Lakeman 2012-12-18 10:51:12 +10:30
parent d76b3a25f9
commit c5d76a057f
4 changed files with 40 additions and 39 deletions

View File

@ -1231,37 +1231,11 @@ int app_rhizome_add_file(int argc, const char *const *argv, const struct command
if (rhizome_open_write(&write, NULL, m->fileLength, RHIZOME_PRIORITY_DEFAULT))
return -1;
FILE *f = fopen(filepath, "r");
if (!f) {
WHY_perror("fopen");
goto cleanup;
if (rhizome_write_file(&write, filepath)){
rhizome_fail_write(&write);
return -1;
}
while(write.file_offset < write.file_length){
int size=write.buffer_size - write.data_size;
if (write.file_offset + size > write.file_length)
size=write.file_length - write.file_offset;
int r = fread(write.buffer + write.data_size, 1, size, f);
if (r==-1){
WHY_perror("fread");
goto cleanup;
}
write.data_size+=r;
// TODO encryption
if (rhizome_flush(&write)){
cleanup:
if (f)
fclose(f);
rhizome_fail_write(&write);
return -1;
}
}
fclose(f);
if (rhizome_finish_write(&write))
return -1;

View File

@ -244,16 +244,10 @@ int rhizome_manifest_bind_file(rhizome_manifest *m_in,const char *filename,int e
int rhizome_manifest_check_file(rhizome_manifest *m_in)
{
long long gotfile = 0;
if (sqlite_exec_int64(&gotfile, "SELECT COUNT(*) FROM FILES WHERE ID='%s' and datavalid=1;", m_in->fileHexHash) != 1) {
WHYF("Failed to count files");
// Don't bother to check the file if we already have it
if (rhizome_exists(m_in->fileHexHash))
return 0;
}
if (gotfile) {
/* Skipping file checks for bundle, as file is already in the database */
return 0;
}
/* Find out whether the payload is expected to be encrypted or not */
m_in->payloadEncryption=rhizome_manifest_get_ll(m_in, "crypt");
@ -261,6 +255,7 @@ int rhizome_manifest_check_file(rhizome_manifest *m_in)
matches the file size stored in the manifest */
long long mfilesize = rhizome_manifest_get_ll(m_in, "filesize");
m_in->fileLength = 0;
if (m_in->dataFileName && m_in->dataFileName[0]) {
struct stat stat;
if (lstat(m_in->dataFileName,&stat) == -1) {

View File

@ -614,6 +614,7 @@ struct rhizome_write{
int rhizome_exists(const char *fileHash);
int rhizome_open_write(struct rhizome_write *write, char *expectedFileHash, int64_t file_length, int priority);
int rhizome_flush(struct rhizome_write *write);
int rhizome_write_file(struct rhizome_write *write, const char *filename);
int rhizome_fail_write(struct rhizome_write *write);
int rhizome_finish_write(struct rhizome_write *write);

View File

@ -105,6 +105,8 @@ int rhizome_flush(struct rhizome_write *write){
if (write->data_size<=0)
return WHY("No content supplied");
// TODO encryption?
sqlite3_blob *blob;
int ret = sqlite3_blob_open(rhizome_db, "main", "FILEBLOBS", "data", write->blob_rowid, 1 /* read/write */, &blob);
if (ret!=SQLITE_OK) {
@ -132,6 +134,36 @@ int rhizome_flush(struct rhizome_write *write){
return 0;
}
/* Expects file to be at least file_length in size */
int rhizome_write_file(struct rhizome_write *write, const char *filename){
FILE *f = fopen(filename, "r");
if (!f)
return WHY_perror("fopen");
while(write->file_offset < write->file_length){
int size=write->buffer_size - write->data_size;
if (write->file_offset + size > write->file_length)
size=write->file_length - write->file_offset;
int r = fread(write->buffer + write->data_size, 1, size, f);
if (r==-1){
WHY_perror("fread");
fclose(f);
return -1;
}
write->data_size+=r;
if (rhizome_flush(write)){
fclose(f);
return -1;
}
}
fclose(f);
return 0;
}
int rhizome_fail_write(struct rhizome_write *write){
if (write->buffer)
free(write->buffer);
@ -205,4 +237,3 @@ failure:
rhizome_fail_write(write);
return -1;
}