diff --git a/Makefile b/Makefile index 8dfe8a9..1cfb8e7 100644 --- a/Makefile +++ b/Makefile @@ -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; diff --git a/blocks/blockstore.c b/blocks/blockstore.c index 333a6c5..a63ea92 100644 --- a/blocks/blockstore.c +++ b/blocks/blockstore.c @@ -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 diff --git a/cmd/ipfs/init.c b/cmd/ipfs/init.c index 3f76dc6..31ac6a2 100644 --- a/cmd/ipfs/init.c +++ b/cmd/ipfs/init.c @@ -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" diff --git a/flatfs/flatfs.c b/flatfs/flatfs.c index a0101d3..39bd3db 100644 --- a/flatfs/flatfs.c +++ b/flatfs/flatfs.c @@ -9,7 +9,7 @@ #include #include -#include "ipfs/os/utils.h" +#include "libp2p/os/utils.h" #define FLATFS_MAX_PREFIX_LENGTH 16 diff --git a/importer/importer.c b/importer/importer.c index 71183c5..229275b 100644 --- a/importer/importer.c +++ b/importer/importer.c @@ -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" diff --git a/include/ipfs/os/utils.h b/include/ipfs/os/utils.h deleted file mode 100644 index 214a741..0000000 --- a/include/ipfs/os/utils.h +++ /dev/null @@ -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 -#include -#include - -/** - * 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 */ diff --git a/include/ipfs/repo/config/config.h b/include/ipfs/repo/config/config.h index 86ecf11..48e0f69 100644 --- a/include/ipfs/repo/config/config.h +++ b/include/ipfs/repo/config/config.h @@ -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" diff --git a/include/ipfs/repo/config/datastore.h b/include/ipfs/repo/config/datastore.h deleted file mode 100644 index e853334..0000000 --- a/include/ipfs/repo/config/datastore.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef __DATASTORE_H__ -#define __DATASTORE_H__ - -#include -#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 diff --git a/include/ipfs/repo/fsrepo/fs_repo.h b/include/ipfs/repo/fsrepo/fs_repo.h index cc6d834..49178f3 100644 --- a/include/ipfs/repo/fsrepo/fs_repo.h +++ b/include/ipfs/repo/fsrepo/fs_repo.h @@ -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 diff --git a/include/ipfs/repo/fsrepo/lmdb_datastore.h b/include/ipfs/repo/fsrepo/lmdb_datastore.h index 1ce6b1d..c290aeb 100644 --- a/include/ipfs/repo/fsrepo/lmdb_datastore.h +++ b/include/ipfs/repo/fsrepo/lmdb_datastore.h @@ -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 diff --git a/main/Makefile b/main/Makefile index 8d5b7d5..4282ac7 100644 --- a/main/Makefile +++ b/main/Makefile @@ -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 \ diff --git a/os/Makefile b/os/Makefile deleted file mode 100644 index 2ab0663..0000000 --- a/os/Makefile +++ /dev/null @@ -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 diff --git a/os/utils.c b/os/utils.c deleted file mode 100644 index 1e7ce25..0000000 --- a/os/utils.c +++ /dev/null @@ -1,172 +0,0 @@ -#include "ipfs/os/utils.h" - -#include -#include -#include -#include -#ifndef __MINGW32__ - #include -#endif -#include -#include -#include - -/** - * 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; -} diff --git a/repo/config/Makefile b/repo/config/Makefile index 576f8f0..0a380f8 100644 --- a/repo/config/Makefile +++ b/repo/config/Makefile @@ -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) diff --git a/repo/config/config.c b/repo/config/config.c index 44100a4..566b5f2 100644 --- a/repo/config/config.c +++ b/repo/config/config.c @@ -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) diff --git a/repo/config/datastore.c b/repo/config/datastore.c deleted file mode 100644 index 1d16b50..0000000 --- a/repo/config/datastore.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include - -#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; -} diff --git a/repo/fsrepo/fs_repo.c b/repo/fsrepo/fs_repo.c index 9f79deb..9fa2880 100644 --- a/repo/fsrepo/fs_repo.c +++ b/repo/fsrepo/fs_repo.c @@ -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" diff --git a/repo/init.c b/repo/init.c index 49db57f..9365bd9 100644 --- a/repo/init.c +++ b/repo/init.c @@ -2,7 +2,7 @@ #include #include -#include "ipfs/os/utils.h" +#include "libp2p/os/utils.h" #include "ipfs/repo/config/config.h" #include "ipfs/repo/fsrepo/fs_repo.h" diff --git a/test/Makefile b/test/Makefile index 8fa7ae2..168350e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 \ diff --git a/test/cmd/ipfs/test_init.h b/test/cmd/ipfs/test_init.h index 6085d55..9cb0c81 100644 --- a/test/cmd/ipfs/test_init.h +++ b/test/cmd/ipfs/test_init.h @@ -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() { diff --git a/test/node/test_resolver.h b/test/node/test_resolver.h index ced344d..e068f18 100644 --- a/test/node/test_resolver.h +++ b/test/node/test_resolver.h @@ -1,7 +1,7 @@ #include #include "ipfs/importer/resolver.h" -#include "ipfs/os/utils.h" +#include "libp2p/os/utils.h" #include "multiaddr/multiaddr.h" #include "ipfs/core/daemon.h" diff --git a/test/repo/test_repo_config.h b/test/repo/test_repo_config.h index ff90f50..78eb828 100644 --- a/test/repo/test_repo_config.h +++ b/test/repo/test_repo_config.h @@ -3,7 +3,7 @@ #include #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; diff --git a/test/test_helper.c b/test/test_helper.c index 2d8b915..7b8fe62 100644 --- a/test/test_helper.c +++ b/test/test_helper.c @@ -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