Finally sorted config file directory rules

If we are trying to build a repository in the home directory of
the user, put everything in a .ipfs directory. Otherwise, use
what was given, even if it does not have a .ipfs suffix.
This commit is contained in:
jmjatlanta 2017-09-21 11:49:47 -05:00
parent 0eab9cc3fc
commit e094528293
3 changed files with 16 additions and 39 deletions

View file

@ -150,16 +150,6 @@ int parse_arguments(int argc, char** argv) {
* The beginning * The beginning
*/ */
int main(int argc, char** argv) { int main(int argc, char** argv) {
// for debugging
libp2p_logger_add_class("null");
libp2p_logger_add_class("bitswap");
libp2p_logger_add_class("secio");
libp2p_logger_add_class("peer_request_queue");
libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("peerstore");
libp2p_logger_add_class("dht_protocol");
libp2p_logger_add_class("peer");
libp2p_logger_add_class("lmdb_datastore");
strip_quotes(argc, argv); strip_quotes(argc, argv);
// CliArguments is the new way to do it. Eventually, all will use this structure // CliArguments is the new way to do it. Eventually, all will use this structure

View file

@ -125,6 +125,9 @@ int repo_config_write_config_file(char* full_filename, struct RepoConfig* config
return 1; return 1;
} }
// forward declaration, actual is in init.c
char* ipfs_repo_get_home_directory(int argc, char** argv);
/** /**
* constructs the FSRepo struct. * constructs the FSRepo struct.
* Remember: ipfs_repo_fsrepo_free must be called * Remember: ipfs_repo_fsrepo_free must be called
@ -137,28 +140,13 @@ int ipfs_repo_fsrepo_new(const char* repo_path, struct RepoConfig* config, struc
*repo = (struct FSRepo*)malloc(sizeof(struct FSRepo)); *repo = (struct FSRepo*)malloc(sizeof(struct FSRepo));
if (repo_path == NULL) { if (repo_path == NULL) {
// get the user's home directory char* ipfs_path = ipfs_repo_get_home_directory(0, NULL);
char* ipfs_path = os_utils_getenv("IPFS_PATH"); (*repo)->path = malloc(strlen(ipfs_path) + 1);
if (ipfs_path == NULL)
ipfs_path = os_utils_get_homedir();
char* default_subdir = "/.ipfs";
unsigned long newPathLen = 0;
if (strstr(ipfs_path, default_subdir) != NULL) {
newPathLen = strlen(ipfs_path) + 1;
} else {
// add /.ipfs to the string
newPathLen = strlen(ipfs_path) + strlen(default_subdir) + 2; // 1 for slash and 1 for end
}
(*repo)->path = malloc(sizeof(char) * newPathLen);
if ((*repo)->path == NULL) { if ((*repo)->path == NULL) {
free( (*repo)); free( (*repo));
return 0; return 0;
} }
if (strstr(ipfs_path, default_subdir) != NULL) { strcpy((*repo)->path, ipfs_path);
strcpy((*repo)->path, ipfs_path);
} else {
os_utils_filepath_join(ipfs_path, default_subdir, (*repo)->path, newPathLen);
}
} else { } else {
int len = strlen(repo_path) + 1; int len = strlen(repo_path) + 1;
(*repo)->path = (char*)malloc(len); (*repo)->path = (char*)malloc(len);

View file

@ -10,11 +10,13 @@
* The basic functions for initializing an IPFS repo * The basic functions for initializing an IPFS repo
*/ */
static char config_dir_path[512];
/** /**
* Get the correct repo home directory. This first looks at the * Get the correct repo home directory. This first looks at the
* command line, then the IPFS_PATH environment variable, * command line, then the IPFS_PATH environment variable,
* then the user's home directory. This is where the .ipfs directory * then the user's home directory. This should come back with a
* is or will be. * directory that contains a config file.
* @param argc number of command line parameters * @param argc number of command line parameters
* @param argv command line parameters * @param argv command line parameters
* @returns the repo home directory * @returns the repo home directory
@ -35,7 +37,12 @@ char* ipfs_repo_get_home_directory(int argc, char** argv) {
result = os_utils_getenv("IPFS_PATH"); result = os_utils_getenv("IPFS_PATH");
} }
if (result == NULL) { // not on command line nor environment var. if (result == NULL) { // not on command line nor environment var.
// get user's home directory (or current directory depending on platform),
// and add .ipfs to it
result = os_utils_get_homedir(); result = os_utils_get_homedir();
strcpy(config_dir_path, result);
strcat(config_dir_path, "/.ipfs");
result = config_dir_path;
} }
return result; return result;
} }
@ -49,15 +56,7 @@ char* ipfs_repo_get_home_directory(int argc, char** argv) {
* @returns true(1) if the directory is there, false(0) if it is not. * @returns true(1) if the directory is there, false(0) if it is not.
*/ */
int ipfs_repo_get_directory(int argc, char** argv, char** repo_dir) { int ipfs_repo_get_directory(int argc, char** argv, char** repo_dir) {
char* home = ipfs_repo_get_home_directory(argc, argv); *repo_dir = ipfs_repo_get_home_directory(argc, argv);
// it shouldn't include the .ipfs directory, but if it does, we're done
if (strstr(home, ".ipfs") == NULL) {
int dir_len = strlen(home) + 7;
*repo_dir = malloc(dir_len);
os_utils_filepath_join(home, ".ipfs", *repo_dir, dir_len);
} else {
*repo_dir = home;
}
return os_utils_directory_exists(*repo_dir); return os_utils_directory_exists(*repo_dir);
} }