2 Main file for attrib shell level 2 function.
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
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.
15 #include "UefiShellLevel2CommandsLib.h"
17 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
23 Determine if a directory has no files in it.
25 @param[in] FileHandle The EFI_HANDLE to the directory.
27 @retval TRUE The directory has no files (or directories).
28 @retval FALSE The directory has at least 1 file or directory in it.
33 IN EFI_HANDLE FileHandle
36 EFI_FILE_INFO
*FileInfo
;
44 for (FileHandleFindFirstFile(FileHandle
, &FileInfo
)
46 ; FileHandleFindNextFile(FileHandle
, FileInfo
, &NoFile
)
48 if (StrStr(FileInfo
->FileName
, L
".") != FileInfo
->FileName
49 &&StrStr(FileInfo
->FileName
, L
"..") != FileInfo
->FileName
) {
57 Delete a node and all nodes under it (including sub directories).
59 @param[in] Node The node to start deleting with.
60 @param[in] Quiet TRUE to print no messages.
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.
70 IN EFI_SHELL_FILE_INFO
*Node
,
71 IN CONST BOOLEAN Quiet
74 SHELL_STATUS ShellStatus
;
75 EFI_SHELL_FILE_INFO
*List
;
76 EFI_SHELL_FILE_INFO
*Node2
;
78 SHELL_PROMPT_RESPONSE
*Resp
;
82 ShellStatus
= SHELL_SUCCESS
;
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
);
91 if ((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
92 if (!IsDirectoryEmpty(Node
->Handle
)) {
94 Status
= ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_RM_LOG_DELETE_CONF
), gShellLevel2HiiHandle
, Node
->FullName
);
95 Status
= ShellPromptForResponse(ShellPromptResponseTypeYesNo
, NULL
, (VOID
**)&Resp
);
97 if (EFI_ERROR(Status
) || *Resp
!= ShellPromptResponseYes
) {
98 SHELL_FREE_NON_NULL(Resp
);
99 return (SHELL_ABORTED
);
101 SHELL_FREE_NON_NULL(Resp
);
104 // empty out the directory
106 Status
= gEfiShellProtocol
->FindFilesInDir(Node
->Handle
, &List
);
107 if (EFI_ERROR(Status
)) {
109 gEfiShellProtocol
->FreeFileList(&List
);
111 return (SHELL_DEVICE_ERROR
);
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
)
118 // skip the directory traversing stuff...
120 if (StrCmp(Node2
->FileName
, L
".") == 0 || StrCmp(Node2
->FileName
, L
"..") == 0) {
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
) {
126 // Update the node filename to have full path with file system identifier
128 TempName
= AllocateZeroPool(StrSize(Node
->FullName
) + StrSize(Node2
->FullName
));
129 if (TempName
== NULL
) {
130 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
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
;
139 // Now try again to open the file
141 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
144 if (!EFI_ERROR(Node2
->Status
)) {
145 ShellStatus
= CascadeDelete(Node2
, Quiet
);
146 } else if (ShellStatus
== SHELL_SUCCESS
) {
147 ShellStatus
= (SHELL_STATUS
)(Node2
->Status
&(~0x80000000));
149 if (ShellStatus
!= SHELL_SUCCESS
) {
151 gEfiShellProtocol
->FreeFileList(&List
);
153 return (ShellStatus
);
157 gEfiShellProtocol
->FreeFileList(&List
);
162 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
164 // now delete the current node...
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
167 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
172 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
174 if (Status
!= EFI_SUCCESS
){
175 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
176 return (SHELL_ACCESS_DENIED
);
178 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
179 return (SHELL_SUCCESS
);
184 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
186 @param[in] List RESERVED. Not used.
187 @param[in] Node The node to analyze.
188 @param[in] Package RESERVED. Not used.
193 IN CONST EFI_SHELL_FILE_INFO
*List
,
194 IN CONST EFI_SHELL_FILE_INFO
*Node
,
195 IN CONST LIST_ENTRY
*Package
198 CONST CHAR16
*TempLocation
;
200 CHAR16
*SearchString
;
204 if (Node
== NULL
|| Node
->FullName
== NULL
) {
208 TempLocation
= StrStr(Node
->FullName
, L
":");
209 if (StrLen(TempLocation
) <= 2) {
211 // Deleting the root directory is invalid.
216 TempLocation
= ShellGetCurrentDir(NULL
);
217 if (TempLocation
== NULL
) {
219 // No working directory is specified so whatever is left is ok.
227 Pattern
= StrnCatGrow(&Pattern
, NULL
, TempLocation
, 0);
228 SearchString
= StrnCatGrow(&SearchString
, &Size
, Node
->FullName
, 0);
229 if (!EFI_ERROR(ShellIsDirectory(SearchString
))) {
230 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"\\", 0);
231 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"*", 0);
234 if (Pattern
== NULL
|| SearchString
== NULL
) {
238 if (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Pattern
, SearchString
)) {
243 SHELL_FREE_NON_NULL(Pattern
);
244 SHELL_FREE_NON_NULL(SearchString
);
250 Function for 'rm' command.
252 @param[in] ImageHandle Handle to the Image (NULL if Internal).
253 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
258 IN EFI_HANDLE ImageHandle
,
259 IN EFI_SYSTEM_TABLE
*SystemTable
264 CHAR16
*ProblemParam
;
266 SHELL_STATUS ShellStatus
;
268 EFI_SHELL_FILE_INFO
*FileList
;
269 EFI_SHELL_FILE_INFO
*Node
;
272 ShellStatus
= SHELL_SUCCESS
;
277 // initialize the shell lib (we must be in non-auto-init...)
279 Status
= ShellInitialize();
280 ASSERT_EFI_ERROR(Status
);
283 // parse the command line
285 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
286 if (EFI_ERROR(Status
)) {
287 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
288 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
289 FreePool(ProblemParam
);
290 ShellStatus
= SHELL_INVALID_PARAMETER
;
298 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
301 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
303 // we insufficient parameters
305 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
306 ShellStatus
= SHELL_INVALID_PARAMETER
;
309 // get a list with each file specified by parameters
310 // if parameter is a directory then add all the files below it to the list
312 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
314 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
316 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
317 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
318 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, (CHAR16
*)Param
);
319 ShellStatus
= SHELL_NOT_FOUND
;
324 if (ShellStatus
== SHELL_SUCCESS
){
326 // loop through the list and make sure we are not aborting...
328 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
329 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
330 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
333 // skip the directory traversing stuff...
335 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
340 // do the deleting of nodes
342 if (EFI_ERROR(Node
->Status
)){
343 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
344 ShellStatus
= SHELL_ACCESS_DENIED
;
347 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
348 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
349 ShellStatus
= SHELL_INVALID_PARAMETER
;
353 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
359 if (FileList
!= NULL
) {
360 Status
= ShellCloseFileMetaArg(&FileList
);
366 // free the command line package
368 ShellCommandLineFreeVarList (Package
);
371 return (ShellStatus
);