-
Notifications
You must be signed in to change notification settings - Fork 15
/
fileutils.cpp
executable file
·536 lines (488 loc) · 15.9 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "fileutils.h"
#include <array>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <memory>
#include <sstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef _POSIX_SPAWN
#include <poll.h>
#include <spawn.h>
#else
#include <signal.h>
#endif
#include "def.h"
#include "dialog.h"
#include "error_dialog.h"
#include "sdlutils.h"
namespace
{
void AsciiToLower(char *c)
{
if (*c >= 'A' && *c <= 'Z') *c -= ('Z' - 'z');
}
char AsciiToLower(char c)
{
AsciiToLower(&c);
return c;
}
void AsciiToLower(std::string *s)
{
for (char &c : *s) AsciiToLower(&c);
}
int WaitPid(pid_t id)
{
int status, ret;
while ((ret = waitpid(id, &status, WNOHANG | WUNTRACED)) == 0)
usleep(50 * 1000);
if (ret < 0) perror("waitpid");
return (WIFEXITED(status) != 0) ? WEXITSTATUS(status) : 0;
}
int SpawnAndWait(const char *argv[], std::string *capture_stdout = nullptr,
std::string *capture_stderr = nullptr)
{
pid_t child_pid;
#ifdef _POSIX_SPAWN
std::vector<std::array<int, 2>> pipes;
std::vector<std::string *> captures;
posix_spawn_file_actions_t child_fd_actions;
int ret;
if (ret = ::posix_spawn_file_actions_init(&child_fd_actions)) return ret;
const auto add_capture_pipe = [&](std::string *capture, int child_fd) {
if (capture == nullptr) return 0;
captures.push_back(capture);
pipes.emplace_back();
if (ret = ::pipe(pipes.back().data())) return ret;
if (ret = ::posix_spawn_file_actions_addclose(
&child_fd_actions, pipes.back()[0]))
return ret;
if (ret = ::posix_spawn_file_actions_adddup2(
&child_fd_actions, pipes.back()[1], child_fd))
return ret;
return 0;
};
if (ret = add_capture_pipe(capture_stdout, 1)) return ret;
if (ret = add_capture_pipe(capture_stderr, 2)) return ret;
// This const cast is OK, see https://stackoverflow.com/a/190208.
if (ret = ::posix_spawnp(&child_pid, argv[0], &child_fd_actions, nullptr,
(char **)argv, nullptr))
return ret;
// Read from pipes
if (!pipes.empty())
{
for (auto &p : pipes) ::close(p[1]);
constexpr std::size_t kBufferSize = 1024;
std::unique_ptr<char[]> buffer { new char[kBufferSize] };
std::vector<::pollfd> poll_fds;
for (auto &p : pipes) poll_fds.push_back(::pollfd { p[0], POLL_IN });
while ((ret = ::poll(&poll_fds[0], poll_fds.size(), -1)) >= 0)
{
bool got_data = false;
for (std::size_t i = 0; i < poll_fds.size(); ++i)
{
if ((poll_fds[i].revents & POLL_IN) == 0) continue;
const int bytes_read
= ::read(pipes[i][0], buffer.get(), kBufferSize);
got_data = true;
if (bytes_read > 0)
captures[i]->append(buffer.get(), bytes_read);
}
if (!got_data) break; // nothing left to read
}
for (auto &p : pipes) ::close(p[0]);
}
ret = WaitPid(child_pid);
::posix_spawn_file_actions_destroy(&child_fd_actions);
return ret;
#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(); }
class ActionResult
{
public:
ActionResult(int errnum, std::string error_message)
: errnum_(errnum)
, message_(std::move(error_message))
{
if (errnum != 0 && message_.empty())
message_.append(std::strerror(errnum));
}
bool ok() const { return errnum_ == 0; }
const std::string &message() const { return message_; }
private:
int errnum_;
std::string message_;
};
template <typename... Args> ActionResult Run(Args... args)
{
const char *execve_args[] = { AsConstCStr(args)..., nullptr };
std::string err;
const int errnum = SpawnAndWait(
execve_args, /*capture_stdout=*/nullptr, /*capture_stderr=*/&err);
if (errnum == 0) { err.clear(); }
else if (!err.empty() && err.back() == '\n')
{
err.resize(err.size() - 1);
}
return ActionResult { errnum, std::move(err) };
}
void JoinPath(const std::string &a, const std::string &b, std::string &out)
{
out = a;
if (a.back() != '/') out += '/';
out.append(b);
}
enum class OverwriteDialogResult
{
YES,
YES_TO_ALL,
NO,
CANCEL
};
OverwriteDialogResult OverwriteDialog(
const std::string &dest_filename, bool is_last)
{
CDialog dlg{"File already exists:"};
dlg.addLabel("Overwrite " + dest_filename + "?");
std::vector<OverwriteDialogResult> options {
OverwriteDialogResult::CANCEL
};
const auto add_option = [&](std::string text, OverwriteDialogResult value) {
dlg.addOption(text);
options.push_back(value);
};
add_option("Yes", OverwriteDialogResult::YES);
if (!is_last) add_option("Yes to all", OverwriteDialogResult::YES_TO_ALL);
add_option("No", OverwriteDialogResult::NO);
if (!is_last) add_option("Cancel", OverwriteDialogResult::CANCEL);
dlg.init();
auto res = options[dlg.execute()];
SDL_utils::renderAll();
return res;
}
using ActionFn = std::function<ActionResult(
const std::string & /*src*/, const std::string & /*dest*/)>;
using ProgressFn = std::function<void(
const std::string & /*desc*/, std::size_t /*i*/, std::size_t /*n*/)>;
// No-op for now.
// TODO: Implement UI progress updates
void DefaultProgressFn(
const std::string &description, std::size_t index, std::size_t count)
{
}
void ActionToDir(const std::vector<std::string> &inputs,
const std::string &dest_dir, const char *description, ActionFn action_fn,
ProgressFn progress_fn = &DefaultProgressFn)
{
bool confirm_overwrite = true;
std::string dest_filename, action_desc;
std::size_t i = 0;
for (const std::string &input : inputs)
{
action_desc = description;
action_desc += ' ';
action_desc.append(File_utils::getFileName(input));
const bool is_last = (i == input.size() - 1);
progress_fn(action_desc, i++, inputs.size());
JoinPath(dest_dir, File_utils::getFileName(input), dest_filename);
if (confirm_overwrite && File_utils::fileExists(dest_filename))
{
switch (OverwriteDialog(dest_filename, is_last))
{
case OverwriteDialogResult::YES: break;
case OverwriteDialogResult::YES_TO_ALL:
confirm_overwrite = false;
break;
case OverwriteDialogResult::NO: continue;
case OverwriteDialogResult::CANCEL: return;
}
}
const auto action_result = action_fn(input, dest_filename);
if (!action_result.ok())
{
std::string title = "Error ";
title += AsciiToLower(action_desc[0]);
title.append(action_desc.data() + 1, action_desc.size() - 1);
switch (ErrorDialog(title, action_result.message(), is_last))
{
case ErrorDialogResult::CONTINUE: continue;
case ErrorDialogResult::ABORT: return;
}
}
}
}
// Returns absolute path to self (for re-launching on Execute failure).
std::string getSelfExecutionPath()
{
// Get execution path
std::string result;
char buf[1024];
int len = readlink("/proc/self/exe", buf, sizeof(buf));
if (len < 0)
{
perror("readlink(\"/proc/self/exe'\", ...)");
return result;
}
result.append(buf, len);
return result;
}
} // namespace
void File_utils::copyFile(
const std::vector<std::string> &srcs, const std::string &dest_dir)
{
ActionToDir(srcs, dest_dir, "Copying",
[&](const std::string &src, const std::string & /*dest*/) {
return Run("cp", "-f", "-r", src, dest_dir);
});
Run("sync", dest_dir);
}
void File_utils::moveFile(
const std::vector<std::string> &srcs, const std::string &dest_dir)
{
ActionToDir(srcs, dest_dir, "Moving",
[&](const std::string &src, const std::string &dest) {
return Run("mv", "-f", src, dest_dir);
});
Run("sync", dest_dir);
}
void File_utils::symlinkFile(
const std::vector<std::string> &srcs, const std::string &dest_dir)
{
ActionToDir(srcs, dest_dir, "Creating symlink",
[&](const std::string &src, const std::string &dest) {
return Run("ln", "-sf", src, dest_dir);
});
Run("sync", dest_dir);
}
void File_utils::renameFile(
const std::string &p_file1, const std::string &p_file2)
{
if (!fileExists(p_file2)
|| OverwriteDialog(p_file2, /*is_last=*/true)
== OverwriteDialogResult::YES)
{
auto result = Run("mv", "-f", p_file1, p_file2);
if (result.ok()) result = Run("sync", p_file2);
if (!result.ok())
{
ErrorDialog("Error renaming " + getFileName(p_file1) + " to "
+ getFileName(p_file2),
result.message());
}
}
}
void File_utils::removeFile(const std::vector<std::string> &p_files)
{
for (const std::string &path : p_files)
{
auto result = Run("rm", "-rf", path);
if (!result.ok())
{
switch (ErrorDialog("Error removing " + path, result.message(),
/*is_last=*/&path == &p_files.back()))
{
case ErrorDialogResult::CONTINUE: continue;
case ErrorDialogResult::ABORT: return;
}
}
}
}
void File_utils::makeDirectory(const std::string &p_file)
{
auto result = Run("mkdir", "-p", p_file);
if (result.ok()) result = Run("sync", p_file);
if (!result.ok()) ErrorDialog("Error creating " + p_file, result.message());
}
const bool File_utils::fileExists(const std::string &p_path)
{
struct stat l_stat;
return stat(p_path.c_str(), &l_stat) == 0;
}
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)
{
SDL_utils::hastalavista(); // Free all resources before exec
char *prev_pwd = ::getcwd(NULL, 0);
::chdir(getPath(p_file).c_str());
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, exec failed
const char *const child_error = std::strerror(errno);
perror("exec error");
std::string error_message = getFileName(p_file) + ": " + child_error;
// Relaunch self and show the error
::chdir(prev_pwd);
const std::string self_path = getSelfExecutionPath();
::free(prev_pwd);
execl(self_path.c_str(), self_path.c_str(), "--show_exec_error",
error_message.c_str(), NULL);
perror("relaunch error");
std::cerr << getSelfExecutionPath() << std::endl;
}
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)
{
struct ::stat l_stat;
if (::stat(p_file.c_str(), &l_stat) == -1)
ErrorDialog("Error getting file size", std::strerror(errno));
return l_stat.st_size;
}
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)
{
ErrorDialog("Error getting disk info", std::strerror(errno));
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;
std::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:"};
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
ErrorDialog(
"Error getting disk info", std::string(FILE_SYSTEM) + " not found");
}
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)
{
ErrorDialog("Error getting file size", std::strerror(errno));
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;
std::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:"};
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;
}
}