]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
This refactors 3 functions out of ShellCommandLib and puts them into a new library...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index adb6f9975902d4e53ae4e8f0e211bc756e29b300..5afcea23107fd62d9955d4447eaa171b25dfa170 100644 (file)
@@ -1,7 +1,7 @@
 /** @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) 2009 - 2011, 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 +44,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 +116,13 @@ 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 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
 **/\r
 SHELL_STATUS\r
 EFIAPI\r
@@ -140,6 +141,17 @@ GetDestinationLocation(
 \r
   DestList = NULL;\r
   DestPath = NULL;\r
+\r
+  if (StrStr(DestDir, L"\\") == DestDir) {\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
+    *DestPathPointer =  DestPath;\r
+    return (SHELL_SUCCESS);\r
+  }\r
   //\r
   // get the destination path\r
   //\r
@@ -152,6 +164,10 @@ GetDestinationLocation(
       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 +178,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 +195,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 +223,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 +236,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 +250,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 +268,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
@@ -287,62 +318,105 @@ ValidateAndMoveFiles(
     NewSize = StrSize(DestPath);\r
     NewSize += StrSize(Node->FileName) + sizeof(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, sizeof(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 = sizeof(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
-          ShellStatus = SHELL_SECURITY_VIOLATION;\r
-        case EFI_WRITE_PROTECTED:\r
-          ShellStatus = SHELL_WRITE_PROTECTED;\r
-        case EFI_OUT_OF_RESOURCES:\r
-          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
-        case EFI_DEVICE_ERROR:\r
-          ShellStatus = SHELL_DEVICE_ERROR;\r
-        case EFI_ACCESS_DENIED:\r
-          ShellStatus = SHELL_ACCESS_DENIED;\r
-      } // switch\r
-    } else {\r
-      ShellPrintEx(-1, -1, L"%s", HiiResultOk);\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
+        //\r
+        // move failed\r
+        //\r
+        switch(Status){\r
+          default:\r
+            ShellStatus = SHELL_INVALID_PARAMETER;\r
+          case EFI_SECURITY_VIOLATION:\r
+            ShellStatus = SHELL_SECURITY_VIOLATION;\r
+          case EFI_WRITE_PROTECTED:\r
+            ShellStatus = SHELL_WRITE_PROTECTED;\r
+          case EFI_OUT_OF_RESOURCES:\r
+            ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+          case EFI_DEVICE_ERROR:\r
+            ShellStatus = SHELL_DEVICE_ERROR;\r
+          case EFI_ACCESS_DENIED:\r
+            ShellStatus = SHELL_ACCESS_DENIED;\r
+        } // switch\r
+      } else {\r
+        ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
+      }\r
     }\r
   } // for loop\r
 \r
@@ -352,6 +426,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 +446,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 +505,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 +526,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 +552,8 @@ ShellCommandRunMv (
     ShellCommandLineFreeVarList (Package);\r
   }\r
 \r
+  SHELL_FREE_NON_NULL(Response);\r
+\r
   if (ShellGetExecutionBreakFlag()) {\r
     return (SHELL_ABORTED);\r
   }\r