aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/wipe_cmd.cpp
blob: d5344fb01dbab4c1e9b6234cdb46c001da7c0b5b (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "wipe_cmd.h"

#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/iohash.h>
#include <zencore/logging.h>
#include <zencore/parallelwork.h>
#include <zencore/string.h>
#include <zencore/timer.h>
#include <zencore/trace.h>
#include <zenutil/workerpools.h>

#include "consoleprogress.h"

#include <signal.h>

#include <iostream>

ZEN_THIRD_PARTY_INCLUDES_START
#include <tsl/robin_map.h>
#include <tsl/robin_set.h>
ZEN_THIRD_PARTY_INCLUDES_END

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>
#else
#	include <fcntl.h>
#	include <sys/file.h>
#	include <sys/stat.h>
#	include <unistd.h>
#endif

namespace zen {

namespace wipe_impl {
	static std::atomic<bool>   AbortFlag		  = false;
	static std::atomic<bool>   PauseFlag		  = false;
	static bool				   IsVerbose		  = false;
	static bool				   Quiet			  = false;
	static ConsoleProgressMode ProgressMode		  = ConsoleProgressMode::Pretty;
	const bool				   SingleThreaded	  = false;
	bool					   BoostWorkerThreads = true;

	WorkerThreadPool& GetIOWorkerPool()
	{
		return SingleThreaded		? GetSyncWorkerPool()
			   : BoostWorkerThreads ? GetLargeWorkerPool(EWorkloadType::Burst)
									: GetMediumWorkerPool(EWorkloadType::Burst);
	}

#undef ZEN_CONSOLE_VERBOSE
#define ZEN_CONSOLE_VERBOSE(fmtstr, ...)                            \
	if (IsVerbose)                                                  \
	{                                                               \
		ZEN_CONSOLE_LOG(zen::logging::Info, fmtstr, ##__VA_ARGS__); \
	}

	static void SignalCallbackHandler(int SigNum)
	{
		if (SigNum == SIGINT)
		{
			PauseFlag = false;
			AbortFlag = true;
		}
#if ZEN_PLATFORM_WINDOWS
		if (SigNum == SIGBREAK)
		{
			PauseFlag = false;
			AbortFlag = true;
		}
#endif	// ZEN_PLATFORM_WINDOWS
	}

	bool IsReadOnly(uint32_t Attributes)
	{
#if ZEN_PLATFORM_WINDOWS
		return IsFileAttributeReadOnly(Attributes);
#else
		return IsFileModeReadOnly(Attributes);
#endif
	}

	bool IsFileWithRetry(const std::filesystem::path& Path)
	{
		std::error_code Ec;
		bool			Result = IsFile(Path, Ec);
		for (size_t Retries = 0; Ec && Retries < 3; Retries++)
		{
			Sleep(100 + int(Retries * 50));
			Ec.clear();
			Result = IsFile(Path, Ec);
		}
		if (Ec)
		{
			zen::ThrowSystemError(Ec.value(), Ec.message());
		}
		return Result;
	}

	bool SetFileReadOnlyWithRetry(const std::filesystem::path& Path, bool ReadOnly)
	{
		std::error_code Ec;
		bool			Result = SetFileReadOnly(Path, ReadOnly, Ec);
		for (size_t Retries = 0; Ec && Retries < 3; Retries++)
		{
			Sleep(100 + int(Retries * 50));
			if (!IsFileWithRetry(Path))
			{
				return false;
			}
			Ec.clear();
			Result = SetFileReadOnly(Path, ReadOnly, Ec);
		}
		if (Ec)
		{
			zen::ThrowSystemError(Ec.value(), Ec.message());
		}
		return Result;
	}

	void RemoveFileWithRetry(const std::filesystem::path& Path)
	{
		std::error_code Ec;
		RemoveFile(Path, Ec);
		for (size_t Retries = 0; Ec && Retries < 3; Retries++)
		{
			Sleep(100 + int(Retries * 50));
			if (!IsFileWithRetry(Path))
			{
				return;
			}
			Ec.clear();
			RemoveFile(Path, Ec);
		}
		if (Ec)
		{
			zen::ThrowSystemError(Ec.value(), Ec.message());
		}
	}

	void RemoveDirWithRetry(const std::filesystem::path& Path)
	{
		std::error_code Ec;
		RemoveDir(Path, Ec);
		for (size_t Retries = 0; Ec && Retries < 3; Retries++)
		{
			Sleep(100 + int(Retries * 50));
			if (!IsDir(Path))
			{
				return;
			}
			Ec.clear();
			RemoveDir(Path, Ec);
		}
		if (Ec)
		{
			zen::ThrowSystemError(Ec.value(), Ec.message());
		}
	}

	bool CleanDirectory(const std::filesystem::path&	  Path,
						std::span<const std::string_view> ExcludeDirectories,
						bool							  RemoveReadonly,
						bool							  Dryrun)
	{
		ZEN_TRACE_CPU("CleanDirectory");
		Stopwatch Timer;

		std::unique_ptr<ProgressBase>			   ProgressOwner(CreateConsoleProgress(ProgressMode));
		std::unique_ptr<ProgressBase::ProgressBar> Progress = ProgressOwner->CreateProgressBar("Clean Folder");

		std::atomic<bool>	  CleanWipe			  = true;
		std::atomic<uint64_t> DiscoveredItemCount = 0;
		std::atomic<uint64_t> DeletedItemCount	  = 0;
		std::atomic<uint64_t> DeletedByteCount	  = 0;
		std::atomic<uint64_t> FailedDeleteCount	  = 0;

		std::vector<std::filesystem::path>			   SubdirectoriesToDelete;
		tsl::robin_map<IoHash, size_t, IoHash::Hasher> SubdirectoriesToDeleteLookup;
		tsl::robin_set<IoHash, IoHash::Hasher>		   SubdirectoriesToKeep;
		RwLock										   SubdirectoriesLock;

		auto AddFoundDirectory = [&](std::filesystem::path Directory, bool Keep) -> bool {
			bool Added = false;
			if (Keep)
			{
				bool IsLeaf = true;
				while (Directory != Path)
				{
					const std::string		   DirectoryString	 = Directory.generic_string();
					IoHash					   DirectoryNameHash = IoHash::HashBuffer(DirectoryString.data(), DirectoryString.length());
					RwLock::ExclusiveLockScope _(SubdirectoriesLock);
					if (auto It = SubdirectoriesToKeep.find(DirectoryNameHash); It == SubdirectoriesToKeep.end())
					{
						SubdirectoriesToKeep.insert(DirectoryNameHash);
						if (IsLeaf)
						{
							Added = true;
						}
					}
					else
					{
						break;
					}
					Directory = Directory.parent_path();
					IsLeaf	  = false;
				}
			}
			else
			{
				bool IsLeaf = true;
				while (Directory != Path)
				{
					const std::string		   DirectoryString	 = Directory.generic_string();
					IoHash					   DirectoryNameHash = IoHash::HashBuffer(DirectoryString.data(), DirectoryString.length());
					RwLock::ExclusiveLockScope _(SubdirectoriesLock);
					if (SubdirectoriesToKeep.contains(DirectoryNameHash))
					{
						break;
					}
					if (auto It = SubdirectoriesToDeleteLookup.find(DirectoryNameHash); It == SubdirectoriesToDeleteLookup.end())
					{
						SubdirectoriesToDeleteLookup.insert({DirectoryNameHash, SubdirectoriesToDelete.size()});
						SubdirectoriesToDelete.push_back(Directory);
						if (IsLeaf)
						{
							Added = true;
						}
					}
					else
					{
						break;
					}
					Directory = Directory.parent_path();
					IsLeaf	  = false;
				}
			}
			return Added;
		};

		ParallelWork Work(AbortFlag, PauseFlag, WorkerThreadPool::EMode::EnableBacklog);

		struct AsyncVisitor : public GetDirectoryContentVisitor
		{
			AsyncVisitor(const std::filesystem::path&							 InPath,
						 std::atomic<bool>&										 InCleanWipe,
						 std::atomic<uint64_t>&									 InDiscoveredItemCount,
						 std::atomic<uint64_t>&									 InDeletedItemCount,
						 std::atomic<uint64_t>&									 InDeletedByteCount,
						 std::atomic<uint64_t>&									 InFailedDeleteCount,
						 std::span<const std::string_view>						 InExcludeDirectories,
						 bool													 InRemoveReadonly,
						 bool													 InDryrun,
						 const std::function<bool(std::filesystem::path, bool)>& InAddFoundDirectoryFunc)
			: Path(InPath)
			, CleanWipe(InCleanWipe)
			, DiscoveredItemCount(InDiscoveredItemCount)
			, DeletedItemCount(InDeletedItemCount)
			, DeletedByteCount(InDeletedByteCount)
			, FailedDeleteCount(InFailedDeleteCount)
			, ExcludeDirectories(InExcludeDirectories)
			, RemoveReadonly(InRemoveReadonly)
			, Dryrun(InDryrun)
			, AddFoundDirectoryFunc(InAddFoundDirectoryFunc)
			{
			}
			virtual void AsyncVisitDirectory(const std::filesystem::path& RelativeRoot, DirectoryContent&& Content) override
			{
				ZEN_TRACE_CPU("CleanDirectory_AsyncVisitDirectory");
				if (!AbortFlag)
				{
					if (!RelativeRoot.empty())
					{
						DiscoveredItemCount++;
					}
					if (Content.FileNames.empty())
					{
						const std::filesystem::path ParentPath	  = Path / RelativeRoot;
						bool						KeepDirectory = RelativeRoot.empty();

						bool Added = AddFoundDirectoryFunc(ParentPath, KeepDirectory);
						if (Added)
						{
							ZEN_CONSOLE_VERBOSE("{} directory {}", KeepDirectory ? "Keeping" : "Removing", ParentPath);
						}
					}
					else
					{
						DiscoveredItemCount += Content.FileNames.size();

						const std::string RelativeRootString = RelativeRoot.generic_string();
						bool			  RemoveContent		 = true;
						for (const std::string_view ExcludeDirectory : ExcludeDirectories)
						{
							if (RelativeRootString.starts_with(ExcludeDirectory))
							{
								if (RelativeRootString.length() > ExcludeDirectory.length())
								{
									const char MaybePathDelimiter = RelativeRootString[ExcludeDirectory.length()];
									if (MaybePathDelimiter == '/' || MaybePathDelimiter == '\\' ||
										MaybePathDelimiter == std::filesystem::path::preferred_separator)
									{
										RemoveContent = false;
										break;
									}
								}
								else
								{
									RemoveContent = false;
									break;
								}
							}
						}

						const std::filesystem::path ParentPath	  = Path / RelativeRoot;
						bool						KeepDirectory = RelativeRoot.empty();

						if (RemoveContent)
						{
							ZEN_TRACE_CPU("DeleteFiles");
							uint64_t RemovedCount = 0;
							for (size_t FileIndex = 0; FileIndex < Content.FileNames.size(); FileIndex++)
							{
								const std::filesystem::path& FileName = Content.FileNames[FileIndex];
								const std::filesystem::path	 FilePath = (ParentPath / FileName).make_preferred();
								try
								{
									const uint32_t Attributes = Content.FileAttributes[FileIndex];
									const bool	   IsReadonly = IsReadOnly(Attributes);
									bool		   RemoveFile = false;
									if (IsReadonly)
									{
										if (RemoveReadonly)
										{
											if (!Dryrun)
											{
												SetFileReadOnlyWithRetry(FilePath, false);
											}
											RemoveFile = true;
										}
									}
									else
									{
										RemoveFile = true;
									}

									if (RemoveFile)
									{
										if (!Dryrun)
										{
											RemoveFileWithRetry(FilePath);
										}
										DeletedItemCount++;
										DeletedByteCount += Content.FileSizes[FileIndex];
										RemovedCount++;
										ZEN_CONSOLE_VERBOSE("Removed file {}", FilePath);
									}
									else
									{
										ZEN_CONSOLE_VERBOSE("Skipped readonly file {}", FilePath);
										KeepDirectory = true;
									}
								}
								catch (const std::exception& Ex)
								{
									ZEN_WARN("Failed removing file {}. Reason: {}", FilePath, Ex.what());
									FailedDeleteCount++;
									CleanWipe	  = false;
									KeepDirectory = true;
								}
							}
							ZEN_CONSOLE_VERBOSE("Removed {} files in {}", RemovedCount, ParentPath);
						}
						else
						{
							ZEN_CONSOLE_VERBOSE("Skipped removal of {} files in {}", Content.FileNames.size(), ParentPath);
						}
						bool Added = AddFoundDirectoryFunc(ParentPath, KeepDirectory);
						if (Added)
						{
							ZEN_CONSOLE_VERBOSE("{} directory {}", KeepDirectory ? "Keeping" : "Removing", ParentPath);
						}
					}
				}
			}
			const std::filesystem::path&					 Path;
			std::atomic<bool>&								 CleanWipe;
			std::atomic<uint64_t>&							 DiscoveredItemCount;
			std::atomic<uint64_t>&							 DeletedItemCount;
			std::atomic<uint64_t>&							 DeletedByteCount;
			std::atomic<uint64_t>&							 FailedDeleteCount;
			std::span<const std::string_view>				 ExcludeDirectories;
			const bool										 RemoveReadonly;
			const bool										 Dryrun;
			std::function<bool(std::filesystem::path, bool)> AddFoundDirectoryFunc;
		} Visitor(Path,
				  CleanWipe,
				  DiscoveredItemCount,
				  DeletedItemCount,
				  DeletedByteCount,
				  FailedDeleteCount,
				  ExcludeDirectories,
				  RemoveReadonly,
				  Dryrun,
				  AddFoundDirectory);

		uint64_t LastUpdateTimeMs = Timer.GetElapsedTimeMs();

		GetDirectoryContent(Path,
							DirectoryContentFlags::IncludeFiles | DirectoryContentFlags::Recursive |
								DirectoryContentFlags::IncludeFileSizes | DirectoryContentFlags::IncludeAttributes,
							Visitor,
							GetIOWorkerPool(),
							Work.PendingWork());

		Work.Wait(ProgressOwner->GetProgressUpdateDelayMS(), [&](bool IsAborted, bool IsPaused, ptrdiff_t PendingWork) {
			ZEN_UNUSED(PendingWork);
			if (Quiet)
			{
				return;
			}
			LastUpdateTimeMs = Timer.GetElapsedTimeMs();

			uint64_t Deleted	  = DeletedItemCount.load();
			uint64_t DeletedBytes = DeletedByteCount.load();
			uint64_t Discovered	  = DiscoveredItemCount.load();
			Progress->UpdateState({.Task		   = "Removing files  ",
								   .Details		   = fmt::format("Found {}, Deleted {} ({})", Discovered, Deleted, NiceBytes(DeletedBytes)),
								   .TotalCount	   = Discovered,
								   .RemainingCount = Discovered - Deleted,
								   .Status		   = ProgressBase::ProgressBar::State::CalculateStatus(IsAborted, IsPaused)},
								  false);
		});

		std::vector<std::filesystem::path> DirectoriesToDelete;
		DirectoriesToDelete.reserve(SubdirectoriesToDelete.size());
		for (auto It : SubdirectoriesToDeleteLookup)
		{
			const IoHash& DirHash = It.first;
			if (auto KeepIt = SubdirectoriesToKeep.find(DirHash); KeepIt == SubdirectoriesToKeep.end())
			{
				DirectoriesToDelete.emplace_back(std::move(SubdirectoriesToDelete[It.second]));
			}
		}

		std::sort(DirectoriesToDelete.begin(),
				  DirectoriesToDelete.end(),
				  [](const std::filesystem::path& Lhs, const std::filesystem::path& Rhs) {
					  return Lhs.string().length() > Rhs.string().length();
				  });

		for (size_t SubDirectoryIndex = 0; SubDirectoryIndex < DirectoriesToDelete.size(); SubDirectoryIndex++)
		{
			ZEN_TRACE_CPU("DeleteDirs");
			const std::filesystem::path& DirectoryToDelete = DirectoriesToDelete[SubDirectoryIndex];
			try
			{
				if (!Dryrun)
				{
					RemoveDirWithRetry(DirectoryToDelete);
				}
				ZEN_CONSOLE_VERBOSE("Removed directory {}", DirectoryToDelete);
				DeletedItemCount++;
			}
			catch (const std::exception& Ex)
			{
				if (!Quiet)
				{
					ZEN_CONSOLE_WARN("Failed removing directory {}. Reason: {}", DirectoryToDelete, Ex.what());
				}
				CleanWipe = false;
				FailedDeleteCount++;
			}

			uint64_t NowMs = Timer.GetElapsedTimeMs();
			if ((NowMs - LastUpdateTimeMs) >= ProgressOwner->GetProgressUpdateDelayMS())
			{
				LastUpdateTimeMs = NowMs;

				uint64_t Deleted	  = DeletedItemCount.load();
				uint64_t DeletedBytes = DeletedByteCount.load();
				uint64_t Discovered	  = DiscoveredItemCount.load();
				Progress->UpdateState({.Task	   = "Removing folders",
									   .Details	   = fmt::format("Found {}, Deleted {} ({})", Discovered, Deleted, NiceBytes(DeletedBytes)),
									   .TotalCount = DirectoriesToDelete.size(),
									   .RemainingCount = DirectoriesToDelete.size() - SubDirectoryIndex},
									  false);
			}
		}

		Progress->Finish();

		uint64_t ElapsedTimeMs = Timer.GetElapsedTimeMs();
		if (!Quiet)
		{
			ZEN_CONSOLE("Wiped folder '{}' {} ({}) ({} failed) in {}",
						Path,
						DeletedItemCount.load(),
						NiceBytes(DeletedByteCount.load()),
						FailedDeleteCount.load(),
						NiceTimeSpanMs(ElapsedTimeMs));
		}
		if (FailedDeleteCount.load() > 0)
		{
			throw std::runtime_error(fmt::format("Failed to delete {} files/directories in '{}'", FailedDeleteCount.load(), Path));
		}
		return CleanWipe;
	}
}  // namespace wipe_impl

WipeCommand::WipeCommand()
{
	m_Options.add_options()("h,help", "Print help");
	m_Options.add_option("", "d", "directory", "Directory to wipe", cxxopts::value(m_Directory), "<directory>");
	m_Options.add_option("", "r", "keep-readonly", "Leave read-only files", cxxopts::value(m_KeepReadOnlyFiles), "<keepreadonly>");
	m_Options.add_option("", "q", "quiet", "Reduce output to console", cxxopts::value(m_Quiet), "<quiet>");
	m_Options.add_option("", "y", "yes", "Don't query for confirmation", cxxopts::value(m_Yes), "<yes>");
	m_Options.add_option("", "", "dryrun", "Do a dry run without deleting anything", cxxopts::value(m_Dryrun), "<dryrun>");
	m_Options.add_option("output", "", "plain-progress", "Show progress using plain output", cxxopts::value(m_PlainProgress), "<progress>");
	m_Options.add_option("output", "", "verbose", "Enable verbose console output", cxxopts::value(m_Verbose), "<verbose>");
	m_Options.add_option("",
						 "",
						 "boost-workers",
						 "Increase the number of worker threads - may cause computer to be less responsive",
						 cxxopts::value(m_BoostWorkerThreads),
						 "<boostworkers>");

	m_Options.parse_positional({"directory"});
}

WipeCommand::~WipeCommand() = default;

void
WipeCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	using namespace wipe_impl;
	ZEN_UNUSED(GlobalOptions);

	ScopedSignalHandler SigIntGuard(SIGINT, SignalCallbackHandler);
#if ZEN_PLATFORM_WINDOWS
	ScopedSignalHandler SigBreakGuard(SIGBREAK, SignalCallbackHandler);
#endif

	if (!ParseOptions(argc, argv))
	{
		return;
	}

	Quiet			   = m_Quiet;
	IsVerbose		   = m_Verbose;
	ProgressMode	   = m_PlainProgress ? ConsoleProgressMode::Plain : ConsoleProgressMode::Pretty;
	BoostWorkerThreads = m_BoostWorkerThreads;

	MakeSafeAbsolutePathInPlace(m_Directory);

	if (!IsDir(m_Directory))
	{
		return;
	}

	while (!m_Yes)
	{
		const std::string Prompt = fmt::format("Do you want to wipe directory '{}'? (yes/no) ", m_Directory);
		printf("%s", Prompt.c_str());
		std::string Reponse;
		std::getline(std::cin, Reponse);
		Reponse = ToLower(Reponse);
		if (Reponse == "y" || Reponse == "yes")
		{
			m_Yes = true;
		}
		else if (Reponse == "n" || Reponse == "no")
		{
			return;
		}
	}

	CleanDirectory(m_Directory, {}, !m_KeepReadOnlyFiles, m_Dryrun);
}

}  // namespace zen