]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
move a function to the correct library.
[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
175 CHAR16 *Temp2;\r
176 UINTN Size;\r
177\r
178 TempLocation = StrStr(Node->FullName, L":");\r
179 if (StrLen(TempLocation) == 2) {\r
180 //\r
181 // Deleting the root directory is invalid.\r
182 //\r
183 return (FALSE);\r
184 }\r
185 TempLocation = ShellGetCurrentDir(NULL);\r
186 Size = 0;\r
187 Temp2 = NULL;\r
188 StrnCatGrow(&Temp2, &Size, TempLocation, 0);\r
189 if (StrStr(Temp2, Node->FullName) != NULL) {\r
190 FreePool(Temp2);\r
191 return (FALSE);\r
192 }\r
193 FreePool(Temp2);\r
194\r
195 return (TRUE);\r
196}\r
197\r
198/**\r
199 Function for 'rm' command.\r
200\r
201 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
202 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
203**/\r
204SHELL_STATUS\r
205EFIAPI\r
206ShellCommandRunRm (\r
207 IN EFI_HANDLE ImageHandle,\r
208 IN EFI_SYSTEM_TABLE *SystemTable\r
209 )\r
210{\r
211 EFI_STATUS Status;\r
212 LIST_ENTRY *Package;\r
213 CHAR16 *ProblemParam;\r
214 CONST CHAR16 *Param;\r
215 SHELL_STATUS ShellStatus;\r
216 UINTN ParamCount;\r
217 EFI_SHELL_FILE_INFO *FileList;\r
218 EFI_SHELL_FILE_INFO *Node;\r
219\r
220 ProblemParam = NULL;\r
221 ShellStatus = SHELL_SUCCESS;\r
222 ParamCount = 0;\r
223 FileList = NULL;\r
224\r
225 //\r
226 // initialize the shell lib (we must be in non-auto-init...)\r
227 //\r
228 Status = ShellInitialize();\r
229 ASSERT_EFI_ERROR(Status);\r
230\r
231 //\r
232 // parse the command line\r
233 //\r
234 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
235 if (EFI_ERROR(Status)) {\r
236 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
237 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
238 FreePool(ProblemParam);\r
239 ShellStatus = SHELL_INVALID_PARAMETER;\r
240 } else {\r
241 ASSERT(FALSE);\r
242 }\r
243 } else {\r
244 //\r
245 // check for "-?"\r
246 //\r
247 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
248 ASSERT(FALSE);\r
249 }\r
250 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
251 //\r
252 // we insufficient parameters\r
253 //\r
254 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
255 ShellStatus = SHELL_INVALID_PARAMETER;\r
256 } else {\r
257 //\r
258 // get a list with each file specified by parameters\r
259 // if parameter is a directory then add all the files below it to the list\r
260 //\r
261 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
262 ; Param != NULL\r
263 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
264 ){\r
265 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
266 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
267 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, (CHAR16*)Param);\r
268 ShellStatus = SHELL_NOT_FOUND;\r
269 break;\r
270 }\r
271 }\r
272\r
273 if (ShellStatus == SHELL_SUCCESS){\r
274 //\r
275 // loop through the list and make sure we are not aborting...\r
276 //\r
277 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
278 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
279 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
280 ){\r
281 //\r
282 // skip the directory traversing stuff...\r
283 //\r
284 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
285 continue;\r
286 }\r
287\r
288 //\r
289 // do the deleting of nodes\r
290 //\r
291 if (EFI_ERROR(Node->Status)){\r
292 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);\r
293 ShellStatus = SHELL_ACCESS_DENIED;\r
294 break;\r
295 }\r
296 if (!IsValidDeleteTarget(FileList, Node, Package)) {\r
297 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);\r
298 ShellStatus = SHELL_INVALID_PARAMETER;\r
299 break;\r
300 }\r
301\r
302 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));\r
303 }\r
304 }\r
305 //\r
306 // Free the fileList\r
307 //\r
308 if (FileList != NULL) {\r
309 Status = ShellCloseFileMetaArg(&FileList);\r
310 }\r
311 FileList = NULL;\r
312 }\r
313\r
314 //\r
315 // free the command line package\r
316 //\r
317 ShellCommandLineFreeVarList (Package);\r
318 }\r
319\r
320 return (ShellStatus);\r
321}\r
322\r