Added a simplistic command line.

From the command line, you can init the repository or add a file.
Directories coming soon...
This commit is contained in:
jmjatlanta 2016-12-21 08:08:44 -05:00
parent f8cdaf0a97
commit 7fa0fc6a7b
11 changed files with 246 additions and 35 deletions

View file

@ -16,6 +16,7 @@ all:
cd flatfs; make all; cd flatfs; make all;
cd datastore; make all; cd datastore; make all;
cd thirdparty; make all; cd thirdparty; make all;
cd main; make all;
cd test; make all; cd test; make all;
clean: clean:
@ -32,6 +33,7 @@ clean:
cd flatfs; make clean; cd flatfs; make clean;
cd datastore; make clean; cd datastore; make clean;
cd thirdparty; make clean; cd thirdparty; make clean;
cd main; make clean;
cd test; make clean; cd test; make clean;
rebuild: clean all rebuild: clean all

View file

@ -2,6 +2,7 @@
#include "ipfs/importer/importer.h" #include "ipfs/importer/importer.h"
#include "ipfs/merkledag/merkledag.h" #include "ipfs/merkledag/merkledag.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
#define MAX_DATA_SIZE 262144 // 1024 * 256; #define MAX_DATA_SIZE 262144 // 1024 * 256;
@ -63,3 +64,45 @@ int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs
return 1; return 1;
} }
/**
* called from the command line
* @param argc the number of arguments
* @param argv the arguments
*/
int ipfs_import(int argc, char** argv) {
/*
* Param 0: ipfs
* param 1: add
* param 2: filename
*/
struct Node* node = NULL;
struct FSRepo* fs_repo = NULL;
// open the repo
int retVal = ipfs_repo_fsrepo_new(NULL, NULL, &fs_repo);
if (retVal == 0) {
return 0;
}
retVal = ipfs_repo_fsrepo_open(fs_repo);
// import the file(s)
retVal = ipfs_import_file(argv[2], &node, fs_repo);
// give some results to the user
int buffer_len = 100;
unsigned char buffer[buffer_len];
retVal = ipfs_cid_hash_to_base58(node->hash, node->hash_size, buffer, buffer_len);
if (retVal == 0) {
printf("Unable to generate hash\n");
return 0;
}
printf("added %s %s\n", buffer, argv[2]);
if (node != NULL)
ipfs_node_free(node);
if (fs_repo != NULL)
ipfs_repo_fsrepo_free(fs_repo);
return retVal;
}

View file

@ -12,4 +12,11 @@
*/ */
int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs_repo); int ipfs_import_file(const char* fileName, struct Node** node, struct FSRepo* fs_repo);
/**
* called from the command line
* @param argc the number of arguments
* @param argv the arguments
*/
int ipfs_import(int argc, char** argv);
#endif /* INCLUDE_IPFS_IMPORTER_IMPORTER_H_ */ #endif /* INCLUDE_IPFS_IMPORTER_IMPORTER_H_ */

17
include/ipfs/repo/init.h Normal file
View file

@ -0,0 +1,17 @@
#pragma once
/***
* Makes a new ipfs repository
* @param path the path to the repository, should end
* with .ipfs, and the directory should already exist.
* @returns true(1) on success
*/
int make_ipfs_repository(const char* path);
/**
* Initialize a repository, called from the command line
* @param argc number of arguments
* @param argv arguments
* @returns true(1) on success
*/
int ipfs_repo_init(int argc, char** argv);

37
main/Makefile Normal file
View file

@ -0,0 +1,37 @@
CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -I../../c-protobuf -g3 -Wall
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
OBJS = main.o \
../blocks/block.o ../blocks/blockstore.o \
../cid/cid.o \
../cmd/ipfs/init.o \
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \
../core/builder.o \
../datastore/ds_helper.o \
../flatfs/flatfs.o \
../importer/importer.o \
../importer/exporter.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/addresses.o ../repo/config/swarm.o ../repo/config/peer.o \
../thirdparty/ipfsaddr/ipfs_addr.o \
../unixfs/unixfs.o \
../../c-protobuf/protobuf.o ../../c-protobuf/varint.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
ipfs: $(OBJS)
$(CC) -o $@ $^ $(LFLAGS) ../../lmdb/libraries/liblmdb/liblmdb.a
all: ipfs
clean:
rm -f *.o
rm -f ipfs

BIN
main/ipfs Executable file

Binary file not shown.

57
main/main.c Normal file
View file

@ -0,0 +1,57 @@
#include <stdio.h>
#include <string.h>
#include "ipfs/repo/init.h"
#include "ipfs/importer/importer.h"
void stripit(int argc, char** argv) {
char tmp[strlen(argv[argc])];
strcpy(tmp, &argv[argc][1]);
tmp[strlen(tmp)-1] = 0;
strcpy(argv[argc], tmp);
return;
}
void strip_quotes(int argc, char** argv) {
for(int i = 0; i < argc; i++) {
if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') {
stripit(i, argv);
}
}
}
#define INIT 1
#define ADD 2
/***
* Basic parsing of command line arguments to figure out where the user wants to go
*/
int parse_arguments(int argc, char** argv) {
if (argc == 1) {
printf("No parameters passed.\n");
return 0;
}
if (strcmp("init", argv[1]) == 0) {
return INIT;
}
if (strcmp("add", argv[1]) == 0) {
return ADD;
}
return -1;
}
/***
* The beginning
*/
int main(int argc, char** argv) {
strip_quotes(argc, argv);
int retVal = parse_arguments(argc, argv);
switch (retVal) {
case (INIT):
return ipfs_repo_init(argc, argv);
break;
case (ADD):
ipfs_import(argc, argv);
break;
}
}

View file

@ -6,7 +6,7 @@ CFLAGS += -g3
endif endif
DEPS = DEPS =
OBJS = OBJS = init.c
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)

77
repo/init.c Normal file
View file

@ -0,0 +1,77 @@
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include "ipfs/os/utils.h"
#include "ipfs/repo/config/config.h"
#include "ipfs/repo/fsrepo/fs_repo.h"
int make_ipfs_repository(const char* path) {
int retVal;
char currDirectory[1024];
struct RepoConfig* repo_config;
printf("initializing ipfs node at %s\n", path);
// build a default repo config
retVal = ipfs_repo_config_new(&repo_config);
if (retVal == 0)
return 0;
printf("generating 2048-bit RSA keypair...");
retVal = ipfs_repo_config_init(repo_config, 2048, path);
if (retVal == 0)
return 0;
printf("done\n");
// now the fs_repo
struct FSRepo* fs_repo;
retVal = ipfs_repo_fsrepo_new(path, repo_config, &fs_repo);
if (retVal == 0)
return 0;
// this builds a new repo
retVal = ipfs_repo_fsrepo_init(fs_repo);
if (retVal == 0) {
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
// give some results to the user
printf("peer identity: %s\n", fs_repo->config->identity->peer_id);
// clean up
ipfs_repo_fsrepo_free(fs_repo);
// make sure the repository exists
retVal = os_utils_filepath_join(path, "config", currDirectory, 1024);
if (retVal == 0)
return 0;
retVal = os_utils_file_exists(currDirectory);
return retVal;
}
/**
* Initialize a repository
*/
int ipfs_repo_init(int argc, char** argv) {
char* home_directory = os_utils_get_homedir();
//allow user to pass directory on command line
if (argc > 2) {
home_directory = argv[2];
}
// get the full path
int dir_len = strlen(home_directory) + 7;
char dir[dir_len];
strcpy(dir, home_directory);
os_utils_filepath_join(home_directory, ".ipfs", dir, dir_len);
// make sure it doesn't already exist
if (os_utils_file_exists(dir)) {
printf("Directory already exists: %s\n", dir);
return 0;
}
// make the directory
if (mkdir(dir, S_IRWXU) == -1) {
printf("Unable to create the directory: %s\n", dir);
return 0;
}
// make the repository
return make_ipfs_repository(dir);
}

View file

@ -15,6 +15,7 @@ OBJS = testit.o test_helper.o \
../merkledag/merkledag.o ../merkledag/node.o \ ../merkledag/merkledag.o ../merkledag/node.o \
../multibase/multibase.o \ ../multibase/multibase.o \
../os/utils.o \ ../os/utils.o \
../repo/init.o \
../repo/fsrepo/fs_repo.o ../repo/fsrepo/jsmn.o ../repo/fsrepo/lmdb_datastore.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/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/datastore.o ../repo/config/gateway.o \

View file

@ -2,6 +2,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
#include "ipfs/repo/init.h"
#include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h" #include "ipfs/os/utils.h"
@ -67,10 +68,9 @@ int remove_directory(const char *path)
return r; return r;
} }
int make_ipfs_repository(const char* path) { int drop_and_build_repository(const char* path) {
int retVal; int retVal = 0;
char currDirectory[1024]; char currDirectory[strlen(path) + 20];
struct RepoConfig* repo_config;
if (os_utils_file_exists(path)) { if (os_utils_file_exists(path)) {
retVal = os_utils_filepath_join(path, "config", currDirectory, 1024); retVal = os_utils_filepath_join(path, "config", currDirectory, 1024);
@ -89,37 +89,7 @@ int make_ipfs_repository(const char* path) {
mkdir(path, S_IRWXU); mkdir(path, S_IRWXU);
} }
// build a default repo config
retVal = ipfs_repo_config_new(&repo_config);
if (retVal == 0)
return 0;
retVal = ipfs_repo_config_init(repo_config, 2048, path);
if (retVal == 0)
return 0;
// now the fs_repo
struct FSRepo* fs_repo;
retVal = ipfs_repo_fsrepo_new(path, repo_config, &fs_repo);
if (retVal == 0)
return 0;
// this builds a new repo
retVal = ipfs_repo_fsrepo_init(fs_repo);
if (retVal == 0) {
ipfs_repo_fsrepo_free(fs_repo);
return 0;
}
// clean up
ipfs_repo_fsrepo_free(fs_repo);
// make sure the repository exists
retVal = os_utils_filepath_join(path, "config", currDirectory, 1024);
if (retVal == 0)
return 0;
retVal = os_utils_file_exists(currDirectory);
return retVal;
}
int drop_and_build_repository(const char* path) {
return make_ipfs_repository(path); return make_ipfs_repository(path);
} }