]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel3CommandsLib/Touch.c
ShellPkg: Update behavior for GetTime() errors.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Touch.c
1 /** @file
2 Main file for Touch shell level 3 function.
3
4 Copyright (c) 2009 - 2011, 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 "UefiShellLevel3CommandsLib.h"
16
17 #include <Library/ShellLib.h>
18
19 /**
20 Do the touch operation on a single handle.
21
22 @param[in] Handle The handle to update the date/time on.
23
24 @retval EFI_ACCESS_DENIED The file referenced by Handle is read only.
25 @retval EFI_SUCCESS The operation was successful.
26 **/
27 EFI_STATUS
28 EFIAPI
29 TouchFileByHandle (
30 IN EFI_HANDLE Handle
31 )
32 {
33 EFI_STATUS Status;
34 EFI_FILE_INFO *FileInfo;
35
36 FileInfo = gEfiShellProtocol->GetFileInfo(Handle);
37 if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) != 0){
38 return (EFI_ACCESS_DENIED);
39 }
40 Status = gRT->GetTime(&FileInfo->ModificationTime, NULL);
41 if (EFI_ERROR(Status)) {
42 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"gRT->GetTime", Status);
43 return (SHELL_DEVICE_ERROR);
44 }
45
46 CopyMem(&FileInfo->LastAccessTime, &FileInfo->ModificationTime, sizeof(EFI_TIME));
47
48 Status = gEfiShellProtocol->SetFileInfo(Handle, FileInfo);
49
50 FreePool(FileInfo);
51
52 return (Status);
53 }
54
55 /**
56 Touch a given file and potantially recurse down if it was a directory.
57
58 @param[in] Name The name of this file.
59 @param[in] FS The name of the file system this file is on.
60 @param[in] Handle The handle of this file already opened.
61 @param[in] Rec TRUE to recurse if possible.
62
63 @retval EFI_INVALID_PARAMETER A parameter was invalid.
64 @retval EFI_SUCCESS The operation was successful.
65 **/
66 EFI_STATUS
67 EFIAPI
68 DoTouchByHandle (
69 IN CONST CHAR16 *Name,
70 IN CHAR16 *FS,
71 IN SHELL_FILE_HANDLE Handle,
72 IN BOOLEAN Rec
73 )
74 {
75 EFI_STATUS Status;
76 EFI_SHELL_FILE_INFO *FileList;
77 EFI_SHELL_FILE_INFO *Walker;
78 CHAR16 *TempSpot;
79
80 Status = EFI_SUCCESS;
81 FileList = NULL;
82 Walker = NULL;
83
84 if (FS == NULL) {
85 FS = StrnCatGrow(&FS, NULL, Name, 0);
86 if (FS != NULL) {
87 TempSpot = StrStr(FS, L"\\");
88 if (TempSpot != NULL) {
89 *TempSpot = CHAR_NULL;
90 }
91 }
92 }
93 if (FS == NULL) {
94 return (EFI_INVALID_PARAMETER);
95 }
96
97 //
98 // do it
99 //
100 Status = TouchFileByHandle(Handle);
101 if (EFI_ERROR(Status)) {
102 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Name, Status);
103 return (Status);
104 }
105
106 //
107 // if it's a directory recurse...
108 //
109 if (FileHandleIsDirectory(Handle) == EFI_SUCCESS && Rec) {
110 //
111 // get each file under this directory
112 //
113 if (EFI_ERROR(gEfiShellProtocol->FindFilesInDir(Handle, &FileList))) {
114 Status = EFI_INVALID_PARAMETER;
115 }
116
117 //
118 // recurse on each
119 //
120 for (Walker = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
121 ; FileList != NULL && !IsNull(&FileList->Link, &Walker->Link) && !EFI_ERROR(Status)
122 ; Walker = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Walker->Link)
123 ){
124 if ( (StrCmp(Walker->FileName, L".") != 0)
125 && (StrCmp(Walker->FileName, L"..") != 0)
126 ){
127 //
128 // Open the file since we need that handle.
129 //
130 Status = gEfiShellProtocol->OpenFileByName (Walker->FullName, &Walker->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
131 if (EFI_ERROR(Status)) {
132 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Walker->FullName, Status);
133 Status = EFI_ACCESS_DENIED;
134 } else {
135 Status = DoTouchByHandle(Walker->FullName, FS, Walker->Handle, TRUE);
136 gEfiShellProtocol->CloseFile(Walker->Handle);
137 Walker->Handle = NULL;
138 }
139 }
140 }
141
142 //
143 // free stuff
144 //
145 if (FileList != NULL && EFI_ERROR(gEfiShellProtocol->FreeFileList(&FileList))) {
146 Status = EFI_INVALID_PARAMETER;
147 }
148 }
149
150 return (Status);
151 }
152
153 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
154 {L"-r", TypeFlag},
155 {NULL, TypeMax}
156 };
157
158 /**
159 Function for 'touch' command.
160
161 @param[in] ImageHandle Handle to the Image (NULL if Internal).
162 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
163 **/
164 SHELL_STATUS
165 EFIAPI
166 ShellCommandRunTouch (
167 IN EFI_HANDLE ImageHandle,
168 IN EFI_SYSTEM_TABLE *SystemTable
169 )
170 {
171 EFI_STATUS Status;
172 LIST_ENTRY *Package;
173 CHAR16 *ProblemParam;
174 CONST CHAR16 *Param;
175 SHELL_STATUS ShellStatus;
176 UINTN ParamCount;
177 EFI_SHELL_FILE_INFO *FileList;
178 EFI_SHELL_FILE_INFO *Node;
179
180 ProblemParam = NULL;
181 ShellStatus = SHELL_SUCCESS;
182 ParamCount = 0;
183 FileList = NULL;
184
185 //
186 // initialize the shell lib (we must be in non-auto-init...)
187 //
188 Status = ShellInitialize();
189 ASSERT_EFI_ERROR(Status);
190
191 Status = CommandInit();
192 ASSERT_EFI_ERROR(Status);
193
194 //
195 // parse the command line
196 //
197 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
198 if (EFI_ERROR(Status)) {
199 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
200 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, ProblemParam);
201 FreePool(ProblemParam);
202 ShellStatus = SHELL_INVALID_PARAMETER;
203 } else {
204 ASSERT(FALSE);
205 }
206 } else {
207 //
208 // check for "-?"
209 //
210 if (ShellCommandLineGetFlag(Package, L"-?")) {
211 ASSERT(FALSE);
212 }
213 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
214 //
215 // we insufficient parameters
216 //
217 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);
218 ShellStatus = SHELL_INVALID_PARAMETER;
219 } else {
220 //
221 // get a list with each file specified by parameters
222 // if parameter is a directory then add all the files below it to the list
223 //
224 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
225 ; Param != NULL
226 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
227 ){
228 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, &FileList);
229 if (EFI_ERROR(Status)) {
230 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, (CHAR16*)Param);
231 ShellStatus = SHELL_NOT_FOUND;
232 break;
233 }
234 //
235 // make sure we completed the param parsing sucessfully...
236 // Also make sure that any previous action was sucessful
237 //
238 if (ShellStatus == SHELL_SUCCESS) {
239 //
240 // check that we have at least 1 file
241 //
242 if (FileList == NULL || IsListEmpty(&FileList->Link)) {
243 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, Param);
244 continue;
245 } else {
246 //
247 // loop through the list and make sure we are not aborting...
248 //
249 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
250 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
251 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
252 ){
253 //
254 // make sure the file opened ok
255 //
256 if (EFI_ERROR(Node->Status)){
257 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Node->FileName, Node->Status);
258 ShellStatus = SHELL_NOT_FOUND;
259 continue;
260 }
261
262 Status = DoTouchByHandle(Node->FullName, NULL, Node->Handle, ShellCommandLineGetFlag(Package, L"-r"));
263 if (EFI_ERROR(Status) && Status != EFI_ACCESS_DENIED) {
264 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Node->FileName, Status);
265 ShellStatus = SHELL_NOT_FOUND;
266 }
267 }
268 }
269 }
270 //
271 // Free the fileList
272 //
273 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
274 Status = ShellCloseFileMetaArg(&FileList);
275 ASSERT_EFI_ERROR(Status);
276 }
277 FileList = NULL;
278 }
279 }
280
281 //
282 // free the command line package
283 //
284 ShellCommandLineFreeVarList (Package);
285 }
286
287 if (ShellGetExecutionBreakFlag()) {
288 return (SHELL_ABORTED);
289 }
290
291 return (ShellStatus);
292 }
293