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