2 Main file for attrib shell level 2 function.
4 Copyright (c) 2009 - 2010, 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
[] = {
25 IN EFI_HANDLE FileHandle
29 EFI_FILE_INFO
*FileInfo
;
36 for (Status
= FileHandleFindFirstFile(FileHandle
, &FileInfo
)
38 ; Status
= FileHandleFindNextFile(FileHandle
, FileInfo
, &NoFile
)
40 if (StrStr(FileInfo
->FileName
, L
".") != FileInfo
->FileName
41 &&StrStr(FileInfo
->FileName
, L
"..") != FileInfo
->FileName
) {
51 IN EFI_SHELL_FILE_INFO
*Node
,
52 IN CONST BOOLEAN Quiet
55 SHELL_STATUS ShellStatus
;
56 EFI_SHELL_FILE_INFO
*List
;
57 EFI_SHELL_FILE_INFO
*Node2
;
59 SHELL_PROMPT_RESPONSE
*Resp
;
62 ShellStatus
= SHELL_SUCCESS
;
66 if ((Node
->Info
->Attribute
& EFI_FILE_READ_ONLY
) == EFI_FILE_READ_ONLY
) {
67 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DETELE_RO
), gShellLevel2HiiHandle
, Node
->FullName
);
68 return (SHELL_ACCESS_DENIED
);
71 if ((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
72 if (!IsDirectoryEmpty(Node
->Handle
)) {
74 Status
= ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_RM_LOG_DELETE_CONF
), gShellLevel2HiiHandle
, Node
->FullName
);
75 Status
= ShellPromptForResponse(ShellPromptResponseTypeYesNo
, NULL
, (VOID
**)&Resp
);
76 ASSERT_EFI_ERROR(Status
);
78 if (EFI_ERROR(Status
) || *Resp
!= ShellPromptResponseYes
) {
79 SHELL_FREE_NON_NULL(Resp
);
80 return (SHELL_ABORTED
);
82 SHELL_FREE_NON_NULL(Resp
);
85 // empty out the directory
87 Status
= gEfiShellProtocol
->FindFilesInDir(Node
->Handle
, &List
);
88 if (EFI_ERROR(Status
)) {
90 gEfiShellProtocol
->FreeFileList(&List
);
92 return (SHELL_DEVICE_ERROR
);
94 for (Node2
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&List
->Link
)
95 ; !IsNull(&List
->Link
, &Node2
->Link
)
96 ; Node2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&List
->Link
, &Node2
->Link
)
99 // skip the directory traversing stuff...
101 if (StrCmp(Node2
->FileName
, L
".") == 0 || StrCmp(Node2
->FileName
, L
"..") == 0) {
104 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
105 ShellStatus
= CascadeDelete(Node2
, Quiet
);
106 if (ShellStatus
!= SHELL_SUCCESS
) {
108 gEfiShellProtocol
->FreeFileList(&List
);
110 return (ShellStatus
);
114 gEfiShellProtocol
->FreeFileList(&List
);
119 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
121 // now delete the current node...
123 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
124 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
129 // We cant allow for the warning here!
131 if (Status
!= EFI_SUCCESS
){
132 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
133 return (SHELL_ACCESS_DENIED
);
135 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
136 return (SHELL_SUCCESS
);
143 IN CONST EFI_SHELL_FILE_INFO
*List
,
144 IN CONST EFI_SHELL_FILE_INFO
*Node
,
145 IN CONST LIST_ENTRY
*Package
148 CONST CHAR16
*TempLocation
;
152 TempLocation
= StrStr(Node
->FullName
, L
":");
153 if (StrLen(TempLocation
) == 2) {
155 // Deleting the root directory is invalid.
159 TempLocation
= ShellGetCurrentDir(NULL
);
162 StrnCatGrow(&Temp2
, &Size
, TempLocation
, 0);
163 if (StrStr(Temp2
, Node
->FullName
) != NULL
) {
173 Function for 'rm' command.
175 @param[in] ImageHandle Handle to the Image (NULL if Internal).
176 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
181 IN EFI_HANDLE ImageHandle
,
182 IN EFI_SYSTEM_TABLE
*SystemTable
187 CHAR16
*ProblemParam
;
189 SHELL_STATUS ShellStatus
;
191 EFI_SHELL_FILE_INFO
*FileList
;
192 EFI_SHELL_FILE_INFO
*Node
;
195 ShellStatus
= SHELL_SUCCESS
;
200 // initialize the shell lib (we must be in non-auto-init...)
202 Status
= ShellInitialize();
203 ASSERT_EFI_ERROR(Status
);
206 // parse the command line
208 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
209 if (EFI_ERROR(Status
)) {
210 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
211 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
212 FreePool(ProblemParam
);
213 ShellStatus
= SHELL_INVALID_PARAMETER
;
221 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
224 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
226 // we insufficient parameters
228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
229 ShellStatus
= SHELL_INVALID_PARAMETER
;
232 // get a list with each file specified by parameters
233 // if parameter is a directory then add all the files below it to the list
235 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
237 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
239 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
240 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
241 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, (CHAR16
*)Param
);
242 ShellStatus
= SHELL_NOT_FOUND
;
247 if (ShellStatus
== SHELL_SUCCESS
){
249 // loop through the list and make sure we are not aborting...
251 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
252 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
253 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
256 // skip the directory traversing stuff...
258 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
263 // do the deleting of nodes
265 if (EFI_ERROR(Node
->Status
)){
266 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
267 ShellStatus
= SHELL_ACCESS_DENIED
;
270 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
271 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
272 ShellStatus
= SHELL_INVALID_PARAMETER
;
276 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
282 if (FileList
!= NULL
) {
283 Status
= ShellCloseFileMetaArg(&FileList
);
289 // free the command line package
291 ShellCommandLineFreeVarList (Package
);
294 return (ShellStatus
);