From 5008cbd8ded1f7ccb4f36eec909894d0e11afe47 Mon Sep 17 00:00:00 2001 From: Jason Hiser <jdhiser@gmail.com> Date: Sat, 16 Feb 2019 12:10:23 -0500 Subject: [PATCH] create install directory if not already exists --- pedi.cpp | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/pedi.cpp b/pedi.cpp index 7507efa..16455c6 100644 --- a/pedi.cpp +++ b/pedi.cpp @@ -17,6 +17,33 @@ using namespace std; +int mkpath(const string& path, const mode_t& mode) +{ + auto s=path; + auto pos=(size_t)0; + + if(s[s.size()-1]!='/') + { + // force trailing / so we can handle everything in loop + s+='/'; + } + + // Caution! side effect in the while loop! + while((pos=s.find_first_of('/',pos))!=string::npos) + { + auto dir=s.substr(0,pos++); + if(dir.size()==0) + continue; // if leading / first time is 0 length + + auto mdret=mkdir(dir.c_str(),mode); + if(mdret!=0 && errno!=EEXIST) + { + return mdret; + } + } + return 0; +} + // Parse some options for the transform static struct option long_options[] = { {"setup", no_argument, 0, 's'}, @@ -516,21 +543,6 @@ void read_configs() auto orig_configfile=trimmed(mf)+".config"; read_config(orig_configfile, my_options.install_dir, my_options.labels); - -#if 0 - for(const auto & s : manifest_files) - { - string install_dir, labels; - auto configfile=trimmed(s)+".config"; - read_config(configfile, install_dir, labels); - - if(my_options.install_dir != install_dir || my_options.labels != labels) - { - cerr<<"Mismatch configuration in "<<orig_configfile<<" and "<<configfile<<endl; - exit(1); - } - } -#endif } auto previously_had_pedi_root=false; @@ -640,6 +652,12 @@ void finish() { if(!configs_read || previously_had_pedi_root) { + auto mkpath_err=mkpath(my_options.install_dir, S_IRUSR | S_IWUSR | S_IXUSR); + if(mkpath_err != 0 ) + { + cout << " Could not make directory " << my_options.install_dir << endl; + exit(1); + } ofstream o(my_options.install_dir+"/"+".pedi_root"); if(my_options.verbose) cout<<"Writing pedi_root file."<<endl; -- GitLab