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