Home | History | Annotate | Download | only in UefiShellLevel2CommandsLib
      1 /** @file
      2   Main file for attrib shell level 2 function.
      3 
      4   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
      5   Copyright (c) 2009 - 2015, 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 "UefiShellLevel2CommandsLib.h"
     17 
     18 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
     19   {L"-q", TypeFlag},
     20   {NULL, TypeMax}
     21   };
     22 
     23 /**
     24   Determine if a directory has no files in it.
     25 
     26   @param[in] FileHandle   The EFI_HANDLE to the directory.
     27 
     28   @retval TRUE  The directory has no files (or directories).
     29   @retval FALSE The directory has at least 1 file or directory in it.
     30 **/
     31 BOOLEAN
     32 IsDirectoryEmpty (
     33   IN EFI_HANDLE   FileHandle
     34   )
     35 {
     36   EFI_FILE_INFO   *FileInfo;
     37   BOOLEAN         NoFile;
     38   BOOLEAN         RetVal;
     39 
     40   RetVal = TRUE;
     41   NoFile = FALSE;
     42   FileInfo = NULL;
     43 
     44   for (FileHandleFindFirstFile(FileHandle, &FileInfo)
     45     ;  !NoFile
     46     ;  FileHandleFindNextFile(FileHandle, FileInfo, &NoFile)
     47    ){
     48     if (StrStr(FileInfo->FileName, L".") != FileInfo->FileName
     49       &&StrStr(FileInfo->FileName, L"..") != FileInfo->FileName) {
     50       RetVal = FALSE;
     51     }
     52   }
     53   return (RetVal);
     54 }
     55 
     56 /**
     57   Delete a node and all nodes under it (including sub directories).
     58 
     59   @param[in] Node   The node to start deleting with.
     60   @param[in] Quiet  TRUE to print no messages.
     61 
     62   @retval SHELL_SUCCESS       The operation was successful.
     63   @retval SHELL_ACCESS_DENIED A file was read only.
     64   @retval SHELL_ABORTED       The abort message was received.
     65   @retval SHELL_DEVICE_ERROR  A device error occured reading this Node.
     66 **/
     67 SHELL_STATUS
     68 CascadeDelete(
     69   IN EFI_SHELL_FILE_INFO  *Node,
     70   IN CONST BOOLEAN        Quiet
     71   )
     72 {
     73   SHELL_STATUS          ShellStatus;
     74   EFI_SHELL_FILE_INFO   *List;
     75   EFI_SHELL_FILE_INFO   *Node2;
     76   EFI_STATUS            Status;
     77   SHELL_PROMPT_RESPONSE *Resp;
     78   CHAR16                *TempName;
     79   UINTN                 NewSize;
     80 
     81   Resp                  = NULL;
     82   ShellStatus           = SHELL_SUCCESS;
     83   List                  = NULL;
     84   Status                = EFI_SUCCESS;
     85 
     86   if ((Node->Info->Attribute & EFI_FILE_READ_ONLY) == EFI_FILE_READ_ONLY) {
     87     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DETELE_RO), gShellLevel2HiiHandle, L"rm", Node->FullName);
     88     return (SHELL_ACCESS_DENIED);
     89   }
     90 
     91   if ((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
     92     if (!IsDirectoryEmpty(Node->Handle)) {
     93       if (!Quiet) {
     94         Status = ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_RM_LOG_DELETE_CONF), gShellLevel2HiiHandle, Node->FullName);
     95         Status = ShellPromptForResponse(ShellPromptResponseTypeYesNo, NULL, (VOID**)&Resp);
     96         ASSERT(Resp != NULL);
     97         if (EFI_ERROR(Status) || *Resp != ShellPromptResponseYes) {
     98           SHELL_FREE_NON_NULL(Resp);
     99           return (SHELL_ABORTED);
    100         }
    101         SHELL_FREE_NON_NULL(Resp);
    102       }
    103       //
    104       // empty out the directory
    105       //
    106       Status = gEfiShellProtocol->FindFilesInDir(Node->Handle, &List);
    107       if (EFI_ERROR(Status)) {
    108         if (List!=NULL) {
    109           gEfiShellProtocol->FreeFileList(&List);
    110         }
    111         return (SHELL_DEVICE_ERROR);
    112       }
    113       for (Node2 = (EFI_SHELL_FILE_INFO   *)GetFirstNode(&List->Link)
    114         ;  !IsNull(&List->Link, &Node2->Link)
    115         ;  Node2 = (EFI_SHELL_FILE_INFO   *)GetNextNode(&List->Link, &Node2->Link)
    116        ){
    117         //
    118         // skip the directory traversing stuff...
    119         //
    120         if (StrCmp(Node2->FileName, L".") == 0 || StrCmp(Node2->FileName, L"..") == 0) {
    121           continue;
    122         }
    123         Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
    124         if (EFI_ERROR(Node2->Status) && StrStr(Node2->FileName, L":") == NULL) {
    125           //
    126           // Update the node filename to have full path with file system identifier
    127           //
    128           NewSize = StrSize(Node->FullName) + StrSize(Node2->FullName);
    129           TempName = AllocateZeroPool(NewSize);
    130           if (TempName == NULL) {
    131             ShellStatus = SHELL_OUT_OF_RESOURCES;
    132           } else {
    133             StrCpyS(TempName, NewSize/sizeof(CHAR16), Node->FullName);
    134             TempName[StrStr(TempName, L":")+1-TempName] = CHAR_NULL;
    135             StrCatS(TempName, NewSize/sizeof(CHAR16), Node2->FullName);
    136             FreePool((VOID*)Node2->FullName);
    137             Node2->FullName = TempName;
    138 
    139             //
    140             // Now try again to open the file
    141             //
    142             Node2->Status = gEfiShellProtocol->OpenFileByName (Node2->FullName, &Node2->Handle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE);
    143           }
    144         }
    145         if (!EFI_ERROR(Node2->Status)) {
    146           ShellStatus = CascadeDelete(Node2, Quiet);
    147         } else if (ShellStatus == SHELL_SUCCESS) {
    148           ShellStatus = (SHELL_STATUS)(Node2->Status&(~0x80000000));
    149         }
    150         if (ShellStatus != SHELL_SUCCESS) {
    151           if (List!=NULL) {
    152             gEfiShellProtocol->FreeFileList(&List);
    153           }
    154           return (ShellStatus);
    155         }
    156       }
    157       if (List!=NULL) {
    158         gEfiShellProtocol->FreeFileList(&List);
    159       }
    160     }
    161   }
    162 
    163   if (!(StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0)) {
    164     //
    165     // now delete the current node...
    166     //
    167     if (!Quiet) {
    168       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE), gShellLevel2HiiHandle, Node->FullName);
    169     }
    170     Status = gEfiShellProtocol->DeleteFile(Node->Handle);
    171     Node->Handle = NULL;
    172   }
    173 
    174   //
    175   // We cant allow for the warning here! (Dont use EFI_ERROR Macro).
    176   //
    177   if (Status != EFI_SUCCESS){
    178     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR), gShellLevel2HiiHandle, Status);
    179     return (SHELL_ACCESS_DENIED);
    180   } else {
    181     if (!Quiet) {
    182       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_COMP), gShellLevel2HiiHandle);
    183     }
    184     return (SHELL_SUCCESS);
    185   }
    186 }
    187 
    188 /**
    189   Determines if a Node is a valid delete target.  Will prevent deleting the root directory.
    190 
    191   @param[in] List       RESERVED.  Not used.
    192   @param[in] Node       The node to analyze.
    193   @param[in] Package    RESERVED.  Not used.
    194 **/
    195 BOOLEAN
    196 IsValidDeleteTarget(
    197   IN CONST EFI_SHELL_FILE_INFO  *List,
    198   IN CONST EFI_SHELL_FILE_INFO  *Node,
    199   IN CONST LIST_ENTRY           *Package
    200   )
    201 {
    202   CONST CHAR16        *TempLocation;
    203   BOOLEAN             RetVal;
    204   CHAR16              *SearchString;
    205   CHAR16              *Pattern;
    206   UINTN               Size;
    207 
    208   if (Node == NULL || Node->FullName == NULL) {
    209     return (FALSE);
    210   }
    211 
    212   TempLocation = StrStr(Node->FullName, L":");
    213   if (StrLen(TempLocation) <= 2) {
    214     //
    215     // Deleting the root directory is invalid.
    216     //
    217     return (FALSE);
    218   }
    219 
    220   TempLocation = ShellGetCurrentDir(NULL);
    221   if (TempLocation == NULL) {
    222     //
    223     // No working directory is specified so whatever is left is ok.
    224     //
    225     return (TRUE);
    226   }
    227 
    228   Pattern       = NULL;
    229   SearchString  = NULL;
    230   Size          = 0;
    231   Pattern       = StrnCatGrow(&Pattern, &Size, TempLocation  , 0);
    232   Pattern       = StrnCatGrow(&Pattern, &Size, L"\\"  , 0);
    233   Size = 0;
    234   SearchString  = StrnCatGrow(&SearchString, &Size, Node->FullName, 0);
    235   if (!EFI_ERROR(ShellIsDirectory(SearchString))) {
    236     SearchString  = StrnCatGrow(&SearchString, &Size, L"\\", 0);
    237     SearchString  = StrnCatGrow(&SearchString, &Size, L"*", 0);
    238   }
    239 
    240   if (Pattern == NULL || SearchString == NULL) {
    241     RetVal = FALSE;
    242   } else {
    243     RetVal = TRUE;
    244     if (gUnicodeCollation->MetaiMatch(gUnicodeCollation, Pattern, SearchString)) {
    245       RetVal = FALSE;
    246     }
    247   }
    248 
    249   SHELL_FREE_NON_NULL(Pattern     );
    250   SHELL_FREE_NON_NULL(SearchString);
    251 
    252   return (RetVal);
    253 }
    254 
    255 /**
    256   Function for 'rm' command.
    257 
    258   @param[in] ImageHandle  Handle to the Image (NULL if Internal).
    259   @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
    260 **/
    261 SHELL_STATUS
    262 EFIAPI
    263 ShellCommandRunRm (
    264   IN EFI_HANDLE        ImageHandle,
    265   IN EFI_SYSTEM_TABLE  *SystemTable
    266   )
    267 {
    268   EFI_STATUS            Status;
    269   LIST_ENTRY            *Package;
    270   CHAR16                *ProblemParam;
    271   CONST CHAR16          *Param;
    272   SHELL_STATUS          ShellStatus;
    273   UINTN                 ParamCount;
    274   EFI_SHELL_FILE_INFO   *FileList;
    275   EFI_SHELL_FILE_INFO   *Node;
    276 
    277   ProblemParam        = NULL;
    278   ShellStatus         = SHELL_SUCCESS;
    279   ParamCount          = 0;
    280   FileList            = NULL;
    281 
    282   //
    283   // initialize the shell lib (we must be in non-auto-init...)
    284   //
    285   Status = ShellInitialize();
    286   ASSERT_EFI_ERROR(Status);
    287 
    288   //
    289   // parse the command line
    290   //
    291   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
    292   if (EFI_ERROR(Status)) {
    293     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
    294       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"rm", ProblemParam);
    295       FreePool(ProblemParam);
    296       ShellStatus = SHELL_INVALID_PARAMETER;
    297     } else {
    298       ASSERT(FALSE);
    299     }
    300   } else {
    301     //
    302     // check for "-?"
    303     //
    304     if (ShellCommandLineGetFlag(Package, L"-?")) {
    305       ASSERT(FALSE);
    306     }
    307     if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
    308       //
    309       // we insufficient parameters
    310       //
    311       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"rm");
    312       ShellStatus = SHELL_INVALID_PARAMETER;
    313     } else {
    314       //
    315       // get a list with each file specified by parameters
    316       // if parameter is a directory then add all the files below it to the list
    317       //
    318       for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
    319           ; Param != NULL
    320           ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
    321          ){
    322         Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
    323         if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {
    324           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"rm", (CHAR16*)Param);
    325           ShellStatus = SHELL_NOT_FOUND;
    326           break;
    327         }
    328       }
    329 
    330       if (ShellStatus == SHELL_SUCCESS){
    331         //
    332         // loop through the list and make sure we are not aborting...
    333         //
    334         for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)
    335             ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()
    336             ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)
    337            ){
    338           //
    339           // skip the directory traversing stuff...
    340           //
    341           if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
    342             continue;
    343           }
    344 
    345           //
    346           // do the deleting of nodes
    347           //
    348           if (EFI_ERROR(Node->Status)){
    349             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR2), gShellLevel2HiiHandle, Node->Status);
    350             ShellStatus = SHELL_ACCESS_DENIED;
    351             break;
    352           }
    353           if (!IsValidDeleteTarget(FileList, Node, Package)) {
    354             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_RM_LOG_DELETE_ERR3), gShellLevel2HiiHandle, Node->FullName);
    355             ShellStatus = SHELL_INVALID_PARAMETER;
    356             break;
    357           }
    358 
    359           ShellStatus = CascadeDelete(Node, ShellCommandLineGetFlag(Package, L"-q"));
    360         }
    361       }
    362       //
    363       // Free the fileList
    364       //
    365       if (FileList != NULL) {
    366         Status = ShellCloseFileMetaArg(&FileList);
    367       }
    368       FileList = NULL;
    369     }
    370 
    371     //
    372     // free the command line package
    373     //
    374     ShellCommandLineFreeVarList (Package);
    375   }
    376 
    377   return (ShellStatus);
    378 }
    379 
    380