forked from zfteam/rs97-commander-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfileutils.cpp
More file actions
executable file
·436 lines (407 loc) · 13.1 KB
/
fileutils.cpp
File metadata and controls
executable file
·436 lines (407 loc) · 13.1 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <unistd.h>
#ifdef _POSIX_SPAWN
#include <spawn.h>
#else
#include <signal.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <sstream>
#include <unistd.h>
#include "fileutils.h"
#include "def.h"
#include "dialog.h"
#include "sdlutils.h"
namespace {
int WaitPid(pid_t id)
{
int status;
while (waitpid(id, &status, WNOHANG) == 0)
usleep(50 * 1000);
if (1 != WIFEXITED(status) || 0 != WEXITSTATUS(status))
{
perror("Child process error");
return -1;
}
return 0;
}
// If child_pid is NULL, waits for the child process to finish.
// Otherwise, doesn't wait and stores child process pid into child_pid.
int SpawnAndWait(const char *argv[])
{
pid_t child_pid;
#ifdef _POSIX_SPAWN
// This const cast is OK, see https://stackoverflow.com/a/190208.
if (::posix_spawnp(&child_pid, argv[0], nullptr, nullptr, (char **)argv, nullptr) != 0)
{
perror(argv[0]);
return -1;
}
return WaitPid(child_pid);
#else
struct sigaction sa, save_quit, save_int;
sigset_t save_mask;
int wait_val;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
/* __sigemptyset(&sa.sa_mask); - done by memset() */
/* sa.sa_flags = 0; - done by memset() */
sigaction(SIGQUIT, &sa, &save_quit);
sigaction(SIGINT, &sa, &save_int);
sigaddset(&sa.sa_mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &sa.sa_mask, &save_mask);
if ((child_pid = vfork()) < 0)
{
wait_val = -1;
goto out;
}
if (child_pid == 0)
{
sigaction(SIGQUIT, &save_quit, NULL);
sigaction(SIGINT, &save_int, NULL);
sigprocmask(SIG_SETMASK, &save_mask, NULL);
// This const cast is OK, see https://stackoverflow.com/a/190208.
if (execvp(argv[0], (char **)argv) == -1)
perror(argv[0]);
exit(127);
}
wait_val = WaitPid(child_pid);
out:
sigaction(SIGQUIT, &save_quit, NULL);
sigaction(SIGINT, &save_int, NULL);
sigprocmask(SIG_SETMASK, &save_mask, NULL);
return wait_val;
#endif
}
const char *AsConstCStr(const char *s) { return s; }
const char *AsConstCStr(const std::string &s) { return s.c_str(); }
template <typename... Args>
bool Run(Args... args)
{
const char *execve_args[] = {AsConstCStr(args)..., nullptr};
return SpawnAndWait(execve_args) == 0;
}
} // namespace
void File_utils::copyFile(const std::vector<std::string> &p_src, const std::string &p_dest)
{
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;
l_fileName = getFileName(*l_it);
l_destFile = p_dest + (p_dest.at(p_dest.size() - 1) == '/' ? "" : "/") + l_fileName;
// Check if destination files already exists
if (l_confirm)
{
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)
{
Run("cp", "-r", *l_it, p_dest);
Run("sync", l_destFile);
}
}
}
void File_utils::moveFile(const std::vector<std::string> &p_src, const std::string &p_dest)
{
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)
{
Run("mv", *l_it, p_dest);
Run("sync", p_dest);
}
}
}
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)
{
Run("mv", p_file1, p_file2);
Run("sync", p_file2);
}
}
void File_utils::removeFile(const std::vector<std::string> &p_files)
{
for (std::vector<std::string>::const_iterator l_it = p_files.begin(); l_it != p_files.end(); ++l_it)
{
Run("rm", "-rf", *l_it);
}
}
void File_utils::makeDirectory(const std::string &p_file)
{
Run("mkdir", "-p", p_file);
Run("sync", p_file);
}
const bool File_utils::fileExists(const std::string &p_path)
{
struct stat l_stat;
return stat(p_path.c_str(), &l_stat) == 0;
}
static void AsciiToLower(std::string *s)
{
for (char &c : *s)
if (c >= 'A' && c <= 'Z')
c -= ('Z' - 'z');
}
std::string File_utils::getLowercaseFileExtension(const std::string &name) {
const auto dot_pos = name.rfind('.');
if (dot_pos == std::string::npos)
return "";
std::string ext = name.substr(dot_pos + 1);
AsciiToLower(&ext);
return ext;
}
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
INHIBIT(std::cout << "File_utils::executeFile: " << p_file << " in " << getPath(p_file) << std::endl;)
// CD to the file's location
chdir(getPath(p_file).c_str());
// Quit
SDL_utils::hastalavista();
// Execute file
if (getLowercaseFileExtension(p_file) == "opk") {
execlp("opkrun", "opkrun", p_file.c_str(), nullptr);
} else {
execl(p_file.c_str(), p_file.c_str(), nullptr);
}
// If we're here, there was an error with the execution
perror("Child process error");
std::cerr << "Error executing file " << p_file << std::endl;
// Relaunch DinguxCommander
const std::string self_name = getSelfExecutionName();
INHIBIT(std::cout << "File_utils::executeFile: " << self_name << " in " << getSelfExecutionPath() << std::endl;)
chdir(getSelfExecutionPath().c_str());
execl(self_name.c_str(), self_name.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 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("");
SDL_utils::pleaseWait();
// Execute command df -h
{
char l_buffer[256];
FILE *l_pipe = popen("df -h " FILE_SYSTEM, "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;
}
}