TianoCore EDK2 master
Loading...
Searching...
No Matches
Rm.c
Go to the documentation of this file.
1
11
12STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
13 { L"-q", TypeFlag },
14 { NULL, TypeMax }
15};
16
25BOOLEAN
27 IN SHELL_FILE_HANDLE FileHandle
28 )
29{
30 EFI_STATUS Status;
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
68 IN CONST BOOLEAN Quiet
69 )
70{
71 SHELL_STATUS ShellStatus;
74 EFI_STATUS Status;
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
205BOOLEAN
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
278EFIAPI
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;
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 //
397 }
398
399 return (ShellStatus);
400}
UINT64 UINTN
BOOLEAN EFIAPI IsNull(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:443
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
BOOLEAN EFIAPI IsListEmpty(IN CONST LIST_ENTRY *ListHead)
Definition: LinkedList.c:403
RETURN_STATUS EFIAPI StrCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:226
LIST_ENTRY *EFIAPI GetNextNode(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:333
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
Definition: String.c:109
LIST_ENTRY *EFIAPI GetFirstNode(IN CONST LIST_ENTRY *List)
Definition: LinkedList.c:298
RETURN_STATUS EFIAPI StrCatS(IN OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:405
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
CHAR16 *EFIAPI StrStr(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString)
Definition: String.c:224
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STATUS EFIAPI FileHandleFindNextFile(IN EFI_FILE_HANDLE DirHandle, OUT EFI_FILE_INFO *Buffer, OUT BOOLEAN *NoFile)
EFI_STATUS EFIAPI FileHandleFindFirstFile(IN EFI_FILE_HANDLE DirHandle, OUT EFI_FILE_INFO **Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_ABORTED
Definition: Shell.h:128
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_DEVICE_ERROR
Definition: Shell.h:63
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_FILE_INFO * FileInfo(IN EFI_FILE_HANDLE FHand)
SHELL_STATUS CascadeDelete(IN EFI_SHELL_FILE_INFO *Node, IN CONST BOOLEAN Quiet)
Definition: Rm.c:66
SHELL_STATUS EFIAPI ShellCommandRunRm(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Rm.c:279
BOOLEAN IsDirectoryEmpty(IN SHELL_FILE_HANDLE FileHandle)
Definition: Rm.c:26
BOOLEAN IsValidDeleteTarget(IN CONST EFI_SHELL_FILE_INFO *List, IN CONST EFI_SHELL_FILE_INFO *Node, IN CONST LIST_ENTRY *Package)
Definition: Rm.c:206
CONST CHAR16 *EFIAPI ShellGetCurrentDir(IN CHAR16 *CONST DeviceName OPTIONAL)
BOOLEAN EFIAPI ShellGetExecutionBreakFlag(VOID)
EFI_STATUS EFIAPI ShellCloseFileMetaArg(IN OUT EFI_SHELL_FILE_INFO **ListHead)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
CHAR16 *EFIAPI StrnCatGrow(IN OUT CHAR16 **Destination, IN OUT UINTN *CurrentSize, IN CONST CHAR16 *Source, IN UINTN Count)
EFI_STATUS EFIAPI ShellPromptForResponse(IN SHELL_PROMPT_REQUEST_TYPE Type, IN CHAR16 *Prompt OPTIONAL, IN OUT VOID **Response OPTIONAL)
SHELL_PROMPT_RESPONSE
Definition: ShellLib.h:1198
EFI_STATUS EFIAPI ShellOpenFileMetaArg(IN CHAR16 *Arg, IN UINT64 OpenMode, IN OUT EFI_SHELL_FILE_INFO **ListHead)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellIsDirectory(IN CONST CHAR16 *DirName)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
#define STRING_TOKEN(t)
CHAR16 FileName[1]
Definition: FileInfo.h:52
LIST_ENTRY Link
Linked list members.
Definition: Shell.h:153
SHELL_FILE_HANDLE Handle
Handle for interacting with the opened file or NULL if closed.
Definition: Shell.h:157
EFI_STATUS Status
Status of opening the file. Valid only if Handle != NULL.
Definition: Shell.h:154
CONST CHAR16 * FullName
Fully qualified filename.
Definition: Shell.h:155
CONST CHAR16 * FileName
name of this file.
Definition: Shell.h:156