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