1 | /** @file
|
---|
2 | Main file for attrib shell level 2 function.
|
---|
3 |
|
---|
4 | (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
|
---|
5 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include "UefiShellLevel2CommandsLib.h"
|
---|
11 |
|
---|
12 | STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
|
---|
13 | {L"-q", TypeFlag},
|
---|
14 | {NULL, TypeMax}
|
---|
15 | };
|
---|
16 |
|
---|
17 | /**
|
---|
18 | Determine if a directory has no files in it.
|
---|
19 |
|
---|
20 | @param[in] FileHandle The EFI_HANDLE to the directory.
|
---|
21 |
|
---|
22 | @retval TRUE The directory has no files (or directories).
|
---|
23 | @retval FALSE The directory has at least 1 file or directory in it.
|
---|
24 | **/
|
---|
25 | BOOLEAN
|
---|
26 | IsDirectoryEmpty (
|
---|
27 | IN EFI_HANDLE FileHandle
|
---|
28 | )
|
---|
29 | {
|
---|
30 | EFI_STATUS Status;
|
---|
31 | EFI_FILE_INFO *FileInfo;
|
---|
32 | BOOLEAN NoFile;
|
---|
33 | BOOLEAN RetVal;
|
---|
34 |
|
---|
35 | RetVal = TRUE;
|
---|
36 | NoFile = FALSE;
|
---|
37 | FileInfo = NULL;
|
---|
38 |
|
---|
39 | for (Status = FileHandleFindFirstFile(FileHandle, &FileInfo)
|
---|
40 | ; !NoFile && !EFI_ERROR (Status)
|
---|
41 | ; FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)
|
---|
42 | ){
|
---|
43 | if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName
|
---|
44 | &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {
|
---|
45 | RetVal = FALSE;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | return (RetVal);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | Delete a node and all nodes under it (including sub directories).
|
---|
53 |
|
---|
54 | @param[in] Node The node to start deleting with.
|
---|
55 | @param[in] Quiet TRUE to print no messages.
|
---|
56 |
|
---|
57 | @retval SHELL_SUCCESS The operation was successful.
|
---|
58 | @retval SHELL_ACCESS_DENIED A file was read only.
|
---|
59 | @retval SHELL_ABORTED The abort message was received.
|
---|
60 | @retval SHELL_DEVICE_ERROR A device error occured reading this Node.
|
---|
61 | **/
|
---|
62 | SHELL_STATUS
|
---|
63 | CascadeDelete(
|
---|
64 | IN EFI_SHELL_FILE_INFO *Node,
|
---|
65 | IN CONST BOOLEAN Quiet
|
---|
66 | )
|
---|
67 | {
|
---|
68 | SHELL_STATUS ShellStatus;
|
---|
69 | EFI_SHELL_FILE_INFO *List;
|
---|
70 | EFI_SHELL_FILE_INFO *Node2;
|
---|
71 | EFI_STATUS Status;
|
---|
72 | SHELL_PROMPT_RESPONSE *Resp;
|
---|
73 | CHAR16 *TempName;
|
---|
74 | UINTN NewSize;
|
---|
75 |
|
---|
76 | Resp = NULL;
|
---|
77 | ShellStatus = SHELL_SUCCESS;
|
---|
78 | List = NULL;
|
---|
79 | Status = EFI_SUCCESS;
|
---|
80 |
|
---|
81 | if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
|
---|
82 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, L"rm", Node->FullName);
|
---|
83 | return (SHELL_ACCESS_DENIED);
|
---|
84 | }
|
---|
85 |
|
---|
86 | if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
|
---|
87 | if (!IsDirectoryEmpty(Node->Handle)) {
|
---|
88 | if (!Quiet) {
|
---|
89 | Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);
|
---|
90 | Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
|
---|
91 | ASSERT(Resp != NULL);
|
---|
92 | if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {
|
---|
93 | SHELL_FREE_NON_NULL(Resp);
|
---|
94 | return (SHELL_ABORTED);
|
---|
95 | }
|
---|
96 | SHELL_FREE_NON_NULL(Resp);
|
---|
97 | }
|
---|
98 | //
|
---|
99 | // empty out the directory
|
---|
100 | //
|
---|
101 | Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);
|
---|
102 | if (EFI_ERROR(Status)) {
|
---|
103 | if (List!=NULL) {
|
---|
104 | gEfiShellProtocol->FreeFileList(&List);
|
---|
105 | }
|
---|
106 | return (SHELL_DEVICE_ERROR);
|
---|
107 | }
|
---|
108 | for (Node2 = (EFI_SHELL_FILE_INFO *)GetFirstNode(&List->Link)
|
---|
109 | ; !IsNull(&List->Link, &Node2->Link)
|
---|
110 | ; Node2 = (EFI_SHELL_FILE_INFO *)GetNextNode(&List->Link, &Node2->Link)
|
---|
111 | ){
|
---|
112 | //
|
---|
113 | // skip the directory traversing stuff...
|
---|
114 | //
|
---|
115 | if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {
|
---|
116 | continue;
|
---|
117 | }
|
---|
118 | Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
|
---|
119 | if (EFI_ERROR(Node2->Status) && StrStr(Node2->FileName, L":") == NULL) {
|
---|
120 | //
|
---|
121 | // Update the node filename to have full path with file system identifier
|
---|
122 | //
|
---|
123 | NewSize = StrSize(Node->FullName) + StrSize(Node2->FullName);
|
---|
124 | TempName = AllocateZeroPool(NewSize);
|
---|
125 | if (TempName == NULL) {
|
---|
126 | ShellStatus = SHELL_OUT_OF_RESOURCES;
|
---|
127 | } else {
|
---|
128 | StrCpyS(TempName, NewSize/sizeof(CHAR16), Node->FullName);
|
---|
129 | TempName[StrStr(TempName, L":")+1-TempName] = CHAR_NULL;
|
---|
130 | StrCatS(TempName, NewSize/sizeof(CHAR16), Node2->FullName);
|
---|
131 | FreePool((VOID*)Node2->FullName);
|
---|
132 | Node2->FullName = TempName;
|
---|
133 |
|
---|
134 | //
|
---|
135 | // Now try again to open the file
|
---|
136 | //
|
---|
137 | Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | if (!EFI_ERROR(Node2->Status)) {
|
---|
141 | ShellStatus = CascadeDelete(Node2, Quiet);
|
---|
142 | } else if (ShellStatus == SHELL_SUCCESS) {
|
---|
143 | ShellStatus = (SHELL_STATUS)(Node2->Status&(~0x80000000));
|
---|
144 | }
|
---|
145 | if (ShellStatus != SHELL_SUCCESS) {
|
---|
146 | if (List!=NULL) {
|
---|
147 | gEfiShellProtocol->FreeFileList(&List);
|
---|
148 | }
|
---|
149 | return (ShellStatus);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | if (List!=NULL) {
|
---|
153 | gEfiShellProtocol->FreeFileList(&List);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {
|
---|
159 | //
|
---|
160 | // now delete the current node...
|
---|
161 | //
|
---|
162 | if (!Quiet) {
|
---|
163 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);
|
---|
164 | }
|
---|
165 | Status = gEfiShellProtocol->DeleteFile(Node->Handle);
|
---|
166 | Node->Handle = NULL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | //
|
---|
170 | // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
|
---|
171 | //
|
---|
172 | if (Status != EFI_SUCCESS){
|
---|
173 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);
|
---|
174 | return (SHELL_ACCESS_DENIED);
|
---|
175 | } else {
|
---|
176 | if (!Quiet) {
|
---|
177 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);
|
---|
178 | }
|
---|
179 | return (SHELL_SUCCESS);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | Determines if a Node is a valid delete target. Will prevent deleting the root directory.
|
---|
185 |
|
---|
186 | @param[in] List RESERVED. Not used.
|
---|
187 | @param[in] Node The node to analyze.
|
---|
188 | @param[in] Package RESERVED. Not used.
|
---|
189 | **/
|
---|
190 | BOOLEAN
|
---|
191 | IsValidDeleteTarget(
|
---|
192 | IN CONST EFI_SHELL_FILE_INFO *List,
|
---|
193 | IN CONST EFI_SHELL_FILE_INFO *Node,
|
---|
194 | IN CONST LIST_ENTRY *Package
|
---|
195 | )
|
---|
196 | {
|
---|
197 | CONST CHAR16 *TempLocation;
|
---|
198 | BOOLEAN RetVal;
|
---|
199 | CHAR16 *SearchString;
|
---|
200 | CHAR16 *Pattern;
|
---|
201 | UINTN Size;
|
---|
202 |
|
---|
203 | if (Node == NULL || Node->FullName == NULL) {
|
---|
204 | return (FALSE);
|
---|
205 | }
|
---|
206 |
|
---|
207 | TempLocation = StrStr(Node->FullName, L":");
|
---|
208 | if (StrLen(TempLocation) <= 2) {
|
---|
209 | //
|
---|
210 | // Deleting the root directory is invalid.
|
---|
211 | //
|
---|
212 | return (FALSE);
|
---|
213 | }
|
---|
214 |
|
---|
215 | TempLocation = ShellGetCurrentDir(NULL);
|
---|
216 | if (TempLocation == NULL) {
|
---|
217 | //
|
---|
218 | // No working directory is specified so whatever is left is ok.
|
---|
219 | //
|
---|
220 | return (TRUE);
|
---|
221 | }
|
---|
222 |
|
---|
223 | Pattern = NULL;
|
---|
224 | SearchString = NULL;
|
---|
225 | Size = 0;
|
---|
226 | Pattern = StrnCatGrow(&Pattern, &Size, TempLocation , 0);
|
---|
227 | Pattern = StrnCatGrow(&Pattern, &Size, L"\\" , 0);
|
---|
228 | Size = 0;
|
---|
229 | SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);
|
---|
230 | if (!EFI_ERROR(ShellIsDirectory(SearchString))) {
|
---|
231 | SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);
|
---|
232 | SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (Pattern == NULL || SearchString == NULL) {
|
---|
236 | RetVal = FALSE;
|
---|
237 | } else {
|
---|
238 | RetVal = TRUE;
|
---|
239 | if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {
|
---|
240 | RetVal = FALSE;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | SHELL_FREE_NON_NULL(Pattern );
|
---|
245 | SHELL_FREE_NON_NULL(SearchString);
|
---|
246 |
|
---|
247 | return (RetVal);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | Function for 'rm' command.
|
---|
252 |
|
---|
253 | @param[in] ImageHandle Handle to the Image (NULL if Internal).
|
---|
254 | @param[in] SystemTable Pointer to the System Table (NULL if Internal).
|
---|
255 | **/
|
---|
256 | SHELL_STATUS
|
---|
257 | EFIAPI
|
---|
258 | ShellCommandRunRm (
|
---|
259 | IN EFI_HANDLE ImageHandle,
|
---|
260 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
261 | )
|
---|
262 | {
|
---|
263 | EFI_STATUS Status;
|
---|
264 | LIST_ENTRY *Package;
|
---|
265 | CHAR16 *ProblemParam;
|
---|
266 | CONST CHAR16 *Param;
|
---|
267 | SHELL_STATUS ShellStatus;
|
---|
268 | UINTN ParamCount;
|
---|
269 | EFI_SHELL_FILE_INFO *FileList;
|
---|
270 | EFI_SHELL_FILE_INFO *Node;
|
---|
271 |
|
---|
272 | ProblemParam = NULL;
|
---|
273 | ShellStatus = SHELL_SUCCESS;
|
---|
274 | ParamCount = 0;
|
---|
275 | FileList = NULL;
|
---|
276 |
|
---|
277 | //
|
---|
278 | // initialize the shell lib (we must be in non-auto-init...)
|
---|
279 | //
|
---|
280 | Status = ShellInitialize();
|
---|
281 | ASSERT_EFI_ERROR(Status);
|
---|
282 |
|
---|
283 | //
|
---|
284 | // parse the command line
|
---|
285 | //
|
---|
286 | Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
|
---|
287 | if (EFI_ERROR(Status)) {
|
---|
288 | if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
|
---|
289 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam);
|
---|
290 | FreePool(ProblemParam);
|
---|
291 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
292 | } else {
|
---|
293 | ASSERT(FALSE);
|
---|
294 | }
|
---|
295 | } else {
|
---|
296 | //
|
---|
297 | // check for "-?"
|
---|
298 | //
|
---|
299 | if (ShellCommandLineGetFlag(Package, L"-?")) {
|
---|
300 | ASSERT(FALSE);
|
---|
301 | }
|
---|
302 | if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
|
---|
303 | //
|
---|
304 | // we insufficient parameters
|
---|
305 | //
|
---|
306 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm");
|
---|
307 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
308 | } else {
|
---|
309 | //
|
---|
310 | // get a list with each file specified by parameters
|
---|
311 | // if parameter is a directory then add all the files below it to the list
|
---|
312 | //
|
---|
313 | for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
|
---|
314 | ; Param != NULL
|
---|
315 | ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
|
---|
316 | ){
|
---|
317 | Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
|
---|
318 | if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {
|
---|
319 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16*)Param);
|
---|
320 | ShellStatus = SHELL_NOT_FOUND;
|
---|
321 | break;
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | if (ShellStatus == SHELL_SUCCESS){
|
---|
326 | //
|
---|
327 | // loop through the list and make sure we are not aborting...
|
---|
328 | //
|
---|
329 | for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
|
---|
330 | ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
|
---|
331 | ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
|
---|
332 | ){
|
---|
333 | //
|
---|
334 | // skip the directory traversing stuff...
|
---|
335 | //
|
---|
336 | if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
|
---|
337 | continue;
|
---|
338 | }
|
---|
339 |
|
---|
340 | //
|
---|
341 | // do the deleting of nodes
|
---|
342 | //
|
---|
343 | if (EFI_ERROR(Node->Status)){
|
---|
344 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);
|
---|
345 | ShellStatus = SHELL_ACCESS_DENIED;
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | if (!IsValidDeleteTarget(FileList, Node, Package)) {
|
---|
349 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);
|
---|
350 | ShellStatus = SHELL_INVALID_PARAMETER;
|
---|
351 | break;
|
---|
352 | }
|
---|
353 |
|
---|
354 | ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));
|
---|
355 | }
|
---|
356 | }
|
---|
357 | //
|
---|
358 | // Free the fileList
|
---|
359 | //
|
---|
360 | if (FileList != NULL) {
|
---|
361 | Status = ShellCloseFileMetaArg(&FileList);
|
---|
362 | }
|
---|
363 | FileList = NULL;
|
---|
364 | }
|
---|
365 |
|
---|
366 | //
|
---|
367 | // free the command line package
|
---|
368 | //
|
---|
369 | ShellCommandLineFreeVarList (Package);
|
---|
370 | }
|
---|
371 |
|
---|
372 | return (ShellStatus);
|
---|
373 | }
|
---|
374 |
|
---|