2 Main file for Touch shell level 3 function.
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
10 #include "UefiShellLevel3CommandsLib.h"
12 #include <Library/ShellLib.h>
15 Do the touch operation on a single handle.
17 @param[in] Handle The handle to update the date/time on.
19 @retval EFI_ACCESS_DENIED The file referenced by Handle is read only.
20 @retval EFI_SUCCESS The operation was successful.
24 IN SHELL_FILE_HANDLE Handle
28 EFI_FILE_INFO
*FileInfo
;
30 FileInfo
= gEfiShellProtocol
->GetFileInfo(Handle
);
31 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) != 0){
32 return (EFI_ACCESS_DENIED
);
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
);
40 CopyMem(&FileInfo
->LastAccessTime
, &FileInfo
->ModificationTime
, sizeof(EFI_TIME
));
42 Status
= gEfiShellProtocol
->SetFileInfo(Handle
, FileInfo
);
50 Touch a given file and potantially recurse down if it was a directory.
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.
57 @retval EFI_INVALID_PARAMETER A parameter was invalid.
58 @retval EFI_SUCCESS The operation was successful.
62 IN CONST CHAR16
*Name
,
64 IN SHELL_FILE_HANDLE Handle
,
69 EFI_SHELL_FILE_INFO
*FileList
;
70 EFI_SHELL_FILE_INFO
*Walker
;
78 FS
= StrnCatGrow(&FS
, NULL
, Name
, 0);
80 TempSpot
= StrStr(FS
, L
"\\");
81 if (TempSpot
!= NULL
) {
82 *TempSpot
= CHAR_NULL
;
87 return (EFI_INVALID_PARAMETER
);
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
);
100 // if it's a directory recurse...
102 if (FileHandleIsDirectory(Handle
) == EFI_SUCCESS
&& Rec
) {
104 // get each file under this directory
106 if (EFI_ERROR(gEfiShellProtocol
->FindFilesInDir(Handle
, &FileList
))) {
107 Status
= EFI_INVALID_PARAMETER
;
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
)
117 if ( (StrCmp(Walker
->FileName
, L
".") != 0)
118 && (StrCmp(Walker
->FileName
, L
"..") != 0)
121 // Open the file since we need that handle.
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
;
128 Status
= DoTouchByHandle(Walker
->FullName
, FS
, Walker
->Handle
, TRUE
);
129 gEfiShellProtocol
->CloseFile(Walker
->Handle
);
130 Walker
->Handle
= NULL
;
138 if (FileList
!= NULL
&& EFI_ERROR(gEfiShellProtocol
->FreeFileList(&FileList
))) {
139 Status
= EFI_INVALID_PARAMETER
;
146 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
152 Function for 'touch' command.
154 @param[in] ImageHandle Handle to the Image (NULL if Internal).
155 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
159 ShellCommandRunTouch (
160 IN EFI_HANDLE ImageHandle
,
161 IN EFI_SYSTEM_TABLE
*SystemTable
166 CHAR16
*ProblemParam
;
168 SHELL_STATUS ShellStatus
;
170 EFI_SHELL_FILE_INFO
*FileList
;
171 EFI_SHELL_FILE_INFO
*Node
;
174 ShellStatus
= SHELL_SUCCESS
;
179 // initialize the shell lib (we must be in non-auto-init...)
181 Status
= ShellInitialize();
182 ASSERT_EFI_ERROR(Status
);
184 Status
= CommandInit();
185 ASSERT_EFI_ERROR(Status
);
188 // parse the command line
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
;
203 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
206 if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
208 // we insufficient parameters
210 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"touch");
211 ShellStatus
= SHELL_INVALID_PARAMETER
;
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
217 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
219 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
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
;
228 // make sure we completed the param parsing sucessfully...
229 // Also make sure that any previous action was sucessful
231 if (ShellStatus
== SHELL_SUCCESS
) {
233 // check that we have at least 1 file
235 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
236 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, L
"touch", Param
);
240 // loop through the list and make sure we are not aborting...
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
)
247 // make sure the file opened ok
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
;
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
;
266 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
267 Status
= ShellCloseFileMetaArg(&FileList
);
268 ASSERT_EFI_ERROR(Status
);
275 // free the command line package
277 ShellCommandLineFreeVarList (Package
);
280 if (ShellGetExecutionBreakFlag()) {
281 return (SHELL_ABORTED
);
284 return (ShellStatus
);