forked from malxau/yori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmdir.c
353 lines (298 loc) · 11 KB
/
rmdir.c
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
/**
* @file rmdir/rmdir.c
*
* Yori shell rmdir
*
* Copyright (c) 2017-2019 Malcolm J. Smith
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS RMDIR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE RMDIR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <yoripch.h>
#include <yorilib.h>
/**
Help text to display to the user.
*/
const
CHAR strRmdirHelpText[] =
"\n"
"Removes directories.\n"
"\n"
"RMDIR [-license] [-b] [-r] [-s] <dir> [<dir>...]\n"
"\n"
" -b Use basic search criteria for directories only\n"
" -l Delete links without contents\n"
" -r Send directories to the recycle bin\n"
" -s Remove all contents of each directory\n";
/**
Display usage text to the user.
*/
BOOL
RmdirHelp()
{
YoriLibOutput(YORI_LIB_OUTPUT_STDOUT, _T("Rmdir %i.%02i\n"), RMDIR_VER_MAJOR, RMDIR_VER_MINOR);
#if YORI_BUILD_ID
YoriLibOutput(YORI_LIB_OUTPUT_STDOUT, _T(" Build %i\n"), YORI_BUILD_ID);
#endif
YoriLibOutput(YORI_LIB_OUTPUT_STDOUT, _T("%hs"), strRmdirHelpText);
return TRUE;
}
/**
Context information when files are found.
*/
typedef struct _RMDIR_CONTEXT {
/**
If TRUE, objects should be sent to the recycle bin rather than directly
deleted.
*/
BOOL RecycleBin;
} RMDIR_CONTEXT, *PRMDIR_CONTEXT;
/**
A callback that is invoked when a file is found that matches a search criteria
specified in the set of strings to enumerate.
@param FilePath Pointer to the file path that was found.
@param FileInfo Information about the file.
@param Depth Specifies the recursion depth. Ignored in this application.
@param Context Pointer to a RMDIR_CONTEXT.
@return TRUE to continute enumerating, FALSE to abort.
*/
BOOL
RmdirFileFoundCallback(
__in PYORI_STRING FilePath,
__in PWIN32_FIND_DATA FileInfo,
__in DWORD Depth,
__in PVOID Context
)
{
DWORD Err = NO_ERROR;
LPTSTR ErrText;
DWORD OldAttributes;
DWORD NewAttributes;
BOOL FileDeleted;
PRMDIR_CONTEXT RmdirContext = (PRMDIR_CONTEXT)Context;
UNREFERENCED_PARAMETER(Depth);
ASSERT(YoriLibIsStringNullTerminated(FilePath));
FileDeleted = FALSE;
//
// Try to delete it.
//
if (RmdirContext->RecycleBin) {
if (YoriLibRecycleBinFile(FilePath)) {
FileDeleted = TRUE;
}
}
if (!FileDeleted) {
if ((FileInfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
if (!DeleteFile(FilePath->StartOfString)) {
Err = GetLastError();
}
} else {
if (!RemoveDirectory(FilePath->StartOfString)) {
Err = GetLastError();
}
}
}
//
// If it fails with access denied, try to remove any readonly, hidden or
// system attributes which might be getting in the way, then try the
// delete again.
//
if (Err == ERROR_ACCESS_DENIED) {
OldAttributes = GetFileAttributes(FilePath->StartOfString);
NewAttributes = OldAttributes & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
if (OldAttributes != NewAttributes) {
SetFileAttributes(FilePath->StartOfString, NewAttributes);
Err = NO_ERROR;
if ((FileInfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
if (!DeleteFile(FilePath->StartOfString)) {
Err = GetLastError();
}
} else {
if (!RemoveDirectory(FilePath->StartOfString)) {
Err = GetLastError();
}
}
if (Err != NO_ERROR) {
SetFileAttributes(FilePath->StartOfString, OldAttributes);
}
}
}
//
// If we still can't delete it, return an error.
//
if (Err != NO_ERROR) {
ErrText = YoriLibGetWinErrorText(Err);
if ((FileInfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("rmdir: delete failed: %y: %s"), FilePath, ErrText);
} else {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("rmdir: rmdir failed: %y: %s"), FilePath, ErrText);
}
YoriLibFreeWinErrorText(ErrText);
}
return TRUE;
}
/**
A callback that is invoked when a directory cannot be successfully enumerated.
@param FilePath Pointer to the file path that could not be enumerated.
@param ErrorCode The Win32 error code describing the failure.
@param Depth Recursion depth, ignored in this application.
@param Context Context, ignored in this function.
@return TRUE to continute enumerating, FALSE to abort.
*/
BOOL
RmdirFileEnumerateErrorCallback(
__in PYORI_STRING FilePath,
__in DWORD ErrorCode,
__in DWORD Depth,
__in PVOID Context
)
{
YORI_STRING UnescapedFilePath;
BOOL Result = FALSE;
UNREFERENCED_PARAMETER(Depth);
UNREFERENCED_PARAMETER(Context);
YoriLibInitEmptyString(&UnescapedFilePath);
if (!YoriLibUnescapePath(FilePath, &UnescapedFilePath)) {
UnescapedFilePath.StartOfString = FilePath->StartOfString;
UnescapedFilePath.LengthInChars = FilePath->LengthInChars;
}
if (ErrorCode == ERROR_FILE_NOT_FOUND || ErrorCode == ERROR_PATH_NOT_FOUND) {
if (Depth == 0) {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("File or directory not found: %y\n"), &UnescapedFilePath);
}
Result = TRUE;
} else {
LPTSTR ErrText = YoriLibGetWinErrorText(ErrorCode);
YORI_STRING DirName;
LPTSTR FilePart;
YoriLibInitEmptyString(&DirName);
DirName.StartOfString = UnescapedFilePath.StartOfString;
FilePart = YoriLibFindRightMostCharacter(&UnescapedFilePath, '\\');
if (FilePart != NULL) {
DirName.LengthInChars = (DWORD)(FilePart - DirName.StartOfString);
} else {
DirName.LengthInChars = UnescapedFilePath.LengthInChars;
}
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("Enumerate of %y failed: %s"), &DirName, ErrText);
YoriLibFreeWinErrorText(ErrText);
}
YoriLibFreeStringContents(&UnescapedFilePath);
return Result;
}
#ifdef YORI_BUILTIN
/**
The main entrypoint for the rmdir builtin command.
*/
#define ENTRYPOINT YoriCmd_YRMDIR
#else
/**
The main entrypoint for the rmdir standalone application.
*/
#define ENTRYPOINT ymain
#endif
/**
The main entrypoint for the rmdir cmdlet.
@param ArgC The number of arguments.
@param ArgV An array of arguments.
@return Exit code of the process indicating success or failure.
*/
DWORD
ENTRYPOINT(
__in DWORD ArgC,
__in YORI_STRING ArgV[]
)
{
BOOL ArgumentUnderstood;
BOOL Recursive;
BOOL BasicEnumeration;
BOOL DeleteLinks;
DWORD MatchFlags;
DWORD StartArg = 0;
DWORD i;
RMDIR_CONTEXT RmdirContext;
YORI_STRING Arg;
ZeroMemory(&RmdirContext, sizeof(RmdirContext));
Recursive = FALSE;
BasicEnumeration = FALSE;
DeleteLinks = FALSE;
for (i = 1; i < ArgC; i++) {
ArgumentUnderstood = FALSE;
ASSERT(YoriLibIsStringNullTerminated(&ArgV[i]));
if (YoriLibIsCommandLineOption(&ArgV[i], &Arg)) {
if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("?")) == 0) {
RmdirHelp();
return EXIT_SUCCESS;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("license")) == 0) {
YoriLibDisplayMitLicense(_T("2017-2019"));
return EXIT_SUCCESS;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("b")) == 0) {
BasicEnumeration = TRUE;
ArgumentUnderstood = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("l")) == 0) {
DeleteLinks = TRUE;
ArgumentUnderstood = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("q")) == 0) {
ArgumentUnderstood = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("r")) == 0) {
ArgumentUnderstood = TRUE;
RmdirContext.RecycleBin = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("s")) == 0) {
Recursive = TRUE;
ArgumentUnderstood = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("s/q")) == 0) {
Recursive = TRUE;
ArgumentUnderstood = TRUE;
} else if (YoriLibCompareStringWithLiteralInsensitive(&Arg, _T("-")) == 0) {
StartArg = i + 1;
ArgumentUnderstood = TRUE;
break;
}
} else {
ArgumentUnderstood = TRUE;
StartArg = i;
break;
}
if (!ArgumentUnderstood) {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("Argument not understood, ignored: %y\n"), &ArgV[i]);
}
}
if (StartArg == 0 || StartArg == ArgC) {
YoriLibOutput(YORI_LIB_OUTPUT_STDERR, _T("rmdir: missing argument\n"));
return EXIT_FAILURE;
}
MatchFlags = YORILIB_FILEENUM_RETURN_DIRECTORIES;
if (Recursive) {
MatchFlags |= YORILIB_FILEENUM_RECURSE_BEFORE_RETURN | YORILIB_FILEENUM_RETURN_FILES;
}
if (BasicEnumeration) {
MatchFlags |= YORILIB_FILEENUM_BASIC_EXPANSION;
}
if (DeleteLinks) {
MatchFlags |= YORILIB_FILEENUM_NO_LINK_TRAVERSE;
}
for (i = StartArg; i < ArgC; i++) {
YoriLibForEachFile(&ArgV[i],
MatchFlags,
0,
RmdirFileFoundCallback,
RmdirFileEnumerateErrorCallback,
&RmdirContext);
}
return EXIT_SUCCESS;
}
// vim:sw=4:ts=4:et: