Moved datastore interface from ipfs to libp2p

yamux
John Jones 2017-04-06 09:33:28 -05:00
parent 950ad31760
commit 94d6005587
23 changed files with 21 additions and 432 deletions

View File

@ -12,7 +12,6 @@ all:
cd importer; make all;
cd merkledag; make all;
cd multibase; make all;
cd os; make all;
cd pin; make all;
cd repo; make all;
cd flatfs; make all;
@ -36,7 +35,6 @@ clean:
cd importer; make clean;
cd merkledag; make clean;
cd multibase; make clean;
cd os; make clean;
cd pin; make clean;
cd repo; make clean;
cd flatfs; make clean;

View File

@ -6,7 +6,7 @@
#include "ipfs/blocks/block.h"
#include "ipfs/datastore/ds_helper.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
/**
* Delete a block based on its Cid

View File

@ -4,7 +4,7 @@
#include "ipfs/cmd/ipfs/init.h"
#include "ipfs/commands/request.h"
#include "ipfs/commands/command_option.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "ipfs/core/ipfs_node.h"
#include "ipfs/core/builder.h"
#include "ipfs/repo/config/config.h"

View File

@ -9,7 +9,7 @@
#include <string.h>
#include <sys/stat.h>
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#define FLATFS_MAX_PREFIX_LENGTH 16

View File

@ -4,7 +4,7 @@
#include "ipfs/importer/importer.h"
#include "ipfs/merkledag/merkledag.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/unixfs/unixfs.h"

View File

@ -1,80 +0,0 @@
/**
* OS specific stuff. This is going to get ugly, but at least its in one place
*/
#ifndef __OS_UTILS_H__
#define __OS_UTILS_H__
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
* a linked list to store filenames
*/
struct FileList {
char* file_name;
struct FileList* next;
};
/**
* Builds a list of files within a directory
* @param path the path to examine
* @returns a FileList struct of the first file
*/
struct FileList* os_utils_list_directory(const char* path);
/**
* Cleans up memory used by a FileList struct
* @param first the struct to free
* @returns true(1)
*/
int os_utils_free_file_list(struct FileList* first);
/**
* Split the filename from the path
* @param in the full path and filename
* @param path only the path part
* @param filename only the file name
* @returns true(1)
*/
int os_utils_split_filename(const char* in, char** path, char** filename);
/**
* get an environment varible from the os
* @param variable the variable to look for
* @returns the results
*/
char* os_utils_getenv(const char* variable);
int os_utils_setenv(const char* variable, const char* value, int overwrite);
/**
* get the user's home directory
* @returns the user's home directory
*/
char* os_utils_get_homedir();
/**
* join 2 pieces of a filepath, being careful about the slashes
* @param root the root part
* @param extension what should be concatenated
* @param results where to put the results
* @param max_len throw an error if the total is longer than max_len
*/
int os_utils_filepath_join(const char* root, const char* extension, char* results, unsigned long max_len);
int os_utils_file_exists(const char* file_name);
int os_utils_file_size(const char* file_name);
int os_utils_directory_writeable(const char* path);
int os_utils_directory_exists(const char* path);
/**
* Determine if the path presented is actually a directory
* @param file_name the path to examine
* @returns true(1) if file_name is actually a directory
*/
int os_utils_is_directory(const char* file_name);
#endif /* utils_h */

View File

@ -1,7 +1,7 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__
#include "datastore.h"
#include "libp2p/db/datastore.h"
#include "identity.h"
#include "swarm.h"
#include "bootstrap_peers.h"

View File

@ -1,62 +0,0 @@
#ifndef __DATASTORE_H__
#define __DATASTORE_H__
#include <stdint.h>
#include "ipfs/blocks/block.h"
//const char* datastore_default_directory = "datastore";
enum DatastoreCursorOp { CURSOR_FIRST, CURSOR_NEXT };
struct Datastore {
char* type;
char* path;
char* storage_max;
int storage_gc_watermark;
char* gc_period;
char* params;
int no_sync;
int hash_on_read;
int bloom_filter_size;
// function pointers for datastore operations
int (*datastore_open)(int argc, char** argv, struct Datastore* datastore);
int (*datastore_close)(struct Datastore* datastore);
int (*datastore_put)(const unsigned char* key, size_t key_size, unsigned char* data, size_t data_length, const struct Datastore* datastore);
int (*datastore_get)(const char* key, size_t key_size,
unsigned char* data, size_t max_data_length, size_t* data_length,
const struct Datastore* datastore);
int (*datastore_cursor_open)(struct Datastore* datastore);
int (*datastore_cursor_close)(struct Datastore* datastore);
int (*datastore_cursor_get)(unsigned char** key, int* key_length, unsigned char** value, int* value_length, enum DatastoreCursorOp op, struct Datastore* datastore);
// generic connection and status variables for the datastore
void* handle; // a handle to the database
void* cursor; // a current cursor
};
/***
* Initialize the structure of the datastore to default settings. Used for
* creating a new datastore on the disk.
* @param datastore the struct to initialize
* @param config_root the path to the root of IPFS
* @returns true(1) on success
*/
int ipfs_repo_config_datastore_init(struct Datastore* datastore, const char* config_root);
/***
* initialize the structure of the datastore
* @param datastore the struct to initialize
* @returns true(1) on success
*/
int ipfs_repo_config_datastore_new(struct Datastore** datastore);
/***
* deallocate the memory and clear resources from a datastore_init
* @param datastore the struct to deallocate
* @returns true(1)
*/
int ipfs_repo_config_datastore_free(struct Datastore* datastore);
#endif

View File

@ -9,6 +9,7 @@
#include "ipfs/repo/config/config.h"
#include "ipfs/unixfs/unixfs.h"
#include "ipfs/merkledag/node.h"
#include "ipfs/blocks/block.h"
/**
* a structure to hold the repo info

View File

@ -1,7 +1,7 @@
#ifndef __FS_REPO_LMDB_DATASTORE_H__
#define __FS_REPO_LMDB_DATASTORE_H__
#include "ipfs/repo/config/datastore.h"
#include "libp2p/db/datastore.h"
/***
* Places the LMDB methods into the datastore's function pointers

View File

@ -17,12 +17,11 @@ OBJS = main.o \
../merkledag/merkledag.o ../merkledag/node.o \
../multibase/multibase.o \
../namesys/*.o \
../os/utils.o \
../pin/pin.o \
../repo/init.o \
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \
../repo/config/config.o ../repo/config/identity.o \
../repo/config/bootstrap_peers.o ../repo/config/datastore.o ../repo/config/gateway.o \
../repo/config/bootstrap_peers.o ../repo/config/gateway.o \
../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
../routing/*.o \
../thirdparty/ipfsaddr/ipfs_addr.o \

View File

@ -1,19 +0,0 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -Wall
ifdef DEBUG
CFLAGS += -g3
endif
LFLAGS =
DEPS =
OBJS = utils.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
all: $(OBJS)
clean:
rm -f *.o

View File

@ -1,172 +0,0 @@
#include "ipfs/os/utils.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef __MINGW32__
#include <pwd.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
/**
* get an environment varible from the os
* @param variable the variable to look for
* @returns the results
*/
char* os_utils_getenv(const char* variable) {
return getenv(variable);
}
/**
* set an environment variable in the os
* @param variable the variable to set
* @param value the value to assign to the variable
* @param overwrite passing a non-zero will allow an existing value to be overwritten
* @returns true(1) on success
*/
int os_utils_setenv(const char* variable, const char* value, int overwrite) {
#ifdef __MINGW32__
return 0;
#else
setenv(variable, value, overwrite);
return 1;
#endif
}
/**
* returns the user's home directory
* @returns the home directory
*/
char* os_utils_get_homedir() {
#ifndef __MINGW32__
struct passwd *pw = getpwuid(getuid());
return pw->pw_dir;
#endif
return ".";
}
/**
* join 2 pieces of a filepath, being careful about the slashes
* @param root the root part
* @param extension what should be concatenated
* @param results where to put the results
* @param max_len throw an error if the total is longer than max_len
*/
int os_utils_filepath_join(const char* root, const char* extension, char* results, unsigned long max_len) {
if (strlen(root) + strlen(extension) + 1 > max_len)
return 0;
strncpy(results, root, strlen(root) + 1);
// one of these should have a slash. If not, add one
if (root[strlen(root)-1] != '/' && extension[0] != '/') {
results[strlen(root)] = '/';
results[strlen(root)+1] = 0;
}
strncat(results, extension, strlen(extension)+1);
return 1;
}
int os_utils_file_exists(const char* file_name) {
if (access(file_name, F_OK) != -1)
return 1;
return 0;
}
int os_utils_directory_exists(const char* directory_name) {
if (access(directory_name, F_OK) != -1)
return 1;
return 0;
}
int os_utils_is_directory(const char* file_name) {
struct stat path_stat;
stat(file_name, &path_stat);
return S_ISDIR(path_stat.st_mode) != 0;
}
int os_utils_split_filename(const char* in, char** path, char** filename) {
int len = strlen(in);
char* pos = strrchr(in, '/');
if (pos != NULL) {
pos++;
*path = (char*)malloc((pos - in) + 1);
strncpy(*path, in, pos-in-1);
(*path)[pos-in-1] = 0;
*filename = (char*)malloc(len - (pos-in) + 1);
strcpy(*filename, pos);
(*filename)[len - (pos-in)] = 0;
} else {
*filename = (char*)malloc(len+1);
strcpy(*filename, in);
}
return 1;
}
struct FileList* os_utils_list_directory(const char* path) {
DIR* dp;
struct dirent *ep;
struct FileList* first = NULL;
struct FileList* last = NULL;
struct FileList* next = NULL;
dp = opendir(path);
if (dp != NULL) {
while ( (ep = readdir(dp)) ) {
if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
// grab the data
next = (struct FileList*)malloc(sizeof(struct FileList));
next->file_name = malloc(strlen(ep->d_name) + 1);
strcpy(next->file_name, ep->d_name);
next->next = NULL;
// put it in the appropriate spot
if (first == NULL) {
first = next;
last = next;
} else {
last->next = next;
last = next;
}
} // not dealing with . or ..
} // while
closedir(dp);
}
return first;
}
int os_utils_free_file_list(struct FileList* first) {
if (first != NULL) {
struct FileList* next = first;
struct FileList* last = NULL;
while (next != NULL) {
last = next->next;
if (next->file_name != NULL) {
free(next->file_name);
}
free(next);
next = last;
}
}
return 1;
}
int os_utils_directory_writeable(const char* path) {
int result = access(path, W_OK);
return result == 0;
}
int os_utils_file_size(const char* path) {
size_t file_size = 0;
// open file
FILE* in_file = fopen(path, "r");
if (in_file == NULL)
return 0;
// determine size
fseek(in_file, 0L, SEEK_END);
file_size = ftell(in_file);
fclose(in_file);
return file_size;
}

View File

@ -7,7 +7,7 @@ endif
LFLAGS =
DEPS = config.h datastore.h identity.h
OBJS = config.o identity.o bootstrap_peers.o datastore.o gateway.o addresses.o swarm.o peer.o
OBJS = config.o identity.o bootstrap_peers.o gateway.o addresses.o swarm.o peer.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

View File

@ -4,7 +4,7 @@
#include "libp2p/utils/linked_list.h"
#include "ipfs/repo/config/config.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "ipfs/repo/config/bootstrap_peers.h"
#include "ipfs/repo/config/swarm.h"
@ -92,7 +92,7 @@ int ipfs_repo_config_init(struct RepoConfig* config, unsigned int num_bits_for_k
return 0;
// datastore
retVal = ipfs_repo_config_datastore_init(config->datastore, repo_path);
retVal = libp2p_datastore_init(config->datastore, repo_path);
if (retVal == 0)
return 0;
@ -149,7 +149,7 @@ int ipfs_repo_config_new(struct RepoConfig** config) {
if (retVal == 0)
return 0;
retVal = ipfs_repo_config_datastore_new(&((*config)->datastore));
retVal = libp2p_datastore_new(&((*config)->datastore));
if (retVal == 0)
return 0;
@ -176,7 +176,7 @@ int ipfs_repo_config_free(struct RepoConfig* config) {
if (&(config->bootstrap_peers) != NULL)
repo_config_bootstrap_peers_free(config->bootstrap_peers);
if (config->datastore != NULL)
ipfs_repo_config_datastore_free(config->datastore);
libp2p_datastore_free(config->datastore);
if (config->addresses != NULL)
repo_config_addresses_free(config->addresses);
if (config->gateway != NULL)

View File

@ -1,75 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "ipfs/repo/config/datastore.h"
#include "ipfs/os/utils.h"
int alloc_and_assign(char** result, const char* string) {
*result = malloc(strlen(string)+1);
if (*result == NULL)
return 0;
strcpy(*result, string);
return 1;
}
/***
* initialize the structure of the datastore
* @param datastore the struct to initialize
* @returns true(1) on success
*/
int ipfs_repo_config_datastore_init(struct Datastore* datastore, const char* config_root) {
unsigned long stringLength = strlen(config_root) + 12;
datastore->path = malloc(sizeof(char) * stringLength);
os_utils_filepath_join(config_root, "datastore", datastore->path, stringLength);
alloc_and_assign(&datastore->type, "lmdb");
alloc_and_assign(&datastore->storage_max, "10GB");
datastore->storage_gc_watermark = 90;
alloc_and_assign(&datastore->gc_period, "1h");
datastore->hash_on_read = 0;
datastore->bloom_filter_size = 0;
datastore->no_sync = 0;
return 1;
}
/***
* initialize the structure of the datastore
* @param datastore the struct to initialize
* @returns true(1) on success
*/
int ipfs_repo_config_datastore_new(struct Datastore** datastore) {
*datastore = malloc(sizeof(struct Datastore));
if (*datastore == NULL)
return 0;
(*datastore)->path = NULL;
(*datastore)->handle = NULL;
(*datastore)->type = NULL;
(*datastore)->storage_max = NULL;
(*datastore)->gc_period = NULL;
(*datastore)->params = NULL;
return 1;
}
/***
* deallocate the memory and clear resources from a datastore_init
* @param datastore the struct to deallocate
* @returns true(1)
*/
int ipfs_repo_config_datastore_free(struct Datastore* datastore) {
if (datastore != NULL)
{
if (datastore->path != NULL)
free(datastore->path);
if (datastore->type != NULL)
free(datastore->type);
if (datastore->storage_max != NULL)
free(datastore->storage_max);
if (datastore->gc_period != NULL)
free(datastore->gc_period);
if (datastore->params != NULL)
free(datastore->params);
if (datastore->handle != NULL)
datastore->datastore_close(datastore);
free(datastore);
}
return 1;
}

View File

@ -5,9 +5,9 @@
#include "libp2p/crypto/key.h"
#include "ipfs/blocks/blockstore.h"
#include "ipfs/datastore/ds_helper.h"
#include "ipfs/repo/config/datastore.h"
#include "libp2p/db/datastore.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "ipfs/repo/fsrepo/lmdb_datastore.h"
#include "jsmn.h"
#include "multiaddr/multiaddr.h"

View File

@ -2,7 +2,7 @@
#include <sys/stat.h>
#include <string.h>
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "ipfs/repo/config/config.h"
#include "ipfs/repo/fsrepo/fs_repo.h"

View File

@ -17,11 +17,10 @@ OBJS = testit.o test_helper.o \
../importer/importer.o ../importer/exporter.o ../importer/resolver.o \
../merkledag/merkledag.o ../merkledag/node.o \
../multibase/multibase.o \
../os/utils.o \
../repo/init.o \
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.o \
../repo/config/config.o ../repo/config/identity.o \
../repo/config/bootstrap_peers.o ../repo/config/datastore.o ../repo/config/gateway.o \
../repo/config/bootstrap_peers.o ../repo/config/gateway.o \
../repo/config/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
../routing/offline.o \
../routing/online.o \

View File

@ -13,7 +13,7 @@
#include "ipfs/commands/argument.h"
#include "ipfs/commands/request.h"
#include "ipfs/commands/command.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
int test_init_new_installation() {

View File

@ -1,7 +1,7 @@
#include <pthread.h>
#include "ipfs/importer/resolver.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
#include "multiaddr/multiaddr.h"
#include "ipfs/core/daemon.h"

View File

@ -3,7 +3,7 @@
#include <sys/stat.h>
#include "ipfs/repo/config/config.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
int test_repo_config_new() {
struct RepoConfig* repoConfig;

View File

@ -4,7 +4,7 @@
#include "ipfs/repo/init.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h"
#include "libp2p/os/utils.h"
/***
* Helper to create a test file in the OS