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