Correctly parsing command line parameters for ipfs add

This commit is contained in:
John Jones 2017-04-06 19:05:30 -05:00
parent 794608a7ea
commit 5d558f5229
2 changed files with 55 additions and 22 deletions

View file

@ -145,7 +145,7 @@ void *ipfs_null_listen (void *ptr)
exit (1);
}
libp2p_logger_log("null", LOGLEVEL_ERROR, "Null listening on %d\n", listen_param->port);
libp2p_logger_log("null", LOGLEVEL_ERROR, "Ipfs listening on %d\n", listen_param->port);
for (;;) {
s = socket_accept4(socketfd, &(listen_param->ipv4), &(listen_param->port));

View file

@ -294,6 +294,58 @@ int ipfs_import_file(const char* root_dir, const char* fileName, struct Node** p
return 1;
}
/**
* Pulls list of files from command line parameters
* @param argc number of command line parameters
* @param argv command line parameters
* @returns a FileList linked list of filenames
*/
struct FileList* ipfs_import_get_filelist(int argc, char** argv) {
struct FileList* first = NULL;
struct FileList* last = NULL;
int skipNext = 0;
for (int i = 2; i < argc; i++) {
if (skipNext) {
skipNext = 0;
continue;
}
if (strcmp(argv[i], "-r") == 0) {
continue;
}
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {
skipNext = 1;
continue;
}
struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList));
current->next = NULL;
current->file_name = argv[i];
// now wire it in
if (first == NULL) {
first = current;
}
if (last != NULL) {
last->next = current;
}
// now set last to current
last = current;
}
return first;
}
/**
* See if the recursive flag was passed on the command line
* @param argc number of command line parameters
* @param argv command line parameters
* @returns true(1) if -r was passed, false(0) otherwise
*/
int ipfs_import_is_recursive(int argc, char** argv) {
for(int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-r") == 0)
return 1;
}
return 0;
}
/**
* called from the command line to import multiple files or directories
@ -308,29 +360,10 @@ int ipfs_import_files(int argc, char** argv) {
* param 3: directoryname
*/
struct FSRepo* fs_repo = NULL;
struct FileList* first = NULL;
struct FileList* last = NULL;
int recursive = 0; // false
int recursive = ipfs_import_is_recursive(argc, argv);
// parse the command line
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-r") == 0) {
recursive = 1;
} else {
struct FileList* current = (struct FileList*)malloc(sizeof(struct FileList));
current->next = NULL;
current->file_name = argv[i];
// now wire it in
if (first == NULL) {
first = current;
}
if (last != NULL) {
last->next = current;
}
// now set last to current
last = current;
}
}
struct FileList* first = ipfs_import_get_filelist(argc, argv);
// open the repo
char* repo_path = NULL;