]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
index 5cfbc9064f8318670fe7c16807ccb65c15577769..1c488246641e1cccfc88bb0f739341c356915522 100644 (file)
@@ -2,7 +2,7 @@
   Main file for cp shell level 2 function.\r
 \r
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2015, 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
@@ -35,7 +35,6 @@
   @retval SHELL_OUT_OF_RESOURCES    a memory allocation failed\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 ValidateAndCopyFiles(\r
   IN CONST EFI_SHELL_FILE_INFO  *FileList,\r
   IN CONST CHAR16               *DestDir,\r
@@ -58,7 +57,6 @@ ValidateAndCopyFiles(
   @retval SHELL_SUCCESS   The source file was copied to the destination\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 CopySingleFile(\r
   IN CONST CHAR16 *Source,\r
   IN CONST CHAR16 *Dest,\r
@@ -134,7 +132,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, CmdName, Dest);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_DIR_FAIL), gShellLevel2HiiHandle, CmdName, Dest);\r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -163,7 +161,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, CmdName, Dest);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL), gShellLevel2HiiHandle, CmdName, Dest);\r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -222,33 +220,36 @@ CopySingleFile(
       //not enough space on destination directory to copy file\r
       //\r
       SHELL_FREE_NON_NULL(DestVolumeInfo);\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle, CmdName);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle, CmdName);\r
       return(SHELL_VOLUME_FULL);\r
     } else {\r
       //\r
       // copy data between files\r
       //\r
       Buffer = AllocateZeroPool(ReadSize);\r
-      ASSERT(Buffer != NULL);\r
+      if (Buffer == NULL) {\r
+        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, CmdName);\r
+        return SHELL_OUT_OF_RESOURCES;\r
+      }\r
       while (ReadSize == PcdGet32(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
         Status = ShellReadFile(SourceHandle, &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, CmdName, Dest);   \r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR), gShellLevel2HiiHandle, CmdName, 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, CmdName, Source);  \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_READ_ERROR), gShellLevel2HiiHandle, CmdName, Source);\r
           break;\r
         }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(DestVolumeInfo);\r
   }\r
-  \r
+\r
   //\r
   // close files\r
   //\r
@@ -288,7 +289,6 @@ CopySingleFile(
   @retval SHELL_OUT_OF_RESOURCES    a memory allocation failed\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 ValidateAndCopyFiles(\r
   IN CONST EFI_SHELL_FILE_INFO  *FileList,\r
   IN CONST CHAR16               *DestDir,\r
@@ -324,7 +324,7 @@ ValidateAndCopyFiles(
   ASSERT(FileList != NULL);\r
   ASSERT(DestDir  != NULL);\r
 \r
-  \r
+\r
   Status = ShellLevel2StripQuotes (DestDir, &CleanFilePathStr);\r
   if (EFI_ERROR (Status)) {\r
     if (Status == EFI_OUT_OF_RESOURCES) {\r
@@ -333,7 +333,7 @@ ValidateAndCopyFiles(
       return SHELL_INVALID_PARAMETER;\r
     }\r
   }\r
-  \r
+\r
   ASSERT (CleanFilePathStr != NULL);\r
 \r
   //\r
@@ -343,7 +343,7 @@ ValidateAndCopyFiles(
     //\r
     // Error for destination not a directory\r
     //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr); \r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);\r
     FreePool (CleanFilePathStr);\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
@@ -369,7 +369,7 @@ ValidateAndCopyFiles(
     // Make sure got -r if required\r
     //\r
     if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle, L"cp");  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle, L"cp");\r
       FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
@@ -381,7 +381,7 @@ ValidateAndCopyFiles(
       //\r
       // Error for destination not a directory\r
       //\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);\r
       FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
@@ -430,7 +430,7 @@ ValidateAndCopyFiles(
           StrCpyS(DestPath, PathSize / sizeof(CHAR16), Cwd);\r
           StrCatS(DestPath, PathSize / sizeof(CHAR16), L"\\");\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);\r
           FreePool (CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
@@ -447,7 +447,7 @@ ValidateAndCopyFiles(
       //\r
       // we have multiple files or a directory in the DestDir\r
       //\r
-      \r
+\r
       //\r
       // Check for leading slash\r
       //\r
@@ -459,7 +459,7 @@ ValidateAndCopyFiles(
           StrCpyS(DestPath, PathSize/sizeof(CHAR16), Cwd);\r
           StrCatS(DestPath, PathSize/sizeof(CHAR16), L"\\");\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp",  CleanFilePathStr); \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp",  CleanFilePathStr);\r
           FreePool(CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
@@ -471,7 +471,7 @@ ValidateAndCopyFiles(
           StrCpyS(DestPath, PathSize/sizeof(CHAR16), Cwd);\r
           StrCatS(DestPath, PathSize/sizeof(CHAR16), L"\\");\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);\r
           FreePool(CleanFilePathStr);\r
           return (SHELL_INVALID_PARAMETER);\r
         }\r
@@ -498,26 +498,26 @@ ValidateAndCopyFiles(
         StrCatS(DestPath, PathSize/sizeof(CHAR16), Node->FileName);\r
       }\r
     }\r
-    \r
+\r
     //\r
     // Make sure the path exists\r
     //\r
     if (EFI_ERROR(VerifyIntermediateDirectories(DestPath))) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle, L"cp", DestPath);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle, L"cp", DestPath);\r
       ShellStatus = SHELL_DEVICE_ERROR;\r
       break;\r
     }\r
 \r
     if ( !EFI_ERROR(ShellIsDirectory(Node->FullName))\r
       && !EFI_ERROR(ShellIsDirectory(DestPath))\r
-      && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == NULL\r
+      && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == 0\r
       ){\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle, L"cp");  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle, L"cp");\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
     if (StringNoCaseCompare(&Node->FullName, &DestPath) == 0) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
@@ -525,7 +525,7 @@ ValidateAndCopyFiles(
     if ((StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName)) == 0)\r
       && (DestPath[StrLen(Node->FullName)] == CHAR_NULL || DestPath[StrLen(Node->FullName)] == L'\\')\r
       ) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
@@ -561,7 +561,7 @@ ValidateAndCopyFiles(
 }\r
 \r
 /**\r
-  Validate and if successful copy all the files from the list into \r
+  Validate and if successful copy all the files from the list into\r
   destination directory.\r
 \r
   @param[in] FileList       The list of files to copy.\r
@@ -573,7 +573,6 @@ ValidateAndCopyFiles(
   @retval SHELL_SUCCESS             The operation was successful.\r
 **/\r
 SHELL_STATUS\r
-EFIAPI\r
 ProcessValidateAndCopyFiles(\r
   IN       EFI_SHELL_FILE_INFO  *FileList,\r
   IN CONST CHAR16               *DestDir,\r
@@ -592,7 +591,7 @@ ProcessValidateAndCopyFiles(
 \r
   ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
   if (List != NULL && List->Link.ForwardLink != List->Link.BackLink) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"cp", DestDir);  \r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"cp", DestDir);\r
     ShellStatus = SHELL_INVALID_PARAMETER;\r
     ShellCloseFileMetaArg(&List);\r
   } else if (List != NULL) {\r
@@ -605,7 +604,7 @@ ProcessValidateAndCopyFiles(
     if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) == 0) {\r
       ShellStatus = ValidateAndCopyFiles(FileList, FullName, SilentMode, RecursiveMode, NULL);\r
     } else {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle, L"cp");  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle, L"cp");\r
       ShellStatus = SHELL_ACCESS_DENIED;\r
     }\r
   } else {\r
@@ -669,7 +668,7 @@ ShellCommandRunCp (
   Status = ShellCommandLineParse (ParamList, &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"cp", ProblemParam);  \r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cp", ProblemParam);\r
       FreePool(ProblemParam);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
@@ -699,7 +698,7 @@ ShellCommandRunCp (
         //\r
         // we have insufficient parameters\r
         //\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"cp");  \r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"cp");\r
         ShellStatus = SHELL_INVALID_PARAMETER;\r
         break;\r
       case 2:\r
@@ -708,19 +707,23 @@ ShellCommandRunCp (
         //\r
         Cwd = ShellGetCurrentDir(NULL);\r
         if (Cwd == NULL){\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cp");  \r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cp");\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"cp", ShellCommandLineGetRawValue(Package, 1));  \r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, 1));\r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else  {\r
             FullCwd = AllocateZeroPool(StrSize(Cwd) + sizeof(CHAR16));\r
-            ASSERT (FullCwd != NULL);\r
-            StrCpyS(FullCwd, StrSize(Cwd)/sizeof(CHAR16)+1, Cwd);\r
-            ShellStatus = ProcessValidateAndCopyFiles(FileList, FullCwd, SilentMode, RecursiveMode);\r
-            FreePool(FullCwd);\r
+            if (FullCwd == NULL) {\r
+              ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"cp");\r
+              ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+            } else {\r
+              StrCpyS (FullCwd, StrSize (Cwd) / sizeof (CHAR16) + 1, Cwd);\r
+              ShellStatus = ProcessValidateAndCopyFiles (FileList, FullCwd, SilentMode, RecursiveMode);\r
+              FreePool (FullCwd);\r
+            }\r
           }\r
         }\r
 \r
@@ -735,7 +738,7 @@ ShellCommandRunCp (
           }\r
           Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
           if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, LoopCounter));  \r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, LoopCounter));\r
             ShellStatus = SHELL_NOT_FOUND;\r
           }\r
         }\r
@@ -749,7 +752,7 @@ ShellCommandRunCp (
             ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
             Status = ShellCloseFileMetaArg(&FileList);\r
             if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);  \r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);\r
               ShellStatus = SHELL_ACCESS_DENIED;\r
             }\r
           }\r