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 CHAR16 AllFiles
[] = L
"*";
19 STATIC CONST SHELL_PARAM_ITEM AttribParamList
[] = {
32 Function for 'attrib' command.
34 @param[in] ImageHandle Handle to the Image (NULL if Internal).
35 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
39 ShellCommandRunAttrib (
40 IN EFI_HANDLE ImageHandle
,
41 IN EFI_SYSTEM_TABLE
*SystemTable
44 UINT64 FileAttributesToAdd
;
45 UINT64 FileAttributesToRemove
;
49 SHELL_STATUS ShellStatus
;
50 UINTN ParamNumberCount
;
51 CONST CHAR16
*FileName
;
52 EFI_SHELL_FILE_INFO
*ListOfFiles
;
53 EFI_SHELL_FILE_INFO
*FileNode
;
54 EFI_FILE_INFO
*FileInfo
;
57 ShellStatus
= SHELL_SUCCESS
;
61 // initialize the shell lib (we must be in non-auto-init...)
63 Status
= ShellInitialize();
64 ASSERT_EFI_ERROR(Status
);
67 // parse the command line
69 Status
= ShellCommandLineParse (AttribParamList
, &Package
, &ProblemParam
, TRUE
);
70 if (EFI_ERROR(Status
)) {
71 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
72 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
73 FreePool(ProblemParam
);
74 ShellStatus
= SHELL_INVALID_PARAMETER
;
83 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
86 FileAttributesToAdd
= 0;
87 FileAttributesToRemove
= 0;
90 // apply or remove each flag
92 if (ShellCommandLineGetFlag(Package
, L
"+a")) {
93 FileAttributesToAdd
|= EFI_FILE_ARCHIVE
;
95 if (ShellCommandLineGetFlag(Package
, L
"-a")) {
96 FileAttributesToRemove
|= EFI_FILE_ARCHIVE
;
98 if (ShellCommandLineGetFlag(Package
, L
"+s")) {
99 FileAttributesToAdd
|= EFI_FILE_SYSTEM
;
101 if (ShellCommandLineGetFlag(Package
, L
"-s")) {
102 FileAttributesToRemove
|= EFI_FILE_SYSTEM
;
104 if (ShellCommandLineGetFlag(Package
, L
"+h")) {
105 FileAttributesToAdd
|= EFI_FILE_HIDDEN
;
107 if (ShellCommandLineGetFlag(Package
, L
"-h")) {
108 FileAttributesToRemove
|= EFI_FILE_HIDDEN
;
110 if (ShellCommandLineGetFlag(Package
, L
"+r")) {
111 FileAttributesToAdd
|= EFI_FILE_READ_ONLY
;
113 if (ShellCommandLineGetFlag(Package
, L
"-r")) {
114 FileAttributesToRemove
|= EFI_FILE_READ_ONLY
;
117 if (FileAttributesToRemove
== 0 && FileAttributesToAdd
== 0) {
119 // Do display as we have no attributes to change
121 for ( ParamNumberCount
= 1
125 FileName
= ShellCommandLineGetRawValue(Package
, ParamNumberCount
);
126 // if we dont have anything left, move on...
127 if (FileName
== NULL
&& ParamNumberCount
== 1) {
128 FileName
= (CHAR16
*)AllFiles
;
129 } else if (FileName
== NULL
) {
132 ASSERT(ListOfFiles
== NULL
);
133 Status
= ShellOpenFileMetaArg((CHAR16
*)FileName
, EFI_FILE_MODE_READ
, &ListOfFiles
);
134 if (EFI_ERROR(Status
)) {
135 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
136 ShellStatus
= SHELL_NOT_FOUND
;
138 for (FileNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListOfFiles
->Link
)
139 ; !IsNull(&ListOfFiles
->Link
, &FileNode
->Link
)
140 ; FileNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ListOfFiles
->Link
, &FileNode
->Link
)
146 STRING_TOKEN (STR_ATTRIB_OUTPUT_LINE
),
147 gShellLevel2HiiHandle
,
148 FileNode
->Info
->Attribute
&EFI_FILE_DIRECTORY
? L
'D':L
' ',
149 FileNode
->Info
->Attribute
&EFI_FILE_ARCHIVE
? L
'A':L
' ',
150 FileNode
->Info
->Attribute
&EFI_FILE_SYSTEM
? L
'S':L
' ',
151 FileNode
->Info
->Attribute
&EFI_FILE_HIDDEN
? L
'H':L
' ',
152 FileNode
->Info
->Attribute
&EFI_FILE_READ_ONLY
? L
'R':L
' ',
156 Status
= ShellCloseFileMetaArg(&ListOfFiles
);
158 if (EFI_ERROR(Status
)) {
159 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_CLOSE_FAIL
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
160 ShellStatus
= SHELL_NOT_FOUND
;
162 } // for loop for handling wildcard filenames
163 } // for loop for printing out the info
164 } else if ((FileAttributesToRemove
& FileAttributesToAdd
) != 0) {
166 // fail as we have conflcting params.
168 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_CON
), gShellLevel2HiiHandle
);
169 ShellStatus
= SHELL_INVALID_PARAMETER
;
172 // enumerate through all the files/directories and apply the attributes
174 for ( ParamNumberCount
= 1
178 FileName
= ShellCommandLineGetRawValue(Package
, ParamNumberCount
);
179 // if we dont have anything left, move on...
180 if (FileName
== NULL
) {
182 // make sure we are not failing on the first one we do... if yes that's an error...
184 if (ParamNumberCount
== 1) {
185 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
186 ShellStatus
= SHELL_INVALID_PARAMETER
;
192 // OpenFileByName / GetFileInfo / Change attributes / SetFileInfo / CloseFile / free memory
193 // for each file or directory on the line.
199 ASSERT(ListOfFiles
== NULL
);
200 Status
= ShellOpenFileMetaArg((CHAR16
*)FileName
, EFI_FILE_MODE_READ
, &ListOfFiles
);
201 if (EFI_ERROR(Status
)) {
202 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
203 ShellStatus
= SHELL_NOT_FOUND
;
205 for (FileNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ListOfFiles
->Link
)
206 ; !IsNull(&ListOfFiles
->Link
, &FileNode
->Link
)
207 ; FileNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ListOfFiles
->Link
, &FileNode
->Link
)
210 // skip the directory traversing stuff...
212 if (StrCmp(FileNode
->FileName
, L
".") == 0 || StrCmp(FileNode
->FileName
, L
"..") == 0) {
216 FileInfo
= gEfiShellProtocol
->GetFileInfo(FileNode
->Handle
);
219 // if we are removing Read-Only we need to do that alone
221 if ((FileAttributesToRemove
& EFI_FILE_READ_ONLY
) == EFI_FILE_READ_ONLY
) {
222 FileInfo
->Attribute
&= ~EFI_FILE_READ_ONLY
;
226 Status
= ShellSetFileInfo(FileNode
->Handle
, FileInfo
);
227 if (EFI_ERROR(Status
)) {;
228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_AD
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
229 ShellStatus
= SHELL_ACCESS_DENIED
;
234 // change the attribute
236 FileInfo
->Attribute
&= ~FileAttributesToRemove
;
237 FileInfo
->Attribute
|= FileAttributesToAdd
;
242 Status
= ShellSetFileInfo(FileNode
->Handle
, FileInfo
);
243 if (EFI_ERROR(Status
)) {;
244 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_AD
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
245 ShellStatus
= SHELL_ACCESS_DENIED
;
248 SHELL_FREE_NON_NULL(FileInfo
);
250 Status
= ShellCloseFileMetaArg(&ListOfFiles
);
252 if (EFI_ERROR(Status
)) {
253 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_CLOSE_FAIL
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamNumberCount
));
254 ShellStatus
= SHELL_NOT_FOUND
;
256 } // for loop for handling wildcard filenames
263 // free the command line package
265 ShellCommandLineFreeVarList (Package
);
270 return (ShellStatus
);