TkN 2.1
Toolkit for Nuclei
tksystem.cpp
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#include <dirent.h>
38#include <cerrno>
39#include <array>
40
41#include "tksystem.h"
42#include "tklog.h"
43
44namespace tkn {
53}
54
55using namespace tkn;
56
57tksystem *tksystem::g_tksystem = nullptr;
58
59tksystem *tksystem::the_tksystem()
60{
61 if ( g_tksystem == nullptr ) {
62 g_tksystem = new tksystem();
63 }
64
65 return g_tksystem;
66}
67
68int tksystem::getdir(tkstring _dir, std::vector<tkstring> &_files)
69{
70 glog.set_class("tksystem");
71 glog.set_method(tkstring::form("getdir(%s, std::vector<tkstring> &_files)",_dir.data()));
72
73 DIR *dp;
74 struct dirent *dirp;
75 if((dp = opendir(_dir.c_str())) == nullptr) {
76 glog << error_v << " error opening " << _dir << do_endl;
77 glog.clear();
78 return errno;
79 }
80
81 while ((dirp = readdir(dp)) != nullptr) {
82 _files.emplace_back(tkstring(dirp->d_name));
83 }
84 closedir(dp);
85
86 glog.clear();
87 return 0;
88}
89
90bool tksystem::exists(tkstring _file_name)
91{
92 glog.set_class("tksystem");
93 glog.set_method(tkstring::form("exists(%s)",_file_name.data()));
94
95 tkstring path = _file_name.remove_last_occurence("/");
96 tkstring name = _file_name.get_last_occurence("/");
97
98 if(path==name) path = "./";
99
100 DIR *dp;
101 struct dirent *dirp;
102 if((dp = opendir(path.c_str())) == nullptr) {
103 // glog << error_v << " error opening " << path << do_endl;
104 // glog.clear();
105 return false;
106 }
107
108 bool found = false;
109 while ((dirp = readdir(dp)) != nullptr) {
110 if(!strcmp(dirp->d_name,name.data())) found = true;
111 }
112 closedir(dp);
113
114 glog.clear();
115 return found;
116}
117
118
119void tksystem::list_files(const tkstring &_dir, const tkstring &_pattern) {
120 std::vector<tkstring> files;
121 getdir(_dir,files);
122 for(const auto &file: files) {
123 if(file.match(_pattern)) std::cout << " -- " << file << std::endl;
124 }
125}
126
127std::vector<tkstring> tksystem::get_list_of_files(const tkstring &_dir, const tkstring &_pattern) {
128 std::vector<tkstring> files;
129 getdir(_dir,files);
130 for(size_t ifile=0 ; ifile<files.size() ; ifile++) {
131 if(!files.at(ifile).match(_pattern)) {
132 files.erase(files.begin()+ifile);
133 ifile--;
134 }
135 }
136 return files;
137}
138
139int tksystem::remove_dir(const tkstring &_dirname, bool _force)
140{
141 if(_force) return system(tkstring::form("rm -rf %s",_dirname.data()));
142 return system(tkstring::form("rm -r %s",_dirname.data()));
143}
144
145int tksystem::remove_file(const tkstring &_filename, bool _force)
146{
147 if(_force) return system(tkstring::form("rm -f %s",_filename.data()));
148 return system(tkstring::form("rm %s",_filename.data()));
149}
150
151int tksystem::make_dir(const tkstring &_dirname, bool _recursive)
152{
153 if(_recursive) return system(tkstring::form("mkdir -p %s",_dirname.data()));
154 return system(tkstring::form("mkdir %s",_dirname.data()));
155}
156
157int tksystem::move(const tkstring &_from,const tkstring &_to)
158{
159 return system(tkstring::form("mv %s %s",_from.data(),_to.data()));
160}
161
162int tksystem::unzip_file(const tkstring &_filename, tkstring _outputdir)
163{
164 if(_outputdir.is_empty()) return system(tkstring::form("unzip %s",_filename.data()));
165 return system(tkstring::form("unzip %s -d %s/",_filename.data(),_outputdir.data()));
166}
167
168int tksystem::ls_dir(const tkstring &_dirname, tkstring _option)
169{
170 return system(tkstring::form("ls %s %s",_option.data(),_dirname.data()));
171}
172
173int tksystem::download_file(const tkstring &_filename, const tkstring &_outputdir, bool _replace_existing, bool _use_filename)
174{
175 tkstring command = "wget -q ";
176 if(!_replace_existing) command += "-N ";
177 command += _filename;
178 if(!_outputdir.is_empty()) {
179 if(_use_filename) command += " -O ";
180 else command += " -P ";
181 command += _outputdir;
182 }
183
184 return system(command.data());
185}
186
188{
189 std::array<char, 128> buffer{};
190 tkstring result;
191 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.data(), "r"), pclose);
192 if (!pipe) {
193 throw std::runtime_error("popen() failed!");
194 }
195 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
196 result += buffer.data();
197 }
198 return result;
199}
200
201#ifdef HAS_ROOT
203ClassImp(tksystem);
204#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
static const char * form(const char *_format,...)
Definition: tkstring.cpp:431
tkstring get_last_occurence(const char *_s1)
Definition: tkstring.cpp:329
bool is_empty() const
Definition: tkstring.h:163
tkstring remove_last_occurence(const char *_s1)
Definition: tkstring.cpp:337
Interface to the system (tested on linux and mac os)
Definition: tksystem.h:50
int make_dir(const tkstring &_dirname, bool _recursive=true)
Definition: tksystem.cpp:151
bool exists(tkstring _file_name)
Definition: tksystem.cpp:90
int unzip_file(const tkstring &_filename, tkstring _outputdir="")
Definition: tksystem.cpp:162
int remove_file(const tkstring &_filename, bool _force=false)
Definition: tksystem.cpp:145
std::vector< tkstring > get_list_of_files(const tkstring &_dir, const tkstring &_pattern="*")
Definition: tksystem.cpp:127
int remove_dir(const tkstring &_dirname, bool _force=false)
Definition: tksystem.cpp:139
int ls_dir(const tkstring &_dirname, tkstring _option="-l")
Definition: tksystem.cpp:168
int getdir(tkstring _dir, std::vector< tkstring > &_files)
Definition: tksystem.cpp:68
int move(const tkstring &_from, const tkstring &_to)
Definition: tksystem.cpp:157
void list_files(const tkstring &_dir, const tkstring &_pattern)
Definition: tksystem.cpp:119
int download_file(const tkstring &_filename, const tkstring &_outputdir="", bool _replace_existing=false, bool _use_filename=false)
Definition: tksystem.cpp:173
tkstring get_system_command_output(tkstring command)
Definition: tksystem.cpp:187
Definition: tklog.cpp:39
tklog & error_v(tklog &log)
Definition: tklog.h:430
tklog & do_endl(tklog &log)
Definition: tklog.h:235