2 Main file for attrib shell level 2 function.
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
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
;
43 for (FileHandleFindFirstFile(FileHandle
, &FileInfo
)
45 ; FileHandleFindNextFile(FileHandle
, FileInfo
, &NoFile
)
47 if (StrStr(FileInfo
->FileName
, L
".") != FileInfo
->FileName
48 &&StrStr(FileInfo
->FileName
, L
"..") != FileInfo
->FileName
) {
56 Delete a node and all nodes under it (including sub directories).
58 @param[in] Node The node to start deleting with.
59 @param[in] Quiet TRUE to print no messages.
61 @retval SHELL_SUCCESS The operation was successful.
62 @retval SHELL_ACCESS_DENIED A file was read only.
63 @retval SHELL_ABORTED The abort message was received.
64 @retval SHELL_DEVICE_ERROR A device error occured reading this Node.
69 IN EFI_SHELL_FILE_INFO
*Node
,
70 IN CONST BOOLEAN Quiet
73 SHELL_STATUS ShellStatus
;
74 EFI_SHELL_FILE_INFO
*List
;
75 EFI_SHELL_FILE_INFO
*Node2
;
77 SHELL_PROMPT_RESPONSE
*Resp
;
80 ShellStatus
= SHELL_SUCCESS
;
84 if ((Node
->Info
->Attribute
& EFI_FILE_READ_ONLY
) == EFI_FILE_READ_ONLY
) {
85 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DETELE_RO
), gShellLevel2HiiHandle
, Node
->FullName
);
86 return (SHELL_ACCESS_DENIED
);
89 if ((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
90 if (!IsDirectoryEmpty(Node
->Handle
)) {
92 Status
= ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_RM_LOG_DELETE_CONF
), gShellLevel2HiiHandle
, Node
->FullName
);
93 Status
= ShellPromptForResponse(ShellPromptResponseTypeYesNo
, NULL
, (VOID
**)&Resp
);
94 ASSERT_EFI_ERROR(Status
);
96 if (EFI_ERROR(Status
) || *Resp
!= ShellPromptResponseYes
) {
97 SHELL_FREE_NON_NULL(Resp
);
98 return (SHELL_ABORTED
);
100 SHELL_FREE_NON_NULL(Resp
);
103 // empty out the directory
105 Status
= gEfiShellProtocol
->FindFilesInDir(Node
->Handle
, &List
);
106 if (EFI_ERROR(Status
)) {
108 gEfiShellProtocol
->FreeFileList(&List
);
110 return (SHELL_DEVICE_ERROR
);
112 for (Node2
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&List
->Link
)
113 ; !IsNull(&List
->Link
, &Node2
->Link
)
114 ; Node2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&List
->Link
, &Node2
->Link
)
117 // skip the directory traversing stuff...
119 if (StrCmp(Node2
->FileName
, L
".") == 0 || StrCmp(Node2
->FileName
, L
"..") == 0) {
122 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
123 ShellStatus
= CascadeDelete(Node2
, Quiet
);
124 if (ShellStatus
!= SHELL_SUCCESS
) {
126 gEfiShellProtocol
->FreeFileList(&List
);
128 return (ShellStatus
);
132 gEfiShellProtocol
->FreeFileList(&List
);
137 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
139 // now delete the current node...
141 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
142 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
147 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
149 if (Status
!= EFI_SUCCESS
){
150 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
151 return (SHELL_ACCESS_DENIED
);
153 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
154 return (SHELL_SUCCESS
);
159 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
161 @param[in] List RESERVED. Not used.
162 @param[in] Node The node to analyze.
163 @param[in] Package RESERVED. Not used.
168 IN CONST EFI_SHELL_FILE_INFO
*List
,
169 IN CONST EFI_SHELL_FILE_INFO
*Node
,
170 IN CONST LIST_ENTRY
*Package
173 CONST CHAR16
*TempLocation
;
175 CHAR16
*SearchString
;
179 if (Node
== NULL
|| Node
->FullName
== NULL
) {
183 TempLocation
= StrStr(Node
->FullName
, L
":");
184 if (StrLen(TempLocation
) <= 2) {
186 // Deleting the root directory is invalid.
191 TempLocation
= ShellGetCurrentDir(NULL
);
192 if (TempLocation
== NULL
) {
194 // No working directory is specified so whatever is left is ok.
202 Pattern
= StrnCatGrow(&Pattern
, NULL
, TempLocation
, 0);
203 SearchString
= StrnCatGrow(&SearchString
, &Size
, Node
->FullName
, 0);
204 if (!EFI_ERROR(ShellIsDirectory(SearchString
))) {
205 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"\\", 0);
206 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"*", 0);
209 if (Pattern
== NULL
|| SearchString
== NULL
) {
213 if (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Pattern
, SearchString
)) {
218 SHELL_FREE_NON_NULL(Pattern
);
219 SHELL_FREE_NON_NULL(SearchString
);
225 Function for 'rm' command.
227 @param[in] ImageHandle Handle to the Image (NULL if Internal).
228 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
233 IN EFI_HANDLE ImageHandle
,
234 IN EFI_SYSTEM_TABLE
*SystemTable
239 CHAR16
*ProblemParam
;
241 SHELL_STATUS ShellStatus
;
243 EFI_SHELL_FILE_INFO
*FileList
;
244 EFI_SHELL_FILE_INFO
*Node
;
247 ShellStatus
= SHELL_SUCCESS
;
252 // initialize the shell lib (we must be in non-auto-init...)
254 Status
= ShellInitialize();
255 ASSERT_EFI_ERROR(Status
);
258 // parse the command line
260 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
261 if (EFI_ERROR(Status
)) {
262 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
263 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
264 FreePool(ProblemParam
);
265 ShellStatus
= SHELL_INVALID_PARAMETER
;
273 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
276 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
278 // we insufficient parameters
280 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
281 ShellStatus
= SHELL_INVALID_PARAMETER
;
284 // get a list with each file specified by parameters
285 // if parameter is a directory then add all the files below it to the list
287 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
289 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
291 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
292 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
293 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, (CHAR16
*)Param
);
294 ShellStatus
= SHELL_NOT_FOUND
;
299 if (ShellStatus
== SHELL_SUCCESS
){
301 // loop through the list and make sure we are not aborting...
303 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
304 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
305 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
308 // skip the directory traversing stuff...
310 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
315 // do the deleting of nodes
317 if (EFI_ERROR(Node
->Status
)){
318 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
319 ShellStatus
= SHELL_ACCESS_DENIED
;
322 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
323 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
324 ShellStatus
= SHELL_INVALID_PARAMETER
;
328 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
334 if (FileList
!= NULL
) {
335 Status
= ShellCloseFileMetaArg(&FileList
);
341 // free the command line package
343 ShellCommandLineFreeVarList (Package
);
346 return (ShellStatus
);