]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index d02a6ae5f5c0560c7bf987b46f26be12bb8a9534..43cd20e1bf6b8f54f3c59f171fac6bdc1e203f69 100644 (file)
@@ -2,7 +2,7 @@
   Main file for mv shell level 2 function.\r
 \r
   (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2018, 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
@@ -17,7 +17,7 @@
 \r
 /**\r
   function to determine if a move is between file systems.\r
-  \r
+\r
   @param FullName [in]    The name of the file to move.\r
   @param Cwd      [in]    The current working directory\r
   @param DestPath [in]    The target location to move to\r
@@ -26,7 +26,6 @@
   @retval FALSE           The move is within a file system.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 IsBetweenFileSystem(\r
   IN CONST CHAR16     *FullName,\r
   IN CONST CHAR16     *Cwd,\r
@@ -58,6 +57,73 @@ IsBetweenFileSystem(
   return (FALSE);\r
 }\r
 \r
+/**\r
+  function to determine if SrcPath is valid to mv.\r
+\r
+  if SrcPath equal CWD then it's invalid.\r
+  if SrcPath is the parent path of CWD then it's invalid.\r
+  is SrcPath is NULL return FALSE.\r
+\r
+  if CwdPath is NULL then ASSERT()\r
+\r
+  @param SrcPath  [in]    The source path.\r
+  @param CwdPath  [in]    The current working directory.\r
+\r
+  @retval TRUE            The source path is valid.\r
+  @retval FALSE           The source path is invalid.\r
+**/\r
+BOOLEAN\r
+IsSoucePathValid(\r
+  IN CONST CHAR16*  SrcPath,\r
+  IN CONST CHAR16*  CwdPath\r
+  )\r
+{\r
+  CHAR16* SrcPathBuffer;\r
+  CHAR16* CwdPathBuffer;\r
+  BOOLEAN Ret;\r
+\r
+  ASSERT (CwdPath != NULL);\r
+  if (SrcPath == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  Ret = TRUE;\r
+\r
+  SrcPathBuffer = AllocateCopyPool (StrSize (SrcPath), SrcPath);\r
+  if (SrcPathBuffer == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  CwdPathBuffer = AllocateCopyPool (StrSize (CwdPath), CwdPath);\r
+  if (CwdPathBuffer == NULL) {\r
+    FreePool(SrcPathBuffer);\r
+    return FALSE;\r
+  }\r
+\r
+  gUnicodeCollation->StrUpr (gUnicodeCollation, SrcPathBuffer);\r
+  gUnicodeCollation->StrUpr (gUnicodeCollation, CwdPathBuffer);\r
+\r
+  if (SrcPathBuffer[StrLen (SrcPathBuffer) -1 ] == L'\\') {\r
+    SrcPathBuffer[StrLen (SrcPathBuffer) - 1] = CHAR_NULL;\r
+  }\r
+\r
+  if (CwdPathBuffer[StrLen (CwdPathBuffer) - 1] == L'\\') {\r
+    CwdPathBuffer[StrLen (CwdPathBuffer) - 1] = CHAR_NULL;\r
+  }\r
+\r
+  if (StrCmp (CwdPathBuffer, SrcPathBuffer) == 0 ||\r
+      ((StrStr (CwdPathBuffer, SrcPathBuffer) == CwdPathBuffer) &&\r
+       (CwdPathBuffer[StrLen (SrcPathBuffer)] == L'\\'))\r
+     ) {\r
+    Ret = FALSE;\r
+  }\r
+\r
+  FreePool (SrcPathBuffer);\r
+  FreePool (CwdPathBuffer);\r
+\r
+  return Ret;\r
+}\r
+\r
 /**\r
   Function to validate that moving a specific file (FileName) to a specific\r
   location (DestPath) is valid.\r
@@ -79,7 +145,6 @@ IsBetweenFileSystem(
   @retval FALSE       The move is not\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 IsValidMove(\r
   IN CONST CHAR16     *SourcePath,\r
   IN CONST CHAR16     *Cwd,\r
@@ -92,25 +157,27 @@ IsValidMove(
   CHAR16  *DestPathCopy;\r
   CHAR16  *DestPathWalker;\r
 \r
-  if (Cwd != NULL && StrCmp(SourcePath, Cwd) == 0) {\r
-    //\r
-    // Invalid move\r
-    //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_CWD), gShellLevel2HiiHandle);\r
-    return (FALSE);\r
+  if ((Cwd != NULL) && ((Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY)) {\r
+    if (!IsSoucePathValid (SourcePath, Cwd)) {\r
+      //\r
+      // Invalid move\r
+      //\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_MV_INV_CWD), gShellLevel2HiiHandle);\r
+      return FALSE;\r
+    }\r
   }\r
 \r
   //\r
   // invalid to move read only or move to a read only destination\r
   //\r
-  if (((Attribute & EFI_FILE_READ_ONLY) != 0) \r
+  if (((Attribute & EFI_FILE_READ_ONLY) != 0)\r
     || (FileStatus == EFI_WRITE_PROTECTED)\r
     || ((DestAttr & EFI_FILE_READ_ONLY) != 0)\r
     ) {\r
     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle, SourcePath);\r
     return (FALSE);\r
-  }  \r
-  \r
+  }\r
+\r
   DestPathCopy = AllocateCopyPool(StrSize(DestPath), DestPath);\r
   if (DestPathCopy == NULL) {\r
     return (FALSE);\r
@@ -129,7 +196,7 @@ IsValidMove(
   // If they're the same, or if source is "above" dest on file path tree\r
   //\r
   if ( StringNoCaseCompare (&DestPathWalker, &SourcePath) == 0 ||\r
-       ((StrStr(DestPathWalker, SourcePath) == DestPathWalker) && \r
+       ((StrStr(DestPathWalker, SourcePath) == DestPathWalker) &&\r
         (DestPathWalker[StrLen(SourcePath)] == '\\')\r
        )\r
      ) {\r
@@ -161,7 +228,6 @@ IsValidMove(
   @retval SHELL_SUCCESS            The operation was sucessful.\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 GetDestinationLocation(\r
   IN CONST CHAR16               *DestParameter,\r
   IN OUT CHAR16                 **DestPathPointer,\r
@@ -245,7 +311,7 @@ GetDestinationLocation(
     //\r
     if (!IsNodeAtEnd(&DestList->Link, &Node->Link)) {\r
       ShellCloseFileMetaArg(&DestList);\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
 \r
@@ -265,7 +331,7 @@ GetDestinationLocation(
       // cant move multiple files onto a single file.\r
       //\r
       ShellCloseFileMetaArg(&DestList);\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
   }\r
@@ -286,7 +352,6 @@ GetDestinationLocation(
   @retval SHELL_SUCCESS     The source file was moved to the destination.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MoveBetweenFileSystems(\r
   IN EFI_SHELL_FILE_INFO  *Node,\r
   IN CONST CHAR16         *DestPath,\r
@@ -334,10 +399,9 @@ MoveBetweenFileSystems(
   @retval SHELL_OUT_OF_RESOURCES    a memory allocation failed\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 CreateFullDestPath(\r
   IN CONST CHAR16 **DestPath,\r
-  OUT CHAR16      **FullDestPath, \r
+  OUT CHAR16      **FullDestPath,\r
   IN CONST CHAR16 *FileName\r
   )\r
 {\r
@@ -373,7 +437,6 @@ CreateFullDestPath(
   @retval SHELL_OUT_OF_RESOURCES  A memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 MoveWithinFileSystems(\r
   IN EFI_SHELL_FILE_INFO  *Node,\r
   IN CHAR16               *DestPath,\r
@@ -454,7 +517,6 @@ MoveWithinFileSystems(
   @retval SHELL_OUT_OF_RESOURCES    a memory allocation failed\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 ValidateAndMoveFiles(\r
   IN EFI_SHELL_FILE_INFO        *FileList,\r
   OUT VOID                      **Resp,\r
@@ -493,7 +555,7 @@ ValidateAndMoveFiles(
       StrCpyS(FullCwd, StrSize(Cwd)/sizeof(CHAR16)+1, Cwd);\r
       StrCatS(FullCwd, StrSize(Cwd)/sizeof(CHAR16)+1, L"\\");\r
     }\r
-  } \r
+  }\r
 \r
   Status = ShellLevel2StripQuotes (DestParameter, &CleanFilePathStr);\r
   if (EFI_ERROR (Status)) {\r
@@ -695,7 +757,7 @@ ShellCommandRunMv (
   Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
   if (EFI_ERROR(Status)) {\r
     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"mv", ProblemParam);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"mv", ProblemParam);\r
       FreePool(ProblemParam);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
@@ -715,7 +777,7 @@ ShellCommandRunMv (
         //\r
         // we have insufficient parameters\r
         //\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mv");  \r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mv");\r
         ShellStatus = SHELL_INVALID_PARAMETER;\r
         break;\r
       case 2:\r
@@ -723,12 +785,12 @@ ShellCommandRunMv (
         // must have valid CWD for single parameter...\r
         //\r
         if (ShellGetCurrentDir(NULL) == NULL){\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"mv");  \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"mv");\r
           ShellStatus = SHELL_INVALID_PARAMETER;\r
         } else {\r
           Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), 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, L"mv", ShellCommandLineGetRawValue(Package, 1));  \r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, 1));\r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else  {\r
             //\r
@@ -736,11 +798,15 @@ ShellCommandRunMv (
             //\r
             CwdSize = StrSize(ShellGetCurrentDir(NULL)) + sizeof(CHAR16);\r
             Cwd = AllocateZeroPool(CwdSize);\r
-            ASSERT (Cwd != NULL);\r
-            StrCpyS(Cwd, CwdSize/sizeof(CHAR16), ShellGetCurrentDir(NULL));\r
-            StrCatS(Cwd, CwdSize/sizeof(CHAR16), L"\\");\r
-            ShellStatus = ValidateAndMoveFiles(FileList, &Response, Cwd);\r
-            FreePool(Cwd);\r
+            if (Cwd == NULL) {\r
+              ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"mv");\r
+              ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+            } else {\r
+              StrCpyS (Cwd, CwdSize / sizeof (CHAR16), ShellGetCurrentDir (NULL));\r
+              StrCatS (Cwd, CwdSize / sizeof (CHAR16), L"\\");\r
+              ShellStatus = ValidateAndMoveFiles (FileList, &Response, Cwd);\r
+              FreePool (Cwd);\r
+            }\r
           }\r
         }\r
 \r
@@ -753,7 +819,7 @@ ShellCommandRunMv (
           }\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, L"mv", ShellCommandLineGetRawValue(Package, LoopCounter));  \r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, LoopCounter));\r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else  {\r
             //\r
@@ -770,7 +836,7 @@ ShellCommandRunMv (
             Status = ShellCloseFileMetaArg(&FileList);\r
             if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
               ShellStatus = SHELL_ACCESS_DENIED;\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, 1), ShellStatus|MAX_BIT);  \r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, 1), ShellStatus|MAX_BIT);\r
             }\r
           }\r
         }\r