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