2 Main file for attrib shell level 2 function.
4 Copyright (c) 2009 - 2011, 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
37 EFI_FILE_INFO
*FileInfo
;
44 for (Status
= FileHandleFindFirstFile(FileHandle
, &FileInfo
)
46 ; Status
= 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
;
81 ShellStatus
= SHELL_SUCCESS
;
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
);
90 if ((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
91 if (!IsDirectoryEmpty(Node
->Handle
)) {
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
);
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 ShellStatus
= CascadeDelete(Node2
, Quiet
);
125 if (ShellStatus
!= SHELL_SUCCESS
) {
127 gEfiShellProtocol
->FreeFileList(&List
);
129 return (ShellStatus
);
133 gEfiShellProtocol
->FreeFileList(&List
);
138 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
140 // now delete the current node...
142 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
143 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
148 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
150 if (Status
!= EFI_SUCCESS
){
151 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
152 return (SHELL_ACCESS_DENIED
);
154 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
155 return (SHELL_SUCCESS
);
160 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
162 @param[in] List RESERVED. Not used.
163 @param[in] Node The node to analyze.
164 @param[in] Package RESERVED. Not used.
169 IN CONST EFI_SHELL_FILE_INFO
*List
,
170 IN CONST EFI_SHELL_FILE_INFO
*Node
,
171 IN CONST LIST_ENTRY
*Package
174 CONST CHAR16
*TempLocation
;
176 CHAR16
*SearchString
;
180 if (Node
== NULL
|| Node
->FullName
== NULL
) {
184 TempLocation
= StrStr(Node
->FullName
, L
":");
185 if (StrLen(TempLocation
) <= 2) {
187 // Deleting the root directory is invalid.
192 TempLocation
= ShellGetCurrentDir(NULL
);
193 if (TempLocation
== NULL
) {
195 // No working directory is specified so whatever is left is ok.
203 Pattern
= StrnCatGrow(&Pattern
, NULL
, TempLocation
, 0);
204 SearchString
= StrnCatGrow(&SearchString
, &Size
, Node
->FullName
, 0);
205 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"*", 0);
207 if (Pattern
== NULL
|| SearchString
== NULL
) {
211 if (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Pattern
, SearchString
)) {
216 SHELL_FREE_NON_NULL(Pattern
);
217 SHELL_FREE_NON_NULL(SearchString
);
223 Function for 'rm' command.
225 @param[in] ImageHandle Handle to the Image (NULL if Internal).
226 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
231 IN EFI_HANDLE ImageHandle
,
232 IN EFI_SYSTEM_TABLE
*SystemTable
237 CHAR16
*ProblemParam
;
239 SHELL_STATUS ShellStatus
;
241 EFI_SHELL_FILE_INFO
*FileList
;
242 EFI_SHELL_FILE_INFO
*Node
;
245 ShellStatus
= SHELL_SUCCESS
;
250 // initialize the shell lib (we must be in non-auto-init...)
252 Status
= ShellInitialize();
253 ASSERT_EFI_ERROR(Status
);
256 // parse the command line
258 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
259 if (EFI_ERROR(Status
)) {
260 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
261 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
262 FreePool(ProblemParam
);
263 ShellStatus
= SHELL_INVALID_PARAMETER
;
271 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
274 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
276 // we insufficient parameters
278 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
279 ShellStatus
= SHELL_INVALID_PARAMETER
;
282 // get a list with each file specified by parameters
283 // if parameter is a directory then add all the files below it to the list
285 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
287 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
289 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
290 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
291 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, (CHAR16
*)Param
);
292 ShellStatus
= SHELL_NOT_FOUND
;
297 if (ShellStatus
== SHELL_SUCCESS
){
299 // loop through the list and make sure we are not aborting...
301 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
302 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
303 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
306 // skip the directory traversing stuff...
308 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
313 // do the deleting of nodes
315 if (EFI_ERROR(Node
->Status
)){
316 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
317 ShellStatus
= SHELL_ACCESS_DENIED
;
320 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
321 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
322 ShellStatus
= SHELL_INVALID_PARAMETER
;
326 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
332 if (FileList
!= NULL
) {
333 Status
= ShellCloseFileMetaArg(&FileList
);
339 // free the command line package
341 ShellCommandLineFreeVarList (Package
);
344 return (ShellStatus
);