]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Remove redundant quotes in file path string for Shell command parameters.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index adb6f9975902d4e53ae4e8f0e211bc756e29b300..fcf5f657b8df9a7cdd8248a56ecf93735e44820c 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for mv shell level 2 function.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -28,6 +29,7 @@
   @param Cwd      [in]    The current working directory\r
   @param DestPath [in]    The target location to move to\r
   @param Attribute[in]    The Attribute of the file\r
+  @param FileStatus[in]   The Status of the file when opened\r
 \r
   @retval TRUE        The move is valid\r
   @retval FALSE       The move is not\r
 BOOLEAN\r
 EFIAPI\r
 IsValidMove(\r
-  IN CONST CHAR16   *FullName,\r
-  IN CONST CHAR16   *Cwd,\r
-  IN CONST CHAR16   *DestPath,\r
-  IN CONST UINT64   Attribute\r
+  IN CONST CHAR16     *FullName,\r
+  IN CONST CHAR16     *Cwd,\r
+  IN CONST CHAR16     *DestPath,\r
+  IN CONST UINT64     Attribute,\r
+  IN CONST EFI_STATUS FileStatus\r
   )\r
 {\r
   CHAR16  *Test;\r
   CHAR16  *Test1;\r
   CHAR16  *TestWalker;\r
-  UINTN   Result;\r
+  INTN    Result;\r
   UINTN   TempLen;\r
   if (Cwd != NULL && StrCmp(FullName, Cwd) == 0) {\r
     //\r
@@ -86,11 +89,11 @@ IsValidMove(
     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_SUB), gShellLevel2HiiHandle);\r
     return (FALSE);\r
   }\r
-  if ((Attribute & EFI_FILE_READ_ONLY) != 0) {\r
+  if (((Attribute & EFI_FILE_READ_ONLY) != 0) || (FileStatus == EFI_WRITE_PROTECTED)) {\r
     //\r
     // invalid to move read only\r
     //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle, FullName);\r
     return (FALSE);\r
   }\r
   Test  = StrStr(FullName, L":");\r
@@ -116,12 +119,14 @@ IsValidMove(
 \r
   if the result is sucessful the caller must free *DestPathPointer.\r
 \r
-  @param[in] DestDir              The original path to the destination\r
-  @param[in,out] DestPathPointer  a pointer to the callee allocated final path.\r
+  @param[in] DestDir               The original path to the destination.\r
+  @param[in, out] DestPathPointer  A pointer to the callee allocated final path.\r
+  @param[in] Cwd                   A pointer to the current working directory.\r
 \r
-  @retval EFI_INVALID_PARAMETR  the DestDir could not be resolved to a location\r
-  @retval EFI_INVALID_PARAMETR  the DestDir could be resolved to more than 1 location\r
-  @retval EFI_SUCCESS           the operation was sucessful\r
+  @retval SHELL_INVALID_PARAMETER  The DestDir could not be resolved to a location.\r
+  @retval SHELL_INVALID_PARAMETER  The DestDir could be resolved to more than 1 location.\r
+  @retval SHELL_INVALID_PARAMETER  Cwd is required and is NULL.\r
+  @retval SHELL_SUCCESS            The operation was sucessful.\r
 **/\r
 SHELL_STATUS\r
 EFIAPI\r
@@ -133,25 +138,53 @@ GetDestinationLocation(
 {\r
   EFI_SHELL_FILE_INFO       *DestList;\r
   EFI_SHELL_FILE_INFO       *Node;\r
-  EFI_STATUS                Status;\r
   CHAR16                    *DestPath;\r
-  CHAR16                    *TempLocation;\r
   UINTN                     NewSize;\r
+  UINTN                     CurrentSize;\r
 \r
   DestList = NULL;\r
   DestPath = NULL;\r
+\r
+  if (StrStr(DestDir, L"\\") == DestDir) {\r
+    if (Cwd == NULL) {\r
+      return SHELL_INVALID_PARAMETER;\r
+    }\r
+    DestPath = AllocateZeroPool(StrSize(Cwd));\r
+    if (DestPath == NULL) {\r
+      return (SHELL_OUT_OF_RESOURCES);\r
+    }\r
+    StrCpy(DestPath, Cwd);\r
+    while (PathRemoveLastItem(DestPath)) ;\r
+\r
+    //\r
+    // Append DestDir beyond '\' which may be present\r
+    //\r
+    CurrentSize = StrSize(DestPath);\r
+    StrnCatGrow(&DestPath, &CurrentSize, &DestDir[1], 0);\r
+\r
+    *DestPathPointer =  DestPath;\r
+    return (SHELL_SUCCESS);\r
+  }\r
   //\r
   // get the destination path\r
   //\r
-  Status = ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);\r
+  ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);\r
   if (DestList == NULL || IsListEmpty(&DestList->Link)) {\r
     //\r
     // Not existing... must be renaming\r
     //\r
-    if ((TempLocation = StrStr(DestDir, L":")) == NULL) {\r
+    if (StrStr(DestDir, L":") == NULL) {\r
+      if (Cwd == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_INVALID_PARAMETER);\r
+      }\r
       NewSize = StrSize(Cwd);\r
       NewSize += StrSize(DestDir);\r
       DestPath = AllocateZeroPool(NewSize);\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
       StrCpy(DestPath, Cwd);\r
       if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
         StrCat(DestPath, L"\\");\r
@@ -162,6 +195,10 @@ GetDestinationLocation(
     } else {\r
       ASSERT(DestPath == NULL);\r
       DestPath = StrnCatGrow(&DestPath, NULL, DestDir, 0);\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
     }\r
   } else {\r
     Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&DestList->Link);\r
@@ -175,6 +212,10 @@ GetDestinationLocation(
     }\r
     if (ShellIsDirectory(Node->FullName)==EFI_SUCCESS) {\r
       DestPath = AllocateZeroPool(StrSize(Node->FullName)+sizeof(CHAR16));\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
       StrCpy(DestPath, Node->FullName);\r
       StrCat(DestPath, L"\\");\r
     } else {\r
@@ -199,6 +240,7 @@ GetDestinationLocation(
   will report any errors to the user and continue to move the rest of the files.\r
 \r
   @param[in] FileList           A LIST_ENTRY* based list of files to move\r
+  @param[out] Resp              pointer to response from question.  Pass back on looped calling\r
   @param[in] DestDir            the destination location\r
 \r
   @retval SHELL_SUCCESS             the files were all moved.\r
@@ -211,6 +253,7 @@ SHELL_STATUS
 EFIAPI\r
 ValidateAndMoveFiles(\r
   IN CONST EFI_SHELL_FILE_INFO  *FileList,\r
+  OUT VOID                      **Resp,\r
   IN CONST CHAR16               *DestDir\r
   )\r
 {\r
@@ -224,27 +267,42 @@ ValidateAndMoveFiles(
   EFI_FILE_INFO             *NewFileInfo;\r
   CHAR16                    *TempLocation;\r
   UINTN                     NewSize;\r
+  UINTN                     Length;\r
+  VOID                      *Response;\r
+  SHELL_FILE_HANDLE         DestHandle;\r
+  CHAR16                    *CleanFilePathStr;\r
 \r
   ASSERT(FileList != NULL);\r
   ASSERT(DestDir  != NULL);\r
 \r
   DestPath = NULL;\r
   Cwd      = ShellGetCurrentDir(NULL);\r
+  Response = *Resp;\r
+\r
+  Status = ShellLevel2StripQuotes (DestDir, &CleanFilePathStr);\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_OUT_OF_RESOURCES) {\r
+      return SHELL_OUT_OF_RESOURCES;\r
+    } else {\r
+      return SHELL_INVALID_PARAMETER;\r
+    }\r
+  }  \r
 \r
   //\r
   // Get and validate the destination location\r
   //\r
-  ShellStatus = GetDestinationLocation(DestDir, &DestPath, Cwd);\r
+  ShellStatus = GetDestinationLocation(CleanFilePathStr, &DestPath, Cwd);\r
+  FreePool (CleanFilePathStr);\r
   if (ShellStatus != SHELL_SUCCESS) {\r
     return (ShellStatus);\r
   }\r
+  DestPath = PathCleanUpDirectories(DestPath);\r
 \r
   HiiOutput   = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_MV_OUTPUT), NULL);\r
   HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
   ASSERT  (DestPath     != NULL);\r
   ASSERT  (HiiResultOk  != NULL);\r
   ASSERT  (HiiOutput    != NULL);\r
-//  ASSERT  (Cwd          != NULL);\r
 \r
   //\r
   // Go through the list of files and directories to move...\r
@@ -256,8 +314,13 @@ ValidateAndMoveFiles(
     if (ShellGetExecutionBreakFlag()) {\r
       break;\r
     }\r
+\r
+    //\r
+    // These should never be NULL\r
+    //\r
     ASSERT(Node->FileName != NULL);\r
     ASSERT(Node->FullName != NULL);\r
+    ASSERT(Node->Info     != NULL);\r
 \r
     //\r
     // skip the directory traversing stuff...\r
@@ -269,7 +332,7 @@ ValidateAndMoveFiles(
     //\r
     // Validate that the move is valid\r
     //\r
-    if (!IsValidMove(Node->FullName, Cwd, DestPath, Node->Info->Attribute)) {\r
+    if (!IsValidMove(Node->FullName, Cwd, DestPath, Node->Info->Attribute, Node->Status)) {\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       continue;\r
     }\r
@@ -285,64 +348,102 @@ ValidateAndMoveFiles(
     // construct the new file info block\r
     //\r
     NewSize = StrSize(DestPath);\r
-    NewSize += StrSize(Node->FileName) + sizeof(EFI_FILE_INFO) + sizeof(CHAR16);\r
+    NewSize += StrSize(Node->FileName) + SIZE_OF_EFI_FILE_INFO + sizeof(CHAR16);\r
     NewFileInfo = AllocateZeroPool(NewSize);\r
-    ASSERT(NewFileInfo != NULL);\r
-    CopyMem(NewFileInfo, Node->Info, sizeof(EFI_FILE_INFO));\r
-    if (DestPath[0] != L'\\') {\r
-      StrCpy(NewFileInfo->FileName, L"\\");\r
-      StrCat(NewFileInfo->FileName, DestPath);\r
+    if (NewFileInfo == NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
+      ShellStatus = SHELL_OUT_OF_RESOURCES;\r
     } else {\r
-      StrCpy(NewFileInfo->FileName, DestPath);\r
-    }\r
-    if (NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] == L'\\') {\r
-      if (Node->FileName[0] == L'\\') {\r
-        //\r
-        // Don't allow for double slashes. Eliminate one of them.\r
-        //\r
-        NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] = CHAR_NULL;\r
+      CopyMem(NewFileInfo, Node->Info, SIZE_OF_EFI_FILE_INFO);\r
+      if (DestPath[0] != L'\\') {\r
+        StrCpy(NewFileInfo->FileName, L"\\");\r
+        StrCat(NewFileInfo->FileName, DestPath);\r
+      } else {\r
+        StrCpy(NewFileInfo->FileName, DestPath);\r
       }\r
-      StrCat(NewFileInfo->FileName, Node->FileName);\r
-    }\r
-    NewFileInfo->Size = sizeof(EFI_FILE_INFO) + StrSize(NewFileInfo->FileName);\r
+      Length = StrLen(NewFileInfo->FileName);\r
+      if (Length > 0) {\r
+        Length--;\r
+      }\r
+      if (NewFileInfo->FileName[Length] == L'\\') {\r
+        if (Node->FileName[0] == L'\\') {\r
+          //\r
+          // Don't allow for double slashes. Eliminate one of them.\r
+          //\r
+          NewFileInfo->FileName[Length] = CHAR_NULL;\r
+        }\r
+        StrCat(NewFileInfo->FileName, Node->FileName);\r
+      }\r
+      NewFileInfo->Size = SIZE_OF_EFI_FILE_INFO + StrSize(NewFileInfo->FileName);\r
+      ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
 \r
-    ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
+      if (!EFI_ERROR(ShellFileExists(NewFileInfo->FileName))) {\r
+        if (Response == NULL) {\r
+          ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);\r
+        }\r
+        switch (*(SHELL_PROMPT_RESPONSE*)Response) {\r
+          case ShellPromptResponseNo:\r
+            FreePool(NewFileInfo);\r
+            continue;\r
+          case ShellPromptResponseCancel:\r
+            *Resp = Response;\r
+            //\r
+            // indicate to stop everything\r
+            //\r
+            FreePool(NewFileInfo);\r
+            FreePool(DestPath);\r
+            FreePool(HiiOutput);\r
+            FreePool(HiiResultOk);\r
+            return (SHELL_ABORTED);\r
+          case ShellPromptResponseAll:\r
+            *Resp = Response;\r
+            break;\r
+          case ShellPromptResponseYes:\r
+            FreePool(Response);\r
+            break;\r
+          default:\r
+            FreePool(Response);\r
+            FreePool(NewFileInfo);\r
+            FreePool(DestPath);\r
+            FreePool(HiiOutput);\r
+            FreePool(HiiResultOk);\r
+            return SHELL_ABORTED;\r
+        }\r
+        Status = ShellOpenFileByName(NewFileInfo->FileName, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);\r
+        ShellDeleteFile(&DestHandle);\r
+      }\r
 \r
-    //\r
-    // Perform the move operation\r
-    //\r
-    Status = ShellSetFileInfo(Node->Handle, NewFileInfo);\r
 \r
-    //\r
-    // Free the info object we used...\r
-    //\r
-    ASSERT  (NewFileInfo != NULL);\r
-    FreePool(NewFileInfo);\r
+      //\r
+      // Perform the move operation\r
+      //\r
+      Status = ShellSetFileInfo(Node->Handle, NewFileInfo);\r
 \r
-    //\r
-    // Check our result\r
-    //\r
-    if (EFI_ERROR(Status)) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
       //\r
-      // move failed\r
+      // Free the info object we used...\r
       //\r
-      switch(Status){\r
-        default:\r
-          ShellStatus = SHELL_INVALID_PARAMETER;\r
-        case EFI_SECURITY_VIOLATION:\r
+      FreePool(NewFileInfo);\r
+\r
+      //\r
+      // Check our result\r
+      //\r
+      if (EFI_ERROR(Status)) {\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
+        ShellStatus = SHELL_INVALID_PARAMETER;\r
+        if (Status == EFI_SECURITY_VIOLATION) {\r
           ShellStatus = SHELL_SECURITY_VIOLATION;\r
-        case EFI_WRITE_PROTECTED:\r
+        } else if (Status == EFI_WRITE_PROTECTED) {\r
           ShellStatus = SHELL_WRITE_PROTECTED;\r
-        case EFI_OUT_OF_RESOURCES:\r
+        } else if (Status == EFI_OUT_OF_RESOURCES) {\r
           ShellStatus = SHELL_OUT_OF_RESOURCES;\r
-        case EFI_DEVICE_ERROR:\r
+        } else if (Status == EFI_DEVICE_ERROR) {\r
           ShellStatus = SHELL_DEVICE_ERROR;\r
-        case EFI_ACCESS_DENIED:\r
+        } else if (Status == EFI_ACCESS_DENIED) {\r
           ShellStatus = SHELL_ACCESS_DENIED;\r
-      } // switch\r
-    } else {\r
-      ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
+        }\r
+      } else {\r
+        ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
+      }\r
     }\r
   } // for loop\r
 \r
@@ -352,6 +453,12 @@ ValidateAndMoveFiles(
   return (ShellStatus);\r
 }\r
 \r
+/**\r
+  Function for 'mv' command.\r
+\r
+  @param[in] ImageHandle  Handle to the Image (NULL if Internal).\r
+  @param[in] SystemTable  Pointer to the System Table (NULL if Internal).\r
+**/\r
 SHELL_STATUS\r
 EFIAPI\r
 ShellCommandRunMv (\r
@@ -366,11 +473,13 @@ ShellCommandRunMv (
   UINTN               ParamCount;\r
   UINTN               LoopCounter;\r
   EFI_SHELL_FILE_INFO *FileList;\r
+  VOID                *Response;\r
 \r
   ProblemParam        = NULL;\r
   ShellStatus         = SHELL_SUCCESS;\r
   ParamCount          = 0;\r
   FileList            = NULL;\r
+  Response            = NULL;\r
 \r
   //\r
   // initialize the shell lib (we must be in non-auto-init...)\r
@@ -423,20 +532,20 @@ ShellCommandRunMv (
             //\r
             // ValidateAndMoveFiles will report errors to the screen itself\r
             //\r
-            ShellStatus = ValidateAndMoveFiles(FileList, ShellGetCurrentDir(NULL));\r
+            ShellStatus = ValidateAndMoveFiles(FileList, &Response, ShellGetCurrentDir(NULL));\r
           }\r
         }\r
 \r
         break;\r
       default:\r
         ///@todo make sure this works with error half way through and continues...\r
-        for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount && ShellStatus == SHELL_SUCCESS ; LoopCounter++) {\r
+        for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount ; LoopCounter++) {\r
           if (ShellGetExecutionBreakFlag()) {\r
             break;\r
           }\r
           Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
           if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, LoopCounter));\r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else  {\r
             //\r
@@ -444,9 +553,9 @@ ShellCommandRunMv (
             // Only change ShellStatus if it's sucessful\r
             //\r
             if (ShellStatus == SHELL_SUCCESS) {\r
-              ShellStatus = ValidateAndMoveFiles(FileList, ShellCommandLineGetRawValue(Package, ParamCount));\r
+              ShellStatus = ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));\r
             } else {\r
-              ValidateAndMoveFiles(FileList, ShellCommandLineGetRawValue(Package, ParamCount));\r
+              ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));\r
             }\r
           }\r
           if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
@@ -470,6 +579,8 @@ ShellCommandRunMv (
     ShellCommandLineFreeVarList (Package);\r
   }\r
 \r
+  SHELL_FREE_NON_NULL(Response);\r
+\r
   if (ShellGetExecutionBreakFlag()) {\r
     return (SHELL_ABORTED);\r
   }\r