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