]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Rm.c
ShellPkg: Refactor string manipulation
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Rm.c
1 /** @file
2 Main file for attrib shell level 2 function.
3
4 Copyright (c) 2009 - 2014, 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 FileInfo = NULL;
43
44 for (FileHandleFindFirstFile(FileHandle, &FileInfo)
45 ; !NoFile
46 ; FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)
47 ){
48 if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName
49 &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {
50 RetVal = FALSE;
51 }
52 }
53 return (RetVal);
54 }
55
56 /**
57 Delete a node and all nodes under it (including sub directories).
58
59 @param[in] Node The node to start deleting with.
60 @param[in] Quiet TRUE to print no messages.
61
62 @retval SHELL_SUCCESS The operation was successful.
63 @retval SHELL_ACCESS_DENIED A file was read only.
64 @retval SHELL_ABORTED The abort message was received.
65 @retval SHELL_DEVICE_ERROR A device error occured reading this Node.
66 **/
67 SHELL_STATUS
68 EFIAPI
69 CascadeDelete(
70 IN EFI_SHELL_FILE_INFO *Node,
71 IN CONST BOOLEAN Quiet
72 )
73 {
74 SHELL_STATUS ShellStatus;
75 EFI_SHELL_FILE_INFO *List;
76 EFI_SHELL_FILE_INFO *Node2;
77 EFI_STATUS Status;
78 SHELL_PROMPT_RESPONSE *Resp;
79 CHAR16 *TempName;
80
81 Resp = NULL;
82 ShellStatus = SHELL_SUCCESS;
83 List = NULL;
84 Status = EFI_SUCCESS;
85
86 if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
87 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, Node->FullName);
88 return (SHELL_ACCESS_DENIED);
89 }
90
91 if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
92 if (!IsDirectoryEmpty(Node->Handle)) {
93 if (!Quiet) {
94 Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);
95 Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
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 = (SHELL_STATUS)(Node2->Status&(~0x80000000));
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 if (!Quiet) {
179 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);
180 }
181 return (SHELL_SUCCESS);
182 }
183 }
184
185 /**
186 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
187
188 @param[in] List RESERVED. Not used.
189 @param[in] Node The node to analyze.
190 @param[in] Package RESERVED. Not used.
191 **/
192 BOOLEAN
193 EFIAPI
194 IsValidDeleteTarget(
195 IN CONST EFI_SHELL_FILE_INFO *List,
196 IN CONST EFI_SHELL_FILE_INFO *Node,
197 IN CONST LIST_ENTRY *Package
198 )
199 {
200 CONST CHAR16 *TempLocation;
201 BOOLEAN RetVal;
202 CHAR16 *SearchString;
203 CHAR16 *Pattern;
204 UINTN Size;
205
206 if (Node == NULL || Node->FullName == NULL) {
207 return (FALSE);
208 }
209
210 TempLocation = StrStr(Node->FullName, L":");
211 if (StrLen(TempLocation) <= 2) {
212 //
213 // Deleting the root directory is invalid.
214 //
215 return (FALSE);
216 }
217
218 TempLocation = ShellGetCurrentDir(NULL);
219 if (TempLocation == NULL) {
220 //
221 // No working directory is specified so whatever is left is ok.
222 //
223 return (TRUE);
224 }
225
226 Pattern = NULL;
227 SearchString = NULL;
228 Size = 0;
229 Pattern = StrnCatGrow(&Pattern , NULL, TempLocation , 0);
230 SearchString = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);
231 if (!EFI_ERROR(ShellIsDirectory(SearchString))) {
232 SearchString = StrnCatGrow(&SearchString, &Size, L"\\", 0);
233 SearchString = StrnCatGrow(&SearchString, &Size, L"*", 0);
234 }
235
236 if (Pattern == NULL || SearchString == NULL) {
237 RetVal = FALSE;
238 } else {
239 RetVal = TRUE;
240 if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {
241 RetVal = FALSE;
242 }
243 }
244
245 SHELL_FREE_NON_NULL(Pattern );
246 SHELL_FREE_NON_NULL(SearchString);
247
248 return (RetVal);
249 }
250
251 /**
252 Function for 'rm' command.
253
254 @param[in] ImageHandle Handle to the Image (NULL if Internal).
255 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
256 **/
257 SHELL_STATUS
258 EFIAPI
259 ShellCommandRunRm (
260 IN EFI_HANDLE ImageHandle,
261 IN EFI_SYSTEM_TABLE *SystemTable
262 )
263 {
264 EFI_STATUS Status;
265 LIST_ENTRY *Package;
266 CHAR16 *ProblemParam;
267 CONST CHAR16 *Param;
268 SHELL_STATUS ShellStatus;
269 UINTN ParamCount;
270 EFI_SHELL_FILE_INFO *FileList;
271 EFI_SHELL_FILE_INFO *Node;
272
273 ProblemParam = NULL;
274 ShellStatus = SHELL_SUCCESS;
275 ParamCount = 0;
276 FileList = NULL;
277
278 //
279 // initialize the shell lib (we must be in non-auto-init...)
280 //
281 Status = ShellInitialize();
282 ASSERT_EFI_ERROR(Status);
283
284 //
285 // parse the command line
286 //
287 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
288 if (EFI_ERROR(Status)) {
289 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
290 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
291 FreePool(ProblemParam);
292 ShellStatus = SHELL_INVALID_PARAMETER;
293 } else {
294 ASSERT(FALSE);
295 }
296 } else {
297 //
298 // check for "-?"
299 //
300 if (ShellCommandLineGetFlag(Package, L"-?")) {
301 ASSERT(FALSE);
302 }
303 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
304 //
305 // we insufficient parameters
306 //
307 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
308 ShellStatus = SHELL_INVALID_PARAMETER;
309 } else {
310 //
311 // get a list with each file specified by parameters
312 // if parameter is a directory then add all the files below it to the list
313 //
314 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
315 ; Param != NULL
316 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
317 ){
318 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
319 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {
320 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, (CHAR16*)Param);
321 ShellStatus = SHELL_NOT_FOUND;
322 break;
323 }
324 }
325
326 if (ShellStatus == SHELL_SUCCESS){
327 //
328 // loop through the list and make sure we are not aborting...
329 //
330 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
331 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
332 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
333 ){
334 //
335 // skip the directory traversing stuff...
336 //
337 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
338 continue;
339 }
340
341 //
342 // do the deleting of nodes
343 //
344 if (EFI_ERROR(Node->Status)){
345 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);
346 ShellStatus = SHELL_ACCESS_DENIED;
347 break;
348 }
349 if (!IsValidDeleteTarget(FileList, Node, Package)) {
350 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);
351 ShellStatus = SHELL_INVALID_PARAMETER;
352 break;
353 }
354
355 ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));
356 }
357 }
358 //
359 // Free the fileList
360 //
361 if (FileList != NULL) {
362 Status = ShellCloseFileMetaArg(&FileList);
363 }
364 FileList = NULL;
365 }
366
367 //
368 // free the command line package
369 //
370 ShellCommandLineFreeVarList (Package);
371 }
372
373 return (ShellStatus);
374 }
375