]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Fix timezone command
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
index ae36807958ffafecbf6f9b3abffa67a286e9b6a5..a576f834464f118b409886e3539b6ae72f5bb4b6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Main file for cp shell level 2 function.\r
 \r
-  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\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
@@ -131,6 +131,7 @@ CopySingleFile(
   if (ShellIsDirectory(Source) == EFI_SUCCESS) {\r
     Status = ShellCreateDirectory(Dest, &DestHandle);\r
     if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_DIR_FAIL), gShellLevel2HiiHandle, Dest);\r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -159,6 +160,7 @@ CopySingleFile(
     //\r
     Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
     if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL), gShellLevel2HiiHandle, Dest);\r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -224,12 +226,23 @@ CopySingleFile(
       ASSERT(Buffer != NULL);\r
       while (ReadSize == PcdGet32(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
         Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);\r
-        Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
+        if (!EFI_ERROR(Status)) {\r
+          Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
+          if (EFI_ERROR(Status)) {\r
+            ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR), gShellLevel2HiiHandle, Dest);\r
+            break;\r
+          }\r
+        } else {\r
+          ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_READ_ERROR), gShellLevel2HiiHandle, Source);\r
+          break;\r
+        }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(DestVolumeInfo);\r
   }\r
-\r
+  \r
   //\r
   // close files\r
   //\r
@@ -282,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                     PathLen;\r
+  UINTN                     PathSize;\r
   CONST CHAR16              *Cwd;\r
   UINTN                     NewSize;\r
+  CHAR16                    *CleanFilePathStr;\r
 \r
   if (Resp == NULL) {\r
     Response = NULL;\r
@@ -294,22 +309,36 @@ ValidateAndCopyFiles(
     Response = *Resp;\r
   }\r
 \r
-  DestPath    = NULL;\r
-  ShellStatus = SHELL_SUCCESS;\r
-  PathLen     = 0;\r
-  Cwd         = ShellGetCurrentDir(NULL);\r
+  DestPath         = NULL;\r
+  ShellStatus      = SHELL_SUCCESS;\r
+  PathSize         = 0;\r
+  Cwd              = ShellGetCurrentDir(NULL);\r
+  CleanFilePathStr = NULL;\r
 \r
   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
+  ASSERT (CleanFilePathStr != NULL);\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
@@ -323,11 +352,11 @@ 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 > PathLen) {\r
-      PathLen = NewSize;\r
+    if (NewSize > PathSize) {\r
+      PathSize = NewSize;\r
     }\r
 \r
     //\r
@@ -335,29 +364,32 @@ 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
 \r
   HiiOutput   = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);\r
   HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
-  DestPath    = AllocateZeroPool(PathLen);\r
+  DestPath    = AllocateZeroPool(PathSize);\r
 \r
   if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {\r
     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
@@ -382,26 +414,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
-          StrCpy(DestPath, Cwd);\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
-          StrCat(DestPath, L"\\");\r
-        } else 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'\\' && CleanFilePathStr[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, DestDir);\r
+        StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
       } else {\r
-        StrCpy(DestPath, DestDir);\r
+        StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
       }\r
     } else {\r
       //\r
@@ -411,50 +444,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
-          StrCpy(DestPath, Cwd);\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
-        StrCat(DestPath, DestDir+1);\r
-        StrCat(DestPath, Node->FileName);\r
-      } else if (StrStr(DestDir, L":") == NULL) {\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(CleanFilePathStr, L":") == NULL) {\r
         if (Cwd != NULL) {\r
-          StrCpy(DestPath, Cwd);\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
-          StrCat(DestPath, L"\\");\r
-        } else 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'\\' && CleanFilePathStr[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, DestDir);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
-          StrCat(DestPath, L"\\");\r
-        } else 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 (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, Node->FileName);\r
+        StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
 \r
       } else {\r
-        StrCpy(DestPath, DestDir);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
-          StrCat(DestPath, L"\\");\r
-        } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
-          ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;\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 (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
+          ((CHAR16*)CleanFilePathStr)[StrLen(CleanFilePathStr)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, Node->FileName);\r
+        StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
       }\r
     }\r
-\r
+    \r
     //\r
     // Make sure the path exists\r
     //\r
@@ -488,7 +523,9 @@ ValidateAndCopyFiles(
 \r
     PathCleanUpDirectories(DestPath);\r
 \r
-    ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
+    if (!SilentMode) {\r
+      ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
+    }\r
 \r
     //\r
     // copy single file...\r
@@ -505,6 +542,7 @@ ValidateAndCopyFiles(
   SHELL_FREE_NON_NULL(DestPath);\r
   SHELL_FREE_NON_NULL(HiiOutput);\r
   SHELL_FREE_NON_NULL(HiiResultOk);\r
+  SHELL_FREE_NON_NULL(CleanFilePathStr);\r
   if (Resp == NULL) {\r
     SHELL_FREE_NON_NULL(Response);\r
   }\r