VirtualBox

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

Last change on this file since 109091 was 108794, checked in by vboxsync, 5 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 11.6 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 ((TempLocation == NULL) || (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 (SearchString == NULL) {
246 RetVal = FALSE;
247 goto Done;
248 }
249
250 if (!EFI_ERROR (ShellIsDirectory (SearchString))) {
251 SearchString = StrnCatGrow (&SearchString, &Size, L"\\", 0);
252 SearchString = StrnCatGrow (&SearchString, &Size, L"*", 0);
253 }
254
255 if ((Pattern == NULL) || (SearchString == NULL)) {
256 RetVal = FALSE;
257 } else {
258 RetVal = TRUE;
259 if (gUnicodeCollation->MetaiMatch (gUnicodeCollation, Pattern, SearchString)) {
260 RetVal = FALSE;
261 }
262 }
263
264Done:
265 SHELL_FREE_NON_NULL (Pattern);
266 SHELL_FREE_NON_NULL (SearchString);
267
268 return (RetVal);
269}
270
271/**
272 Function for 'rm' command.
273
274 @param[in] ImageHandle Handle to the Image (NULL if Internal).
275 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
276**/
277SHELL_STATUS
278EFIAPI
279ShellCommandRunRm (
280 IN EFI_HANDLE ImageHandle,
281 IN EFI_SYSTEM_TABLE *SystemTable
282 )
283{
284 EFI_STATUS Status;
285 LIST_ENTRY *Package;
286 CHAR16 *ProblemParam;
287 CONST CHAR16 *Param;
288 SHELL_STATUS ShellStatus;
289 UINTN ParamCount;
290 EFI_SHELL_FILE_INFO *FileList;
291 EFI_SHELL_FILE_INFO *Node;
292
293 ProblemParam = NULL;
294 ShellStatus = SHELL_SUCCESS;
295 ParamCount = 0;
296 FileList = NULL;
297
298 //
299 // initialize the shell lib (we must be in non-auto-init...)
300 //
301 Status = ShellInitialize ();
302 ASSERT_EFI_ERROR (Status);
303
304 //
305 // parse the command line
306 //
307 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
308 if (EFI_ERROR (Status)) {
309 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
310 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam);
311 FreePool (ProblemParam);
312 ShellStatus = SHELL_INVALID_PARAMETER;
313 } else {
314 ASSERT (FALSE);
315 }
316 } else {
317 //
318 // check for "-?"
319 //
320 if (ShellCommandLineGetFlag (Package, L"-?")) {
321 ASSERT (FALSE);
322 }
323
324 if (ShellCommandLineGetRawValue (Package, 1) == NULL) {
325 //
326 // we insufficient parameters
327 //
328 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm");
329 ShellStatus = SHELL_INVALID_PARAMETER;
330 } else {
331 //
332 // get a list with each file specified by parameters
333 // if parameter is a directory then add all the files below it to the list
334 //
335 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue (Package, ParamCount)
336 ; Param != NULL
337 ; ParamCount++, Param = ShellCommandLineGetRawValue (Package, ParamCount)
338 )
339 {
340 Status = ShellOpenFileMetaArg ((CHAR16 *)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
341 if (EFI_ERROR (Status) || (FileList == NULL) || IsListEmpty (&FileList->Link)) {
342 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16 *)Param);
343 ShellStatus = SHELL_NOT_FOUND;
344 break;
345 }
346 }
347
348 if (ShellStatus == SHELL_SUCCESS) {
349 //
350 // loop through the list and make sure we are not aborting...
351 //
352 for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode (&FileList->Link)
353 ; !IsNull (&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag ()
354 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode (&FileList->Link, &Node->Link)
355 )
356 {
357 //
358 // skip the directory traversing stuff...
359 //
360 if ((StrCmp (Node->FileName, L".") == 0) || (StrCmp (Node->FileName, L"..") == 0)) {
361 continue;
362 }
363
364 //
365 // do the deleting of nodes
366 //
367 if (EFI_ERROR (Node->Status)) {
368 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);
369 ShellStatus = SHELL_ACCESS_DENIED;
370 break;
371 }
372
373 if (!IsValidDeleteTarget (FileList, Node, Package)) {
374 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);
375 ShellStatus = SHELL_INVALID_PARAMETER;
376 break;
377 }
378
379 ShellStatus = CascadeDelete (Node, ShellCommandLineGetFlag (Package, L"-q"));
380 }
381 }
382
383 //
384 // Free the fileList
385 //
386 if (FileList != NULL) {
387 Status = ShellCloseFileMetaArg (&FileList);
388 }
389
390 FileList = NULL;
391 }
392
393 //
394 // free the command line package
395 //
396 ShellCommandLineFreeVarList (Package);
397 }
398
399 return (ShellStatus);
400}
Note: See TracBrowser for help on using the repository browser.

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