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