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