VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c@ 105670

Last change on this file since 105670 was 99404, checked in by vboxsync, 21 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette