-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathdelete.cpp
More file actions
1127 lines (940 loc) · 29.3 KB
/
delete.cpp
File metadata and controls
1127 lines (940 loc) · 29.3 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
/*
delete.cpp
Удаление файлов
*/
/*
Copyright © 1996 Eugene Roshal
Copyright © 2000 Far Group
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// BUGBUG
#include "platform.headers.hpp"
// Self:
#include "delete.hpp"
// Internal:
#include "flink.hpp"
#include "scantree.hpp"
#include "treelist.hpp"
#include "constitle.hpp"
#include "taskbar.hpp"
#include "interf.hpp"
#include "keyboard.hpp"
#include "message.hpp"
#include "config.hpp"
#include "pathmix.hpp"
#include "dirmix.hpp"
#include "panelmix.hpp"
#include "mix.hpp"
#include "dirinfo.hpp"
#include "wakeful.hpp"
#include "stddlg.hpp"
#include "lang.hpp"
#include "FarDlgBuilder.hpp"
#include "strmix.hpp"
#include "uuids.far.dialogs.hpp"
#include "cvtname.hpp"
#include "fileattr.hpp"
#include "copy_progress.hpp"
#include "global.hpp"
#include "log.hpp"
// Platform:
#include "platform.fs.hpp"
// Common:
#include "common/scope_exit.hpp"
#include "common/view/enumerate.hpp"
// External:
#include "format.hpp"
//----------------------------------------------------------------------------
struct total_items
{
size_t Items{};
size_t Size{};
};
struct progress
{
size_t Value;
size_t Total;
};
class delete_progress: progress_impl
{
enum
{
DlgW = 76,
DlgH = 10,
};
enum items
{
pr_console_title,
pr_doublebox,
pr_message,
pr_file,
pr_wipe_progress,
pr_separator,
pr_total_files,
pr_total_progress,
pr_count
};
public:
delete_progress(bool const Wipe, bool const Total)
{
auto ProgressDlgItems = MakeDialogItems<items::pr_count>(
{
{ DI_TEXT, {{ 0, 0 }, { 0, 0 }}, DIF_HIDDEN, {}, },
{ DI_DOUBLEBOX, {{ 3, 1 }, { DlgW - 4, DlgH - 2 }}, DIF_NONE, msg(Wipe? lng::MDeleteWipeTitle : lng::MDeleteTitle), },
{ DI_TEXT, {{ 5, 2 }, { DlgW - 6, 2 }}, DIF_NONE, msg(Wipe? lng::MDeletingWiping : lng::MDeleting) },
{ DI_TEXT, {{ 5, 3 }, { DlgW - 6, 3 }}, DIF_NONE, {} },
{ DI_TEXT, {{ 5, 4 }, { DlgW - 6, 4 }}, DIF_NONE, {} },
{ DI_TEXT, {{ 5, 5 }, { DlgW - 6, 5 }}, DIF_SEPARATOR, {} },
{ DI_TEXT, {{ 5, 6 }, { DlgW - 6, 6 }}, DIF_NONE, {} },
{ DI_TEXT, {{ 5, 7 }, { DlgW - 6, 7 }}, DIF_NONE, {} },
});
if (!Wipe)
{
ProgressDlgItems[items::pr_wipe_progress].Flags |= DIF_HIDDEN;
for (const auto& i: irange(pr_separator, pr_total_progress + 1))
{
--ProgressDlgItems[i].Y1;
--ProgressDlgItems[i].Y2;
}
--ProgressDlgItems[items::pr_doublebox].Y2;
}
if (!Total)
{
ProgressDlgItems[items::pr_total_progress].Flags |= DIF_HIDDEN;
--ProgressDlgItems[items::pr_doublebox].Y2;
ProgressDlgItems[items::pr_console_title].strData = ProgressDlgItems[items::pr_doublebox].strData;
}
const int DialogHeight = ProgressDlgItems[items::pr_doublebox].Y2 - ProgressDlgItems[items::pr_doublebox].Y1 + 1 + 2;
init(ProgressDlgItems, { -1, -1, DlgW, DialogHeight });
}
void set_wipe_percent(size_t const Percent) const
{
m_Dialog->SendMessage(DM_SETTEXTPTR, items::pr_wipe_progress, UNSAFE_CSTR(make_progressbar(DlgW - 10, Percent, true, true)));
}
void update(string_view const Name, progress const Files) const
{
m_Dialog->SendMessage(DM_SETTEXTPTR, items::pr_file, UNSAFE_CSTR(null_terminated(Name)));
if (Files.Total)
{
const auto Percent = ToPercent(Files.Value, Files.Total);
const auto Title = view_as<const wchar_t*>(m_Dialog->SendMessage(DM_GETCONSTTEXTPTR, items::pr_doublebox, {}));
m_Dialog->SendMessage(DM_SETTEXTPTR, items::pr_console_title, UNSAFE_CSTR(concat(L'{', str(Percent), L"%} "sv, Title)));
m_Dialog->SendMessage(DM_SETTEXTPTR, items::pr_total_progress, UNSAFE_CSTR(make_progressbar(DlgW - 10, Percent, true, true)));
}
const auto Str = copy_progress::FormatCounter(lng::MCopyFilesTotalInfo, lng::MCopyBytesTotalInfo, Files.Value, Files.Total, Files.Total != 0, copy_progress::CanvasWidth() - 5);
m_Dialog->SendMessage(DM_SETTEXTPTR, items::pr_total_files, UNSAFE_CSTR(Str));
}
};
class ShellDelete : noncopyable
{
public:
ShellDelete(panel_ptr SrcPanel, delete_type Type);
private:
bool ConfirmDeleteReadOnlyFile(string_view Name, os::fs::attributes Attr);
bool ShellRemoveFile(string_view Name, progress Files, delete_progress const& Progress);
bool ERemoveDirectory(string_view Name, delete_type Type, bool& RetryRecycleAsRemove);
bool RemoveToRecycleBin(string_view Name, bool dir, bool& RetryRecycleAsRemove, bool& Skip);
void process_item(
panel_ptr SrcPanel,
const os::fs::find_data& SelFindData,
const total_items& Total,
delete_progress const& Progress,
const time_check& TimeCheck,
bool CannotRecycleTryRemove = false
);
std::optional<bool>
m_DeleteReadOnly,
m_SkipWipe;
bool m_SkipFileErrors{};
bool m_SkipFolderErrors{};
bool m_DeleteFolders{};
unsigned ProcessedItems{};
bool m_UpdateDiz{};
delete_type m_DeleteType;
};
static bool EraseFileData(string_view const Name, progress Files, delete_progress const& Progress)
{
os::fs::file_walker File;
if (!File.Open(Name, FILE_READ_DATA | FILE_WRITE_DATA, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_SEQUENTIAL_SCAN))
return false;
unsigned long long FileSize;
if (!File.GetSize(FileSize))
return false;
if (!FileSize)
return true; // nothing to do here
const DWORD BufSize=65536;
if (!File.InitWalk(BufSize))
return false;
const time_check TimeCheck(time_check::mode::immediate);
std::mt19937 mt(clock()); // std::random_device doesn't work in w2k
std::uniform_int_distribution CharDist(0, UCHAR_MAX);
auto BufInit = false;
do
{
static std::array<BYTE, BufSize> Buf;
if (!BufInit)
{
if (Global->Opt->WipeSymbol == -1)
{
std::generate(ALL_RANGE(Buf), [&]{ return CharDist(mt); });
}
else
{
Buf.fill(Global->Opt->WipeSymbol);
BufInit = true;
}
}
if (!File.Write(Buf.data(), File.GetChunkSize()))
return false;
if (TimeCheck)
{
if (CheckForEscAndConfirmAbort())
cancel_operation();
Progress.update(Name, Files);
Progress.set_wipe_percent(File.GetPercent());
}
}
while(File.Step());
if (!File.SetPointer(0, nullptr, FILE_BEGIN))
return false;
if (!File.SetEnd())
return false;
return true;
}
static bool EraseFile(string_view const Name, progress Files, delete_progress const& Progress)
{
if (!os::fs::set_file_attributes(Name, FILE_ATTRIBUTE_NORMAL))
return false;
if (!EraseFileData(Name, Files, Progress))
return false;
const auto strTempName = MakeTemp({}, false);
if (!os::fs::move_file(Name, strTempName))
return false;
return os::fs::delete_file(strTempName);
}
static bool EraseDirectory(string_view const Name)
{
auto Path = Name;
if (!CutToParent(Path))
{
Path = {};
}
const auto strTempName = MakeTemp({}, false, Path);
if (!os::fs::move_file(Name, strTempName))
{
return false;
}
return os::fs::remove_directory(strTempName);
}
static void show_confirmation(
panel_ptr const SrcPanel,
delete_type const DeleteType,
size_t const SelCount,
const os::fs::find_data& SingleSelData
)
{
if (!Global->Opt->Confirm.Delete)
return;
lng TitleId, MessageId, ButtonId;
const UUID* Id;
if (DeleteType == delete_type::erase)
{
TitleId = lng::MDeleteWipeTitle;
MessageId = lng::MAskWipe;
ButtonId = lng::MDeleteWipe;
Id = &DeleteWipeId;
}
else if (DeleteType == delete_type::remove)
{
TitleId = lng::MDeleteTitle;
MessageId = lng::MAskDelete;
ButtonId = lng::MDelete;
Id = &DeleteFileFolderId;
}
else if (DeleteType == delete_type::recycle)
{
TitleId = lng::MDeleteTitle;
MessageId = lng::MAskDeleteRecycle;
ButtonId = lng::MDeleteRecycle;
Id = &DeleteRecycleId;
}
else
UNREACHABLE;
std::vector<string> items;
bool HighlightSelected = Global->Opt->DelOpt.HighlightSelected;
if (SelCount == 1)
{
const auto IsFolder = (SingleSelData.Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
items.emplace_back(format(msg(MessageId), msg(IsFolder? lng::MAskDeleteFolder : lng::MAskDeleteFile)));
items.emplace_back(QuoteOuterSpace(SingleSelData.FileName));
if (SingleSelData.Attributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
Id = &DeleteLinkId;
auto FullName = ConvertNameToFull(SingleSelData.FileName);
if (GetReparsePointInfo(FullName, FullName))
NormalizeSymlinkName(FullName);
items.emplace_back(msg(lng::MAskDeleteLink));
items.emplace_back(std::move(FullName));
}
if (HighlightSelected)
{
string name, sname;
if (SrcPanel->GetCurName(name, sname))
{
inplace::QuoteOuterSpace(name);
HighlightSelected = SingleSelData.FileName != name;
}
}
}
else
{
items.emplace_back(format(msg(MessageId), msg(lng::MAskDeleteObjects)));
const auto ItemsToShow = std::min(std::min(std::max(static_cast<size_t>(Global->Opt->DelOpt.ShowSelected), size_t{ 1 }), SelCount), size_t{ScrY / 2u});
const auto ItemsMore = SelCount - ItemsToShow;
for (const auto& i: SrcPanel->enum_selected())
{
items.emplace_back(QuoteOuterSpace(i.FileName));
if (items.size() - 1 == ItemsToShow)
{
if (ItemsMore)
items.emplace_back(format(msg(lng::MAskDeleteAndMore), ItemsMore));
break;
}
}
}
intptr_t FirstHighlighted = 0, LastHighlighted = 0;
DialogBuilder Builder(TitleId, {}, [&](Dialog* Dlg, intptr_t Msg, intptr_t Param1, void* Param2)
{
if (HighlightSelected && Msg == DN_CTLCOLORDLGITEM && in_closed_range(FirstHighlighted, Param1, LastHighlighted))
{
auto& Colors = *static_cast<const FarDialogItemColors*>(Param2);
Colors.Colors[0] = Colors.Colors[1];
}
return Dlg->DefProc(Msg, Param1, Param2);
});
const auto MaxWidth = ScrX + 1 - 6 * 2;
if (SelCount == 1)
{
for (const auto& [Item, Index]: enumerate(items))
{
inplace::truncate_center(Item, MaxWidth);
Builder.AddText(Item).Flags = DIF_CENTERTEXT | DIF_SHOWAMPERSAND;
if (Index == 1)
FirstHighlighted = LastHighlighted = Builder.GetLastID();
}
}
else
{
for (const auto& [Item, Index]: enumerate(items))
{
if (Index == 1)
Builder.AddSeparator();
inplace::truncate_center(Item, MaxWidth);
Builder.AddText(Item).Flags = DIF_SHOWAMPERSAND;
if (Index == 1)
FirstHighlighted = Builder.GetLastID();
}
LastHighlighted = Builder.GetLastID();
}
Builder.AddOKCancel(ButtonId, lng::MCancel);
Builder.SetId(*Id);
if (DeleteType != delete_type::recycle)
Builder.SetDialogMode(DMODE_WARNINGSTYLE);
if (!Builder.ShowDialog())
cancel_operation();
}
static total_items calculate_total(panel_ptr const SrcPanel)
{
const time_check TimeCheck;
total_items Total;
dirinfo_progress const DirinfoProgress(msg(lng::MDeletingTitle));
const auto DirInfoCallback = [&](string_view const Name, unsigned long long const ItemsCount, unsigned long long const Size)
{
if (!TimeCheck)
return;
DirinfoProgress.set_name(Name);
DirinfoProgress.set_count(Total.Items + ItemsCount);
DirinfoProgress.set_size(Total.Size + Size);
};
// BUGBUG
for (const auto& i: SrcPanel->enum_selected())
{
++Total.Items;
if (i.Attributes & FILE_ATTRIBUTE_DIRECTORY && !os::fs::is_directory_symbolic_link(i))
{
DirInfoData Data{};
if (GetDirInfo(i.FileName, Data, nullptr, DirInfoCallback, 0) <= 0)
return {};
Total.Items += Data.FileCount + Data.DirCount;
Total.Size += Data.FileSize;
}
}
return Total;
}
void ShellDelete::process_item(
panel_ptr const SrcPanel,
const os::fs::find_data& SelFindData,
const total_items& Total,
delete_progress const& Progress,
const time_check& TimeCheck,
bool const CannotRecycleTryRemove
)
{
const auto& strSelName = SelFindData.FileName;
const auto& strSelShortName = SelFindData.AlternateFileName();
if (strSelName.empty() || IsRelativeRoot(strSelName) || IsRootPath(strSelName))
return;
if (TimeCheck)
{
if (CheckForEscAndConfirmAbort())
cancel_operation();
Progress.update(strSelName, { ProcessedItems, Total.Items });
}
if (!(SelFindData.Attributes & FILE_ATTRIBUTE_DIRECTORY))
{
if (ConfirmDeleteReadOnlyFile(strSelName, SelFindData.Attributes))
{
if (ShellRemoveFile(strSelName, { ProcessedItems, Total.Items }, Progress) && m_UpdateDiz)
SrcPanel->DeleteDiz(strSelName, strSelShortName);
}
return;
}
const auto DirSymLink = os::fs::is_directory_symbolic_link(SelFindData);
if (!m_DeleteFolders && !CannotRecycleTryRemove)
{
const auto strFullName = ConvertNameToFull(strSelName);
if (os::fs::is_not_empty_directory(strFullName))
{
auto MsgCode = message_result::first_button; // для symlink не нужно подтверждение
if (!DirSymLink)
{
auto Uuid = &DeleteFolderId;
auto
TitleId = lng::MDeleteFolderTitle,
ConfirmId = lng::MDeleteFolderConfirm,
DeleteId = lng::MDeleteFileDelete;
if (m_DeleteType == delete_type::erase)
{
TitleId = lng::MWipeFolderTitle;
ConfirmId = lng::MWipeFolderConfirm;
DeleteId = lng::MDeleteFileWipe;
Uuid = &WipeFolderId;
}
else if (m_DeleteType == delete_type::recycle)
{
ConfirmId = lng::MRecycleFolderConfirm;
DeleteId = lng::MDeleteRecycle;
Uuid = &DeleteFolderRecycleId;
}
MsgCode=Message(MSG_WARNING,
msg(TitleId),
{
msg(ConfirmId),
strFullName
},
{ DeleteId, lng::MDeleteFileAll, lng::MDeleteFileSkip, lng::MDeleteFileCancel },
{}, Uuid);
}
if (MsgCode == message_result::first_button)
{
// Nop
}
else if (MsgCode == message_result::second_button)
{
m_DeleteFolders = true;
}
else if (MsgCode == message_result::third_button)
{
return;
}
else
{
cancel_operation();
}
}
}
if (!DirSymLink && m_DeleteType != delete_type::recycle)
{
ScanTree ScTree(true, true, FALSE);
const auto strSelFullName = IsAbsolutePath(strSelName)?
strSelName :
path::join(SrcPanel->GetCurDir(), strSelName);
ScTree.SetFindPath(strSelFullName, L"*"sv);
const time_check TreeTimeCheck(time_check::mode::immediate);
os::fs::find_data FindData;
string strFullName;
while (ScTree.GetNextName(FindData,strFullName))
{
if (TreeTimeCheck)
{
if (CheckForEscAndConfirmAbort())
cancel_operation();
Progress.update(strFullName, { ProcessedItems, Total.Items });
}
if (FindData.Attributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (os::fs::is_directory_symbolic_link(FindData))
{
if (FindData.Attributes & FILE_ATTRIBUTE_READONLY && !os::fs::set_file_attributes(strFullName, FILE_ATTRIBUTE_NORMAL)) //BUGBUG
{
LOGWARNING(L"set_file_attributes({}): {}"sv, strFullName, last_error());
}
bool Dummy = false;
if (!ERemoveDirectory(strFullName, m_DeleteType, Dummy))
{
ScTree.SkipDir();
continue;
}
TreeList::DelTreeName(strFullName);
if (m_UpdateDiz)
SrcPanel->DeleteDiz(strFullName,strSelShortName);
continue;
}
if (!m_DeleteFolders && !ScTree.IsDirSearchDone() && os::fs::is_not_empty_directory(strFullName))
{
const auto MsgCode = Message(MSG_WARNING,
msg(m_DeleteType == delete_type::erase? lng::MWipeFolderTitle : lng::MDeleteFolderTitle),
{
msg(m_DeleteType == delete_type::erase? lng::MWipeFolderConfirm : lng::MDeleteFolderConfirm),
strFullName
},
{ m_DeleteType == delete_type::erase? lng::MDeleteFileWipe : lng::MDeleteFileDelete, lng::MDeleteFileAll, lng::MDeleteFileSkip, lng::MDeleteFileCancel },
{}, m_DeleteType == delete_type::erase? &WipeFolderId : &DeleteFolderId); // ??? other UUID ???
if (MsgCode == message_result::first_button)
{
// Nop
}
else if (MsgCode == message_result::second_button)
{
m_DeleteFolders = true;
}
else if (MsgCode == message_result::third_button)
{
ScTree.SkipDir();
continue;
}
else
{
cancel_operation();
}
}
if (ScTree.IsDirSearchDone())
{
if (FindData.Attributes & FILE_ATTRIBUTE_READONLY && !os::fs::set_file_attributes(strFullName, FILE_ATTRIBUTE_NORMAL)) //BUGBUG
{
LOGWARNING(L"set_file_attributes({}): {}"sv, strFullName, last_error());
}
bool Dummy = false;
if (!ERemoveDirectory(strFullName, m_DeleteType, Dummy))
{
//ScTree.SkipDir();
continue;
}
TreeList::DelTreeName(strFullName);
}
}
else
{
if (ConfirmDeleteReadOnlyFile(strFullName,FindData.Attributes))
{
// BUGBUG check result
ShellRemoveFile(strFullName, { ProcessedItems, Total.Items }, Progress);
}
}
}
}
if (SelFindData.Attributes & FILE_ATTRIBUTE_READONLY && !os::fs::set_file_attributes(strSelName, FILE_ATTRIBUTE_NORMAL)) //BUGBUG
{
LOGWARNING(L"set_file_attributes({}): {}"sv, strSelName, last_error());
}
bool RetryRecycleAsRemove = false;
if (ERemoveDirectory(
strSelName,
m_DeleteType == delete_type::recycle && DirSymLink && !IsWindowsVistaOrGreater()?
delete_type::remove :
m_DeleteType,
RetryRecycleAsRemove
))
{
TreeList::DelTreeName(strSelName);
if (m_UpdateDiz)
SrcPanel->DeleteDiz(strSelName,strSelShortName);
}
else if (RetryRecycleAsRemove)
{
--ProcessedItems;
m_DeleteType = delete_type::remove;
process_item(SrcPanel, SelFindData, Total, Progress, TimeCheck, true);
m_DeleteType = delete_type::recycle;
}
}
ShellDelete::ShellDelete(panel_ptr SrcPanel, delete_type const Type):
m_DeleteFolders(!Global->Opt->Confirm.DeleteFolder),
m_UpdateDiz(Global->Opt->Diz.UpdateMode == DIZ_UPDATE_ALWAYS || (SrcPanel->IsDizDisplayed() && Global->Opt->Diz.UpdateMode == DIZ_UPDATE_IF_DISPLAYED)),
m_DeleteType(Type)
{
if (m_UpdateDiz)
SrcPanel->ReadDiz();
const auto strDizName = SrcPanel->GetDizName();
const auto CheckDiz = [&] { return !strDizName.empty() && os::fs::exists(strDizName); };
const auto DizPresent = CheckDiz();
const auto SelCount = SrcPanel->GetSelCount();
if (!SelCount)
return;
os::fs::find_data SingleSelData;
if (!SrcPanel->get_first_selected(SingleSelData))
return;
if (m_DeleteType == delete_type::recycle && os::fs::drive::get_type(GetPathRoot(ConvertNameToFull(SingleSelData.FileName))) != DRIVE_FIXED)
m_DeleteType = delete_type::remove;
show_confirmation(SrcPanel, m_DeleteType, SelCount, SingleSelData);
const auto NeedSetUpADir = CheckUpdateAnotherPanel(SrcPanel, SingleSelData.FileName);
SCOPE_EXIT
{
if (m_UpdateDiz && DizPresent == CheckDiz())
SrcPanel->FlushDiz();
ShellUpdatePanels(SrcPanel, NeedSetUpADir);
};
if (SrcPanel->GetType() == panel_type::TREE_PANEL)
FarChDir(L"\\"sv);
ConsoleTitle::SetFarTitle(msg(lng::MDeletingTitle));
SCOPED_ACTION(taskbar::indeterminate);
SCOPED_ACTION(wakeful);
SetCursorType(false, 0);
delete_progress const Progress(m_DeleteType == delete_type::erase, Global->Opt->DelOpt.ShowTotal);
const auto Total = Global->Opt->DelOpt.ShowTotal? calculate_total(SrcPanel) : total_items{};
const time_check TimeCheck(time_check::mode::immediate);
for (const auto& i: SrcPanel->enum_selected())
{
process_item(SrcPanel, i, Total, Progress, TimeCheck);
}
}
bool ShellDelete::ConfirmDeleteReadOnlyFile(string_view const Name, os::fs::attributes Attr)
{
if (!(Attr & FILE_ATTRIBUTE_READONLY))
return true;
if (!Global->Opt->Confirm.RO)
m_DeleteReadOnly = true;
message_result MsgCode;
if (m_DeleteReadOnly)
{
MsgCode = *m_DeleteReadOnly? message_result::first_button : message_result::third_button;
}
else
{
static constexpr std::tuple
EraseData{ lng::MAskWipeRO, lng::MDeleteFileWipe, &DeleteAskWipeROId },
DeleteData{ lng::MAskDeleteRO, lng::MDeleteFileDelete, &DeleteAskDeleteROId };
const auto& [AskId, ButtonId, UidPtr] = m_DeleteType == delete_type::erase? EraseData : DeleteData;
MsgCode = Message(MSG_WARNING,
msg(lng::MWarning),
{
msg(lng::MDeleteRO),
string(Name),
msg(AskId)
},
{ ButtonId, lng::MDeleteFileAll, lng::MDeleteFileSkip, lng::MDeleteFileSkipAll, lng::MDeleteFileCancel },
{}, UidPtr);
}
switch (MsgCode)
{
case message_result::second_button:
m_DeleteReadOnly = true;
[[fallthrough]];
case message_result::first_button:
if (!os::fs::set_file_attributes(Name, FILE_ATTRIBUTE_NORMAL)) //BUGBUG
{
LOGWARNING(L"set_file_attributes({}): {}"sv, Name, last_error());
}
return true;
case message_result::fourth_button:
m_DeleteReadOnly = false;
[[fallthrough]];
case message_result::third_button:
return false;
default:
cancel_operation();
}
}
static bool confirm_erase_file_with_hardlinks(string_view const File, std::optional<bool>& Wipe)
{
message_result MsgCode;
if (Wipe)
{
MsgCode = *Wipe? message_result::first_button : message_result::third_button;
}
else
{
MsgCode = [&]
{
const auto Hardlinks = GetNumberOfLinks(File);
return !Hardlinks || *Hardlinks < 2?
message_result::first_button :
Message(MSG_WARNING,
msg(lng::MError),
{
string(File),
msg(lng::MDeleteHardLink1),
msg(lng::MDeleteHardLink2),
msg(lng::MDeleteHardLink3)
},
{ lng::MDeleteFileWipe, lng::MDeleteFileAll, lng::MDeleteFileSkip, lng::MDeleteFileSkipAll, lng::MDeleteCancel },
{}, &WipeHardLinkId);
}();
}
switch(MsgCode)
{
case message_result::second_button:
Wipe = true;
[[fallthrough]];
case message_result::first_button:
return true;
case message_result::fourth_button:
Wipe = false;
[[fallthrough]];
case message_result::third_button:
return false;
default:
cancel_operation();
}
}
static bool erase_file_with_retry(string_view const Name, std::optional<bool>& Wipe, progress const Files, delete_progress const& Progress, bool& SkipErrors)
{
return
confirm_erase_file_with_hardlinks(Name, Wipe) &&
retryable_ui_operation([&]{ return EraseFile(Name, Files, Progress); }, Name, lng::MCannotDeleteFile, SkipErrors);
}
static bool delete_file_with_retry(string_view const Name, bool& SkipErrors)
{
return retryable_ui_operation([&]{ return os::fs::delete_file(Name); }, Name, lng::MCannotDeleteFile, SkipErrors);
}
bool ShellDelete::ShellRemoveFile(string_view const Name, progress Files, delete_progress const& Progress)
{
ProcessedItems++;
const auto strFullName = ConvertNameToFull(Name);
switch (m_DeleteType)
{
case delete_type::erase:
return erase_file_with_retry(strFullName, m_SkipWipe, Files, Progress, m_SkipFileErrors);
case delete_type::remove:
return delete_file_with_retry(strFullName, m_SkipFileErrors);
case delete_type::recycle:
{
auto RetryRecycleAsRemove = false, Skip = false;
const auto Result = retryable_ui_operation([&]{ return RemoveToRecycleBin(strFullName, false, RetryRecycleAsRemove, Skip) || RetryRecycleAsRemove || Skip; }, strFullName, lng::MCannotRecycleFile, m_SkipFileErrors);
if (Result && !RetryRecycleAsRemove && !Skip)
return true;
if (RetryRecycleAsRemove)
return delete_file_with_retry(strFullName, m_SkipFileErrors);
return false;
}
default:
UNREACHABLE;
}
}
bool ShellDelete::ERemoveDirectory(string_view const Name, delete_type const Type, bool& RetryRecycleAsRemove)
{
ProcessedItems++;
switch (Type)
{
case delete_type::remove:
case delete_type::erase:
return retryable_ui_operation([&]{ return (Type == delete_type::erase? EraseDirectory : os::fs::remove_directory)(Name); }, Name, lng::MCannotDeleteFolder, m_SkipFolderErrors);
case delete_type::recycle:
{
auto Skip = false;
return
retryable_ui_operation([&]{ return RemoveToRecycleBin(Name, true, RetryRecycleAsRemove, Skip) || RetryRecycleAsRemove || Skip; }, Name, lng::MCannotRecycleFolder, m_SkipFolderErrors) &&
!Skip &&
!RetryRecycleAsRemove;
}
default:
UNREACHABLE;
}
}
static void break_links_for_old_os(string_view const Name)
{
// При удалении в корзину папки с симлинками получим траблу, если предварительно линки не убрать.
if (IsWindowsVistaOrGreater() || !os::fs::is_directory(Name))
return;
string strFullName2;
os::fs::find_data FindData;
ScanTree ScTree(true, true, FALSE);
ScTree.SetFindPath(Name, L"*"sv);
bool MessageShown = false;
bool SkipErrors = false;
while (ScTree.GetNextName(FindData, strFullName2))
{
if (!os::fs::is_directory_symbolic_link(FindData))
continue;
if (!MessageShown)
{
MessageShown = true;
if (Message(MSG_WARNING,
msg(lng::MWarning),
{
msg(lng::MRecycleFolderConfirmDeleteLink1),
msg(lng::MRecycleFolderConfirmDeleteLink2),
msg(lng::MRecycleFolderConfirmDeleteLink3),
msg(lng::MRecycleFolderConfirmDeleteLink4)
},
{ lng::MYes, lng::MCancel },
{}, &RecycleFolderConfirmDeleteLinkId
) != message_result::first_button)
{
cancel_operation();
}
}
EDeleteReparsePoint(strFullName2, FindData.Attributes, SkipErrors);
}
}
bool ShellDelete::RemoveToRecycleBin(string_view const Name, bool dir, bool& RetryRecycleAsRemove, bool& Skip)
{
RetryRecycleAsRemove = false;
const auto strFullName = ConvertNameToReal(Name); // Real: Subst-диск заменен на оригинальный
// ConvertNameToFull(Name) оставляет subst (если был) и удаление будет безвозвратным
break_links_for_old_os(strFullName);
if (os::fs::move_to_recycle_bin(strFullName))
return true;
if (dir? m_SkipFolderErrors : m_SkipFileErrors)
return false;
const auto ErrorState = last_error();
switch (Message(MSG_WARNING, ErrorState,
msg(lng::MError),
{
msg(dir? lng::MCannotRecycleFolder : lng::MCannotRecycleFile),
QuoteOuterSpace(strFullName),
msg(lng::MTryToDeletePermanently)
},
{ lng::MDeleteFileDelete, lng::MDeleteSkip, lng::MDeleteSkipAll, lng::MDeleteCancel },
{}, dir? &CannotRecycleFolderId : &CannotRecycleFileId))
{