diff --git a/Makefile b/Makefile index a53a427..eace40a 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ default: - g++ -lrpm -I/usr/include/rpm -lrpmio -o listrpm main.cpp + g++ -g -o mkauto_inst main.cpp -lstdc++fs diff --git a/auto_inst.files/auto_inst.cfg.pl.enabledmedia b/auto_inst.files/auto_inst.cfg.pl.enabledmedia deleted file mode 100644 index f92971b..0000000 --- a/auto_inst.files/auto_inst.cfg.pl.enabledmedia +++ /dev/null @@ -1,8 +0,0 @@ - 'enabled_media' => [ - 'Core Release', - 'Core Updates', - 'Nonfree Release', - 'Nonfree Updates', - 'Tainted Release', - 'Tainted Updates' - ], diff --git a/auto_inst.files/auto_inst.cfg.pl.header b/auto_inst.files/auto_inst.cfg.pl.header index 071af03..30b3a8b 100644 --- a/auto_inst.files/auto_inst.cfg.pl.header +++ b/auto_inst.files/auto_inst.cfg.pl.header @@ -13,4 +13,9 @@ $o = { 'shadow' => 1 }, 'autoExitInstall' => '1', + 'interactiveSteps' => [ + 'doPartitionDisks', + 'formatPartitions', + 'summary' + ], 'default_packages' => [ diff --git a/auto_inst.files/auto_inst.cfg.pl.interactivestep b/auto_inst.files/auto_inst.cfg.pl.interactivestep deleted file mode 100644 index 10e9ac2..0000000 --- a/auto_inst.files/auto_inst.cfg.pl.interactivestep +++ /dev/null @@ -1,5 +0,0 @@ - 'interactiveSteps' => [ - 'doPartitionDisks', - 'formatPartitions', - 'summary' - ], diff --git a/auto_inst.files/auto_inst.cfg.pl.users b/auto_inst.files/auto_inst.cfg.pl.users deleted file mode 100644 index 3f4f887..0000000 --- a/auto_inst.files/auto_inst.cfg.pl.users +++ /dev/null @@ -1,13 +0,0 @@ - 'users' => [ - { - 'gid' => '1000', - 'groups' => [], - 'icon' => 'default', - 'name' => '', - 'password' => '', - 'realname' => undef, - 'shell' => '/bin/bash', - 'uid' => '1000' - } - ] - }; diff --git a/config/savedir.conf b/config/savedir.conf new file mode 100644 index 0000000..549172e --- /dev/null +++ b/config/savedir.conf @@ -0,0 +1,9 @@ +/var/lib/mysql +/var/lib/bluetooth +/var/lib/flatpak +/var/lib/upower +/var/www +/opt +/usr/local +/srv +/etc/cron.* diff --git a/main.cpp b/main.cpp index 2644edd..cf3943b 100644 --- a/main.cpp +++ b/main.cpp @@ -13,100 +13,208 @@ #include #include #include -#include +#include #include - -//#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace fs = std::filesystem; +#include using namespace std; +namespace fs = experimental::filesystem; // Global variables int verbose = false; +string tab = "\t\t\t\t\t"; +bool verif = false; +bool list = false; +bool mountpoints = false; +bool partitions = false; +bool generateauto_inst = false; + void help(char * cmd) { printf("Usage: %s [-lavVf]\n", cmd); - printf("-l List of the first level packages installed\n"); - printf("-f Verify the modified files and save them\n"); - printf("-a Execute all commands\n" ); - printf("-V Display the version of this software\n"); - printf("-h Display this help\n"); - printf("-v verbose\n"); + printf("-a Execute all commands\n" ); + printf("-f Verify the modified files and save them\n"); + printf("-g Generate auto_inst.cfg.pl\n"); + printf("-h Display this help\n"); + printf("-l List of the first level packages installed\n"); + printf("-m Keep mount points\n"); + printf("-p Keep partitioning\n"); + printf("-v Verbose\n"); + printf("-V Display the version of this software\n"); exit(EXIT_FAILURE); } -void whatprovides(string cmd, vector& pkgs) +void whatprovides(string filepath) { - string pkg; - string command; - string line; - bool erased = false; - array buffer; - vector ::iterator iter; + string cmd = "urpmi_rpm-find-leaves"; + cmd += " >" + filepath; + system(cmd.c_str()); +} - for ( iter = pkgs.begin(); iter < pkgs.end();) +string array2string(array buffer) +{ + int i = 0; + string line; + + while(buffer[i] != '\n' ) { - pkg = string(*iter); - command = cmd + pkg; - printf("Searching whatprovides for %s\n", pkg.c_str()); - unique_ptr pipe(popen(command.c_str(), "r"), pclose); - if (!pipe) { - throw std::runtime_error("popen() failed!"); - } - while (fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) + line.push_back(buffer[i]); + i++; + } + return line; +} + +bool savenonemptydir(string filepath, string needle="*") +{ + int rsyncErrors = 0; + int pos; + string rsyncdestination = "files/"; + string rsynccmd = "rsync -aqP --relative "; + + if (fs::is_directory(filepath)) + { + pos = filepath.find(needle); + if (pos != string::npos and (pos == filepath.length() - 2) and !fs::is_empty(filepath) ) { - printf("=> %s\n", buffer.data()); - line = string(buffer.cbegin(), buffer.cend()); - if(line.find(pkg) == std::string::npos) - { - printf("erasing %s\n", pkg.c_str() ); - pkgs.erase(iter); - erased = true; - break; - } - } - if (!erased) - { - iter++; - }else - { - erased = false; - buffer.fill(0); + cout << filepath << endl; } } + return rsyncErrors; } +bool fstabAnanlyze() +{ + string fstabpath = "/etc/fstab"; + string line; + bool returncode = EXIT_SUCCESS; + int pos = 0; + int posstart = 0; + int npos = 0; + vector fstabarray; + ifstream file; + int nline = 0; + array mountpoints{"/", "/home /var, "}; + + file.open(fstabpath); + if(file.good()) + { + while (getline(file, line)) + { + while ((pos = line.find(" ") != string::npos)) + { + + fstabarray.push_back(line.substr(posstart, pos - posstart)); + posstart = pos; + } + npos = 0; + if () + + } + }else + { + cout << "Error opening " + fstabpath << endl; + exit(EXIT_FAILURE); + } + + + + return returncode; +} +bool savedirs() +{ + bool returncode = false; + string filepath; + string line; + string rsyncdestination = "files/"; + string rsynccmd = "rsync -aqP --relative -m "; + int rsyncErrors = 0; + + ifstream confFile("config/savedir.conf"); + if (confFile.is_open()) + { + while (getline(confFile, line)) + { + filepath = line; + //if (fs::exists(filepath)) + //{ + if (verbose) cout << "Directory to save: " + filepath << endl; + rsynccmd = "rsync -aqP --relative " + filepath + " " + rsyncdestination; + if (verbose) cout << "rsync command: " + rsynccmd << endl; + if (system(rsynccmd.c_str()) != 0) + { + rsyncErrors ++; + } + //}else cout << filepath << "not exists"<< endl; + } + }else + { + cout << "can't open config/savedir.conf" << endl; + exit(EXIT_FAILURE); + } + return returncode; +} + +int recursedir(string dir, string needle, function func) +{ + int returncode = EXIT_SUCCESS; + string filepath; + + for (const auto & file : fs::recursive_directory_iterator(dir)) + { + filepath = (string ) file.path(); + cout << filepath << endl; + returncode |= func(dir, needle); + } + return returncode; +} + +/* +int saveOptLocal() +{ + int result; + int rsyncErrors = EXIT_SUCCESS; + string cmd; + string rsynccmd = "rsync -aqP --relative -m "; + string rsyncdestination = "files/"; + + for(auto var : dirlist) + { + cout << "Saving" + var << endl; + cmd = rsynccmd + var + " " + rsyncdestination; + if (verbose) printf("rsync command: %s\n", cmd.c_str()); + if ((result = system(cmd.c_str())) != 0) + { + rsyncErrors ++; + } + } + return rsyncErrors; +} +*/ + void verification() { int result; int i = 0; int rsyncErrors = 0; - array buffer; + array buffer; string cmd = "rpm -a --verify"; string line; string filepath; string rsyncdestination = "files/"; - string rsynccmd = "rsync -aqP"; + string rsynccmd = "rsync -aqP --relative "; + string etcdir = "/etc"; + string dir; + int log; + + + if (verbose) cout << "save '.d' non empty directories"; + recursedir(etcdir, ".d",&savenonemptydir); + savedirs(); printf("Verifying packages installed\n"); + flush(cout); unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); if (!pipe) { @@ -114,90 +222,308 @@ void verification() } while(fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) { - i = 0; - while(buffer[i] != '\n' ) - { - line.push_back(buffer[i]); - i++; - } - - printf("line: %s<=====\n", line.c_str()); - if (line.at(2) == '5') + line = array2string(buffer); + log = line.find(".log"); + //cout << "log = " << line.length() << " ---> "; + //printf("%s\n", line.c_str()); + if (line.at(2) == '5' and log != (line.length() - 4)) { line.erase(0, 13); - if (verbose) cout << "===>>>>" << line.c_str() << "\n"; - rsynccmd = "rsync -aqP --relative " + line + " " + rsyncdestination; - /*rsynccmd.append("rsync -aqP"); - rsynccmd.append(line); - rsynccmd.append(" "); - rsynccmd.append(rsyncdestination);*/ - if (verbose) printf("%s<====\n", rsynccmd.c_str()); - if ((result = system(rsynccmd.c_str())) != 0) + if (verbose) cout << "file to save: " + line<< endl; + cmd = rsynccmd + line + " " + rsyncdestination; + if (verbose) printf("rsync command: %s\n", cmd.c_str()); + if ((result = system(cmd.c_str())) != 0) { + printf("Rsync error: %i\n", result); rsyncErrors ++; } } buffer.fill(0); - line.clear(); - rsynccmd.clear(); } + + // save all non empties directories finishing by ".d" in /etc if (rsyncErrors != 0) printf("rsync returned %i errors", rsyncErrors); } -int mkauto_inst() +bool enabledmedias(string filename) { - bool returnCode = 0; - string dirpath = "auto_inst.files"; + bool returnCode = EXIT_SUCCESS; + bool start = false; + bool flag = false; + bool ignore = false; + string temp = ""; + string medias = "\t\t'enabled_media' => [\n"; + string line; + int i = 0; + printf("searching enabled medias\n"); + ifstream file("/etc/urpmi/urpmi.cfg"); + if (file.is_open()) + { + while (getline(file, line)) + { + //printf("%s\n", line.c_str()); + //cout << "start =" << start; + if (start == false) + { + if (line.front() != '{' and line.front() != '\0' and line.front() != '}') + { + //cout << "passed"; + start = true; + flag = false; + i = 0; + do + { + i = line.find(" ", i+1); + if (line[i-1] == '\\') + { + flag = true; + line.erase(i-1, 1); + }else + { + break; + } + }while(flag == true); + line.erase(i); + temp = line; + printf("%s", temp.c_str()); + line.clear(); + }else + { + continue; + } + }else + { array buffer; + if (line.front() == '}') + { + if (!temp.empty() and ignore != true) + { + cout << " ==> adding to medias list\n"; + medias += tab + temp + ",\n"; + }else + { + cout << endl; + } + start = false; + temp.clear(); + ignore = false; + continue; + } + if (line.find("ignore") != string::npos) + { + //cout << "found 'ignore'\n"; + ignore = true; + } + } + } + printf("Medias:\n%s\n", medias.c_str()); + file.close(); + medias += tab + "],\n"; + ofstream file(filename, ofstream::app); + file << medias.c_str(); + file.close(); + }else return EXIT_FAILURE; return returnCode; } +bool keyboardlayout(string filename) +{ + bool returncode = EXIT_SUCCESS; + int i; + string cmd = "setxkbmap -query"; + string line; + string str; + array buffer; + + printf("looking for keyboard layout\n"); + flush(cout); + unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + while(fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) + { + line = array2string(buffer); + cout << line << endl; + i = line.find("layout:"); + if (i != string::npos) + { + i = line.find_last_of(' '); + line.erase(0, i+1); + break; + } + cout << line << endl; + line.clear(); + buffer.fill(0); + } + cout << "result =>" << line << endl; + ofstream file(filename, ofstream::app); + if(file.is_open()) + { + str = "\t\t'keyboard' => {\n" + tab + "'GRP_TOGGLE' => '',\n" + tab + "'KEYBOARD' => '" + line + "'\n" + tab + "},\n"; + file << str; + file.close(); + }else return EXIT_FAILURE; + return returncode; +} + +string getvalue( string line) +{ + string value; + int i; + + if ((i = line.find("=")) != string::npos) + { + value = line.substr(i+1, string::npos); + } + return value; +} + +bool mklocale(string filename) +{ + string localeconf = "/etc/locale.conf"; + bool returncode = EXIT_SUCCESS; + int i; + int flag = 0; + int utf8 = 0; + string country; + string lang; + string line; + string str; + string value; + vector languages; + + ifstream ifile (localeconf); + ofstream ofile (filename, ofstream::app); + if (ifile.is_open() and ofile.is_open()) + { + while (getline(ifile, line)) + { + value = getvalue(line); + //cout << "Value ===>" + value << endl; + if((i = line.find("COUNTRY")) != string::npos) + { + country = value; + }else if((i = line.find("LANG=")) != string::npos) + { + lang = value.substr(0, 2); + if (value.find("UTF-8") != string::npos) + { + utf8 = 1; + } + }else if((i = line.find("LANGUAGE")) != string::npos) + { + i = value.find(":"); + languages.push_back(value.substr(i+1,string::npos)); + } + } + } + ofstream file(filename, ofstream::app); + str = "\t\t'locale' => {\n" + tab + "'IM' => undef,\n" + tab + "'country' => '" + country + "'\n" + tab + "'lang' => '" + lang + "'\n"; + str += tab + "'langs' => {\n"; + //for(auto & elem : languages) + flag = 0; + for(i=0; i< languages.size(); i++) + { + if (flag == 0) + { + str += tab + "\t\t'" + languages[i] + "' => 1"; + }else + { + str += ",\n" + tab + "\t\t'" + languages[i] + "' => 1"; + } + } + str += "\n" + tab + "\t\t},\n"; + str += tab + "'utf8' => 1\n" + tab + "},\n"; + file << str; + file.close(); + return returncode; +} + +bool miscellaneous(string filename) +{ + bool returncode = EXIT_SUCCESS; + int r; + bool numlock = 0; + string str; + + r = system("systemctl status numlock"); + if (r == 0) + { + numlock = 1; + } + str = "\t\t'miscellaneous' => {\n" + tab + "'numlock' => " + to_string(numlock) + ",\n" + tab + "'HDPARM' => 1,\n\t\t},\n"; + ofstream file(filename, ofstream::app); + file << str; + file.close(); + + return returncode; +} + +int mkauto_inst(string pkgsfile) +{ + bool returnCode = EXIT_SUCCESS; + bool flag =false; + string dirpath = "auto_inst.files"; + string auto_inst_file = "auto_inst.cfg.pl"; + string header = "auto_inst.cfg.pl.header"; + string users = "auto_inst.cfg.pl.users"; + string footer = "auto_inst.cfg.pl.footer"; + string cmd = "cp " + dirpath + "/" + header + " ./" + auto_inst_file; + int i = 0; + string line; + cout << "\n\n" + cmd << endl; + returnCode |= system(cmd.c_str()); + + cout << "copying packages\n"; + ifstream ifile(pkgsfile); + ofstream ofile("./" + auto_inst_file, ofstream::app); + if (ifile.is_open() and ofile.is_open()) + { + while (getline(ifile, line)) + { + + if (flag == false) + { + flag = true; + line = tab + line; + ofile << line; + }else + { + line = ",\n" + tab + line; + ofile << line; + } + } + ofile << "\n" + tab + "],\n"; + ifile.close(); + ofile.close(); + }else + { + cout << "error file not open"; + return 1; + } + + returnCode |= enabledmedias(auto_inst_file); + returnCode |= keyboardlayout(auto_inst_file); + returnCode |= mklocale(auto_inst_file); + returnCode |= miscellaneous(auto_inst_file); + return returnCode; +} + int main(int argc, char** argv) { - - - /*rpmdbMatchIterator mi; - //int type, count; - //char *name; - rpmdb db; - Header h; - rpmtd td; - rpmTagVal result; - rpmts ts = NULL; - rpmds ds = NULL; - const char *DNEVR; -*/ - - struct utsname buffer; - int i = 0; - errno = 0; - bool x64 = 0; - string line; - string tmpfile = "/tmp/installedrpmlist.txt"; + //bool x64 = 0; string pkgsfile = "pkgsfile.txt"; - string rpmcmd = "/usr/bin/rpm -q"; - string rpmfullcmd; - string whatrequirescmd; - size_t found; - string pkg; - vector pkgs; - vector ::iterator iter; - set requires; - set ::iterator reqiter; - ofstream fh; - int verif = false; int opt = 0; - int list = false; - rpmfullcmd = rpmcmd + "a >" + tmpfile; - whatrequirescmd = "urpmq --whatrequires "; + int exitcode = EXIT_SUCCESS; // listing arguments if(argc>1) { - while((opt = getopt(argc, argv, "valVf")) != -1) + while((opt = getopt(argc, argv, "valmpVfg")) != -1) { switch (opt) { @@ -210,10 +536,20 @@ int main(int argc, char** argv) case 'a': list = true; verif = true; + generateauto_inst = true; break; case 'v': verbose = true; break; + case 'g': + generateauto_inst = true; + list = true; + break; + case 'm': + mountpoints = true; + break; + case 'p': + partitions = true; default: help(argv[0]); } @@ -223,12 +559,7 @@ int main(int argc, char** argv) cout << "no args\n"; help(argv[0]); } - if (verbose == true) - { - printf("%s\n", rpmfullcmd.c_str()); - printf("%s\n", whatrequirescmd.c_str()); - } - +/* // getting processeur architecture if (uname(&buffer) < 0) { @@ -240,72 +571,19 @@ int main(int argc, char** argv) x64 = true; } if (verbose) printf("architecture is %s\n", buffer.machine); +*/ if (list == true) { - // launching rpm -qa ... - if (system(rpmfullcmd.c_str()) != 0) - { - cout << "system return an error\n"; - exit(EXIT_FAILURE); - } - - // opening file generated by rpm -qa - cout << "Opening file generated by rpm -qa\n"; - ifstream myfile (tmpfile); - if (myfile.is_open()) - { - while ( getline (myfile,line) ) - { - found = line.find_last_of("-"); // searching first "." delimiting version string - found = line.find_last_of("-", found - 1); - if (found != std::string::npos) - { - line.erase(found, string::npos); // erasing version string - } - /*found = line.find_last_of("-"); - if (found != std::string::npos) - { - line.erase(found, string::npos); // erasing version string - }*/ - pkgs.push_back(line); // inserting line in array (set) - } - myfile.close(); - }else - { - cout << "Unable to open file\n"; - exit(EXIT_FAILURE); - } - /* - for ( iter = pkgs.begin(); iter < pkgs.end();) - { - found = string(*iter).find("lib"); - if (found != string::npos and found == 0) - { - printf("erasing %s\n", string(*iter).c_str() );printf("\n"); - pkgs.erase(iter); - }else - { - iter++; - } - } - */ - // searching whatrequires for every pkg - - whatprovides(whatrequirescmd, pkgs); - // saving to file - if (verbose) cout << "Saving result to file" << pkgsfile << "\n"; - fh.open(pkgsfile); - for( iter = pkgs.begin(); iter < pkgs.end(); ++iter) - { - fh << *iter << " "; - } - fh.close(); + whatprovides(pkgsfile); } if( verif == true) { verification(); } - + if(generateauto_inst == true) + { + mkauto_inst(pkgsfile); + } cout << "finished\n"; - exit(EXIT_SUCCESS); + exit(exitcode); } diff --git a/listrpm.kdev4 b/mkauto_inst.kdev4 similarity index 70% rename from listrpm.kdev4 rename to mkauto_inst.kdev4 index b175ec1..b95be83 100644 --- a/listrpm.kdev4 +++ b/mkauto_inst.kdev4 @@ -1,3 +1,3 @@ [Project] Manager=KDevCustomMakeManager -Name=listrpm +Name=mkauto_inst