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