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