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
*/
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);
// 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;
}
// forward declaration, actual is in init.c
char* ipfs_repo_get_home_directory(int argc, char** argv);
/**
* constructs the FSRepo struct.
* 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));
if (repo_path == NULL) {
// get the user's home directory
char* ipfs_path = os_utils_getenv("IPFS_PATH");
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);
char* ipfs_path = ipfs_repo_get_home_directory(0, NULL);
(*repo)->path = malloc(strlen(ipfs_path) + 1);
if ((*repo)->path == NULL) {
free( (*repo));
return 0;
}
if (strstr(ipfs_path, default_subdir) != NULL) {
strcpy((*repo)->path, ipfs_path);
} else {
os_utils_filepath_join(ipfs_path, default_subdir, (*repo)->path, newPathLen);
}
strcpy((*repo)->path, ipfs_path);
} else {
int len = strlen(repo_path) + 1;
(*repo)->path = (char*)malloc(len);

View file

@ -10,11 +10,13 @@
* 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
* command line, then the IPFS_PATH environment variable,
* then the user's home directory. This is where the .ipfs directory
* is or will be.
* then the user's home directory. This should come back with a
* directory that contains a config file.
* @param argc number of command line parameters
* @param argv command line parameters
* @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");
}
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();
strcpy(config_dir_path, result);
strcat(config_dir_path, "/.ipfs");
result = config_dir_path;
}
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.
*/
int ipfs_repo_get_directory(int argc, char** argv, char** repo_dir) {
char* home = 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;
}
*repo_dir = ipfs_repo_get_home_directory(argc, argv);
return os_utils_directory_exists(*repo_dir);
}