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