]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
ShellPkg: Fix SimpleTextInputEx based "CTRL-S" by emptying the buffer to prevent...
[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
b54fd049 4 Copyright (c) 2009 - 2011, 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
205 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);\r
206\r
207 if (Pattern == NULL || SearchString == NULL) {\r
208 RetVal = FALSE;\r
209 } else {\r
210 RetVal = TRUE;\r
211 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {\r
212 RetVal = FALSE;\r
213 }\r
214 }\r
215\r
216 SHELL_FREE_NON_NULL(Pattern );\r
217 SHELL_FREE_NON_NULL(SearchString);\r
218\r
219 return (RetVal);\r
a405b86d 220}\r
221\r
222/**\r
223 Function for 'rm' command.\r
224\r
225 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
226 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
227**/\r
228SHELL_STATUS\r
229EFIAPI\r
230ShellCommandRunRm (\r
231 IN EFI_HANDLE ImageHandle,\r
232 IN EFI_SYSTEM_TABLE *SystemTable\r
233 )\r
234{\r
235 EFI_STATUS Status;\r
236 LIST_ENTRY *Package;\r
237 CHAR16 *ProblemParam;\r
238 CONST CHAR16 *Param;\r
239 SHELL_STATUS ShellStatus;\r
240 UINTN ParamCount;\r
241 EFI_SHELL_FILE_INFO *FileList;\r
242 EFI_SHELL_FILE_INFO *Node;\r
243\r
244 ProblemParam = NULL;\r
245 ShellStatus = SHELL_SUCCESS;\r
246 ParamCount = 0;\r
247 FileList = NULL;\r
248\r
249 //\r
250 // initialize the shell lib (we must be in non-auto-init...)\r
251 //\r
252 Status = ShellInitialize();\r
253 ASSERT_EFI_ERROR(Status);\r
254\r
255 //\r
256 // parse the command line\r
257 //\r
258 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
259 if (EFI_ERROR(Status)) {\r
260 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
261 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
262 FreePool(ProblemParam);\r
263 ShellStatus = SHELL_INVALID_PARAMETER;\r
264 } else {\r
265 ASSERT(FALSE);\r
266 }\r
267 } else {\r
268 //\r
269 // check for "-?"\r
270 //\r
271 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
272 ASSERT(FALSE);\r
273 }\r
274 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
275 //\r
276 // we insufficient parameters\r
277 //\r
278 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
279 ShellStatus = SHELL_INVALID_PARAMETER;\r
280 } else {\r
281 //\r
282 // get a list with each file specified by parameters\r
283 // if parameter is a directory then add all the files below it to the list\r
284 //\r
285 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
286 ; Param != NULL\r
287 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
288 ){\r
289 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
290 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
291 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, (CHAR16*)Param);\r
292 ShellStatus = SHELL_NOT_FOUND;\r
293 break;\r
294 }\r
295 }\r
296\r
297 if (ShellStatus == SHELL_SUCCESS){\r
298 //\r
299 // loop through the list and make sure we are not aborting...\r
300 //\r
301 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
302 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
303 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
304 ){\r
305 //\r
306 // skip the directory traversing stuff...\r
307 //\r
308 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
309 continue;\r
310 }\r
311\r
312 //\r
313 // do the deleting of nodes\r
314 //\r
315 if (EFI_ERROR(Node->Status)){\r
316 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
317 ShellStatus = SHELL_ACCESS_DENIED;\r
318 break;\r
319 }\r
320 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
321 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
322 ShellStatus = SHELL_INVALID_PARAMETER;\r
323 break;\r
324 }\r
325\r
326 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
327 }\r
328 }\r
329 //\r
330 // Free the fileList\r
331 //\r
332 if (FileList != NULL) {\r
333 Status = ShellCloseFileMetaArg(&FileList);\r
334 }\r
335 FileList = NULL;\r
336 }\r
337\r
338 //\r
339 // free the command line package\r
340 //\r
341 ShellCommandLineFreeVarList (Package);\r
342 }\r
343\r
344 return (ShellStatus);\r
345}\r
346\r