summaryrefslogtreecommitdiff
path: root/Core/ShellPkg/Library/UefiShellLevel3CommandsLib/Touch.c
blob: 639346fb31eff120510e6c3672090d1fe8d24f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/** @file
  Main file for Touch shell level 3 function.

  (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved. <BR>
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UefiShellLevel3CommandsLib.h"

#include <Library/ShellLib.h>

/**
  Do the touch operation on a single handle.

  @param[in] Handle   The handle to update the date/time on.

  @retval EFI_ACCESS_DENIED The file referenced by Handle is read only.
  @retval EFI_SUCCESS       The operation was successful.
**/
EFI_STATUS
TouchFileByHandle (
  IN EFI_HANDLE Handle
  )
{
  EFI_STATUS    Status;
  EFI_FILE_INFO *FileInfo;

  FileInfo = gEfiShellProtocol->GetFileInfo(Handle);
  if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) != 0){
    return (EFI_ACCESS_DENIED);
  }
  Status = gRT->GetTime(&FileInfo->ModificationTime, NULL);
  if (EFI_ERROR(Status)) {
    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"gRT->GetTime", Status);
    return (SHELL_DEVICE_ERROR);
  }

  CopyMem(&FileInfo->LastAccessTime, &FileInfo->ModificationTime, sizeof(EFI_TIME));

  Status = gEfiShellProtocol->SetFileInfo(Handle, FileInfo);

  FreePool(FileInfo);

  return (Status);
}

/**
  Touch a given file and potantially recurse down if it was a directory.

  @param[in] Name   The name of this file.
  @param[in] FS     The name of the file system this file is on.
  @param[in] Handle The handle of this file already opened.
  @param[in] Rec    TRUE to recurse if possible.

  @retval EFI_INVALID_PARAMETER A parameter was invalid.
  @retval EFI_SUCCESS           The operation was successful.
**/
EFI_STATUS
DoTouchByHandle (
  IN CONST CHAR16       *Name,
  IN       CHAR16       *FS,
  IN SHELL_FILE_HANDLE  Handle,
  IN BOOLEAN            Rec
  )
{
  EFI_STATUS          Status;
  EFI_SHELL_FILE_INFO *FileList;
  EFI_SHELL_FILE_INFO *Walker;
  CHAR16              *TempSpot;

  Status      = EFI_SUCCESS;
  FileList    = NULL;
  Walker      = NULL;

  if (FS == NULL) {
    FS = StrnCatGrow(&FS, NULL, Name, 0);
    if (FS != NULL) {
      TempSpot = StrStr(FS, L"\\");
      if (TempSpot != NULL) {
        *TempSpot = CHAR_NULL;
      }
    }
  }
  if (FS == NULL) {
    return (EFI_INVALID_PARAMETER);
  }

  //
  // do it
  //
  Status = TouchFileByHandle(Handle);
  if (EFI_ERROR(Status)) {
    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel3HiiHandle, L"touch", Name);  
    return (Status);
  }

  //
  // if it's a directory recurse...
  //
  if (FileHandleIsDirectory(Handle) == EFI_SUCCESS && Rec) {
    //
    // get each file under this directory
    //
    if (EFI_ERROR(gEfiShellProtocol->FindFilesInDir(Handle, &FileList))) {
      Status = EFI_INVALID_PARAMETER;
    }

    //
    // recurse on each
    //
    for (Walker = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
      ;  FileList != NULL && !IsNull(&FileList->Link, &Walker->Link) && !EFI_ERROR(Status)
      ;  Walker = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Walker->Link)
     ){
      if ( (StrCmp(Walker->FileName, L".") != 0)
        && (StrCmp(Walker->FileName, L"..") != 0)
       ){
        //
        // Open the file since we need that handle.
        //
        Status = gEfiShellProtocol->OpenFileByName (Walker->FullName, &Walker->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
        if (EFI_ERROR(Status)) {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel3HiiHandle, L"touch", Walker->FullName);  
          Status = EFI_ACCESS_DENIED;
        } else {
          Status = DoTouchByHandle(Walker->FullName, FS, Walker->Handle, TRUE);
          gEfiShellProtocol->CloseFile(Walker->Handle);
          Walker->Handle = NULL;
        }
      }
    }

    //
    // free stuff
    //
    if (FileList != NULL && EFI_ERROR(gEfiShellProtocol->FreeFileList(&FileList))) {
      Status = EFI_INVALID_PARAMETER;
    }
  }

  return (Status);
}

STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  {L"-r", TypeFlag},
  {NULL, TypeMax}
  };

/**
  Function for 'touch' command.

  @param[in] ImageHandle  Handle to the Image (NULL if Internal).
  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
**/
SHELL_STATUS
EFIAPI
ShellCommandRunTouch (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS          Status;
  LIST_ENTRY          *Package;
  CHAR16              *ProblemParam;
  CONST CHAR16        *Param;
  SHELL_STATUS        ShellStatus;
  UINTN               ParamCount;
  EFI_SHELL_FILE_INFO *FileList;
  EFI_SHELL_FILE_INFO *Node;

  ProblemParam        = NULL;
  ShellStatus         = SHELL_SUCCESS;
  ParamCount          = 0;
  FileList            = NULL;

  //
  // initialize the shell lib (we must be in non-auto-init...)
  //
  Status = ShellInitialize();
  ASSERT_EFI_ERROR(Status);

  Status = CommandInit();
  ASSERT_EFI_ERROR(Status);

  //
  // parse the command line
  //
  Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
  if (EFI_ERROR(Status)) {
    if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"touch", ProblemParam);  
      FreePool(ProblemParam);
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      ASSERT(FALSE);
    }
  } else {
    //
    // check for "-?"
    //
    if (ShellCommandLineGetFlag(Package, L"-?")) {
      ASSERT(FALSE);
    }
    if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
      //
      // we insufficient parameters
      //
      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle, L"touch");  
      ShellStatus = SHELL_INVALID_PARAMETER;
    } else {
      //
      // get a list with each file specified by parameters
      // if parameter is a directory then add all the files below it to the list
      //
      for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
          ; Param != NULL
          ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
         ){
        Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, &FileList);
        if (EFI_ERROR(Status)) {
          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"touch", (CHAR16*)Param);  
          ShellStatus = SHELL_NOT_FOUND;
          break;
        }
        //
        // make sure we completed the param parsing sucessfully...
        // Also make sure that any previous action was sucessful
        //
        if (ShellStatus == SHELL_SUCCESS) {
          //
          // check that we have at least 1 file
          //
          if (FileList == NULL || IsListEmpty(&FileList->Link)) {
            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, L"touch", Param);  
            continue;
          } else {
            //
            // loop through the list and make sure we are not aborting...
            //
            for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
                ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
                ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
               ){
              //
              // make sure the file opened ok
              //
              if (EFI_ERROR(Node->Status)){
                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel3HiiHandle, L"touch", Node->FileName);  
                ShellStatus = SHELL_NOT_FOUND;
                continue;
              }

              Status = DoTouchByHandle(Node->FullName, NULL, Node->Handle, ShellCommandLineGetFlag(Package, L"-r"));
              if (EFI_ERROR(Status) && Status != EFI_ACCESS_DENIED) {
                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel3HiiHandle, L"touch", Node->FileName);  
                ShellStatus = SHELL_NOT_FOUND;
              }
            }
          }
        }
        //
        // Free the fileList
        //
        if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
          Status = ShellCloseFileMetaArg(&FileList);
          ASSERT_EFI_ERROR(Status);
        }
        FileList = NULL;
      }
    }

    //
    // free the command line package
    //
    ShellCommandLineFreeVarList (Package);
  }

  if (ShellGetExecutionBreakFlag()) {
    return (SHELL_ABORTED);
  }

  return (ShellStatus);
}