-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathfile.c
More file actions
3761 lines (3100 loc) · 111 KB
/
file.c
File metadata and controls
3761 lines (3100 loc) · 111 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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File management.
Copyright (C) 1994-2025
Free Software Foundation, Inc.
Written by:
Janne Kukonlehto, 1994, 1995
Fred Leeflang, 1994, 1995
Miguel de Icaza, 1994, 1995, 1996
Jakub Jelinek, 1995, 1996
Norbert Warmuth, 1997
Pavel Machek, 1998
Andrew Borodin <aborodin@vmail.ru>, 2011-2022
The copy code was based in GNU's cp, and was written by:
Torbjorn Granlund, David MacKenzie, and Jim Meyering.
The move code was based in GNU's mv, and was written by:
Mike Parker and David MacKenzie.
Janne Kukonlehto added much error recovery to them for being used
in an interactive program.
This file is part of the Midnight Commander.
The Midnight Commander 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 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Please note that all dialogs used here must be safe for background
* operations.
*/
/** \file src/filemanager/file.c
* \brief Source: file management
*/
/* {{{ Include files */
#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "lib/global.h"
#include "lib/tty/tty.h"
#include "lib/tty/key.h"
#include "lib/search.h"
#include "lib/strutil.h"
#include "lib/util.h"
#include "lib/vfs/vfs.h"
#include "lib/vfs/utilvfs.h"
#include "lib/widget.h"
#include "src/setup.h"
#ifdef ENABLE_BACKGROUND
#include "src/background.h" // do_background()
#endif
#include "src/util.h" // file_error_message()
/* Needed for other_panel and WTree */
#include "dir.h"
#include "filenot.h"
#include "tree.h"
#include "filemanager.h" // other_panel
#include "layout.h" // rotate_dash()
#include "ioblksize.h" // io_blksize()
#include "file.h"
/* }}} */
/*** global variables ****************************************************************************/
/* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
const char *op_names[3] = {
N_ ("DialogTitle|Copy"),
N_ ("DialogTitle|Move"),
N_ ("DialogTitle|Delete"),
};
/*** file scope macro definitions ****************************************************************/
#define FILEOP_UPDATE_INTERVAL 2
#define FILEOP_STALLING_INTERVAL 4
#define FILEOP_UPDATE_INTERVAL_US (FILEOP_UPDATE_INTERVAL * G_USEC_PER_SEC)
#define FILEOP_STALLING_INTERVAL_US (FILEOP_STALLING_INTERVAL * G_USEC_PER_SEC)
/*** file scope type declarations ****************************************************************/
/* This is a hard link cache */
typedef struct
{
const struct vfs_class *vfs;
dev_t dev;
ino_t ino;
mode_t st_mode;
vfs_path_t *src_vpath;
vfs_path_t *dst_vpath;
} link_t;
/* Status of the destination file */
typedef enum
{
DEST_NONE = 0, // Not created
DEST_SHORT_QUERY, // Created, not fully copied, query to do
DEST_SHORT_KEEP, // Created, not fully copied, keep it
DEST_SHORT_DELETE, // Created, not fully copied, delete it
DEST_FULL // Created, fully copied
} dest_status_t;
/* Status of hard link creation */
typedef enum
{
HARDLINK_OK = 0, // Hardlink was created successfully
HARDLINK_CACHED, // Hardlink was added to the cache
HARDLINK_NOTLINK, // This is not a hard link
HARDLINK_UNSUPPORTED, // VFS doesn't support hard links
HARDLINK_ERROR, // Hard link creation error
HARDLINK_ABORT // Stop file operation after hardlink creation error
} hardlink_status_t;
/*
* This array introduced to avoid translation problems. The former (op_names)
* is assumed to be nouns, suitable in dialog box titles; this one should
* contain whatever is used in prompt itself (i.e. in russian, it's verb).
* (I don't use spaces around the words, because someday they could be
* dropped, when widgets get smarter)
*/
/* TRANSLATORS: no need to translate 'FileOperation', it's just a context prefix */
static const char *op_names1[] = {
N_ ("FileOperation|Copy"),
N_ ("FileOperation|Move"),
N_ ("FileOperation|Delete"),
};
/*
* These are formats for building a prompt. Parts encoded as follows:
* %o - operation from op_names1
* %f - file/files or files/directories, as appropriate
* %m - "with source mask" or question mark for delete
* %s - source name (truncated)
* %d - number of marked files
* %n - the '\n' symbol to form two-line prompt for delete or space for other operations
*/
/* xgettext:no-c-format */
static const char *one_format = N_ ("%o %f%n\"%s\"%m");
/* xgettext:no-c-format */
static const char *many_format = N_ ("%o %d %f%m");
static const char *prompt_parts[] = {
N_ ("file"), N_ ("files"), N_ ("directory"), N_ ("directories"), N_ ("files/directories"),
// TRANSLATORS: keep leading space here to split words in Copy/Move dialog
N_ (" with source mask:")
};
/*** forward declarations (file scope functions) *************************************************/
/*** file scope variables ************************************************************************/
/* the hard link cache */
static GSList *linklist = NULL;
/* the files-to-be-erased list */
static GQueue *erase_list = NULL;
/*
* This list holds information about just created target directories and is used to detect
* when an directory is copied into itself (we don't want to copy infinitely).
*/
static GSList *dest_dirs = NULL;
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
dirsize_status_locate_buttons (dirsize_status_msg_t *dsm)
{
status_msg_t *sm = STATUS_MSG (dsm);
Widget *wd = WIDGET (sm->dlg);
int y, x;
WRect r;
y = wd->rect.y + 5;
x = wd->rect.x;
if (!dsm->allow_skip)
{
// single button: "Abort"
x += (wd->rect.cols - dsm->abort_button->rect.cols) / 2;
r = dsm->abort_button->rect;
r.y = y;
r.x = x;
widget_set_size_rect (dsm->abort_button, &r);
}
else
{
// two buttons: "Abort" and "Skip"
int cols;
cols = dsm->abort_button->rect.cols + dsm->skip_button->rect.cols + 1;
x += (wd->rect.cols - cols) / 2;
r = dsm->abort_button->rect;
r.y = y;
r.x = x;
widget_set_size_rect (dsm->abort_button, &r);
x += dsm->abort_button->rect.cols + 1;
r = dsm->skip_button->rect;
r.y = y;
r.x = x;
widget_set_size_rect (dsm->skip_button, &r);
}
}
/* --------------------------------------------------------------------------------------------- */
static char *
build_dest (file_op_context_t *ctx, const char *src, const char *dest, FileProgressStatus *status)
{
char *s, *q;
const char *fnsource;
*status = FILE_CONT;
s = g_strdup (src);
// We remove \n from the filename since regex routines would use \n as an anchor
// this is just to be allowed to maniupulate file names with \n on it
for (q = s; *q != '\0'; q++)
if (*q == '\n')
*q = ' ';
fnsource = x_basename (s);
if (!mc_search_run (ctx->search_handle, fnsource, 0, strlen (fnsource), NULL))
{
q = NULL;
*status = FILE_SKIP;
}
else
{
q = mc_search_prepare_replace_str2 (ctx->search_handle, ctx->dest_mask);
if (ctx->search_handle->error != MC_SEARCH_E_OK)
{
if (ctx->search_handle->error_str != NULL)
message (D_ERROR, MSG_ERROR, "%s", ctx->search_handle->error_str);
*status = FILE_ABORT;
}
}
MC_PTR_FREE (s);
if (*status == FILE_CONT)
{
char *repl_dest;
repl_dest = mc_search_prepare_replace_str2 (ctx->search_handle, dest);
if (ctx->search_handle->error == MC_SEARCH_E_OK)
s = mc_build_filename (repl_dest, q, (char *) NULL);
else
{
if (ctx->search_handle->error_str != NULL)
message (D_ERROR, MSG_ERROR, "%s", ctx->search_handle->error_str);
*status = FILE_ABORT;
}
g_free (repl_dest);
}
g_free (q);
return s;
}
/* --------------------------------------------------------------------------------------------- */
static void
free_link (void *data)
{
link_t *lp = (link_t *) data;
vfs_path_free (lp->src_vpath, TRUE);
vfs_path_free (lp->dst_vpath, TRUE);
g_free (lp);
}
/* --------------------------------------------------------------------------------------------- */
static inline void *
free_erase_list (GQueue *lp)
{
if (lp != NULL)
g_queue_free_full (lp, free_link);
return NULL;
}
/* --------------------------------------------------------------------------------------------- */
static inline void *
free_linklist (GSList *lp)
{
g_slist_free_full (lp, free_link);
return NULL;
}
/* --------------------------------------------------------------------------------------------- */
static const link_t *
is_in_linklist (const GSList *lp, const vfs_path_t *vpath, const struct stat *sb)
{
const struct vfs_class *class;
ino_t ino = sb->st_ino;
dev_t dev = sb->st_dev;
class = vfs_path_get_last_path_vfs (vpath);
for (; lp != NULL; lp = (const GSList *) g_slist_next (lp))
{
const link_t *lnk = (const link_t *) lp->data;
if (lnk->vfs == class && lnk->ino == ino && lnk->dev == dev)
return lnk;
}
return NULL;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Check and made hardlink
*
* @return FALSE if the inode wasn't found in the cache and TRUE if it was found
* and a hardlink was successfully made
*/
static hardlink_status_t
check_hardlinks (file_op_context_t *ctx, const vfs_path_t *src_vpath, const struct stat *src_stat,
const vfs_path_t *dst_vpath, gboolean *ignore_all)
{
link_t *lnk;
ino_t ino = src_stat->st_ino;
dev_t dev = src_stat->st_dev;
if (src_stat->st_nlink < 2)
return HARDLINK_NOTLINK;
if ((vfs_file_class_flags (src_vpath) & VFSF_NOLINKS) != 0)
return HARDLINK_UNSUPPORTED;
lnk = (link_t *) is_in_linklist (linklist, src_vpath, src_stat);
if (lnk != NULL)
{
int stat_result;
struct stat link_stat;
stat_result = mc_stat (lnk->src_vpath, &link_stat);
if (stat_result == 0 && link_stat.st_ino == ino && link_stat.st_dev == dev)
{
const struct vfs_class *lp_name_class;
const struct vfs_class *my_vfs;
lp_name_class = vfs_path_get_last_path_vfs (lnk->src_vpath);
my_vfs = vfs_path_get_last_path_vfs (src_vpath);
if (lp_name_class == my_vfs)
{
const struct vfs_class *p_class, *dst_name_class;
dst_name_class = vfs_path_get_last_path_vfs (dst_vpath);
p_class = vfs_path_get_last_path_vfs (lnk->dst_vpath);
if (dst_name_class == p_class)
{
gboolean ok;
while (!(ok = (mc_stat (lnk->dst_vpath, &link_stat) == 0)) && !*ignore_all)
{
FileProgressStatus status;
status = file_error (ctx, TRUE, _ ("Cannot stat hardlink source file\n%s"),
vfs_path_as_str (lnk->dst_vpath));
if (status == FILE_ABORT)
return HARDLINK_ABORT;
if (status == FILE_RETRY)
continue;
if (status == FILE_IGNORE_ALL)
*ignore_all = TRUE;
break;
}
// if stat() finished unsuccessfully, don't try to create link
if (!ok)
return HARDLINK_ERROR;
while (!(ok = (mc_link (lnk->dst_vpath, dst_vpath) == 0)) && !*ignore_all)
{
FileProgressStatus status;
status = file_error (ctx, TRUE, _ ("Cannot create target hardlink\n%s"),
vfs_path_as_str (dst_vpath));
if (status == FILE_ABORT)
return HARDLINK_ABORT;
if (status == FILE_RETRY)
continue;
if (status == FILE_IGNORE_ALL)
*ignore_all = TRUE;
break;
}
// Success?
return (ok ? HARDLINK_OK : HARDLINK_ERROR);
}
}
}
if (!*ignore_all)
{
FileProgressStatus status;
/* Message w/o "Retry" action.
*
* FIXME: Can't say what errno is here. Define it and don't display.
*
* file_error() displays a message with text representation of errno
* and the string passed to file_error() should provide the format "%s"
* for that at end (see previous file_error() call for the reference).
* But if format for errno isn't provided, it is safe, because C standard says:
* "If the format is exhausted while arguments remain, the excess arguments
* are evaluated (as always) but are otherwise ignored" (ISO/IEC 9899:1999,
* section 7.19.6.1, paragraph 2).
*
*/
errno = 0;
status = file_error (ctx, FALSE, _ ("Cannot create target hardlink\n%s"),
vfs_path_as_str (dst_vpath));
if (status == FILE_ABORT)
return HARDLINK_ABORT;
if (status == FILE_IGNORE_ALL)
*ignore_all = TRUE;
}
return HARDLINK_ERROR;
}
lnk = g_try_new (link_t, 1);
if (lnk != NULL)
{
lnk->vfs = vfs_path_get_last_path_vfs (src_vpath);
lnk->ino = ino;
lnk->dev = dev;
lnk->st_mode = 0;
lnk->src_vpath = vfs_path_clone (src_vpath);
lnk->dst_vpath = vfs_path_clone (dst_vpath);
linklist = g_slist_prepend (linklist, lnk);
}
return HARDLINK_CACHED;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Duplicate the contents of the symbolic link src_vpath in dst_vpath.
* Try to make a stable symlink if the option "stable symlink" was
* set in the file mask dialog.
* If dst_path is an existing symlink it will be deleted silently
* (upper levels take already care of existing files at dst_vpath).
*/
static FileProgressStatus
make_symlink (file_op_context_t *ctx, const vfs_path_t *src_vpath, const vfs_path_t *dst_vpath)
{
const char *src_path;
const char *dst_path;
char link_target[MC_MAXPATHLEN];
int len;
FileProgressStatus return_status;
struct stat dst_stat;
gboolean dst_is_symlink;
vfs_path_t *link_target_vpath = NULL;
src_path = vfs_path_as_str (src_vpath);
dst_path = vfs_path_as_str (dst_vpath);
dst_is_symlink = (mc_lstat (dst_vpath, &dst_stat) == 0) && S_ISLNK (dst_stat.st_mode);
retry_src_readlink:
len = mc_readlink (src_vpath, link_target, sizeof (link_target) - 1);
if (len < 0)
{
if (ctx->ignore_all)
return_status = FILE_IGNORE_ALL;
else
{
return_status = file_error (ctx, TRUE, _ ("Cannot read source link\n%s"), src_path);
if (return_status == FILE_IGNORE_ALL)
ctx->ignore_all = TRUE;
if (return_status == FILE_RETRY)
goto retry_src_readlink;
}
goto ret;
}
link_target[len] = '\0';
if (ctx->stable_symlinks && !(vfs_file_is_local (src_vpath) && vfs_file_is_local (dst_vpath)))
{
message (D_ERROR, MSG_ERROR,
_ ("Cannot make stable symlinks across "
"non-local filesystems:\n\nOption Stable Symlinks will be disabled"));
ctx->stable_symlinks = FALSE;
}
if (ctx->stable_symlinks && !g_path_is_absolute (link_target))
{
const char *r;
r = strrchr (src_path, PATH_SEP);
if (r != NULL)
{
size_t slen;
GString *p;
vfs_path_t *q;
slen = r - src_path + 1;
p = g_string_sized_new (slen + len);
g_string_append_len (p, src_path, slen);
if (g_path_is_absolute (dst_path))
q = vfs_path_from_str_flags (dst_path, VPF_NO_CANON);
else
q = vfs_path_build_filename (p->str, dst_path, (char *) NULL);
if (vfs_path_tokens_count (q) > 1)
{
char *s = NULL;
vfs_path_t *tmp_vpath1, *tmp_vpath2;
g_string_append_len (p, link_target, len);
tmp_vpath1 = vfs_path_vtokens_get (q, -1, 1);
tmp_vpath2 = vfs_path_from_str (p->str);
s = diff_two_paths (tmp_vpath1, tmp_vpath2);
vfs_path_free (tmp_vpath2, TRUE);
vfs_path_free (tmp_vpath1, TRUE);
g_strlcpy (link_target, s != NULL ? s : p->str, sizeof (link_target));
g_free (s);
}
g_string_free (p, TRUE);
vfs_path_free (q, TRUE);
}
}
link_target_vpath = vfs_path_from_str_flags (link_target, VPF_NO_CANON);
retry_dst_symlink:
if (mc_symlink (link_target_vpath, dst_vpath) == 0)
{
// Success
return_status = FILE_CONT;
goto ret;
}
/*
* if dst_exists, it is obvious that this had failed.
* We can delete the old symlink and try again...
*/
if (dst_is_symlink && mc_unlink (dst_vpath) == 0
&& mc_symlink (link_target_vpath, dst_vpath) == 0)
{
// Success
return_status = FILE_CONT;
goto ret;
}
if (ctx->ignore_all)
return_status = FILE_IGNORE_ALL;
else
{
return_status = file_error (ctx, TRUE, _ ("Cannot create target symlink\n%s"), dst_path);
if (return_status == FILE_IGNORE_ALL)
ctx->ignore_all = TRUE;
if (return_status == FILE_RETRY)
goto retry_dst_symlink;
}
ret:
vfs_path_free (link_target_vpath, TRUE);
return return_status;
}
/* --------------------------------------------------------------------------------------------- */
/**
* do_compute_dir_size:
*
* Computes the number of bytes used by the files in a directory
*/
static FileProgressStatus
do_compute_dir_size (const vfs_path_t *dirname_vpath, dirsize_status_msg_t *dsm, size_t *dir_count,
size_t *ret_marked, uintmax_t *ret_total, mc_stat_fn stat_func)
{
static gint64 timestamp = 0;
// update with 25 FPS rate
static const gint64 delay = G_USEC_PER_SEC / 25;
status_msg_t *sm = STATUS_MSG (dsm);
int res;
struct stat s;
DIR *dir;
struct vfs_dirent *dirent;
FileProgressStatus ret = FILE_CONT;
(*dir_count)++;
dir = mc_opendir (dirname_vpath);
if (dir == NULL)
return ret;
while (ret == FILE_CONT && (dirent = mc_readdir (dir)) != NULL)
{
vfs_path_t *tmp_vpath;
if (DIR_IS_DOT (dirent->d_name) || DIR_IS_DOTDOT (dirent->d_name))
continue;
tmp_vpath = vfs_path_append_new (dirname_vpath, dirent->d_name, (char *) NULL);
res = stat_func (tmp_vpath, &s);
if (res == 0)
{
if (S_ISDIR (s.st_mode))
ret = do_compute_dir_size (tmp_vpath, dsm, dir_count, ret_marked, ret_total,
stat_func);
else
{
ret = FILE_CONT;
(*ret_marked)++;
*ret_total += (uintmax_t) s.st_size;
}
if (ret == FILE_CONT && sm->update != NULL && mc_time_elapsed (×tamp, delay))
{
dsm->dirname_vpath = tmp_vpath;
dsm->dir_count = *dir_count;
dsm->total_size = *ret_total;
ret = sm->update (sm);
}
}
vfs_path_free (tmp_vpath, TRUE);
}
mc_closedir (dir);
return ret;
}
/* --------------------------------------------------------------------------------------------- */
/**
* panel_compute_totals:
*
* compute the number of files and the number of bytes
* used up by the whole selection, recursing directories
* as required. In addition, it checks to see if it will
* overwrite any files by doing the copy.
*/
static FileProgressStatus
panel_compute_totals (const WPanel *panel, dirsize_status_msg_t *sm, size_t *ret_count,
uintmax_t *ret_total, gboolean follow_symlinks)
{
int i;
size_t dir_count = 0;
mc_stat_fn stat_func = follow_symlinks ? mc_stat : mc_lstat;
for (i = 0; i < panel->dir.len; i++)
{
const file_entry_t *fe = &panel->dir.list[i];
const struct stat *s;
if (fe->f.marked == 0)
continue;
s = &fe->st;
if (S_ISDIR (s->st_mode) || (follow_symlinks && link_isdir (fe) && fe->f.stale_link == 0))
{
vfs_path_t *p;
FileProgressStatus status;
p = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
status = do_compute_dir_size (p, sm, &dir_count, ret_count, ret_total, stat_func);
vfs_path_free (p, TRUE);
if (status != FILE_CONT)
return status;
}
else
{
(*ret_count)++;
*ret_total += (uintmax_t) s->st_size;
}
}
return FILE_CONT;
}
/* --------------------------------------------------------------------------------------------- */
/** Initialize variables for progress bars */
static FileProgressStatus
panel_operate_init_totals (const WPanel *panel, const vfs_path_t *source,
const struct stat *source_stat, file_op_context_t *ctx,
gboolean compute_totals, filegui_dialog_type_t dialog_type)
{
FileProgressStatus status;
#ifdef ENABLE_BACKGROUND
if (mc_global.we_are_background)
return FILE_CONT;
#endif
if (verbose && compute_totals)
{
dirsize_status_msg_t dsm;
gboolean stale_link = FALSE;
memset (&dsm, 0, sizeof (dsm));
dsm.allow_skip = TRUE;
status_msg_init (STATUS_MSG (&dsm), _ ("Directory scanning"), 0, dirsize_status_init_cb,
dirsize_status_update_cb, dirsize_status_deinit_cb);
ctx->total_count = 0;
ctx->total_bytes = 0;
if (source == NULL)
status = panel_compute_totals (panel, &dsm, &ctx->total_count, &ctx->total_bytes,
ctx->follow_links);
else if (S_ISDIR (source_stat->st_mode)
|| (ctx->follow_links
&& file_is_symlink_to_dir (source, (struct stat *) source_stat, &stale_link)
&& !stale_link))
{
size_t dir_count = 0;
status = do_compute_dir_size (source, &dsm, &dir_count, &ctx->total_count,
&ctx->total_bytes, ctx->stat_func);
}
else
{
ctx->total_count++;
ctx->total_bytes += (uintmax_t) source_stat->st_size;
status = FILE_CONT;
}
status_msg_deinit (STATUS_MSG (&dsm));
ctx->totals_computed = (status == FILE_CONT);
if (status == FILE_SKIP)
status = FILE_CONT;
}
else
{
status = FILE_CONT;
ctx->total_count = panel->marked;
ctx->total_bytes = panel->total;
ctx->totals_computed = verbose && dialog_type == FILEGUI_DIALOG_ONE_ITEM;
}
// destroy already created UI for single file rename operation
file_progress_ui_destroy (ctx);
file_progress_ui_create (ctx, TRUE, dialog_type);
return status;
}
/* --------------------------------------------------------------------------------------------- */
static void
progress_update_one (gboolean success, file_op_context_t *ctx, off_t add)
{
gint64 tv_current;
static gint64 tv_start = -1;
ctx->total_progress_count++;
ctx->total_progress_bytes += (uintmax_t) add;
if (!success)
return;
tv_current = g_get_monotonic_time ();
if (tv_start < 0)
tv_start = tv_current;
else if (tv_current - tv_start > FILEOP_UPDATE_INTERVAL_US)
{
if (verbose && ctx->dialog_type == FILEGUI_DIALOG_MULTI_ITEM)
{
file_progress_show_count (ctx);
file_progress_show_total (ctx, ctx->total_progress_bytes, tv_current, TRUE);
}
tv_start = tv_current;
}
}
/* --------------------------------------------------------------------------------------------- */
static FileProgressStatus
real_warn_same_file (file_op_context_t *ctx, enum OperationMode mode, const char *fmt,
const char *a, const char *b)
{
char *msg;
int result = 0;
const char *head_msg;
int width_a, width_b, width;
const gint64 t = g_get_monotonic_time ();
head_msg = mode == Foreground ? MSG_ERROR : _ ("Background process error");
width_a = str_term_width1 (a);
width_b = str_term_width1 (b);
width = COLS - 8;
if (width_a > width)
{
if (width_b > width)
{
char *s;
s = g_strndup (str_trunc (a, width), width);
b = str_trunc (b, width);
msg = g_strdup_printf (fmt, s, b);
g_free (s);
}
else
{
a = str_trunc (a, width);
msg = g_strdup_printf (fmt, a, b);
}
}
else
{
if (width_b > width)
b = str_trunc (b, width);
msg = g_strdup_printf (fmt, a, b);
}
result = query_dialog (head_msg, msg, D_ERROR, 2, _ ("&Skip"), _ ("&Abort"));
g_free (msg);
do_refresh ();
ctx->pauses += g_get_monotonic_time () - t;
return (result == 1) ? FILE_ABORT : FILE_SKIP;
}
/* --------------------------------------------------------------------------------------------- */
static FileProgressStatus
warn_same_file (file_op_context_t *ctx, const char *fmt, const char *a, const char *b)
{
#ifdef ENABLE_BACKGROUND
union
{
void *p;
FileProgressStatus (*f) (file_op_context_t *ctx, enum OperationMode, const char *fmt,
const char *a, const char *b);
} pntr;
pntr.f = real_warn_same_file;
if (mc_global.we_are_background)
return parent_call (pntr.p, ctx, 3, strlen (fmt), fmt, strlen (a), a, strlen (b), b);
#endif
return real_warn_same_file (ctx, Foreground, fmt, a, b);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
check_same_file (file_op_context_t *ctx, const char *a, const struct stat *ast, const char *b,
const struct stat *bst, FileProgressStatus *status)
{
if (ast->st_dev != bst->st_dev || ast->st_ino != bst->st_ino)
return FALSE;
if (S_ISDIR (ast->st_mode))
*status = warn_same_file (ctx, _ ("\"%s\"\nand\n\"%s\"\nare the same directory"), a, b);
else
*status = warn_same_file (ctx, _ ("\"%s\"\nand\n\"%s\"\nare the same file"), a, b);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/* {{{ Query/status report routines */
static FileProgressStatus
real_do_file_error (file_op_context_t *ctx, enum OperationMode mode, gboolean allow_retry,
const char *error)
{
gint64 t = 0;
int result;
const char *msg;
if (ctx != NULL)
t = g_get_monotonic_time ();
msg = mode == Foreground ? MSG_ERROR : _ ("Background process error");
if (allow_retry)
result = query_dialog (msg, error, D_ERROR, 4, _ ("&Ignore"), _ ("Ignore a&ll"),
_ ("&Retry"), _ ("&Abort"));
else
result =
query_dialog (msg, error, D_ERROR, 3, _ ("&Ignore"), _ ("Ignore a&ll"), _ ("&Abort"));
if (ctx != NULL)
ctx->pauses += g_get_monotonic_time () - t;
switch (result)
{
case 0:
do_refresh ();
return FILE_IGNORE;
case 1:
do_refresh ();
return FILE_IGNORE_ALL;
case 2:
if (allow_retry)
{
do_refresh ();
return FILE_RETRY;
}
MC_FALLTHROUGH;
case 3:
default:
return FILE_ABORT;
}
}
/* --------------------------------------------------------------------------------------------- */
static FileProgressStatus
real_query_recursive (file_op_context_t *ctx, enum OperationMode mode, const char *s)
{
if (ctx->recursive_result < RECURSIVE_ALWAYS)
{
const char *msg;
char *text;
const gint64 t = g_get_monotonic_time ();
msg = mode == Foreground
? _ ("Directory\n%s\nis not empty.\nDelete it recursively?")
: _ ("Background process:\nDirectory\n%s\nis not empty.\nDelete it recursively?");
// delete password and try to show a full path
text = g_strdup_printf (msg, path_trunc (s, -1));
if (safe_delete)
query_set_sel (1);
ctx->recursive_result = query_dialog (op_names[OP_DELETE], text, D_ERROR, 5, _ ("&Yes"),
_ ("&No"), _ ("A&ll"), _ ("Non&e"), _ ("&Abort"));
g_free (text);