]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
This patch replaces StrCpy with StrnCpy or refactors out the usage of StrCpy through...
[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
0d807dae 4 Copyright (c) 2009 - 2014, 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
0d807dae 42 FileInfo = NULL;\r
a405b86d 43\r
e755a4ca 44 for (FileHandleFindFirstFile(FileHandle, &FileInfo)\r
a405b86d 45 ; !NoFile\r
e755a4ca 46 ; FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)\r
a405b86d 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
22feb630 79 CHAR16 *TempName;\r
81957f8d 80 UINTN NewSize;\r
a405b86d 81\r
82 Resp = NULL;\r
83 ShellStatus = SHELL_SUCCESS;\r
84 List = NULL;\r
85 Status = EFI_SUCCESS;\r
86\r
87 if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {\r
88 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, Node->FullName);\r
89 return (SHELL_ACCESS_DENIED);\r
90 }\r
91\r
92 if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {\r
93 if (!IsDirectoryEmpty(Node->Handle)) {\r
94 if (!Quiet) {\r
95 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);\r
96 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);\r
a405b86d 97 ASSERT(Resp != NULL);\r
98 if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {\r
99 SHELL_FREE_NON_NULL(Resp);\r
100 return (SHELL_ABORTED);\r
101 }\r
102 SHELL_FREE_NON_NULL(Resp);\r
103 }\r
104 //\r
105 // empty out the directory\r
106 //\r
107 Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);\r
108 if (EFI_ERROR(Status)) {\r
109 if (List!=NULL) {\r
110 gEfiShellProtocol->FreeFileList(&List);\r
111 }\r
112 return (SHELL_DEVICE_ERROR);\r
113 }\r
114 for (Node2 = (EFI_SHELL_FILE_INFO *)GetFirstNode(&List->Link)\r
115 ; !IsNull(&List->Link, &Node2->Link)\r
116 ; Node2 = (EFI_SHELL_FILE_INFO *)GetNextNode(&List->Link, &Node2->Link)\r
117 ){\r
118 //\r
119 // skip the directory traversing stuff...\r
120 //\r
121 if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {\r
122 continue;\r
123 }\r
124 Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);\r
22feb630
JC
125 if (EFI_ERROR(Node2->Status) && StrStr(Node2->FileName, L":") == NULL) {\r
126 //\r
127 // Update the node filename to have full path with file system identifier\r
128 //\r
81957f8d
JC
129 NewSize = StrSize(Node->FullName) + StrSize(Node2->FullName);\r
130 TempName = AllocateZeroPool(NewSize);\r
7461437c 131 if (TempName == NULL) {\r
73c5c619 132 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
7461437c 133 } else {\r
81957f8d 134 StrnCpy(TempName, Node->FullName, NewSize/sizeof(CHAR16) -1);\r
7461437c 135 TempName[StrStr(TempName, L":")+1-TempName] = CHAR_NULL;\r
81957f8d 136 StrnCat(TempName, Node2->FullName, NewSize/sizeof(CHAR16) -1 - StrLen(TempName));\r
7461437c 137 FreePool((VOID*)Node2->FullName);\r
138 Node2->FullName = TempName;\r
139\r
140 //\r
141 // Now try again to open the file\r
142 //\r
143 Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);\r
144 }\r
145 }\r
146 if (!EFI_ERROR(Node2->Status)) {\r
147 ShellStatus = CascadeDelete(Node2, Quiet);\r
148 } else if (ShellStatus == SHELL_SUCCESS) {\r
75a68c69 149 ShellStatus = (SHELL_STATUS)(Node2->Status&(~0x80000000));\r
22feb630 150 }\r
a405b86d 151 if (ShellStatus != SHELL_SUCCESS) {\r
152 if (List!=NULL) {\r
153 gEfiShellProtocol->FreeFileList(&List);\r
154 }\r
155 return (ShellStatus);\r
156 }\r
157 }\r
158 if (List!=NULL) {\r
159 gEfiShellProtocol->FreeFileList(&List);\r
160 }\r
161 }\r
162 }\r
163\r
164 if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {\r
165 //\r
166 // now delete the current node...\r
167 //\r
168 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);\r
169 Status = gEfiShellProtocol->DeleteFile(Node->Handle);\r
170 Node->Handle = NULL;\r
171 }\r
172\r
173 //\r
b54fd049 174 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).\r
a405b86d 175 //\r
176 if (Status != EFI_SUCCESS){\r
177 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);\r
178 return (SHELL_ACCESS_DENIED);\r
179 } else {\r
a737ea73
JC
180 if (!Quiet) {\r
181 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);\r
182 }\r
a405b86d 183 return (SHELL_SUCCESS);\r
184 }\r
185}\r
186\r
b54fd049 187/**\r
188 Determins if a Node is a valid delete target. Will prevent deleting the root directory.\r
189\r
190 @param[in] List RESERVED. Not used.\r
191 @param[in] Node The node to analyze.\r
192 @param[in] Package RESERVED. Not used.\r
193**/\r
a405b86d 194BOOLEAN\r
195EFIAPI\r
196IsValidDeleteTarget(\r
197 IN CONST EFI_SHELL_FILE_INFO *List,\r
198 IN CONST EFI_SHELL_FILE_INFO *Node,\r
199 IN CONST LIST_ENTRY *Package\r
200 )\r
201{\r
202 CONST CHAR16 *TempLocation;\r
eef1ed46 203 BOOLEAN RetVal;\r
204 CHAR16 *SearchString;\r
205 CHAR16 *Pattern;\r
a405b86d 206 UINTN Size;\r
207\r
eef1ed46 208 if (Node == NULL || Node->FullName == NULL) {\r
209 return (FALSE);\r
210 }\r
211\r
a405b86d 212 TempLocation = StrStr(Node->FullName, L":");\r
eef1ed46 213 if (StrLen(TempLocation) <= 2) {\r
a405b86d 214 //\r
215 // Deleting the root directory is invalid.\r
216 //\r
217 return (FALSE);\r
218 }\r
eef1ed46 219\r
a405b86d 220 TempLocation = ShellGetCurrentDir(NULL);\r
eef1ed46 221 if (TempLocation == NULL) {\r
222 //\r
223 // No working directory is specified so whatever is left is ok.\r
224 //\r
225 return (TRUE);\r
a405b86d 226 }\r
a405b86d 227\r
eef1ed46 228 Pattern = NULL;\r
229 SearchString = NULL;\r
230 Size = 0;\r
231 Pattern = StrnCatGrow(&Pattern , NULL, TempLocation , 0);\r
232 SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);\r
a32980dd 233 if (!EFI_ERROR(ShellIsDirectory(SearchString))) {\r
234 SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);\r
235 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);\r
236 }\r
eef1ed46 237\r
238 if (Pattern == NULL || SearchString == NULL) {\r
239 RetVal = FALSE;\r
240 } else {\r
241 RetVal = TRUE;\r
242 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {\r
243 RetVal = FALSE;\r
244 }\r
245 }\r
246\r
247 SHELL_FREE_NON_NULL(Pattern );\r
248 SHELL_FREE_NON_NULL(SearchString);\r
249\r
250 return (RetVal);\r
a405b86d 251}\r
252\r
253/**\r
254 Function for 'rm' command.\r
255\r
256 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
257 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
258**/\r
259SHELL_STATUS\r
260EFIAPI\r
261ShellCommandRunRm (\r
262 IN EFI_HANDLE ImageHandle,\r
263 IN EFI_SYSTEM_TABLE *SystemTable\r
264 )\r
265{\r
266 EFI_STATUS Status;\r
267 LIST_ENTRY *Package;\r
268 CHAR16 *ProblemParam;\r
269 CONST CHAR16 *Param;\r
270 SHELL_STATUS ShellStatus;\r
271 UINTN ParamCount;\r
272 EFI_SHELL_FILE_INFO *FileList;\r
273 EFI_SHELL_FILE_INFO *Node;\r
274\r
275 ProblemParam = NULL;\r
276 ShellStatus = SHELL_SUCCESS;\r
277 ParamCount = 0;\r
278 FileList = NULL;\r
279\r
280 //\r
281 // initialize the shell lib (we must be in non-auto-init...)\r
282 //\r
283 Status = ShellInitialize();\r
284 ASSERT_EFI_ERROR(Status);\r
285\r
286 //\r
287 // parse the command line\r
288 //\r
289 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
290 if (EFI_ERROR(Status)) {\r
291 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
292 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
293 FreePool(ProblemParam);\r
294 ShellStatus = SHELL_INVALID_PARAMETER;\r
295 } else {\r
296 ASSERT(FALSE);\r
297 }\r
298 } else {\r
299 //\r
300 // check for "-?"\r
301 //\r
302 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
303 ASSERT(FALSE);\r
304 }\r
305 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
306 //\r
307 // we insufficient parameters\r
308 //\r
309 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
310 ShellStatus = SHELL_INVALID_PARAMETER;\r
311 } else {\r
312 //\r
313 // get a list with each file specified by parameters\r
314 // if parameter is a directory then add all the files below it to the list\r
315 //\r
316 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
317 ; Param != NULL\r
318 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
319 ){\r
320 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
321 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
322 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, (CHAR16*)Param);\r
323 ShellStatus = SHELL_NOT_FOUND;\r
324 break;\r
325 }\r
326 }\r
327\r
328 if (ShellStatus == SHELL_SUCCESS){\r
329 //\r
330 // loop through the list and make sure we are not aborting...\r
331 //\r
332 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
333 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
334 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
335 ){\r
336 //\r
337 // skip the directory traversing stuff...\r
338 //\r
339 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
340 continue;\r
341 }\r
342\r
343 //\r
344 // do the deleting of nodes\r
345 //\r
346 if (EFI_ERROR(Node->Status)){\r
347 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
348 ShellStatus = SHELL_ACCESS_DENIED;\r
349 break;\r
350 }\r
351 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
352 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
353 ShellStatus = SHELL_INVALID_PARAMETER;\r
354 break;\r
355 }\r
356\r
357 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
358 }\r
359 }\r
360 //\r
361 // Free the fileList\r
362 //\r
363 if (FileList != NULL) {\r
364 Status = ShellCloseFileMetaArg(&FileList);\r
365 }\r
366 FileList = NULL;\r
367 }\r
368\r
369 //\r
370 // free the command line package\r
371 //\r
372 ShellCommandLineFreeVarList (Package);\r
373 }\r
374\r
375 return (ShellStatus);\r
376}\r
377\r