2 Main file for Touch shell level 3 function.
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
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.
15 #include "UefiShellLevel3CommandsLib.h"
17 #include <Library/ShellLib.h>
26 EFI_FILE_INFO
*FileInfo
;
28 FileInfo
= gEfiShellProtocol
->GetFileInfo(Handle
);
29 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) != 0){
30 return (EFI_ACCESS_DENIED
);
32 Status
= gRT
->GetTime(&FileInfo
->ModificationTime
, NULL
);
33 ASSERT_EFI_ERROR(Status
);
34 CopyMem(&FileInfo
->LastAccessTime
, &FileInfo
->ModificationTime
, sizeof(EFI_TIME
));
36 Status
= gEfiShellProtocol
->SetFileInfo(Handle
, FileInfo
);
46 IN CONST CHAR16
*Name
,
48 IN SHELL_FILE_HANDLE Handle
,
53 EFI_SHELL_FILE_INFO
*FileList
;
54 EFI_SHELL_FILE_INFO
*Walker
;
62 FS
= StrnCatGrow(&FS
, NULL
, Name
, 0);
63 TempSpot
= StrStr(FS
, L
"\\");
64 if (TempSpot
!= NULL
) {
65 *TempSpot
= CHAR_NULL
;
72 Status
= TouchFileByHandle(Handle
);
73 if (EFI_ERROR(Status
)) {
74 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NO_OPEN
), gShellLevel3HiiHandle
, Name
, Status
);
79 // if it's a directory recurse...
81 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
&& Rec
) {
83 // get each file under this directory
85 if (EFI_ERROR(gEfiShellProtocol
->FindFilesInDir(Handle
, &FileList
))) {
86 Status
= EFI_INVALID_PARAMETER
;
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
)
96 if ( (StrCmp(Walker
->FileName
, L
".") != 0)
97 && (StrCmp(Walker
->FileName
, L
"..") != 0)
100 // Open the file since we need that handle.
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
;
107 Status
= DoTouchByHandle(Walker
->FullName
, FS
, Walker
->Handle
, TRUE
);
108 gEfiShellProtocol
->CloseFile(Walker
->Handle
);
109 Walker
->Handle
= NULL
;
117 if (FileList
!= NULL
&& EFI_ERROR(gEfiShellProtocol
->FreeFileList(&FileList
))) {
118 Status
= EFI_INVALID_PARAMETER
;
125 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
131 Function for 'touch' command.
133 @param[in] ImageHandle Handle to the Image (NULL if Internal).
134 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
138 ShellCommandRunTouch (
139 IN EFI_HANDLE ImageHandle
,
140 IN EFI_SYSTEM_TABLE
*SystemTable
145 CHAR16
*ProblemParam
;
147 SHELL_STATUS ShellStatus
;
149 EFI_SHELL_FILE_INFO
*FileList
;
150 EFI_SHELL_FILE_INFO
*Node
;
153 ShellStatus
= SHELL_SUCCESS
;
158 // initialize the shell lib (we must be in non-auto-init...)
160 Status
= ShellInitialize();
161 ASSERT_EFI_ERROR(Status
);
163 Status
= CommandInit();
164 ASSERT_EFI_ERROR(Status
);
167 // parse the command line
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
;
182 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
185 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
187 // we insufficient parameters
189 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
);
190 ShellStatus
= SHELL_INVALID_PARAMETER
;
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
196 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
198 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
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
;
207 // make sure we completed the param parsing sucessfully...
208 // Also make sure that any previous action was sucessful
210 if (ShellStatus
== SHELL_SUCCESS
) {
212 // check that we have at least 1 file
214 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
215 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, Param
);
219 // loop through the list and make sure we are not aborting...
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
)
226 // make sure the file opened ok
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
;
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
;
245 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
246 Status
= ShellCloseFileMetaArg(&FileList
);
247 ASSERT_EFI_ERROR(Status
);
254 // free the command line package
256 ShellCommandLineFreeVarList (Package
);
259 if (ShellGetExecutionBreakFlag()) {
260 return (SHELL_ABORTED
);
263 return (ShellStatus
);