2 Main file for cp shell level 2 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellLevel2CommandsLib.h"
17 #include <Guid/FileSystemInfo.h>
18 #include <Guid/FileSystemVolumeLabelInfo.h>
21 Function to take a list of files to copy and a destination location and do
22 the verification and copying of those files to that location. This function
23 will report any errors to the user and halt.
25 @param[in] FileList A LIST_ENTRY* based list of files to move.
26 @param[in] DestDir The destination location.
27 @param[in] SilentMode TRUE to eliminate screen output.
28 @param[in] RecursiveMode TRUE to copy directories.
29 @param[in] Resp The response to the overwrite query (if always).
31 @retval SHELL_SUCCESS the files were all moved.
32 @retval SHELL_INVALID_PARAMETER a parameter was invalid
33 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
34 @retval SHELL_WRITE_PROTECTED the destination was write protected
35 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
40 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
41 IN CONST CHAR16
*DestDir
,
42 IN BOOLEAN SilentMode
,
43 IN BOOLEAN RecursiveMode
,
48 Function to Copy one file to another location
50 If the destination exists the user will be prompted and the result put into *resp
52 @param[in] Source pointer to source file name
53 @param[in] Dest pointer to destination file name
54 @param[out] Resp pointer to response from question. Pass back on looped calling
55 @param[in] SilentMode whether to run in quiet mode or not
56 @param[in] CmdName Source command name requesting single file copy
58 @retval SHELL_SUCCESS The source file was copied to the destination
63 IN CONST CHAR16
*Source
,
64 IN CONST CHAR16
*Dest
,
66 IN BOOLEAN SilentMode
,
67 IN CONST CHAR16
*CmdName
72 SHELL_FILE_HANDLE SourceHandle
;
73 SHELL_FILE_HANDLE DestHandle
;
78 EFI_SHELL_FILE_INFO
*List
;
79 SHELL_STATUS ShellStatus
;
80 UINT64 SourceFileSize
;
82 EFI_FILE_PROTOCOL
*DestVolumeFP
;
83 EFI_FILE_SYSTEM_INFO
*DestVolumeInfo
;
84 UINTN DestVolumeInfoSize
;
92 DestVolumeInfo
= NULL
;
93 ShellStatus
= SHELL_SUCCESS
;
95 ReadSize
= PcdGet32(PcdShellFileOperationSize
);
96 // Why bother copying a file to itself
97 if (StrCmp(Source
, Dest
) == 0) {
98 return (SHELL_SUCCESS
);
102 // if the destination file existed check response and possibly prompt user
104 if (ShellFileExists(Dest
) == EFI_SUCCESS
) {
105 if (Response
== NULL
&& !SilentMode
) {
106 Status
= ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel
, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR
), gShellLevel2HiiHandle
, &Response
);
109 // possibly return based on response
112 switch (*(SHELL_PROMPT_RESPONSE
*)Response
) {
113 case ShellPromptResponseNo
:
115 // return success here so we dont stop the process
117 return (SHELL_SUCCESS
);
118 case ShellPromptResponseCancel
:
121 // indicate to stop everything
123 return (SHELL_ABORTED
);
124 case ShellPromptResponseAll
:
126 case ShellPromptResponseYes
:
129 return SHELL_ABORTED
;
134 if (ShellIsDirectory(Source
) == EFI_SUCCESS
) {
135 Status
= ShellCreateDirectory(Dest
, &DestHandle
);
136 if (EFI_ERROR(Status
)) {
137 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_DIR_FAIL
), gShellLevel2HiiHandle
, CmdName
, Dest
);
138 return (SHELL_ACCESS_DENIED
);
142 // Now copy all the files under the directory...
146 StrnCatGrow(&TempName
, &Size
, Source
, 0);
147 StrnCatGrow(&TempName
, &Size
, L
"\\*", 0);
148 if (TempName
!= NULL
) {
149 ShellOpenFileMetaArg((CHAR16
*)TempName
, EFI_FILE_MODE_READ
, &List
);
150 *TempName
= CHAR_NULL
;
151 StrnCatGrow(&TempName
, &Size
, Dest
, 0);
152 StrnCatGrow(&TempName
, &Size
, L
"\\", 0);
153 ShellStatus
= ValidateAndCopyFiles(List
, TempName
, SilentMode
, TRUE
, Resp
);
154 ShellCloseFileMetaArg(&List
);
155 SHELL_FREE_NON_NULL(TempName
);
159 Status
= ShellDeleteFileByName(Dest
);
162 // open file with create enabled
164 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
165 if (EFI_ERROR(Status
)) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL
), gShellLevel2HiiHandle
, CmdName
, Dest
);
167 return (SHELL_ACCESS_DENIED
);
173 Status
= ShellOpenFileByName (Source
, &SourceHandle
, EFI_FILE_MODE_READ
, 0);
174 if (EFI_ERROR (Status
)) {
175 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_CP_SRC_OPEN_FAIL
), gShellLevel2HiiHandle
, CmdName
, Source
);
176 return (SHELL_ACCESS_DENIED
);
180 //get file size of source file and freespace available on destination volume
182 ShellGetFileSize(SourceHandle
, &SourceFileSize
);
183 ShellGetFileSize(DestHandle
, &DestFileSize
);
186 //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space
188 if(DestFileSize
< SourceFileSize
){
189 SourceFileSize
-= DestFileSize
;
195 //get the system volume info to check the free space
197 DestVolumeFP
= ConvertShellHandleToEfiFileProtocol(DestHandle
);
198 DestVolumeInfo
= NULL
;
199 DestVolumeInfoSize
= 0;
200 Status
= DestVolumeFP
->GetInfo(
202 &gEfiFileSystemInfoGuid
,
207 if (Status
== EFI_BUFFER_TOO_SMALL
) {
208 DestVolumeInfo
= AllocateZeroPool(DestVolumeInfoSize
);
209 Status
= DestVolumeFP
->GetInfo(
211 &gEfiFileSystemInfoGuid
,
218 //check if enough space available on destination drive to complete copy
220 if (DestVolumeInfo
!= NULL
&& (DestVolumeInfo
->FreeSpace
< SourceFileSize
)) {
222 //not enough space on destination directory to copy file
224 SHELL_FREE_NON_NULL(DestVolumeInfo
);
225 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_FAIL
), gShellLevel2HiiHandle
, CmdName
);
226 return(SHELL_VOLUME_FULL
);
229 // copy data between files
231 Buffer
= AllocateZeroPool(ReadSize
);
232 ASSERT(Buffer
!= NULL
);
233 while (ReadSize
== PcdGet32(PcdShellFileOperationSize
) && !EFI_ERROR(Status
)) {
234 Status
= ShellReadFile(SourceHandle
, &ReadSize
, Buffer
);
235 if (!EFI_ERROR(Status
)) {
236 Status
= ShellWriteFile(DestHandle
, &ReadSize
, Buffer
);
237 if (EFI_ERROR(Status
)) {
238 ShellStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
239 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR
), gShellLevel2HiiHandle
, CmdName
, Dest
);
243 ShellStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
244 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_READ_ERROR
), gShellLevel2HiiHandle
, CmdName
, Source
);
249 SHELL_FREE_NON_NULL(DestVolumeInfo
);
255 if (DestHandle
!= NULL
) {
256 ShellCloseFile(&DestHandle
);
259 if (SourceHandle
!= NULL
) {
260 ShellCloseFile(&SourceHandle
);
271 function to take a list of files to copy and a destination location and do
272 the verification and copying of those files to that location. This function
273 will report any errors to the user and halt.
275 The key is to have this function called ONLY once. this allows for the parameter
276 verification to happen correctly.
278 @param[in] FileList A LIST_ENTRY* based list of files to move.
279 @param[in] DestDir The destination location.
280 @param[in] SilentMode TRUE to eliminate screen output.
281 @param[in] RecursiveMode TRUE to copy directories.
282 @param[in] Resp The response to the overwrite query (if always).
284 @retval SHELL_SUCCESS the files were all moved.
285 @retval SHELL_INVALID_PARAMETER a parameter was invalid
286 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
287 @retval SHELL_WRITE_PROTECTED the destination was write protected
288 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
292 ValidateAndCopyFiles(
293 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
294 IN CONST CHAR16
*DestDir
,
295 IN BOOLEAN SilentMode
,
296 IN BOOLEAN RecursiveMode
,
302 CONST EFI_SHELL_FILE_INFO
*Node
;
303 SHELL_STATUS ShellStatus
;
310 CHAR16
*CleanFilePathStr
;
319 ShellStatus
= SHELL_SUCCESS
;
321 Cwd
= ShellGetCurrentDir(NULL
);
322 CleanFilePathStr
= NULL
;
324 ASSERT(FileList
!= NULL
);
325 ASSERT(DestDir
!= NULL
);
328 Status
= ShellLevel2StripQuotes (DestDir
, &CleanFilePathStr
);
329 if (EFI_ERROR (Status
)) {
330 if (Status
== EFI_OUT_OF_RESOURCES
) {
331 return SHELL_OUT_OF_RESOURCES
;
333 return SHELL_INVALID_PARAMETER
;
337 ASSERT (CleanFilePathStr
!= NULL
);
340 // If we are trying to copy multiple files... make sure we got a directory for the target...
342 if (EFI_ERROR(ShellIsDirectory(CleanFilePathStr
)) && FileList
->Link
.ForwardLink
!= FileList
->Link
.BackLink
) {
344 // Error for destination not a directory
346 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, L
"cp", CleanFilePathStr
);
347 FreePool (CleanFilePathStr
);
348 return (SHELL_INVALID_PARAMETER
);
350 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
351 ; !IsNull(&FileList
->Link
, &Node
->Link
)
352 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
355 // skip the directory traversing stuff...
357 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
361 NewSize
= StrSize(CleanFilePathStr
);
362 NewSize
+= StrSize(Node
->FullName
);
363 NewSize
+= (Cwd
== NULL
)? 0 : StrSize(Cwd
);
364 if (NewSize
> PathSize
) {
369 // Make sure got -r if required
371 if (!RecursiveMode
&& !EFI_ERROR(ShellIsDirectory(Node
->FullName
))) {
372 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_REQ
), gShellLevel2HiiHandle
, L
"cp");
373 FreePool (CleanFilePathStr
);
374 return (SHELL_INVALID_PARAMETER
);
378 // make sure got dest as dir if needed
380 if (!EFI_ERROR(ShellIsDirectory(Node
->FullName
)) && EFI_ERROR(ShellIsDirectory(CleanFilePathStr
))) {
382 // Error for destination not a directory
384 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, L
"cp", CleanFilePathStr
);
385 FreePool (CleanFilePathStr
);
386 return (SHELL_INVALID_PARAMETER
);
390 HiiOutput
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_CP_OUTPUT
), NULL
);
391 HiiResultOk
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_GEN_RES_OK
), NULL
);
392 DestPath
= AllocateZeroPool(PathSize
);
394 if (DestPath
== NULL
|| HiiOutput
== NULL
|| HiiResultOk
== NULL
) {
395 SHELL_FREE_NON_NULL(DestPath
);
396 SHELL_FREE_NON_NULL(HiiOutput
);
397 SHELL_FREE_NON_NULL(HiiResultOk
);
398 FreePool (CleanFilePathStr
);
399 return (SHELL_OUT_OF_RESOURCES
);
403 // Go through the list of files to copy...
405 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
406 ; !IsNull(&FileList
->Link
, &Node
->Link
)
407 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
409 if (ShellGetExecutionBreakFlag()) {
412 ASSERT(Node
->FileName
!= NULL
);
413 ASSERT(Node
->FullName
!= NULL
);
416 // skip the directory traversing stuff...
418 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
422 if (FileList
->Link
.ForwardLink
== FileList
->Link
.BackLink
// 1 item
423 && EFI_ERROR(ShellIsDirectory(CleanFilePathStr
)) // not an existing directory
425 if (StrStr(CleanFilePathStr
, L
":") == NULL
) {
427 // simple copy of a single file
430 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
)-1);
432 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, L
"cp", CleanFilePathStr
);
433 FreePool (CleanFilePathStr
);
434 return (SHELL_INVALID_PARAMETER
);
436 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && CleanFilePathStr
[0] != L
'\\') {
437 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
438 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && CleanFilePathStr
[0] == L
'\\') {
439 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
441 StrnCat(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
443 StrnCpy(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) -1);
447 // we have multiple files or a directory in the DestDir
451 // Check for leading slash
453 if (CleanFilePathStr
[0] == L
'\\') {
455 // Copy to the root of CWD
458 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
) -1);
460 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, L
"cp", CleanFilePathStr
);
461 FreePool(CleanFilePathStr
);
462 return (SHELL_INVALID_PARAMETER
);
464 while (PathRemoveLastItem(DestPath
));
465 StrnCat(DestPath
, CleanFilePathStr
+1, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
466 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
467 } else if (StrStr(CleanFilePathStr
, L
":") == NULL
) {
469 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
) -1);
471 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, L
"cp", CleanFilePathStr
);
472 FreePool(CleanFilePathStr
);
473 return (SHELL_INVALID_PARAMETER
);
475 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && CleanFilePathStr
[0] != L
'\\') {
476 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
477 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && CleanFilePathStr
[0] == L
'\\') {
478 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
480 StrnCat(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
481 if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
482 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
483 } else if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
484 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
486 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
489 StrnCpy(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) -1);
490 if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
491 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
492 } else if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
493 ((CHAR16
*)CleanFilePathStr
)[StrLen(CleanFilePathStr
)-1] = CHAR_NULL
;
495 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
500 // Make sure the path exists
502 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath
))) {
503 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_WNF
), gShellLevel2HiiHandle
, L
"cp", DestPath
);
504 ShellStatus
= SHELL_DEVICE_ERROR
;
508 if ( !EFI_ERROR(ShellIsDirectory(Node
->FullName
))
509 && !EFI_ERROR(ShellIsDirectory(DestPath
))
510 && StrniCmp(Node
->FullName
, DestPath
, StrLen(DestPath
)) == NULL
512 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_PARENT
), gShellLevel2HiiHandle
, L
"cp");
513 ShellStatus
= SHELL_INVALID_PARAMETER
;
516 if (StringNoCaseCompare(&Node
->FullName
, &DestPath
) == 0) {
517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
, L
"cp");
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
522 if ((StrniCmp(Node
->FullName
, DestPath
, StrLen(Node
->FullName
)) == 0)
523 && (DestPath
[StrLen(Node
->FullName
)] == CHAR_NULL
|| DestPath
[StrLen(Node
->FullName
)] == L
'\\')
525 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
, L
"cp");
526 ShellStatus
= SHELL_INVALID_PARAMETER
;
530 PathCleanUpDirectories(DestPath
);
533 ShellPrintEx(-1, -1, HiiOutput
, Node
->FullName
, DestPath
);
537 // copy single file...
539 ShellStatus
= CopySingleFile(Node
->FullName
, DestPath
, &Response
, SilentMode
, L
"cp");
540 if (ShellStatus
!= SHELL_SUCCESS
) {
544 if (ShellStatus
== SHELL_SUCCESS
&& Resp
== NULL
) {
545 ShellPrintEx(-1, -1, L
"%s", HiiResultOk
);
548 SHELL_FREE_NON_NULL(DestPath
);
549 SHELL_FREE_NON_NULL(HiiOutput
);
550 SHELL_FREE_NON_NULL(HiiResultOk
);
551 SHELL_FREE_NON_NULL(CleanFilePathStr
);
553 SHELL_FREE_NON_NULL(Response
);
556 return (ShellStatus
);
561 Validate and if successful copy all the files from the list into
562 destination directory.
564 @param[in] FileList The list of files to copy.
565 @param[in] DestDir The directory to copy files to.
566 @param[in] SilentMode TRUE to eliminate screen output.
567 @param[in] RecursiveMode TRUE to copy directories.
569 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
570 @retval SHELL_SUCCESS The operation was successful.
574 ProcessValidateAndCopyFiles(
575 IN EFI_SHELL_FILE_INFO
*FileList
,
576 IN CONST CHAR16
*DestDir
,
577 IN BOOLEAN SilentMode
,
578 IN BOOLEAN RecursiveMode
581 SHELL_STATUS ShellStatus
;
582 EFI_SHELL_FILE_INFO
*List
;
583 EFI_FILE_INFO
*FileInfo
;
590 ShellOpenFileMetaArg((CHAR16
*)DestDir
, EFI_FILE_MODE_READ
, &List
);
591 if (List
!= NULL
&& List
->Link
.ForwardLink
!= List
->Link
.BackLink
) {
592 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_MARG_ERROR
), gShellLevel2HiiHandle
, L
"cp", DestDir
);
593 ShellStatus
= SHELL_INVALID_PARAMETER
;
594 ShellCloseFileMetaArg(&List
);
595 } else if (List
!= NULL
) {
596 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
) != NULL
);
597 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
!= NULL
);
598 FileInfo
= gEfiShellProtocol
->GetFileInfo(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->Handle
);
599 ASSERT(FileInfo
!= NULL
);
600 StrnCatGrow(&FullName
, NULL
, ((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
, 0);
601 ShellCloseFileMetaArg(&List
);
602 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) == 0) {
603 ShellStatus
= ValidateAndCopyFiles(FileList
, FullName
, SilentMode
, RecursiveMode
, NULL
);
605 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_ERROR
), gShellLevel2HiiHandle
, L
"cp");
606 ShellStatus
= SHELL_ACCESS_DENIED
;
609 ShellCloseFileMetaArg(&List
);
610 ShellStatus
= ValidateAndCopyFiles(FileList
, DestDir
, SilentMode
, RecursiveMode
, NULL
);
613 SHELL_FREE_NON_NULL(FileInfo
);
614 SHELL_FREE_NON_NULL(FullName
);
615 return (ShellStatus
);
618 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
625 Function for 'cp' command.
627 @param[in] ImageHandle Handle to the Image (NULL if Internal).
628 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
633 IN EFI_HANDLE ImageHandle
,
634 IN EFI_SYSTEM_TABLE
*SystemTable
639 CHAR16
*ProblemParam
;
640 SHELL_STATUS ShellStatus
;
643 EFI_SHELL_FILE_INFO
*FileList
;
645 BOOLEAN RecursiveMode
;
649 ShellStatus
= SHELL_SUCCESS
;
654 // initialize the shell lib (we must be in non-auto-init...)
656 Status
= ShellInitialize();
657 ASSERT_EFI_ERROR(Status
);
659 Status
= CommandInit();
660 ASSERT_EFI_ERROR(Status
);
663 // parse the command line
665 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
666 if (EFI_ERROR(Status
)) {
667 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
668 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, L
"cp", ProblemParam
);
669 FreePool(ProblemParam
);
670 ShellStatus
= SHELL_INVALID_PARAMETER
;
678 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
683 // Initialize SilentMode and RecursiveMode
685 if (gEfiShellProtocol
->BatchIsActive()) {
688 SilentMode
= ShellCommandLineGetFlag(Package
, L
"-q");
690 RecursiveMode
= ShellCommandLineGetFlag(Package
, L
"-r");
692 switch (ParamCount
= ShellCommandLineGetCount(Package
)) {
696 // we have insufficient parameters
698 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
, L
"cp");
699 ShellStatus
= SHELL_INVALID_PARAMETER
;
703 // must have valid CWD for single parameter...
705 Cwd
= ShellGetCurrentDir(NULL
);
707 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
, L
"cp");
708 ShellStatus
= SHELL_INVALID_PARAMETER
;
710 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
711 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
) || EFI_ERROR(Status
)) {
712 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, L
"cp", ShellCommandLineGetRawValue(Package
, 1));
713 ShellStatus
= SHELL_NOT_FOUND
;
715 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, Cwd
, SilentMode
, RecursiveMode
);
722 // Make a big list of all the files...
724 for (ParamCount
--, LoopCounter
= 1 ; LoopCounter
< ParamCount
&& ShellStatus
== SHELL_SUCCESS
; LoopCounter
++) {
725 if (ShellGetExecutionBreakFlag()) {
728 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, LoopCounter
), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
729 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
730 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, L
"cp", ShellCommandLineGetRawValue(Package
, LoopCounter
));
731 ShellStatus
= SHELL_NOT_FOUND
;
734 if (ShellStatus
!= SHELL_SUCCESS
) {
735 Status
= ShellCloseFileMetaArg(&FileList
);
738 // now copy them all...
740 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
741 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, PathCleanUpDirectories((CHAR16
*)ShellCommandLineGetRawValue(Package
, ParamCount
)), SilentMode
, RecursiveMode
);
742 Status
= ShellCloseFileMetaArg(&FileList
);
743 if (EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
) {
744 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_FILE
), gShellLevel2HiiHandle
, L
"cp", ShellCommandLineGetRawValue(Package
, ParamCount
), ShellStatus
|MAX_BIT
);
745 ShellStatus
= SHELL_ACCESS_DENIED
;
750 } // switch on parameter count
752 if (FileList
!= NULL
) {
753 ShellCloseFileMetaArg(&FileList
);
757 // free the command line package
759 ShellCommandLineFreeVarList (Package
);
762 if (ShellGetExecutionBreakFlag()) {
763 return (SHELL_ABORTED
);
766 return (ShellStatus
);