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