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