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