]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg/MV: Fix MV to deny moving parent of current directory
authorChen A Chen <chen.a.chen@intel.com>
Tue, 22 Nov 2016 08:16:02 +0000 (16:16 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 5 Dec 2016 01:18:32 +0000 (09:18 +0800)
When user types "mv -r fs0:\A\ fs1:\" under directory
"fs0:\A\B\", MV command should deny such movement.

The patch fixes the above issue.
It also denies moving current directory.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c

index efaaeb2d7b3e665f6aef94c140e1fb934eb112d9..71e43367c64d026f9dc4abac41f64b35b79aa9f9 100644 (file)
@@ -57,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
@@ -90,12 +157,14 @@ 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