]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Remove redundant quotes in file path string for Shell command parameters.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
index 3a0068393929e2d48ab1e6b0aad85b6dd3d666b0..52c1de8a96644f10fc2b17d3a33f0c490096f183 100644 (file)
@@ -295,11 +295,13 @@ ValidateAndCopyFiles(
   CHAR16                    *HiiResultOk;\r
   CONST EFI_SHELL_FILE_INFO *Node;\r
   SHELL_STATUS              ShellStatus;\r
+  EFI_STATUS                Status;\r
   CHAR16                    *DestPath;\r
   VOID                      *Response;\r
   UINTN                     PathSize;\r
   CONST CHAR16              *Cwd;\r
   UINTN                     NewSize;\r
+  CHAR16                    *CleanFilePathStr;\r
 \r
   if (Resp == NULL) {\r
     Response = NULL;\r
@@ -315,14 +317,25 @@ ValidateAndCopyFiles(
   ASSERT(FileList != NULL);\r
   ASSERT(DestDir  != NULL);\r
 \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
   // If we are trying to copy multiple files... make sure we got a directory for the target...\r
   //\r
-  if (EFI_ERROR(ShellIsDirectory(DestDir)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
+  if (EFI_ERROR(ShellIsDirectory(CleanFilePathStr)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
     //\r
     // Error for destination not a directory\r
     //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, CleanFilePathStr);\r
+    FreePool (CleanFilePathStr);\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
   for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
@@ -336,7 +349,7 @@ ValidateAndCopyFiles(
       continue;\r
     }\r
 \r
-    NewSize =  StrSize(DestDir);\r
+    NewSize =  StrSize(CleanFilePathStr);\r
     NewSize += StrSize(Node->FullName);\r
     NewSize += (Cwd == NULL)? 0 : StrSize(Cwd);\r
     if (NewSize > PathSize) {\r
@@ -348,17 +361,19 @@ ValidateAndCopyFiles(
     //\r
     if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle);\r
+      FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
 \r
     //\r
     // make sure got dest as dir if needed\r
     //\r
-    if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(DestDir))) {\r
+    if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(CleanFilePathStr))) {\r
       //\r
       // Error for destination not a directory\r
       //\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, CleanFilePathStr);\r
+      FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
   }\r
@@ -371,6 +386,7 @@ ValidateAndCopyFiles(
     SHELL_FREE_NON_NULL(DestPath);\r
     SHELL_FREE_NON_NULL(HiiOutput);\r
     SHELL_FREE_NON_NULL(HiiResultOk);\r
+    FreePool (CleanFilePathStr);\r
     return (SHELL_OUT_OF_RESOURCES);\r
   }\r
 \r
@@ -395,26 +411,27 @@ ValidateAndCopyFiles(
     }\r
 \r
     if (FileList->Link.ForwardLink == FileList->Link.BackLink // 1 item\r
-      && EFI_ERROR(ShellIsDirectory(DestDir))                 // not an existing directory\r
+      && EFI_ERROR(ShellIsDirectory(CleanFilePathStr))                 // not an existing directory\r
       ) {\r
-      if (StrStr(DestDir, L":") == NULL) {\r
+      if (StrStr(CleanFilePathStr, L":") == NULL) {\r
         //\r
         // simple copy of a single file\r
         //\r
         if (Cwd != NULL) {\r
           StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16)-1);\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
+          FreePool (CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
-        if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
+        if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
           StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
+        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrnCat(DestPath, DestDir, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
       } else {\r
-        StrnCpy(DestPath, DestDir, PathSize/sizeof(CHAR16) -1);\r
+        StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
       }\r
     } else {\r
       //\r
@@ -424,50 +441,52 @@ ValidateAndCopyFiles(
       //\r
       // Check for leading slash\r
       //\r
-      if (DestDir[0] == L'\\') {\r
+      if (CleanFilePathStr[0] == L'\\') {\r
          //\r
          // Copy to the root of CWD\r
          //\r
         if (Cwd != NULL) {\r
           StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
+          FreePool(CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
         while (PathRemoveLastItem(DestPath));\r
-        StrnCat(DestPath, DestDir+1, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        StrnCat(DestPath, CleanFilePathStr+1, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
         StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-      } else if (StrStr(DestDir, L":") == NULL) {\r
+      } else if (StrStr(CleanFilePathStr, L":") == NULL) {\r
         if (Cwd != NULL) {\r
           StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
+          FreePool(CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
-        if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
+        if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
           StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
+        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrnCat(DestPath, DestDir, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
+        StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
           StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-        } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
+        } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
         StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
 \r
       } else {\r
-        StrnCpy(DestPath, DestDir, PathSize/sizeof(CHAR16) -1);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
+        StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
+        if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
           StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
-        } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
-          ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;\r
+        } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
+          ((CHAR16*)CleanFilePathStr)[StrLen(CleanFilePathStr)-1] = CHAR_NULL;\r
         }\r
         StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
       }\r
     }\r
-\r
+    FreePool (CleanFilePathStr);\r
     //\r
     // Make sure the path exists\r
     //\r