TianoCore EDK2 master
Loading...
Searching...
No Matches
Attrib.c
Go to the documentation of this file.
1
11
12STATIC CONST CHAR16 AllFiles[] = L"*";
13
14STATIC CONST SHELL_PARAM_ITEM AttribParamList[] = {
15 { L"-a", TypeFlag },
16 { L"+a", TypeFlag },
17 { L"-s", TypeFlag },
18 { L"+s", TypeFlag },
19 { L"-h", TypeFlag },
20 { L"+h", TypeFlag },
21 { L"-r", TypeFlag },
22 { L"+r", TypeFlag },
23 { NULL, TypeMax }
24};
25
33EFIAPI
35 IN EFI_HANDLE ImageHandle,
36 IN EFI_SYSTEM_TABLE *SystemTable
37 )
38{
39 UINT64 FileAttributesToAdd;
40 UINT64 FileAttributesToRemove;
41 EFI_STATUS Status;
42 LIST_ENTRY *Package;
43 CHAR16 *ProblemParam;
44 SHELL_STATUS ShellStatus;
45 UINTN ParamNumberCount;
46 CONST CHAR16 *FileName;
47 EFI_SHELL_FILE_INFO *ListOfFiles;
48 EFI_SHELL_FILE_INFO *FileNode;
50
51 ListOfFiles = NULL;
52 ShellStatus = SHELL_SUCCESS;
53 ProblemParam = NULL;
54
55 //
56 // initialize the shell lib (we must be in non-auto-init...)
57 //
58 Status = ShellInitialize ();
59 ASSERT_EFI_ERROR (Status);
60
61 //
62 // parse the command line
63 //
64 Status = ShellCommandLineParse (AttribParamList, &Package, &ProblemParam, TRUE);
65 if (EFI_ERROR (Status)) {
66 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
67 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"attrib", ProblemParam);
68 FreePool (ProblemParam);
69 ShellStatus = SHELL_INVALID_PARAMETER;
70 } else {
71 ASSERT (FALSE);
72 }
73 } else {
74 //
75 // check for "-?"
76 //
77 if (ShellCommandLineGetFlag (Package, L"-?")) {
78 ASSERT (FALSE);
79 } else {
80 FileAttributesToAdd = 0;
81 FileAttributesToRemove = 0;
82
83 //
84 // apply or remove each flag
85 //
86 if (ShellCommandLineGetFlag (Package, L"+a")) {
87 FileAttributesToAdd |= EFI_FILE_ARCHIVE;
88 }
89
90 if (ShellCommandLineGetFlag (Package, L"-a")) {
91 FileAttributesToRemove |= EFI_FILE_ARCHIVE;
92 }
93
94 if (ShellCommandLineGetFlag (Package, L"+s")) {
95 FileAttributesToAdd |= EFI_FILE_SYSTEM;
96 }
97
98 if (ShellCommandLineGetFlag (Package, L"-s")) {
99 FileAttributesToRemove |= EFI_FILE_SYSTEM;
100 }
101
102 if (ShellCommandLineGetFlag (Package, L"+h")) {
103 FileAttributesToAdd |= EFI_FILE_HIDDEN;
104 }
105
106 if (ShellCommandLineGetFlag (Package, L"-h")) {
107 FileAttributesToRemove |= EFI_FILE_HIDDEN;
108 }
109
110 if (ShellCommandLineGetFlag (Package, L"+r")) {
111 FileAttributesToAdd |= EFI_FILE_READ_ONLY;
112 }
113
114 if (ShellCommandLineGetFlag (Package, L"-r")) {
115 FileAttributesToRemove |= EFI_FILE_READ_ONLY;
116 }
117
118 if ((FileAttributesToRemove == 0) && (FileAttributesToAdd == 0)) {
119 //
120 // Do display as we have no attributes to change
121 //
122 for ( ParamNumberCount = 1
123 ;
124 ; ParamNumberCount++
125 )
126 {
127 FileName = ShellCommandLineGetRawValue (Package, ParamNumberCount);
128 // if we dont have anything left, move on...
129 if ((FileName == NULL) && (ParamNumberCount == 1)) {
130 FileName = (CHAR16 *)AllFiles;
131 } else if (FileName == NULL) {
132 break;
133 }
134
135 ASSERT (ListOfFiles == NULL);
136 Status = ShellOpenFileMetaArg ((CHAR16 *)FileName, EFI_FILE_MODE_READ, &ListOfFiles);
137 if (EFI_ERROR (Status)) {
138 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
139 ShellStatus = SHELL_NOT_FOUND;
140 } else {
141 for (FileNode = (EFI_SHELL_FILE_INFO *)GetFirstNode (&ListOfFiles->Link)
142 ; !IsNull (&ListOfFiles->Link, &FileNode->Link)
143 ; FileNode = (EFI_SHELL_FILE_INFO *)GetNextNode (&ListOfFiles->Link, &FileNode->Link)
144 )
145 {
147 -1,
148 -1,
149 NULL,
150 STRING_TOKEN (STR_ATTRIB_OUTPUT_LINE),
151 gShellLevel2HiiHandle,
152 FileNode->Info->Attribute&EFI_FILE_DIRECTORY ? L'D' : L' ',
153 FileNode->Info->Attribute&EFI_FILE_ARCHIVE ? L'A' : L' ',
154 FileNode->Info->Attribute&EFI_FILE_SYSTEM ? L'S' : L' ',
155 FileNode->Info->Attribute&EFI_FILE_HIDDEN ? L'H' : L' ',
156 FileNode->Info->Attribute&EFI_FILE_READ_ONLY ? L'R' : L' ',
157 FileNode->FileName
158 );
159
161 ShellStatus = SHELL_ABORTED;
162 break;
163 }
164 }
165
166 Status = ShellCloseFileMetaArg (&ListOfFiles);
167 ListOfFiles = NULL;
168 if (EFI_ERROR (Status)) {
169 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_CLOSE_FAIL), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
170 ShellStatus = SHELL_NOT_FOUND;
171 }
172 } // for loop for handling wildcard filenames
173 } // for loop for printing out the info
174 } else if ((FileAttributesToRemove & FileAttributesToAdd) != 0) {
175 //
176 // fail as we have conflcting params.
177 //
178 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellLevel2HiiHandle, L"attrib");
179 ShellStatus = SHELL_INVALID_PARAMETER;
180 } else {
181 //
182 // enumerate through all the files/directories and apply the attributes
183 //
184 for ( ParamNumberCount = 1
185 ;
186 ; ParamNumberCount++
187 )
188 {
189 FileName = ShellCommandLineGetRawValue (Package, ParamNumberCount);
190 // if we dont have anything left, move on...
191 if (FileName == NULL) {
192 //
193 // make sure we are not failing on the first one we do... if yes that's an error...
194 //
195 if (ParamNumberCount == 1) {
196 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"attrib");
197 ShellStatus = SHELL_INVALID_PARAMETER;
198 }
199
200 break;
201 }
202
203 //
204 // OpenFileByName / GetFileInfo / Change attributes / SetFileInfo / CloseFile / free memory
205 // for each file or directory on the line.
206 //
207
208 //
209 // Open the file(s)
210 //
211 ASSERT (ListOfFiles == NULL);
212 Status = ShellOpenFileMetaArg ((CHAR16 *)FileName, EFI_FILE_MODE_READ, &ListOfFiles);
213 if (EFI_ERROR (Status)) {
214 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
215 ShellStatus = SHELL_NOT_FOUND;
216 } else {
217 for (FileNode = (EFI_SHELL_FILE_INFO *)GetFirstNode (&ListOfFiles->Link)
218 ; !IsNull (&ListOfFiles->Link, &FileNode->Link)
219 ; FileNode = (EFI_SHELL_FILE_INFO *)GetNextNode (&ListOfFiles->Link, &FileNode->Link)
220 )
221 {
222 //
223 // skip the directory traversing stuff...
224 //
225 if ((StrCmp (FileNode->FileName, L".") == 0) || (StrCmp (FileNode->FileName, L"..") == 0)) {
226 continue;
227 }
228
229 FileInfo = gEfiShellProtocol->GetFileInfo (FileNode->Handle);
230
231 //
232 // if we are removing Read-Only we need to do that alone
233 //
234 if ((FileAttributesToRemove & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
235 FileInfo->Attribute &= ~EFI_FILE_READ_ONLY;
236 //
237 // SetFileInfo
238 //
239 Status = ShellSetFileInfo (FileNode->Handle, FileInfo);
240 if (EFI_ERROR (Status)) {
241 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_AD), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
242 ShellStatus = SHELL_ACCESS_DENIED;
243 }
244 }
245
246 //
247 // change the attribute
248 //
249 FileInfo->Attribute &= ~FileAttributesToRemove;
250 FileInfo->Attribute |= FileAttributesToAdd;
251
252 //
253 // SetFileInfo
254 //
255 Status = ShellSetFileInfo (FileNode->Handle, FileInfo);
256 if (EFI_ERROR (Status)) {
257 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_AD), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
258 ShellStatus = SHELL_ACCESS_DENIED;
259 }
260
261 SHELL_FREE_NON_NULL (FileInfo);
262 }
263
264 Status = ShellCloseFileMetaArg (&ListOfFiles);
265 ListOfFiles = NULL;
266 if (EFI_ERROR (Status)) {
267 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_CLOSE_FAIL), gShellLevel2HiiHandle, L"attrib", ShellCommandLineGetRawValue (Package, ParamNumberCount));
268 ShellStatus = SHELL_NOT_FOUND;
269 }
270 } // for loop for handling wildcard filenames
271 }
272 }
273 }
274 }
275
276 //
277 // free the command line package
278 //
280
281 //
282 // return the status
283 //
284 return (ShellStatus);
285}
UINT64 UINTN
SHELL_STATUS EFIAPI ShellCommandRunAttrib(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Attrib.c:34
BOOLEAN EFIAPI IsNull(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:443
LIST_ENTRY *EFIAPI GetNextNode(IN CONST LIST_ENTRY *List, IN CONST LIST_ENTRY *Node)
Definition: LinkedList.c:333
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
Definition: String.c:109
LIST_ENTRY *EFIAPI GetFirstNode(IN CONST LIST_ENTRY *List)
Definition: LinkedList.c:298
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_ABORTED
Definition: Shell.h:128
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_FILE_INFO * FileInfo(IN EFI_FILE_HANDLE FHand)
EFI_STATUS EFIAPI ShellSetFileInfo(IN SHELL_FILE_HANDLE FileHandle, IN EFI_FILE_INFO *FileInfo)
Definition: UefiShellLib.c:601
BOOLEAN EFIAPI ShellGetExecutionBreakFlag(VOID)
EFI_STATUS EFIAPI ShellCloseFileMetaArg(IN OUT EFI_SHELL_FILE_INFO **ListHead)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
@ TypeFlag
A flag that is present or not present only (IE "-a").
Definition: ShellLib.h:699
EFI_STATUS EFIAPI ShellOpenFileMetaArg(IN CHAR16 *Arg, IN UINT64 OpenMode, IN OUT EFI_SHELL_FILE_INFO **ListHead)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define STRING_TOKEN(t)
UINT64 Attribute
Definition: FileInfo.h:47
LIST_ENTRY Link
Linked list members.
Definition: Shell.h:153
SHELL_FILE_HANDLE Handle
Handle for interacting with the opened file or NULL if closed.
Definition: Shell.h:157
EFI_FILE_INFO * Info
Pointer to the FileInfo struct for this file or NULL.
Definition: Shell.h:158
CONST CHAR16 * FileName
name of this file.
Definition: Shell.h:156