2016-12-12 11:27:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "ipfs/importer/importer.h"
|
2016-12-14 17:07:43 +00:00
|
|
|
#include "ipfs/merkledag/merkledag.h"
|
2016-12-12 11:27:06 +00:00
|
|
|
|
|
|
|
#define MAX_DATA_SIZE 262144 // 1024 * 256;
|
|
|
|
|
2016-12-14 11:25:09 +00:00
|
|
|
/***
|
|
|
|
* Imports OS files into the datastore
|
|
|
|
*/
|
|
|
|
|
2016-12-12 11:27:06 +00:00
|
|
|
/**
|
|
|
|
* read the next chunk of bytes, create a node, and add a link to the node in the passed-in node
|
|
|
|
* @param file the file handle
|
|
|
|
* @param node the node to add to
|
|
|
|
* @returns number of bytes read
|
|
|
|
*/
|
2016-12-14 17:07:43 +00:00
|
|
|
size_t ipfs_import_chunk(FILE* file, struct Node* node, struct FSRepo* fs_repo) {
|
2016-12-12 11:27:06 +00:00
|
|
|
unsigned char buffer[MAX_DATA_SIZE];
|
2016-12-14 17:07:43 +00:00
|
|
|
size_t bytes_read = fread(buffer, 1, MAX_DATA_SIZE, file);
|
2016-12-15 10:40:24 +00:00
|
|
|
|
2016-12-12 11:27:06 +00:00
|
|
|
if (node->data_size == 0) {
|
2016-12-14 11:25:09 +00:00
|
|
|
ipfs_node_set_data(node, buffer, bytes_read);
|
2016-12-12 11:27:06 +00:00
|
|
|
} else {
|
|
|
|
// create a new node, and link to the parent
|
2016-12-14 11:25:09 +00:00
|
|
|
struct Node* new_node = NULL;
|
|
|
|
ipfs_node_new_from_data(buffer, bytes_read, &new_node);
|
2016-12-12 11:27:06 +00:00
|
|
|
// persist
|
2016-12-14 17:07:43 +00:00
|
|
|
ipfs_merkledag_add(new_node, fs_repo);
|
2016-12-15 10:40:24 +00:00
|
|
|
// put link in parent node
|
2016-12-14 11:25:09 +00:00
|
|
|
struct NodeLink* new_link = NULL;
|
2016-12-15 17:38:08 +00:00
|
|
|
ipfs_node_link_new("", new_node->cached->hash, new_node->cached->hash_length, &new_link);
|
2016-12-14 11:25:09 +00:00
|
|
|
ipfs_node_add_link(node, new_link);
|
|
|
|
ipfs_node_free(new_node);
|
2016-12-12 11:27:06 +00:00
|
|
|
}
|
2016-12-14 17:07:43 +00:00
|
|
|
if (bytes_read != MAX_DATA_SIZE) {
|
|
|
|
// persist the main node
|
|
|
|
ipfs_merkledag_add(node, fs_repo);
|
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a node based on an incoming file
|
|
|
|
* @param file_name the file to import
|
|
|
|
* @param node the root node (could have links to others)
|
|
|
|
* @returns true(1) on success
|
|
|
|
*/
|
2016-12-14 17:07:43 +00:00
|
|
|
int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs_repo) {
|
2016-12-12 11:27:06 +00:00
|
|
|
int retVal = 1;
|
2016-12-14 11:25:09 +00:00
|
|
|
int bytes_read = MAX_DATA_SIZE;
|
2016-12-12 11:27:06 +00:00
|
|
|
|
|
|
|
FILE* file = fopen(fileName, "rb");
|
2016-12-14 11:25:09 +00:00
|
|
|
retVal = ipfs_node_new(node);
|
|
|
|
if (retVal == 0)
|
|
|
|
return 0;
|
2016-12-12 11:27:06 +00:00
|
|
|
|
|
|
|
// add all nodes
|
2016-12-14 11:25:09 +00:00
|
|
|
while ( bytes_read == MAX_DATA_SIZE) {
|
2016-12-14 17:07:43 +00:00
|
|
|
bytes_read = ipfs_import_chunk(file, *node, fs_repo);
|
2016-12-14 11:25:09 +00:00
|
|
|
}
|
2016-12-12 11:27:06 +00:00
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|