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