2016-10-27 03:32:23 +00:00
# include <stdlib.h>
2016-10-27 18:11:34 +00:00
# include <stdio.h>
2017-09-20 17:39:26 +00:00
# include <pthread.h>
2016-10-27 03:32:23 +00:00
2016-10-31 16:13:42 +00:00
# include "ipfs/cmd/ipfs/init.h"
2016-11-10 13:28:51 +00:00
# include "ipfs/commands/request.h"
2016-10-31 16:13:42 +00:00
# include "ipfs/commands/command_option.h"
2017-04-06 14:33:28 +00:00
# include "libp2p/os/utils.h"
2016-10-31 16:13:42 +00:00
# include "ipfs/core/ipfs_node.h"
# include "ipfs/core/builder.h"
# include "ipfs/repo/config/config.h"
# include "ipfs/repo/fsrepo/fs_repo.h"
2016-10-27 01:14:07 +00:00
2016-10-27 18:11:34 +00:00
const int nBitsForKeypairDefault = 2048 ;
/***
* runs before major processing during initialization
* @ param request the request
* @ returns 0 if a problem , otherwise a 1
*/
int init_pre_run ( struct Request * request ) {
//TODO: make sure daemon is not running
2016-11-10 13:28:51 +00:00
return 1 ;
2016-10-27 18:11:34 +00:00
}
2016-11-10 21:36:34 +00:00
/**
* This actually opens the repo and gets things set up
2016-11-17 20:07:59 +00:00
* @ param repo the repo information
2016-11-10 21:36:34 +00:00
* @ returns true ( 1 ) on success
*/
2016-11-17 20:07:59 +00:00
int initialize_ipns_keyspace ( struct FSRepo * repo ) {
//open fs repo
int retVal = ipfs_repo_fsrepo_open ( repo ) ;
if ( retVal = = 0 )
return 0 ;
2016-10-27 18:11:34 +00:00
//TODO: make a new node, then close it
//TODO: setup offline routing on new node
2016-12-14 11:25:09 +00:00
struct IpfsNode * ipfs_node = NULL ;
struct Context * ctx = NULL ;
struct BuildCfg * bld_cfg = NULL ;
2016-10-27 18:11:34 +00:00
//TODO: see line 185 of init.go, what does core.BldCfg{Repo: r} do? BldCfg is a structure
2016-11-10 21:36:34 +00:00
retVal = ipfs_core_builder_new_node ( ctx , bld_cfg , ipfs_node ) ;
2016-10-27 18:11:34 +00:00
//return namesys_initialize_keyspace(ctx, ipfs_node->DAG, ipfs_node->Namesys, ipfs_node->pinning, ipfs_node->private_key);
2016-11-10 13:28:51 +00:00
return retVal ;
2016-10-27 18:11:34 +00:00
}
/**
* called by init_run , to do the heavy lifting
2016-10-31 16:13:42 +00:00
* @ param out_file an output stream ( stdout )
2016-11-10 13:28:51 +00:00
* @ param repo_root a path that is where the . ipfs directory will be put
2016-10-27 18:11:34 +00:00
* @ param empty true ( 1 ) if empty , false ( 0 ) if not
2016-10-31 16:13:42 +00:00
* @ param num_bits_for_keypair number of bits for key pair
2016-10-27 18:11:34 +00:00
* @ param conf the configuration struct
* @ returns 0 on error , 1 on success
*/
2016-10-31 16:13:42 +00:00
int do_init ( FILE * out_file , char * repo_root , int empty , int num_bits_for_keypair , struct RepoConfig * conf ) {
// make sure the directory is writable
if ( ! os_utils_directory_writeable ( repo_root ) )
return 0 ;
// verify that it is not already initialized
2016-11-10 13:28:51 +00:00
if ( fs_repo_is_initialized ( repo_root ) )
2016-10-31 16:13:42 +00:00
return 0 ;
2016-10-27 18:11:34 +00:00
//TODO: If the conf is null, make one
2017-07-31 17:50:12 +00:00
if ( conf - > identity - > peer = = NULL | | conf - > identity - > peer - > id = = NULL ) {
2017-04-13 14:31:58 +00:00
int retVal = ipfs_repo_config_init ( conf , num_bits_for_keypair , repo_root , 4001 , NULL ) ;
2016-11-10 13:28:51 +00:00
if ( retVal = = 0 )
return 0 ;
}
// initialize the fs repo
2016-11-17 20:07:59 +00:00
struct FSRepo * repo ;
int retVal = ipfs_repo_fsrepo_new ( repo_root , conf , & repo ) ;
2016-11-10 13:28:51 +00:00
if ( retVal = = 0 )
return 0 ;
2016-11-17 20:07:59 +00:00
retVal = ipfs_repo_fsrepo_init ( repo ) ;
if ( retVal = = 0 )
return 0 ;
2016-10-27 18:11:34 +00:00
//TODO: add default assets
2016-11-17 20:07:59 +00:00
return initialize_ipns_keyspace ( repo ) ;
2016-10-27 18:11:34 +00:00
}
/***
* does major processing during initialization
* @ param request the request
* @ returns 0 if a problem , otherwise a 1
*/
int init_run ( struct Request * request ) {
// TODO: make sure offline
// TODO: check parameters for logic errors
// TODO: Initialize
2016-11-30 16:46:41 +00:00
struct RepoConfig * conf ;
2016-12-14 11:25:09 +00:00
if ( ipfs_repo_config_new ( & conf ) = = 0 )
return 0 ;
2016-10-27 18:11:34 +00:00
// TODO: handle files in request
// do the heavy lifting
2016-11-10 13:28:51 +00:00
int num_bits_for_key_pair = request - > cmd . options [ 0 ] - > default_int_val ;
2016-11-30 16:46:41 +00:00
return do_init ( stdout , request - > invoc_context - > config_root , 1 , num_bits_for_key_pair , conf ) ;
2016-10-27 18:11:34 +00:00
}
/***
* does the cleanup after major processing during initialization
* @ param request the request
* @ returns 0 if a problem , otherwise a 1
*/
int init_post_run ( struct Request * request ) {
// nothing to do
return 1 ;
}
2016-11-10 13:28:51 +00:00
int ipfs_cmd_ipfs_init_command_new ( struct Command * cmd ) {
int retVal = 1 ;
2016-10-27 01:14:07 +00:00
// help text
cmd - > help_text . tagline = " Initializes IPFS config file. " ;
cmd - > help_text . short_description = " \n Initializes IPFS configuration files and generates a new keypair. \n \n ipfs uses a repository in the local file system. By default, the repo is \n located at ~/.ipfs. To change the repo location, set the $IPFS_PATH \n environment variable.: \n \n export IPFS_PATH=/path/to/ipfsrepo " ;
2016-10-27 03:32:23 +00:00
2016-10-27 02:13:32 +00:00
cmd - > argument_count = 1 ;
2016-10-27 18:11:34 +00:00
cmd - > option_count = 2 ;
2016-11-10 13:28:51 +00:00
commands_command_init ( cmd ) ;
2016-10-27 03:32:23 +00:00
// allocate memory for array of pointers
2016-11-10 13:28:51 +00:00
retVal = commands_argument_init ( cmd - > arguments [ 0 ] , " default-config " , 0 , 0 , " Initialize with the given configuration " ) ;
if ( retVal = = 0 )
return 0 ;
2016-10-27 03:32:23 +00:00
cmd - > arguments [ 0 ] - > enable_stdin = 1 ;
2016-10-27 18:11:34 +00:00
// options
cmd - > options [ 0 ] - > name_count = 2 ;
2016-11-10 13:28:51 +00:00
retVal = commands_command_option_init ( cmd - > options [ 0 ] , " Number of bits to use in the generated RSA private key " ) ;
2016-10-27 18:11:34 +00:00
cmd - > options [ 0 ] - > names [ 0 ] = " bits " ;
cmd - > options [ 0 ] - > names [ 1 ] = " b " ;
cmd - > options [ 0 ] - > kind = integer ;
cmd - > options [ 0 ] - > default_int_val = nBitsForKeypairDefault ;
cmd - > options [ 1 ] - > name_count = 2 ;
2016-11-10 13:28:51 +00:00
retVal = commands_command_option_init ( cmd - > options [ 1 ] , " Don't add and pin help files to the local storage " ) ;
2016-10-27 18:11:34 +00:00
cmd - > options [ 1 ] - > default_bool_val = 0 ;
cmd - > options [ 1 ] - > names [ 0 ] = " empty-repo " ;
cmd - > options [ 1 ] - > names [ 1 ] = " e " ;
2016-11-10 13:28:51 +00:00
2016-10-27 18:11:34 +00:00
// function pointers
cmd - > pre_run = init_pre_run ;
cmd - > run = init_run ;
cmd - > post_run = init_post_run ;
2016-10-27 02:13:32 +00:00
return retVal ;
}
2016-11-10 13:28:51 +00:00
/***
* Uninitializes all the dynamic memory caused by get_init_command
* @ param command the struct
* @ returns 0 on failure , otherwise 1
*/
int ipfs_cmd_ipfs_init_command_free ( struct Command * command ) {
// NOTE: commands_command_free takes care of arguments and command_options
commands_command_free ( command ) ;
return 1 ;
}