-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileutils.cpp
351 lines (333 loc) · 11.6 KB
/
fileutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <sys/stat.h>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <sstream>
#include "fileutils.h"
#include "def.h"
#include "dialog.h"
#include "sdlutils.h"
#define SPECIAL_CHARS "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
void File_utils::copyFile(const std::vector<std::string> &p_src, const std::string &p_dest)
{
std::string l_command("");
std::string l_destFile("");
std::string l_fileName("");
bool l_loop(true);
bool l_confirm(true);
bool l_execute(true);
for (std::vector<std::string>::const_iterator l_it = p_src.begin(); l_loop && (l_it != p_src.end()); ++l_it)
{
l_execute = true;
// Check if destination files already exists
if (l_confirm)
{
l_fileName = getFileName(*l_it);
l_destFile = p_dest + (p_dest.at(p_dest.size() - 1) == '/' ? "" : "/") + l_fileName;
if (fileExists(l_destFile))
{
INHIBIT(std::cout << "File " << l_destFile << " already exists => ask for confirmation" << std::endl;)
CDialog l_dialog("Question:", 0, 0);
l_dialog.addLabel("Overwrite " + l_fileName + "?");
l_dialog.addOption("Yes");
l_dialog.addOption("Yes to all");
l_dialog.addOption("No");
l_dialog.addOption("Cancel");
l_dialog.init();
switch (l_dialog.execute())
{
case 1:
// Yes
break;
case 2:
// Yes to all
l_confirm = false;
break;
case 3:
// No
l_execute = false;
break;
default:
// Cancel
l_execute = false;
l_loop = false;
break;
}
}
}
if (l_execute)
{
l_command = "\\cp -r " + specialChars(*l_it) + " " + specialChars(p_dest);
INHIBIT(std::cout << "Command: " << l_command << std::endl;)
system(l_command.c_str());
}
}
}
void File_utils::moveFile(const std::vector<std::string> &p_src, const std::string &p_dest)
{
std::string l_command("");
std::string l_destFile("");
std::string l_fileName("");
bool l_loop(true);
bool l_confirm(true);
bool l_execute(true);
for (std::vector<std::string>::const_iterator l_it = p_src.begin(); l_loop && (l_it != p_src.end()); ++l_it)
{
l_execute = true;
// Check if destination files already exists
if (l_confirm)
{
l_fileName = getFileName(*l_it);
l_destFile = p_dest + (p_dest.at(p_dest.size() - 1) == '/' ? "" : "/") + l_fileName;
if (fileExists(l_destFile))
{
INHIBIT(std::cout << "File " << l_destFile << " already exists => ask for confirmation" << std::endl;)
CDialog l_dialog("Question:", 0, 0);
l_dialog.addLabel("Overwrite " + l_fileName + "?");
l_dialog.addOption("Yes");
l_dialog.addOption("Yes to all");
l_dialog.addOption("No");
l_dialog.addOption("Cancel");
l_dialog.init();
switch (l_dialog.execute())
{
case 1:
// Yes
break;
case 2:
// Yes to all
l_confirm = false;
break;
case 3:
// No
l_execute = false;
break;
default:
// Cancel
l_execute = false;
l_loop = false;
break;
}
}
}
if (l_execute)
{
l_command = "\\mv " + specialChars(*l_it) + " " + specialChars(p_dest);
INHIBIT(std::cout << "Command: " << l_command << std::endl;)
system(l_command.c_str());
}
}
}
void File_utils::renameFile(const std::string &p_file1, const std::string &p_file2)
{
bool l_execute(true);
// Check if destination files already exists
if (fileExists(p_file2))
{
INHIBIT(std::cout << "File " << p_file2 << " already exists => ask for confirmation" << std::endl;)
CDialog l_dialog("Question:", 0, 0);
l_dialog.addLabel("Overwrite " + getFileName(p_file2) + "?");
l_dialog.addOption("Yes");
l_dialog.addOption("No");
l_dialog.init();
if (l_dialog.execute() != 1)
l_execute = false;
}
if (l_execute)
{
std::string l_command = "\\mv " + specialChars(p_file1) + " " + specialChars(p_file2);
INHIBIT(std::cout << "Command: " << l_command << std::endl;)
system(l_command.c_str());
}
}
void File_utils::removeFile(const std::vector<std::string> &p_files)
{
std::string l_command("");
for (std::vector<std::string>::const_iterator l_it = p_files.begin(); l_it != p_files.end(); ++l_it)
{
l_command = "\\rm -rf " + specialChars(*l_it);
INHIBIT(std::cout << "Command: " << l_command << std::endl;)
system(l_command.c_str());
}
}
void File_utils::makeDirectory(const std::string &p_file)
{
std::string l_command = "\\mkdir -p " + specialChars(p_file);
INHIBIT(std::cout << "Command: " << l_command << std::endl;)
system(l_command.c_str());
}
const bool File_utils::fileExists(const std::string &p_path)
{
struct stat l_stat;
return stat(p_path.c_str(), &l_stat) == 0;
}
const std::string File_utils::getFileName(const std::string &p_path)
{
size_t l_pos = p_path.rfind('/');
return p_path.substr(l_pos + 1);
}
const std::string File_utils::getPath(const std::string &p_path)
{
size_t l_pos = p_path.rfind('/');
return p_path.substr(0, l_pos);
}
void File_utils::executeFile(const std::string &p_file)
{
// Command
std::string l_command = "./" + specialChars(getFileName(p_file));
INHIBIT(std::cout << "File_utils::executeFile: " << l_command << " in " << getPath(p_file) << std::endl;)
// CD to the file's location
chdir(getPath(p_file).c_str());
// Quit
SDL_utils::hastalavista();
// Execute file
execlp("/bin/sh", "/bin/sh", "-c", l_command.c_str(), NULL);
// If we're here, there was an error with the execution
std::cerr << "Error executing file " << p_file << std::endl;
// Relaunch DinguxCommander
l_command = "./" + specialChars(getSelfExecutionName());
INHIBIT(std::cout << "File_utils::executeFile: " << l_command << " in " << getSelfExecutionPath() << std::endl;)
chdir(getSelfExecutionPath().c_str());
execlp(l_command.c_str(), l_command.c_str(), NULL);
}
const std::string File_utils::getSelfExecutionPath(void)
{
// Get execution path
std::string l_exePath("");
char l_buff[255];
int l_i = readlink("/proc/self/exe", l_buff, 255);
l_exePath = l_buff;
l_exePath = l_exePath.substr(0, l_i);
l_i = l_exePath.rfind("/");
l_exePath = l_exePath.substr(0, l_i);
return l_exePath;
}
const std::string File_utils::getSelfExecutionName(void)
{
// Get execution path
std::string l_exePath("");
char l_buff[255];
int l_i = readlink("/proc/self/exe", l_buff, 255);
l_exePath = l_buff;
l_exePath = l_exePath.substr(0, l_i);
l_i = l_exePath.rfind("/");
l_exePath = l_exePath.substr(l_i + 1);
return l_exePath;
}
void File_utils::stringReplace(std::string &p_string, const std::string &p_search, const std::string &p_replace)
{
// Replace all occurrences of p_search by p_replace in p_string
size_t l_pos = p_string.find(p_search, 0);
while (l_pos != std::string::npos)
{
p_string.replace(l_pos, p_search.length(), p_replace);
l_pos = p_string.find(p_search, l_pos + p_replace.length());
}
}
const std::string File_utils::specialChars(const std::string &p_string)
{
// Insert a '\' before special characters
std::string l_ret(p_string);
const std::string l_specialChars(SPECIAL_CHARS);
const size_t l_length = l_specialChars.size();
std::string l_char("");
for (unsigned int l_i = 0; l_i < l_length; ++l_i)
{
l_char = l_specialChars.substr(l_i, 1);
stringReplace(l_ret, l_char, "\\" + l_char);
}
return l_ret;
}
const unsigned long int File_utils::getFileSize(const std::string &p_file)
{
unsigned long int l_ret(0);
struct stat l_stat;
if (stat(p_file.c_str(), &l_stat) == -1)
std::cerr << "File_utils::getFileSize: Error stat " << p_file << std::endl;
else
l_ret = l_stat.st_size;
return l_ret;
}
void File_utils::diskInfo(void)
{
std::string l_line("");
// Execute command df -h
{
char l_buffer[256];
FILE *l_pipe = popen("df -h", "r");
if (l_pipe == NULL)
{
std::cerr << "File_utils::diskInfo: Error popen" << std::endl;
return;
}
while (l_line.empty() && fgets(l_buffer, sizeof(l_buffer), l_pipe) != NULL)
if (strstr(l_buffer, FILE_SYSTEM) != NULL)
l_line = l_buffer;
pclose(l_pipe);
}
if (!l_line.empty())
{
// Separate line by spaces
std::istringstream l_iss(l_line);
std::vector<std::string> l_tokens;
copy(std::istream_iterator<std::string>(l_iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(l_tokens));
// Display dialog
CDialog l_dialog("Disk information:", 0, 0);
l_dialog.addLabel("Size: " + l_tokens[1]);
l_dialog.addLabel("Used: " + l_tokens[2] + " (" + l_tokens[4] + ")");
l_dialog.addLabel("Available: " + l_tokens[3]);
l_dialog.addOption("OK");
l_dialog.init();
l_dialog.execute();
}
else
std::cerr << "File_utils::diskInfo: Unable to find " << FILE_SYSTEM << std::endl;
}
void File_utils::diskUsed(const std::vector<std::string> &p_files)
{
std::string l_line("");
// Waiting message
SDL_utils::pleaseWait();
// Build and execute command
{
std::string l_command("du -csh");
for (std::vector<std::string>::const_iterator l_it = p_files.begin(); l_it != p_files.end(); ++l_it)
l_command = l_command + " \"" + *l_it + "\"";
char l_buffer[256];
FILE *l_pipe = popen(l_command.c_str(), "r");
if (l_pipe == NULL)
{
std::cerr << "File_utils::diskUsed: Error popen" << std::endl;
return;
}
while (fgets(l_buffer, sizeof(l_buffer), l_pipe) != NULL);
l_line = l_buffer;
pclose(l_pipe);
}
// Separate line by spaces
{
std::istringstream l_iss(l_line);
std::vector<std::string> l_tokens;
copy(std::istream_iterator<std::string>(l_iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(l_tokens));
l_line = l_tokens[0];
}
// Dialog
std::ostringstream l_stream;
CDialog l_dialog("Disk used:", 0, 0);
l_stream << p_files.size() << " items selected";
l_dialog.addLabel(l_stream.str());
l_dialog.addLabel("Disk used: " + l_line);
l_dialog.addOption("OK");
l_dialog.init();
l_dialog.execute();
}
void File_utils::formatSize(std::string &p_size)
{
// Format 123456789 to 123,456,789
int l_i = p_size.size() - 3;
while (l_i > 0)
{
p_size.insert(l_i, ",");
l_i -= 3;
}
}