]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Attrib.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Attrib.c
1 /** @file
2 Main file for attrib shell level 2 function.
3
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
9
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.
12
13 **/
14
15 #include "UefiShellLevel2CommandsLib.h"
16
17 STATIC CONST CHAR16 AllFiles[] = L"*";
18
19 STATIC CONST SHELL_PARAM_ITEM AttribParamList[] = {
20 {L"-a", TypeFlag},
21 {L"+a", TypeFlag},
22 {L"-s", TypeFlag},
23 {L"+s", TypeFlag},
24 {L"-h", TypeFlag},
25 {L"+h", TypeFlag},
26 {L"-r", TypeFlag},
27 {L"+r", TypeFlag},
28 {NULL, TypeMax}
29 };
30
31 /**
32 Function for 'attrib' command.
33
34 @param[in] ImageHandle Handle to the Image (NULL if Internal).
35 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
36 **/
37 SHELL_STATUS
38 EFIAPI
39 ShellCommandRunAttrib (
40 IN EFI_HANDLE ImageHandle,
41 IN EFI_SYSTEM_TABLE *SystemTable
42 )
43 {
44 UINT64 FileAttributesToAdd;
45 UINT64 FileAttributesToRemove;
46 EFI_STATUS Status;
47 LIST_ENTRY *Package;
48 CHAR16 *ProblemParam;
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;
55
56 ListOfFiles = NULL;
57 ShellStatus = SHELL_SUCCESS;
58 ProblemParam = NULL;
59
60 //
61 // initialize the shell lib (we must be in non-auto-init...)
62 //
63 Status = ShellInitialize();
64 ASSERT_EFI_ERROR(Status);
65
66 //
67 // parse the command line
68 //
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;
75 } else {
76 ASSERT(FALSE);
77 }
78 } else {
79
80 //
81 // check for "-?"
82 //
83 if (ShellCommandLineGetFlag(Package, L"-?")) {
84 ASSERT(FALSE);
85 } else {
86 FileAttributesToAdd = 0;
87 FileAttributesToRemove = 0;
88
89 //
90 // apply or remove each flag
91 //
92 if (ShellCommandLineGetFlag(Package, L"+a")) {
93 FileAttributesToAdd |= EFI_FILE_ARCHIVE;
94 }
95 if (ShellCommandLineGetFlag(Package, L"-a")) {
96 FileAttributesToRemove |= EFI_FILE_ARCHIVE;
97 }
98 if (ShellCommandLineGetFlag(Package, L"+s")) {
99 FileAttributesToAdd |= EFI_FILE_SYSTEM;
100 }
101 if (ShellCommandLineGetFlag(Package, L"-s")) {
102 FileAttributesToRemove |= EFI_FILE_SYSTEM;
103 }
104 if (ShellCommandLineGetFlag(Package, L"+h")) {
105 FileAttributesToAdd |= EFI_FILE_HIDDEN;
106 }
107 if (ShellCommandLineGetFlag(Package, L"-h")) {
108 FileAttributesToRemove |= EFI_FILE_HIDDEN;
109 }
110 if (ShellCommandLineGetFlag(Package, L"+r")) {
111 FileAttributesToAdd |= EFI_FILE_READ_ONLY;
112 }
113 if (ShellCommandLineGetFlag(Package, L"-r")) {
114 FileAttributesToRemove |= EFI_FILE_READ_ONLY;
115 }
116
117 if (FileAttributesToRemove == 0 && FileAttributesToAdd == 0) {
118 //
119 // Do display as we have no attributes to change
120 //
121 for ( ParamNumberCount = 1
122 ;
123 ; ParamNumberCount++
124 ){
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) {
130 break;
131 }
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;
137 } else {
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)
141 ){
142 ShellPrintHiiEx(
143 -1,
144 -1,
145 NULL,
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' ',
153 FileNode->FileName
154 );
155 }
156 Status = ShellCloseFileMetaArg(&ListOfFiles);
157 ListOfFiles = NULL;
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;
161 }
162 } // for loop for handling wildcard filenames
163 } // for loop for printing out the info
164 } else if ((FileAttributesToRemove & FileAttributesToAdd) != 0) {
165 //
166 // fail as we have conflcting params.
167 //
168 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellLevel2HiiHandle);
169 ShellStatus = SHELL_INVALID_PARAMETER;
170 } else {
171 //
172 // enumerate through all the files/directories and apply the attributes
173 //
174 for ( ParamNumberCount = 1
175 ;
176 ; ParamNumberCount++
177 ){
178 FileName = ShellCommandLineGetRawValue(Package, ParamNumberCount);
179 // if we dont have anything left, move on...
180 if (FileName == NULL) {
181 //
182 // make sure we are not failing on the first one we do... if yes that's an error...
183 //
184 if (ParamNumberCount == 1) {
185 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
186 ShellStatus = SHELL_INVALID_PARAMETER;
187 }
188 break;
189 }
190
191 //
192 // OpenFileByName / GetFileInfo / Change attributes / SetFileInfo / CloseFile / free memory
193 // for each file or directory on the line.
194 //
195
196 //
197 // Open the file(s)
198 //
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;
204 } else {
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)
208 ){
209 //
210 // skip the directory traversing stuff...
211 //
212 if (StrCmp(FileNode->FileName, L".") == 0 || StrCmp(FileNode->FileName, L"..") == 0) {
213 continue;
214 }
215
216 FileInfo = gEfiShellProtocol->GetFileInfo(FileNode->Handle);
217
218 //
219 // if we are removing Read-Only we need to do that alone
220 //
221 if ((FileAttributesToRemove & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
222 FileInfo->Attribute &= ~EFI_FILE_READ_ONLY;
223 //
224 // SetFileInfo
225 //
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;
230 }
231 }
232
233 //
234 // change the attribute
235 //
236 FileInfo->Attribute &= ~FileAttributesToRemove;
237 FileInfo->Attribute |= FileAttributesToAdd;
238
239 //
240 // SetFileInfo
241 //
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;
246 }
247
248 SHELL_FREE_NON_NULL(FileInfo);
249 }
250 Status = ShellCloseFileMetaArg(&ListOfFiles);
251 ListOfFiles = NULL;
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;
255 }
256 } // for loop for handling wildcard filenames
257 }
258 }
259 }
260 }
261
262 //
263 // free the command line package
264 //
265 ShellCommandLineFreeVarList (Package);
266
267 //
268 // return the status
269 //
270 return (ShellStatus);
271 }