]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
1. Fix TOCTOU issue in VariableSmm, FtwSmm, FpdtSmm, SmmCorePerformance SMM handler...
[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
a32980dd 4 Copyright (c) 2009 - 2012, 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
36 EFI_STATUS Status;\r
37 EFI_FILE_INFO *FileInfo;\r
38 BOOLEAN NoFile;\r
39 BOOLEAN RetVal;\r
40\r
41 RetVal = TRUE;\r
42 NoFile = FALSE;\r
43\r
44 for (Status = FileHandleFindFirstFile(FileHandle, &FileInfo)\r
45 ; !NoFile\r
46 ; Status = FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)\r
47 ){\r
48 if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName\r
49 &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {\r
50 RetVal = FALSE;\r
51 }\r
52 }\r
53 return (RetVal);\r
54}\r
55\r
b54fd049 56/**\r
57 Delete a node and all nodes under it (including sub directories).\r
58\r
59 @param[in] Node The node to start deleting with.\r
60 @param[in] Quiet TRUE to print no messages.\r
61\r
62 @retval SHELL_SUCCESS The operation was successful.\r
63 @retval SHELL_ACCESS_DENIED A file was read only.\r
64 @retval SHELL_ABORTED The abort message was received.\r
65 @retval SHELL_DEVICE_ERROR A device error occured reading this Node.\r
66**/\r
a405b86d 67SHELL_STATUS\r
68EFIAPI\r
69CascadeDelete(\r
70 IN EFI_SHELL_FILE_INFO *Node,\r
71 IN CONST BOOLEAN Quiet\r
72 )\r
73{\r
74 SHELL_STATUS ShellStatus;\r
75 EFI_SHELL_FILE_INFO *List;\r
76 EFI_SHELL_FILE_INFO *Node2;\r
77 EFI_STATUS Status;\r
78 SHELL_PROMPT_RESPONSE *Resp;\r
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
95 ASSERT_EFI_ERROR(Status);\r
96 ASSERT(Resp != NULL);\r
97 if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {\r
98 SHELL_FREE_NON_NULL(Resp);\r
99 return (SHELL_ABORTED);\r
100 }\r
101 SHELL_FREE_NON_NULL(Resp);\r
102 }\r
103 //\r
104 // empty out the directory\r
105 //\r
106 Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);\r
107 if (EFI_ERROR(Status)) {\r
108 if (List!=NULL) {\r
109 gEfiShellProtocol->FreeFileList(&List);\r
110 }\r
111 return (SHELL_DEVICE_ERROR);\r
112 }\r
113 for (Node2 = (EFI_SHELL_FILE_INFO *)GetFirstNode(&List->Link)\r
114 ; !IsNull(&List->Link, &Node2->Link)\r
115 ; Node2 = (EFI_SHELL_FILE_INFO *)GetNextNode(&List->Link, &Node2->Link)\r
116 ){\r
117 //\r
118 // skip the directory traversing stuff...\r
119 //\r
120 if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {\r
121 continue;\r
122 }\r
123 Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);\r
124 ShellStatus = CascadeDelete(Node2, Quiet);\r
125 if (ShellStatus != SHELL_SUCCESS) {\r
126 if (List!=NULL) {\r
127 gEfiShellProtocol->FreeFileList(&List);\r
128 }\r
129 return (ShellStatus);\r
130 }\r
131 }\r
132 if (List!=NULL) {\r
133 gEfiShellProtocol->FreeFileList(&List);\r
134 }\r
135 }\r
136 }\r
137\r
138 if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {\r
139 //\r
140 // now delete the current node...\r
141 //\r
142 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);\r
143 Status = gEfiShellProtocol->DeleteFile(Node->Handle);\r
144 Node->Handle = NULL;\r
145 }\r
146\r
147 //\r
b54fd049 148 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).\r
a405b86d 149 //\r
150 if (Status != EFI_SUCCESS){\r
151 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);\r
152 return (SHELL_ACCESS_DENIED);\r
153 } else {\r
154 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);\r
155 return (SHELL_SUCCESS);\r
156 }\r
157}\r
158\r
b54fd049 159/**\r
160 Determins if a Node is a valid delete target. Will prevent deleting the root directory.\r
161\r
162 @param[in] List RESERVED. Not used.\r
163 @param[in] Node The node to analyze.\r
164 @param[in] Package RESERVED. Not used.\r
165**/\r
a405b86d 166BOOLEAN\r
167EFIAPI\r
168IsValidDeleteTarget(\r
169 IN CONST EFI_SHELL_FILE_INFO *List,\r
170 IN CONST EFI_SHELL_FILE_INFO *Node,\r
171 IN CONST LIST_ENTRY *Package\r
172 )\r
173{\r
174 CONST CHAR16 *TempLocation;\r
eef1ed46 175 BOOLEAN RetVal;\r
176 CHAR16 *SearchString;\r
177 CHAR16 *Pattern;\r
a405b86d 178 UINTN Size;\r
179\r
eef1ed46 180 if (Node == NULL || Node->FullName == NULL) {\r
181 return (FALSE);\r
182 }\r
183\r
a405b86d 184 TempLocation = StrStr(Node->FullName, L":");\r
eef1ed46 185 if (StrLen(TempLocation) <= 2) {\r
a405b86d 186 //\r
187 // Deleting the root directory is invalid.\r
188 //\r
189 return (FALSE);\r
190 }\r
eef1ed46 191\r
a405b86d 192 TempLocation = ShellGetCurrentDir(NULL);\r
eef1ed46 193 if (TempLocation == NULL) {\r
194 //\r
195 // No working directory is specified so whatever is left is ok.\r
196 //\r
197 return (TRUE);\r
a405b86d 198 }\r
a405b86d 199\r
eef1ed46 200 Pattern = NULL;\r
201 SearchString = NULL;\r
202 Size = 0;\r
203 Pattern = StrnCatGrow(&Pattern , NULL, TempLocation , 0);\r
204 SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);\r
a32980dd 205 if (!EFI_ERROR(ShellIsDirectory(SearchString))) {\r
206 SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);\r
207 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);\r
208 }\r
eef1ed46 209\r
210 if (Pattern == NULL || SearchString == NULL) {\r
211 RetVal = FALSE;\r
212 } else {\r
213 RetVal = TRUE;\r
214 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {\r
215 RetVal = FALSE;\r
216 }\r
217 }\r
218\r
219 SHELL_FREE_NON_NULL(Pattern );\r
220 SHELL_FREE_NON_NULL(SearchString);\r
221\r
222 return (RetVal);\r
a405b86d 223}\r
224\r
225/**\r
226 Function for 'rm' command.\r
227\r
228 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
229 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
230**/\r
231SHELL_STATUS\r
232EFIAPI\r
233ShellCommandRunRm (\r
234 IN EFI_HANDLE ImageHandle,\r
235 IN EFI_SYSTEM_TABLE *SystemTable\r
236 )\r
237{\r
238 EFI_STATUS Status;\r
239 LIST_ENTRY *Package;\r
240 CHAR16 *ProblemParam;\r
241 CONST CHAR16 *Param;\r
242 SHELL_STATUS ShellStatus;\r
243 UINTN ParamCount;\r
244 EFI_SHELL_FILE_INFO *FileList;\r
245 EFI_SHELL_FILE_INFO *Node;\r
246\r
247 ProblemParam = NULL;\r
248 ShellStatus = SHELL_SUCCESS;\r
249 ParamCount = 0;\r
250 FileList = NULL;\r
251\r
252 //\r
253 // initialize the shell lib (we must be in non-auto-init...)\r
254 //\r
255 Status = ShellInitialize();\r
256 ASSERT_EFI_ERROR(Status);\r
257\r
258 //\r
259 // parse the command line\r
260 //\r
261 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
262 if (EFI_ERROR(Status)) {\r
263 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
264 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
265 FreePool(ProblemParam);\r
266 ShellStatus = SHELL_INVALID_PARAMETER;\r
267 } else {\r
268 ASSERT(FALSE);\r
269 }\r
270 } else {\r
271 //\r
272 // check for "-?"\r
273 //\r
274 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
275 ASSERT(FALSE);\r
276 }\r
277 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
278 //\r
279 // we insufficient parameters\r
280 //\r
281 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
282 ShellStatus = SHELL_INVALID_PARAMETER;\r
283 } else {\r
284 //\r
285 // get a list with each file specified by parameters\r
286 // if parameter is a directory then add all the files below it to the list\r
287 //\r
288 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
289 ; Param != NULL\r
290 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
291 ){\r
292 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
293 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
294 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, (CHAR16*)Param);\r
295 ShellStatus = SHELL_NOT_FOUND;\r
296 break;\r
297 }\r
298 }\r
299\r
300 if (ShellStatus == SHELL_SUCCESS){\r
301 //\r
302 // loop through the list and make sure we are not aborting...\r
303 //\r
304 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
305 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
306 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
307 ){\r
308 //\r
309 // skip the directory traversing stuff...\r
310 //\r
311 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
312 continue;\r
313 }\r
314\r
315 //\r
316 // do the deleting of nodes\r
317 //\r
318 if (EFI_ERROR(Node->Status)){\r
319 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
320 ShellStatus = SHELL_ACCESS_DENIED;\r
321 break;\r
322 }\r
323 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
324 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
325 ShellStatus = SHELL_INVALID_PARAMETER;\r
326 break;\r
327 }\r
328\r
329 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
330 }\r
331 }\r
332 //\r
333 // Free the fileList\r
334 //\r
335 if (FileList != NULL) {\r
336 Status = ShellCloseFileMetaArg(&FileList);\r
337 }\r
338 FileList = NULL;\r
339 }\r
340\r
341 //\r
342 // free the command line package\r
343 //\r
344 ShellCommandLineFreeVarList (Package);\r
345 }\r
346\r
347 return (ShellStatus);\r
348}\r
349\r