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