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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 Björn Stenberg
* Copyright (C) 2024 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string-extra.h"
#include "debug.h"
#include "misc.h"
#include "plugin.h"
#include "dir.h"
#include "tree.h"
#include "fileop.h"
#include "pathfuncs.h"
#include "settings.h"
#include "lang.h"
#include "yesno.h"
#include "splash.h"
#include "keyboard.h"
/* Used for directory move, copy and delete */
struct file_op_params
{
char path[MAX_PATH]; /* Buffer for full path */
const char* toplevel_name;
bool is_dir;
int objects; /* how many files and subdirectories*/
int processed;
unsigned long long total_size;
unsigned long long processed_size;
size_t append; /* Append position in 'path' for stack push */
size_t extra_len; /* Length added by dst path compared to src */
};
static int prompt_name(char* buf, size_t bufsz)
{
if (kbd_input(buf, bufsz, NULL) < 0)
return FORC_CANCELLED;
/* at least prevent escapes out of the base directory from keyboard-
entered filenames; the file code should reject other invalidities */
if (*buf != '\0' && !strchr(buf, PATH_SEPCH) && !is_dotdir_name(buf))
return FORC_SUCCESS;
return FORC_UNKNOWN_FAILURE;
}
static bool poll_cancel_action(int operation, struct file_op_params *param)
{
static unsigned long last_tick;
if (operation == FOC_COUNT)
{
if (param->objects <= 1)
last_tick = current_tick;
else if (TIME_AFTER(current_tick, last_tick + HZ/2))
{
clear_screen_buffer(false);
splashf(0, "%s (%d)", str(LANG_SCANNING_DISK), param->objects);
last_tick = current_tick;
}
}
else
{
const char *op_str = (operation == FOC_DELETE) ? str(LANG_DELETING) :
(operation == FOC_COPY) ? str(LANG_COPYING) :
str(LANG_MOVING);
if ((operation == FOC_DELETE || !param->total_size) &&
param->objects > 0)
{
splash_progress(param->processed, param->objects,
"%s %s", op_str, param->toplevel_name);
}
else if (param->total_size >= 10 * 1024 * 1024)
{
int total_shft = (int) (param->total_size >> 15);
int current_shft = (int) (param->processed_size >> 15);
splash_progress(current_shft, total_shft,
"%s %s (%d MiB)\n%d MiB",
op_str, param->toplevel_name,
total_shft >> 5, current_shft >> 5);
}
else if (param->total_size >= 1024)
{
int total_kib = (int) (param->total_size >> 10);
int current_kib = (int) (param->processed_size >> 10);
splash_progress(current_kib, total_kib,
"%s %s (%d KiB)\n%d KiB",
op_str, param->toplevel_name,
total_kib, current_kib);
}
}
return ACTION_STD_CANCEL == get_action(CONTEXT_STD, TIMEOUT_NOBLOCK);
}
static void init_file_op(struct file_op_params *param,
const char *basename,
const char *selected_file)
{
/* Assumes: basename will never be NULL */
if (selected_file == NULL)
{
param->append = strlcpy(param->path, basename, sizeof (param->path));
path_basename(basename, &basename);
param->toplevel_name = basename;
}
else
{
param->append = path_append(param->path, basename,
selected_file, sizeof (param->path));
param->toplevel_name = selected_file;
}
param->is_dir = dir_exists(param->path);
param->extra_len = 0;
param->objects = 0; /* how many files and subdirectories*/
param->processed = 0;
param->total_size = 0;
param->processed_size = 0;
}
/* counts file objects, deletes file objects */
static int directory_fileop(struct file_op_params *param, enum file_op_current fileop)
{
errno = 0;
DIR *dir = opendir(param->path);
if (!dir) {
if (errno == EMFILE) {
return FORC_TOO_MANY_SUBDIRS;
}
return FORC_PATH_NOT_EXIST; /* open error */
}
int rc = FORC_SUCCESS;
size_t append = param->append;
/* walk through the directory content */
while (rc == FORC_SUCCESS) {
errno = 0; /* distinguish failure from eod */
struct dirent *entry = readdir(dir);
if (!entry) {
if (errno) {
rc = FORC_PATH_NOT_EXIST;
}
break;
}
struct dirinfo info = dir_get_info(dir, entry);
if ((info.attribute & ATTR_DIRECTORY) &&
is_dotdir_name(entry->d_name)) {
continue; /* skip these */
}
/* append name to current directory */
param->append = append + path_append(¶m->path[append],
PA_SEP_HARD, entry->d_name,
sizeof (param->path) - append);
if (fileop == FOC_DELETE)
{
param->processed++;
/* at this point we've already counted and never had a path too long
in the other branch so we 'should not' encounter one here either */
if (param->processed > param->objects)
{
rc = FORC_UNKNOWN_FAILURE;
break;
}
if (info.attribute & ATTR_DIRECTORY) {
/* remove a subdirectory */
rc = directory_fileop(param, fileop); /* recursion */
} else {
/* remove a file */
if (poll_cancel_action(FOC_DELETE, param))
{
rc = FORC_CANCELLED;
break;
}
rc = remove(param->path);
}
}
else /* count objects */
{
param->objects++;
param->total_size += info.size;
if (poll_cancel_action(FOC_COUNT, param))
{
rc = FORC_CANCELLED;
break;
}
if (param->append + param->extra_len >= sizeof (param->path)) {
rc = FORC_PATH_TOO_LONG;
break; /* no space left in buffer */
}
if (info.attribute & ATTR_DIRECTORY) {
/* enter subdirectory */
rc = directory_fileop(param, FOC_COUNT); /* recursion */
}
}
param->append = append; /* other functions may use param, reset append */
/* Remove basename we added above */
param->path[append] = '\0';
}
closedir(dir);
if (fileop == FOC_DELETE && rc == FORC_SUCCESS) {
/* remove the now empty directory */
if (poll_cancel_action(FOC_DELETE, param))
{
rc = FORC_CANCELLED;
} else {
rc = rmdir(param->path);
}
}
return rc;
}
/* Walk a directory tree and count the number of objects (dirs & files)
* also check that enough resources exist to do an operation */
static int check_count_fileobjects(struct file_op_params *param)
{
cpu_boost(true);
int rc = directory_fileop(param, FOC_COUNT);
cpu_boost(false);
DEBUGF("%s res:(%d) objects %d \n", __func__, rc, param->objects);
return rc;
}
/* Attempt to just rename a file or directory */
static int move_by_rename(struct file_op_params *src,
const char *dst_path,
unsigned int *pflags)
{
unsigned int flags = *pflags;
int rc = FORC_UNKNOWN_FAILURE;
if (!(flags & (PASTE_COPY | PASTE_EXDEV))) {
if ((flags & PASTE_OVERWRITE) || !file_exists(dst_path)) {
/* Just try to move the directory / file */
rc = rename(src->path, dst_path);
#ifdef HAVE_MULTIVOLUME
if (rc < FORC_SUCCESS && errno == EXDEV) {
/* Failed because cross volume rename doesn't work */
*pflags |= PASTE_EXDEV; /* force a move instead */
}
#endif /* HAVE_MULTIVOLUME */
/* if (errno == ENOTEMPTY && (flags & PASTE_OVERWRITE)) {
* Directory is not empty thus rename() will not do a quick overwrite */
}
}
return rc;
}
/* Paste a file */
static int copy_move_file(struct file_op_params *src, const char *dst_path,
unsigned int flags)
{
/* Try renaming first */
int rc = move_by_rename(src, dst_path, &flags);
if (rc == FORC_SUCCESS)
{
src->total_size = 0; /* switch from counting size to number of items */
return rc;
}
/* See if we can get the plugin buffer for the file copy buffer */
size_t buffersize;
char *buffer = (char *) plugin_get_buffer(&buffersize);
if (buffer == NULL || buffersize < 512) {
/* Not large enough, try for a disk sector worth of stack
instead */
buffersize = 512;
buffer = (char *)alloca(buffersize);
}
if (buffer == NULL) {
return FORC_NO_BUFFER_AVAIL;
}
buffersize &= ~0x1ff; /* Round buffer size to multiple of sector size */
int src_fd = open(src->path, O_RDONLY);
if (src_fd >= 0) {
off_t src_sz = lseek(src_fd, 0, SEEK_END);
if (!src->total_size && !src->processed) /* single file copy */
src->total_size = src_sz;
lseek(src_fd, 0, SEEK_SET);
int oflag = O_WRONLY|O_CREAT;
if (!(flags & PASTE_OVERWRITE)) {
oflag |= O_EXCL;
}
int dst_fd = open(dst_path, oflag, 0666);
if (dst_fd >= 0) {
off_t total_size = 0;
off_t next_cancel_test = 0; /* No excessive button polling */
rc = FORC_SUCCESS;
while (rc == FORC_SUCCESS) {
if (total_size >= next_cancel_test) {
next_cancel_test = total_size + 0x10000;
if (poll_cancel_action(!(flags & PASTE_COPY) ?
FOC_MOVE : FOC_COPY, src))
{
rc = FORC_CANCELLED;
break;
}
}
ssize_t bytesread = read(src_fd, buffer, buffersize);
if (bytesread <= 0) {
if (bytesread < 0) {
rc = FORC_READ_FAILURE;
}
/* else eof on buffer boundary; nothing to write */
break;
}
ssize_t byteswritten = write(dst_fd, buffer, bytesread);
if (byteswritten < bytesread) {
/* Some I/O error */
rc = FORC_WRITE_FAILURE;
break;
}
total_size += byteswritten;
src->processed_size += byteswritten;
if (bytesread < (ssize_t)buffersize) {
/* EOF with trailing bytes */
break;
}
}
if (rc == FORC_SUCCESS) {
if (total_size != src_sz)
rc = FORC_UNKNOWN_FAILURE;
else {
/* If overwriting, set the correct length if original was longer */
rc = ftruncate(dst_fd, total_size) * 10;
}
}
close(dst_fd);
if (rc != FORC_SUCCESS) {
/* Copy failed. Cleanup. */
remove(dst_path);
}
}
close(src_fd);
}
if (rc == FORC_SUCCESS && !(flags & PASTE_COPY)) {
/* Remove the source file */
rc = remove(src->path) * 10;
}
return rc;
}
/* Paste a directory */
static int copy_move_directory(struct file_op_params *src,
struct file_op_params *dst,
unsigned int flags)
{
DIR *srcdir = opendir(src->path);
if (!srcdir)
return FORC_PATH_NOT_EXIST;
/* Make a directory to copy things to */
int rc = mkdir(dst->path) * 10;
if (rc < 0 && errno == EEXIST && (flags & PASTE_OVERWRITE)) {
/* Exists and overwrite was approved */
rc = FORC_SUCCESS;
}
size_t srcap = src->append, dstap = dst->append;
/* Walk through the directory content; this loop will exit as soon as
there's a problem */
while (rc == FORC_SUCCESS) {
errno = 0; /* Distinguish failure from eod */
struct dirent *entry = readdir(srcdir);
if (!entry) {
if (errno) {
rc = FORC_PATH_NOT_EXIST;
}
break;
}
struct dirinfo info = dir_get_info(srcdir, entry);
if ((info.attribute & ATTR_DIRECTORY) &&
is_dotdir_name(entry->d_name)) {
continue; /* Skip these */
}
/* Append names to current directories */
src->append = srcap +
path_append(&src->path[srcap], PA_SEP_HARD, entry->d_name,
sizeof (src->path) - srcap);
dst->append = dstap +
path_append(&dst->path[dstap], PA_SEP_HARD, entry->d_name,
sizeof (dst->path) - dstap);
/* src length was already checked by check_count_fileobjects() */
if (dst->append >= sizeof (dst->path)) {
rc = FORC_PATH_TOO_LONG; /* No space left in buffer */
break;
}
src->processed++;
if (src->processed > src->objects)
{
rc = FORC_UNKNOWN_FAILURE;
break;
}
if (poll_cancel_action(!(flags & PASTE_COPY) ?
FOC_MOVE : FOC_COPY, src))
{
rc = FORC_CANCELLED;
break;
}
DEBUGF("Copy %s to %s\n", src->path, dst->path);
if (info.attribute & ATTR_DIRECTORY) {
src->processed_size += info.size;
/* Copy/move a subdirectory */
rc = copy_move_directory(src, dst, flags); /* recursion */;
} else {
/* Copy/move a file */
rc = copy_move_file(src, dst->path, flags);
}
/* Remove basenames we added above */
src->path[srcap] = '\0';
dst->path[dstap] = '\0';
}
if (rc == FORC_SUCCESS && !(flags & PASTE_COPY)) {
/* Remove the now empty directory */
rc = rmdir(src->path) * 10;
}
closedir(srcdir);
return rc;
}
/************************************************************************************/
/* PUBLIC FUNCTIONS */
/************************************************************************************/
/* Copy or move a file or directory see: file_op_flags */
int copy_move_fileobject(const char *src_path, const char *dst_path, unsigned int flags)
{
if (!src_path[0])
return FORC_NOOP;
struct file_op_params src, dst;
/* Figure out the name of the selection */
const char *nameptr;
path_basename(src_path, &nameptr);
/* Final target is current directory plus name of selection */
init_file_op(&dst, dst_path, nameptr);
if (dst.append >= sizeof (dst.path))
return FORC_PATH_TOO_LONG;
int rel = relate(src_path, dst.path);
if (rel == RELATE_SAME)
return FORC_NOOP;
if (rel == RELATE_DIFFERENT) {
int rc;
if (file_exists(dst.path)) {
/* If user chooses not to overwrite, cancel */
if (confirm_overwrite_yesno() == YESNO_NO) {
return FORC_NOOVERWRT;
}
flags |= PASTE_OVERWRITE;
}
init_file_op(&src, src_path, NULL);
if (src.append >= sizeof (src.path))
return FORC_PATH_TOO_LONG;
/* Now figure out what we're doing */
cpu_boost(true);
if (src.is_dir) {
/* Copy or move a subdirectory */
/* Try renaming first */
rc = move_by_rename(&src, dst.path, &flags);
if (rc < FORC_SUCCESS) {
int extra_len = dst.append - src.append;
if (extra_len > 0)
src.extra_len = extra_len;
rc = check_count_fileobjects(&src);
if (rc == FORC_SUCCESS) {
rc = copy_move_directory(&src, &dst, flags);
}
}
} else {
/* Copy or move a file */
rc = copy_move_file(&src, dst.path, flags);
}
cpu_boost(false);
DEBUGF("%s res: %d, ct: %d/%d %s\n",
__func__, rc, src.objects, src.processed, src.path);
return rc;
}
/* Else Some other relation / failure */
DEBUGF("%s res: %d, rel: %d\n", __func__, FORC_UNKNOWN_FAILURE, rel);
return FORC_UNKNOWN_FAILURE;
}
int create_dir(void)
{
int rc;
char dirname[MAX_PATH];
size_t pathlen = path_append(dirname, getcwd(NULL, 0), PA_SEP_HARD,
sizeof (dirname));
char *basename = dirname + pathlen;
if (pathlen >= sizeof (dirname))
return FORC_PATH_TOO_LONG;
rc = prompt_name(basename, sizeof (dirname) - pathlen);
if (rc == FORC_SUCCESS)
rc = mkdir(dirname) * 10;
return rc;
}
/* share code for file and directory deletion, saves space */
int delete_fileobject(const char *selected_file)
{
int rc;
struct file_op_params param;
init_file_op(¶m, selected_file, NULL);
if (param.append >= sizeof (param.path))
return FORC_PATH_TOO_LONG;
/* Note: delete_fileobject() will happily delete whatever
* path is passed (after confirmation) */
if (confirm_delete_yesno(param.path) != YESNO_YES) {
return FORC_CANCELLED;
}
if (param.is_dir) {
int rc = check_count_fileobjects(¶m);
DEBUGF("%s res: %d, ct: %d, %s", __func__, rc, param.objects, param.path);
if (rc != FORC_SUCCESS)
return rc;
}
clear_screen_buffer(true);
if (param.is_dir) { /* if directory */
cpu_boost(true);
rc = directory_fileop(¶m, FOC_DELETE);
cpu_boost(false);
} else {
rc = remove(param.path) * 10;
}
return rc;
}
int rename_file(const char *selected_file)
{
int rc;
char newname[MAX_PATH];
const char *oldbase, *selection = selected_file;
path_basename(selection, &oldbase);
size_t pathlen = oldbase - selection;
char *newbase = newname + pathlen;
if (strmemccpy(newname, selection, sizeof (newname)) == NULL)
return FORC_PATH_TOO_LONG;
rc = prompt_name(newbase, sizeof (newname) - pathlen);
if (rc != FORC_SUCCESS)
return rc;
if (!strcmp(oldbase, newbase))
return FORC_NOOP; /* No change at all */
int rel = relate(selection, newname);
if (rel == RELATE_DIFFERENT)
{
if (file_exists(newname)) { /* don't overwrite */
return FORC_PATH_EXISTS;
}
return rename(selection, newname) * 10;
}
if (rel == RELATE_SAME)
return rename(selection, newname) * 10;
/* Else Some other relation / failure */
return FORC_UNKNOWN_FAILURE;
}
|