]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Update "ls" command to better handle "-r" parameter
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index adb6f9975902d4e53ae4e8f0e211bc756e29b300..65e83ccc0f4336130f150df627acb0fec860a577 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
+  Copyright (c) 2013, Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 2013, 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
@@ -44,7 +45,7 @@ IsValidMove(
   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
@@ -116,12 +117,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 +136,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 +193,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 +210,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 +238,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 +251,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,12 +265,16 @@ ValidateAndMoveFiles(
   EFI_FILE_INFO             *NewFileInfo;\r
   CHAR16                    *TempLocation;\r
   UINTN                     NewSize;\r
+  UINTN                     Length;\r
+  VOID                      *Response;\r
+  SHELL_FILE_HANDLE         DestHandle;\r
 \r
   ASSERT(FileList != NULL);\r
   ASSERT(DestDir  != NULL);\r
 \r
   DestPath = NULL;\r
   Cwd      = ShellGetCurrentDir(NULL);\r
+  Response = *Resp;\r
 \r
   //\r
   // Get and validate the destination location\r
@@ -238,6 +283,7 @@ ValidateAndMoveFiles(
   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
@@ -285,64 +331,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 +436,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 +456,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 +515,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 +536,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 +562,8 @@ ShellCommandRunMv (
     ShellCommandLineFreeVarList (Package);\r
   }\r
 \r
+  SHELL_FREE_NON_NULL(Response);\r
+\r
   if (ShellGetExecutionBreakFlag()) {\r
     return (SHELL_ABORTED);\r
   }\r