]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Refactor string manipulation in cp command
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
index a93ef33c039c02dd602add040a5ac28855b8ee59..f9b83e6fa4565db8e3fc54dc52942c2bb4315d54 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for mv shell level 2 function.\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 2014, 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
@@ -28,6 +29,7 @@
   @param Cwd      [in]    The current working directory\r
   @param DestPath [in]    The target location to move to\r
   @param Attribute[in]    The Attribute of the file\r
+  @param FileStatus[in]   The Status of the file when opened\r
 \r
   @retval TRUE        The move is valid\r
   @retval FALSE       The move is not\r
 BOOLEAN\r
 EFIAPI\r
 IsValidMove(\r
-  IN CONST CHAR16   *FullName,\r
-  IN CONST CHAR16   *Cwd,\r
-  IN CONST CHAR16   *DestPath,\r
-  IN CONST UINT64   Attribute\r
+  IN CONST CHAR16     *FullName,\r
+  IN CONST CHAR16     *Cwd,\r
+  IN CONST CHAR16     *DestPath,\r
+  IN CONST UINT64     Attribute,\r
+  IN CONST EFI_STATUS FileStatus\r
   )\r
 {\r
   CHAR16  *Test;\r
@@ -86,11 +89,11 @@ IsValidMove(
     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_SUB), gShellLevel2HiiHandle);\r
     return (FALSE);\r
   }\r
-  if ((Attribute & EFI_FILE_READ_ONLY) != 0) {\r
+  if (((Attribute & EFI_FILE_READ_ONLY) != 0) || (FileStatus == EFI_WRITE_PROTECTED)) {\r
     //\r
     // invalid to move read only\r
     //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle, FullName);\r
     return (FALSE);\r
   }\r
   Test  = StrStr(FullName, L":");\r
@@ -135,10 +138,9 @@ GetDestinationLocation(
 {\r
   EFI_SHELL_FILE_INFO       *DestList;\r
   EFI_SHELL_FILE_INFO       *Node;\r
-  EFI_STATUS                Status;\r
   CHAR16                    *DestPath;\r
-  CHAR16                    *TempLocation;\r
   UINTN                     NewSize;\r
+  UINTN                     CurrentSize;\r
 \r
   DestList = NULL;\r
   DestPath = NULL;\r
@@ -153,18 +155,25 @@ GetDestinationLocation(
     }\r
     StrCpy(DestPath, Cwd);\r
     while (PathRemoveLastItem(DestPath)) ;\r
+\r
+    //\r
+    // Append DestDir beyond '\' which may be present\r
+    //\r
+    CurrentSize = StrSize(DestPath);\r
+    StrnCatGrow(&DestPath, &CurrentSize, &DestDir[1], 0);\r
+\r
     *DestPathPointer =  DestPath;\r
     return (SHELL_SUCCESS);\r
   }\r
   //\r
   // get the destination path\r
   //\r
-  Status = ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);\r
+  ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);\r
   if (DestList == NULL || IsListEmpty(&DestList->Link)) {\r
     //\r
     // Not existing... must be renaming\r
     //\r
-    if ((TempLocation = StrStr(DestDir, L":")) == NULL) {\r
+    if (StrStr(DestDir, L":") == NULL) {\r
       if (Cwd == NULL) {\r
         ShellCloseFileMetaArg(&DestList);\r
         return (SHELL_INVALID_PARAMETER);\r
@@ -283,7 +292,6 @@ ValidateAndMoveFiles(
   ASSERT  (DestPath     != NULL);\r
   ASSERT  (HiiResultOk  != NULL);\r
   ASSERT  (HiiOutput    != NULL);\r
-//  ASSERT  (Cwd          != NULL);\r
 \r
   //\r
   // Go through the list of files and directories to move...\r
@@ -295,8 +303,13 @@ ValidateAndMoveFiles(
     if (ShellGetExecutionBreakFlag()) {\r
       break;\r
     }\r
+\r
+    //\r
+    // These should never be NULL\r
+    //\r
     ASSERT(Node->FileName != NULL);\r
     ASSERT(Node->FullName != NULL);\r
+    ASSERT(Node->Info     != NULL);\r
 \r
     //\r
     // skip the directory traversing stuff...\r
@@ -308,7 +321,7 @@ ValidateAndMoveFiles(
     //\r
     // Validate that the move is valid\r
     //\r
-    if (!IsValidMove(Node->FullName, Cwd, DestPath, Node->Info->Attribute)) {\r
+    if (!IsValidMove(Node->FullName, Cwd, DestPath, Node->Info->Attribute, Node->Status)) {\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       continue;\r
     }\r
@@ -324,13 +337,13 @@ ValidateAndMoveFiles(
     // construct the new file info block\r
     //\r
     NewSize = StrSize(DestPath);\r
-    NewSize += StrSize(Node->FileName) + sizeof(EFI_FILE_INFO) + sizeof(CHAR16);\r
+    NewSize += StrSize(Node->FileName) + SIZE_OF_EFI_FILE_INFO + sizeof(CHAR16);\r
     NewFileInfo = AllocateZeroPool(NewSize);\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
-      CopyMem(NewFileInfo, Node->Info, sizeof(EFI_FILE_INFO));\r
+      CopyMem(NewFileInfo, Node->Info, SIZE_OF_EFI_FILE_INFO);\r
       if (DestPath[0] != L'\\') {\r
         StrCpy(NewFileInfo->FileName, L"\\");\r
         StrCat(NewFileInfo->FileName, DestPath);\r
@@ -350,7 +363,7 @@ ValidateAndMoveFiles(
         }\r
         StrCat(NewFileInfo->FileName, Node->FileName);\r
       }\r
-      NewFileInfo->Size = sizeof(EFI_FILE_INFO) + StrSize(NewFileInfo->FileName);\r
+      NewFileInfo->Size = SIZE_OF_EFI_FILE_INFO + StrSize(NewFileInfo->FileName);\r
       ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
 \r
       if (!EFI_ERROR(ShellFileExists(NewFileInfo->FileName))) {\r
@@ -405,23 +418,18 @@ ValidateAndMoveFiles(
       //\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
+        ShellStatus = SHELL_INVALID_PARAMETER;\r
+        if (Status == EFI_SECURITY_VIOLATION) {\r
+          ShellStatus = SHELL_SECURITY_VIOLATION;\r
+        } else if (Status == EFI_WRITE_PROTECTED) {\r
+          ShellStatus = SHELL_WRITE_PROTECTED;\r
+        } else if (Status == EFI_OUT_OF_RESOURCES) {\r
+          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+        } else if (Status == EFI_DEVICE_ERROR) {\r
+          ShellStatus = SHELL_DEVICE_ERROR;\r
+        } else if (Status == EFI_ACCESS_DENIED) {\r
+          ShellStatus = SHELL_ACCESS_DENIED;\r
+        }\r
       } else {\r
         ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
       }\r