]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
MdeModulePkg: Refine code to use Strn**S safe functions instead of Str**S ones in...
[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
191 Determins if a Node is a valid delete target. Will prevent deleting the root directory.\r
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
234 Pattern = StrnCatGrow(&Pattern , NULL, TempLocation , 0);\r
235 SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);\r
a32980dd 236 if (!EFI_ERROR(ShellIsDirectory(SearchString))) {\r
237 SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);\r
238 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);\r
239 }\r
eef1ed46 240\r
241 if (Pattern == NULL || SearchString == NULL) {\r
242 RetVal = FALSE;\r
243 } else {\r
244 RetVal = TRUE;\r
245 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {\r
246 RetVal = FALSE;\r
247 }\r
248 }\r
249\r
250 SHELL_FREE_NON_NULL(Pattern );\r
251 SHELL_FREE_NON_NULL(SearchString);\r
252\r
253 return (RetVal);\r
a405b86d 254}\r
255\r
256/**\r
257 Function for 'rm' command.\r
258\r
259 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
260 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
261**/\r
262SHELL_STATUS\r
263EFIAPI\r
264ShellCommandRunRm (\r
265 IN EFI_HANDLE ImageHandle,\r
266 IN EFI_SYSTEM_TABLE *SystemTable\r
267 )\r
268{\r
269 EFI_STATUS Status;\r
270 LIST_ENTRY *Package;\r
271 CHAR16 *ProblemParam;\r
272 CONST CHAR16 *Param;\r
273 SHELL_STATUS ShellStatus;\r
274 UINTN ParamCount;\r
275 EFI_SHELL_FILE_INFO *FileList;\r
276 EFI_SHELL_FILE_INFO *Node;\r
277\r
278 ProblemParam = NULL;\r
279 ShellStatus = SHELL_SUCCESS;\r
280 ParamCount = 0;\r
281 FileList = NULL;\r
282\r
283 //\r
284 // initialize the shell lib (we must be in non-auto-init...)\r
285 //\r
286 Status = ShellInitialize();\r
287 ASSERT_EFI_ERROR(Status);\r
288\r
289 //\r
290 // parse the command line\r
291 //\r
292 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
293 if (EFI_ERROR(Status)) {\r
294 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
099e8ff5 295 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam); \r
a405b86d 296 FreePool(ProblemParam);\r
297 ShellStatus = SHELL_INVALID_PARAMETER;\r
298 } else {\r
299 ASSERT(FALSE);\r
300 }\r
301 } else {\r
302 //\r
303 // check for "-?"\r
304 //\r
305 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
306 ASSERT(FALSE);\r
307 }\r
308 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
309 //\r
310 // we insufficient parameters\r
311 //\r
099e8ff5 312 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm"); \r
a405b86d 313 ShellStatus = SHELL_INVALID_PARAMETER;\r
314 } else {\r
315 //\r
316 // get a list with each file specified by parameters\r
317 // if parameter is a directory then add all the files below it to the list\r
318 //\r
319 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
320 ; Param != NULL\r
321 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
322 ){\r
323 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
324 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
099e8ff5 325 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16*)Param); \r
a405b86d 326 ShellStatus = SHELL_NOT_FOUND;\r
327 break;\r
328 }\r
329 }\r
330\r
331 if (ShellStatus == SHELL_SUCCESS){\r
332 //\r
333 // loop through the list and make sure we are not aborting...\r
334 //\r
335 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
336 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
337 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
338 ){\r
339 //\r
340 // skip the directory traversing stuff...\r
341 //\r
342 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
343 continue;\r
344 }\r
345\r
346 //\r
347 // do the deleting of nodes\r
348 //\r
349 if (EFI_ERROR(Node->Status)){\r
350 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
351 ShellStatus = SHELL_ACCESS_DENIED;\r
352 break;\r
353 }\r
354 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
355 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
356 ShellStatus = SHELL_INVALID_PARAMETER;\r
357 break;\r
358 }\r
359\r
360 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
361 }\r
362 }\r
363 //\r
364 // Free the fileList\r
365 //\r
366 if (FileList != NULL) {\r
367 Status = ShellCloseFileMetaArg(&FileList);\r
368 }\r
369 FileList = NULL;\r
370 }\r
371\r
372 //\r
373 // free the command line package\r
374 //\r
375 ShellCommandLineFreeVarList (Package);\r
376 }\r
377\r
378 return (ShellStatus);\r
379}\r
380\r