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