]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
Cd - add more input verification.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index 7530289a52f56d8e00ff316261693d9ba7e7adca..dc9ef64d7f317c14fa77ff5b167411d6bebee051 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
@@ -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 (ChopLastSlash(DestPath)) ;\r
+    *DestPathPointer =  DestPath;\r
+    return (SHELL_SUCCESS);\r
+  }\r
   //\r
   // get the destination path\r
   //\r
@@ -211,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
@@ -223,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
@@ -237,12 +251,15 @@ ValidateAndMoveFiles(
   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
@@ -251,6 +268,7 @@ ValidateAndMoveFiles(
   if (ShellStatus != SHELL_SUCCESS) {\r
     return (ShellStatus);\r
   }\r
+  DestPath = CleanPath(DestPath);\r
 \r
   HiiOutput   = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_MV_OUTPUT), NULL);\r
   HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
@@ -325,9 +343,45 @@ ValidateAndMoveFiles(
         StrCat(NewFileInfo->FileName, Node->FileName);\r
       }\r
       NewFileInfo->Size = sizeof(EFI_FILE_INFO) + StrSize(NewFileInfo->FileName);\r
-\r
       ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
 \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
       //\r
       // Perform the move operation\r
       //\r
@@ -336,7 +390,6 @@ ValidateAndMoveFiles(
       //\r
       // Free the info object we used...\r
       //\r
-      ASSERT  (NewFileInfo != NULL);\r
       FreePool(NewFileInfo);\r
 \r
       //\r
@@ -373,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
@@ -387,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
@@ -444,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
@@ -465,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
@@ -491,6 +552,8 @@ ShellCommandRunMv (
     ShellCommandLineFreeVarList (Package);\r
   }\r
 \r
+  SHELL_FREE_NON_NULL(Response);\r
+\r
   if (ShellGetExecutionBreakFlag()) {\r
     return (SHELL_ABORTED);\r
   }\r