TkN 2.7
Toolkit for Nuclei
Loading...
Searching...
No Matches
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 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#include <dirent.h>
15#include <cerrno>
16#include <array>
17#include <memory>
18
19#include "tksystem.h"
20#include "tklog.h"
21
22namespace tkn {
31}
32
33using namespace tkn;
34
36{
37 // Function-local static initialisation is guaranteed to be thread-safe
38 // since C++11. Keep process-lifetime ownership, as for the historical
39 // singleton, to avoid static destruction-order issues.
40 static tksystem *instance = new tksystem();
41 return instance;
42}
43
44int tksystem::getdir(tkstring _dir, std::vector<tkstring> &_files)
45{
46 glog.set_class("tksystem");
47 glog.set_method(tkstring::form("getdir(%s, std::vector<tkstring> &_files)",_dir.data()));
48
49 DIR *dp;
50 struct dirent *dirp;
51 if((dp = opendir(_dir.c_str())) == nullptr) {
52 glog << error_v << " error opening " << _dir << do_endl;
53 glog.clear();
54 return errno;
55 }
56
57 while ((dirp = readdir(dp)) != nullptr) {
58 _files.emplace_back(tkstring(dirp->d_name));
59 }
60 closedir(dp);
61
62 glog.clear();
63 return 0;
64}
65
66bool tksystem::exists(tkstring _file_name)
67{
68 glog.set_class("tksystem");
69 glog.set_method(tkstring::form("exists(%s)",_file_name.data()));
70
71 tkstring path = _file_name.remove_last_occurence("/");
72 tkstring name = _file_name.get_last_occurence("/");
73
74 if(path==name) path = "./";
75
76 DIR *dp;
77 const struct dirent *dirp;
78 if((dp = opendir(path.c_str())) == nullptr) {
79 // glog << error_v << " error opening " << path << do_endl;
80 // glog.clear();
81 return false;
82 }
83
84 bool found = false;
85 while ((dirp = readdir(dp)) != nullptr) {
86 if(!strcmp(dirp->d_name,name.data())) found = true;
87 }
88 closedir(dp);
89
90 glog.clear();
91 return found;
92}
93
94
95void tksystem::list_files(const tkstring &_dir, const tkstring &_pattern) {
96 std::vector<tkstring> files;
97 getdir(_dir,files);
98 for(const auto &file: files) {
99 if(file.match(_pattern)) std::cout << " -- " << file << std::endl;
100 }
101}
102
103std::vector<tkstring> tksystem::get_list_of_files(const tkstring &_dir, const tkstring &_pattern) {
104 std::vector<tkstring> files;
105 getdir(_dir,files);
106 for(size_t ifile=0 ; ifile<files.size() ; ifile++) {
107 if(!files.at(ifile).match(_pattern)) {
108 files.erase(files.begin()+ifile);
109 ifile--;
110 }
111 }
112 return files;
113}
114
115int tksystem::remove_dir(const tkstring &_dirname, bool _force)
116{
117 if(_force) return system(tkstring::form("rm -rf %s",_dirname.data()));
118 return system(tkstring::form("rm -r %s",_dirname.data()));
119}
120
121int tksystem::remove_file(const tkstring &_filename, bool _force)
122{
123 if(_force) return system(tkstring::form("rm -f %s",_filename.data()));
124 return system(tkstring::form("rm %s",_filename.data()));
125}
126
127int tksystem::make_dir(const tkstring &_dirname, bool _recursive)
128{
129 if(_recursive) return system(tkstring::form("mkdir -p %s",_dirname.data()));
130 return system(tkstring::form("mkdir %s",_dirname.data()));
131}
132
133int tksystem::move(const tkstring &_from,const tkstring &_to)
134{
135 return system(tkstring::form("mv %s %s",_from.data(),_to.data()));
136}
137
138int tksystem::unzip_file(const tkstring &_filename, tkstring _outputdir)
139{
140 if(_outputdir.is_empty()) return system(tkstring::form("unzip %s",_filename.data()));
141 return system(tkstring::form("unzip %s -d %s/",_filename.data(),_outputdir.data()));
142}
143
144int tksystem::ls_dir(const tkstring &_dirname, tkstring _option)
145{
146 return system(tkstring::form("ls %s %s",_option.data(),_dirname.data()));
147}
148
149int tksystem::download_file(const tkstring &_filename, const tkstring &_outputdir, bool _replace_existing, bool _use_filename)
150{
151 tkstring command = "wget -q --timeout=30 --tries=2 ";
152 if(!_replace_existing) command += "-N ";
153 command += _filename;
154 if(!_outputdir.is_empty()) {
155 if(_use_filename) command += " -O ";
156 else command += " -P ";
157 command += _outputdir;
158 }
159
160 return system(command.data());
161}
162
164{
165 std::array<char, 128> buffer{};
166 tkstring result;
167 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.data(), "r"), pclose);
168 if (!pipe) {
169 throw std::runtime_error("popen() failed!");
170 }
171 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
172 result += buffer.data();
173 }
174 return result;
175}
176
177#ifdef HAS_ROOT
179ClassImp(tksystem);
180#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
static const char * form(const char *_format,...)
Definition tkstring.cpp:438
tkstring get_last_occurence(const char *_s1)
Definition tkstring.cpp:352
bool is_empty() const
Definition tkstring.h:145
tkstring remove_last_occurence(const char *_s1)
Definition tkstring.cpp:360
Interface to the system (tested on linux and mac os)
Definition tksystem.h:27
int make_dir(const tkstring &_dirname, bool _recursive=true)
Definition tksystem.cpp:127
bool exists(tkstring _file_name)
Definition tksystem.cpp:66
int unzip_file(const tkstring &_filename, tkstring _outputdir="")
Definition tksystem.cpp:138
int remove_file(const tkstring &_filename, bool _force=false)
Definition tksystem.cpp:121
std::vector< tkstring > get_list_of_files(const tkstring &_dir, const tkstring &_pattern="*")
Definition tksystem.cpp:103
static tksystem * the_tksystem()
db_manager is a singleton
Definition tksystem.cpp:35
int remove_dir(const tkstring &_dirname, bool _force=false)
Definition tksystem.cpp:115
int ls_dir(const tkstring &_dirname, tkstring _option="-l")
Definition tksystem.cpp:144
int getdir(tkstring _dir, std::vector< tkstring > &_files)
Definition tksystem.cpp:44
int move(const tkstring &_from, const tkstring &_to)
Definition tksystem.cpp:133
void list_files(const tkstring &_dir, const tkstring &_pattern)
Definition tksystem.cpp:95
int download_file(const tkstring &_filename, const tkstring &_outputdir="", bool _replace_existing=false, bool _use_filename=false)
Definition tksystem.cpp:149
tkstring get_system_command_output(tkstring command)
Definition tksystem.cpp:163
Definition tklog.cpp:16
tklog & error_v(tklog &log)
Definition tklog.h:407
tklog & do_endl(tklog &log)
Definition tklog.h:212