2 Main file for attrib shell level 2 function.
4 Copyright (c) 2009 - 2013, 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
;
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
);
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 if (EFI_ERROR(Node2
->Status
) && StrStr(Node2
->FileName
, L
":") == NULL
) {
125 // Update the node filename to have full path with file system identifier
127 TempName
= AllocateZeroPool(StrSize(Node
->FullName
) + StrSize(Node2
->FullName
));
128 if (TempName
== NULL
) {
129 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
131 StrCpy(TempName
, Node
->FullName
);
132 TempName
[StrStr(TempName
, L
":")+1-TempName
] = CHAR_NULL
;
133 StrCat(TempName
, Node2
->FullName
);
134 FreePool((VOID
*)Node2
->FullName
);
135 Node2
->FullName
= TempName
;
138 // Now try again to open the file
140 Node2
->Status
= gEfiShellProtocol
->OpenFileByName (Node2
->FullName
, &Node2
->Handle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
);
143 if (!EFI_ERROR(Node2
->Status
)) {
144 ShellStatus
= CascadeDelete(Node2
, Quiet
);
145 } else if (ShellStatus
== SHELL_SUCCESS
) {
146 ShellStatus
= (SHELL_STATUS
)(Node2
->Status
&(~0x80000000));
148 if (ShellStatus
!= SHELL_SUCCESS
) {
150 gEfiShellProtocol
->FreeFileList(&List
);
152 return (ShellStatus
);
156 gEfiShellProtocol
->FreeFileList(&List
);
161 if (!(StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0)) {
163 // now delete the current node...
165 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE
), gShellLevel2HiiHandle
, Node
->FullName
);
166 Status
= gEfiShellProtocol
->DeleteFile(Node
->Handle
);
171 // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
173 if (Status
!= EFI_SUCCESS
){
174 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR
), gShellLevel2HiiHandle
, Status
);
175 return (SHELL_ACCESS_DENIED
);
177 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_COMP
), gShellLevel2HiiHandle
);
178 return (SHELL_SUCCESS
);
183 Determins if a Node is a valid delete target. Will prevent deleting the root directory.
185 @param[in] List RESERVED. Not used.
186 @param[in] Node The node to analyze.
187 @param[in] Package RESERVED. Not used.
192 IN CONST EFI_SHELL_FILE_INFO
*List
,
193 IN CONST EFI_SHELL_FILE_INFO
*Node
,
194 IN CONST LIST_ENTRY
*Package
197 CONST CHAR16
*TempLocation
;
199 CHAR16
*SearchString
;
203 if (Node
== NULL
|| Node
->FullName
== NULL
) {
207 TempLocation
= StrStr(Node
->FullName
, L
":");
208 if (StrLen(TempLocation
) <= 2) {
210 // Deleting the root directory is invalid.
215 TempLocation
= ShellGetCurrentDir(NULL
);
216 if (TempLocation
== NULL
) {
218 // No working directory is specified so whatever is left is ok.
226 Pattern
= StrnCatGrow(&Pattern
, NULL
, TempLocation
, 0);
227 SearchString
= StrnCatGrow(&SearchString
, &Size
, Node
->FullName
, 0);
228 if (!EFI_ERROR(ShellIsDirectory(SearchString
))) {
229 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"\\", 0);
230 SearchString
= StrnCatGrow(&SearchString
, &Size
, L
"*", 0);
233 if (Pattern
== NULL
|| SearchString
== NULL
) {
237 if (gUnicodeCollation
->MetaiMatch(gUnicodeCollation
, Pattern
, SearchString
)) {
242 SHELL_FREE_NON_NULL(Pattern
);
243 SHELL_FREE_NON_NULL(SearchString
);
249 Function for 'rm' command.
251 @param[in] ImageHandle Handle to the Image (NULL if Internal).
252 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
257 IN EFI_HANDLE ImageHandle
,
258 IN EFI_SYSTEM_TABLE
*SystemTable
263 CHAR16
*ProblemParam
;
265 SHELL_STATUS ShellStatus
;
267 EFI_SHELL_FILE_INFO
*FileList
;
268 EFI_SHELL_FILE_INFO
*Node
;
271 ShellStatus
= SHELL_SUCCESS
;
276 // initialize the shell lib (we must be in non-auto-init...)
278 Status
= ShellInitialize();
279 ASSERT_EFI_ERROR(Status
);
282 // parse the command line
284 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
285 if (EFI_ERROR(Status
)) {
286 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
287 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
288 FreePool(ProblemParam
);
289 ShellStatus
= SHELL_INVALID_PARAMETER
;
297 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
300 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
302 // we insufficient parameters
304 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
305 ShellStatus
= SHELL_INVALID_PARAMETER
;
308 // get a list with each file specified by parameters
309 // if parameter is a directory then add all the files below it to the list
311 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
313 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
315 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
316 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
317 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, (CHAR16
*)Param
);
318 ShellStatus
= SHELL_NOT_FOUND
;
323 if (ShellStatus
== SHELL_SUCCESS
){
325 // loop through the list and make sure we are not aborting...
327 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
328 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
329 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
332 // skip the directory traversing stuff...
334 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
339 // do the deleting of nodes
341 if (EFI_ERROR(Node
->Status
)){
342 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2
), gShellLevel2HiiHandle
, Node
->Status
);
343 ShellStatus
= SHELL_ACCESS_DENIED
;
346 if (!IsValidDeleteTarget(FileList
, Node
, Package
)) {
347 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3
), gShellLevel2HiiHandle
, Node
->FullName
);
348 ShellStatus
= SHELL_INVALID_PARAMETER
;
352 ShellStatus
= CascadeDelete(Node
, ShellCommandLineGetFlag(Package
, L
"-q"));
358 if (FileList
!= NULL
) {
359 Status
= ShellCloseFileMetaArg(&FileList
);
365 // free the command line package
367 ShellCommandLineFreeVarList (Package
);
370 return (ShellStatus
);