2 Main file for attrib shell level 2 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellLevel2CommandsLib.h"
18 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
24 Determine if a directory has no files in it.
26 @param[in] FileHandle The EFI_HANDLE to the directory.
28 @retval TRUE The directory has no files (or directories).
29 @retval FALSE The directory has at least 1 file or directory in it.
34 IN EFI_HANDLE FileHandle
37 EFI_FILE_INFO
*FileInfo
;
45 for (FileHandleFindFirstFile(FileHandle
, &FileInfo
)
47 ; FileHandleFindNextFile(FileHandle
, FileInfo
, &NoFile
)
49 if (StrStr(FileInfo
->FileName
, L
".") != FileInfo
->FileName
50 &&StrStr(FileInfo
->FileName
, L
"..") != FileInfo
->FileName
) {
58 Delete a node and all nodes under it (including sub directories).
60 @param[in] Node The node to start deleting with.
61 @param[in] Quiet TRUE to print no messages.
63 @retval SHELL_SUCCESS The operation was successful.
64 @retval SHELL_ACCESS_DENIED A file was read only.
65 @retval SHELL_ABORTED The abort message was received.
66 @retval SHELL_DEVICE_ERROR A device error occured reading this Node.
71 IN EFI_SHELL_FILE_INFO
*Node
,
72 IN CONST BOOLEAN Quiet
75 SHELL_STATUS ShellStatus
;
76 EFI_SHELL_FILE_INFO
*List
;
77 EFI_SHELL_FILE_INFO
*Node2
;
79 SHELL_PROMPT_RESPONSE
*Resp
;
84 ShellStatus
= SHELL_SUCCESS
;
88 if ((Node
->Info
->Attribute
& EFI_FILE_READ_ONLY
) == EFI_FILE_READ_ONLY
) {
89 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DETELE_RO
), gShellLevel2HiiHandle
, L
"rm", Node
->FullName
);
90 return (SHELL_ACCESS_DENIED
);
93 if ((Node
->Info
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
) {
94 if (!IsDirectoryEmpty(Node
->Handle
)) {
96 Status
= ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN(STR_RM_LOG_DELETE_CONF
), gShellLevel2HiiHandle
, Node
->FullName
);
97 Status
= ShellPromptForResponse(ShellPromptResponseTypeYesNo
, NULL
, (VOID
**)&Resp
);
99 if (EFI_ERROR(Status
) || *Resp
!= ShellPromptResponseYes
) {
100 SHELL_FREE_NON_NULL(Resp
);
101 return (SHELL_ABORTED
);
103 SHELL_FREE_NON_NULL(Resp
);
106 // empty out the directory
108 Status
= gEfiShellProtocol
->FindFilesInDir(Node
->Handle
, &List
);
109 if (EFI_ERROR(Status
)) {
111 gEfiShellProtocol
->FreeFileList(&List
);
113 return (SHELL_DEVICE_ERROR
);
115 for (Node2
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&List
->Link
)
116 ; !IsNull(&List
->Link
, &Node2
->Link
)
117 ; Node2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&List
->Link
, &Node2
->Link
)
120 // skip the directory traversing stuff...
122 if (StrCmp(Node2
->FileName
, L
".") == 0 || StrCmp(Node2
->FileName
, L
"..") == 0) {
125 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
126 if (EFI_ERROR(Node2
->Status
) && StrStr(Node2
->FileName
, L
":") == NULL
) {
128 // Update the node filename to have full path with file system identifier
130 NewSize
= StrSize(Node
->FullName
) + StrSize(Node2
->FullName
);
131 TempName
= AllocateZeroPool(NewSize
);
132 if (TempName
== NULL
) {
133 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
135 StrnCpy(TempName
, Node
->FullName
, NewSize
/sizeof(CHAR16
) -1);
136 TempName
[StrStr(TempName
, L
":")+1-TempName
] = CHAR_NULL
;
137 StrnCat(TempName
, Node2
->FullName
, NewSize
/sizeof(CHAR16
) -1 - StrLen(TempName
));
138 FreePool((VOID
*)Node2
->FullName
);
139 Node2
->FullName
= TempName
;
142 // Now try again to open the file
144 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
147 if (!EFI_ERROR(Node2
->Status
)) {
148 ShellStatus
= CascadeDelete(Node2
, Quiet
);
149 } else if (ShellStatus
== SHELL_SUCCESS
) {
150 ShellStatus
= (SHELL_STATUS
)(Node2
->Status
&(~0x80000000));
152 if (ShellStatus
!= SHELL_SUCCESS
) {
154 gEfiShellProtocol
->FreeFileList(&List
);
156 return (ShellStatus
);
160 gEfiShellProtocol
->FreeFileList(&List
);
165 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
167 // now delete the current node...
170 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
172 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
177 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
179 if (Status
!= EFI_SUCCESS
){
180 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
181 return (SHELL_ACCESS_DENIED
);
184 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
186 return (SHELL_SUCCESS
);
191 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
193 @param[in] List RESERVED. Not used.
194 @param[in] Node The node to analyze.
195 @param[in] Package RESERVED. Not used.
200 IN CONST EFI_SHELL_FILE_INFO
*List
,
201 IN CONST EFI_SHELL_FILE_INFO
*Node
,
202 IN CONST LIST_ENTRY
*Package
205 CONST CHAR16
*TempLocation
;
207 CHAR16
*SearchString
;
211 if (Node
== NULL
|| Node
->FullName
== NULL
) {
215 TempLocation
= StrStr(Node
->FullName
, L
":");
216 if (StrLen(TempLocation
) <= 2) {
218 // Deleting the root directory is invalid.
223 TempLocation
= ShellGetCurrentDir(NULL
);
224 if (TempLocation
== NULL
) {
226 // No working directory is specified so whatever is left is ok.
234 Pattern
= StrnCatGrow(&Pattern
, NULL
, TempLocation
, 0);
235 SearchString
= StrnCatGrow(&SearchString
, &Size
, Node
->FullName
, 0);
236 if (!EFI_ERROR(ShellIsDirectory(SearchString
))) {
237 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"\\", 0);
238 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"*", 0);
241 if (Pattern
== NULL
|| SearchString
== NULL
) {
245 if (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Pattern
, SearchString
)) {
250 SHELL_FREE_NON_NULL(Pattern
);
251 SHELL_FREE_NON_NULL(SearchString
);
257 Function for 'rm' command.
259 @param[in] ImageHandle Handle to the Image (NULL if Internal).
260 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
265 IN EFI_HANDLE ImageHandle
,
266 IN EFI_SYSTEM_TABLE
*SystemTable
271 CHAR16
*ProblemParam
;
273 SHELL_STATUS ShellStatus
;
275 EFI_SHELL_FILE_INFO
*FileList
;
276 EFI_SHELL_FILE_INFO
*Node
;
279 ShellStatus
= SHELL_SUCCESS
;
284 // initialize the shell lib (we must be in non-auto-init...)
286 Status
= ShellInitialize();
287 ASSERT_EFI_ERROR(Status
);
290 // parse the command line
292 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
293 if (EFI_ERROR(Status
)) {
294 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
295 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, L
"rm", ProblemParam
);
296 FreePool(ProblemParam
);
297 ShellStatus
= SHELL_INVALID_PARAMETER
;
305 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
308 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
310 // we insufficient parameters
312 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
, L
"rm");
313 ShellStatus
= SHELL_INVALID_PARAMETER
;
316 // get a list with each file specified by parameters
317 // if parameter is a directory then add all the files below it to the list
319 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
321 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
323 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
324 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
325 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, L
"rm", (CHAR16
*)Param
);
326 ShellStatus
= SHELL_NOT_FOUND
;
331 if (ShellStatus
== SHELL_SUCCESS
){
333 // loop through the list and make sure we are not aborting...
335 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
336 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
337 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
340 // skip the directory traversing stuff...
342 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
347 // do the deleting of nodes
349 if (EFI_ERROR(Node
->Status
)){
350 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
351 ShellStatus
= SHELL_ACCESS_DENIED
;
354 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
355 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
356 ShellStatus
= SHELL_INVALID_PARAMETER
;
360 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
366 if (FileList
!= NULL
) {
367 Status
= ShellCloseFileMetaArg(&FileList
);
373 // free the command line package
375 ShellCommandLineFreeVarList (Package
);
378 return (ShellStatus
);