2 Main file for cp shell level 2 function.
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLevel2CommandsLib.h"
16 #include <Guid/FileSystemInfo.h>
17 #include <Guid/FileSystemVolumeLabelInfo.h>
20 Function to take a list of files to copy and a destination location and do
21 the verification and copying of those files to that location. This function
22 will report any errors to the user and halt.
24 @param[in] FileList A LIST_ENTRY* based list of files to move.
25 @param[in] DestDir The destination location.
26 @param[in] SilentMode TRUE to eliminate screen output.
27 @param[in] RecursiveMode TRUE to copy directories.
28 @param[in] Resp The response to the overwrite query (if always).
30 @retval SHELL_SUCCESS the files were all moved.
31 @retval SHELL_INVALID_PARAMETER a parameter was invalid
32 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
33 @retval SHELL_WRITE_PROTECTED the destination was write protected
34 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
39 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
40 IN CONST CHAR16
*DestDir
,
41 IN BOOLEAN SilentMode
,
42 IN BOOLEAN RecursiveMode
,
47 Function to Copy one file to another location
49 If the destination exists the user will be prompted and the result put into *resp
51 @param[in] Source pointer to source file name
52 @param[in] Dest pointer to destination file name
53 @param[out] Resp pointer to response from question. Pass back on looped calling
54 @param[in] SilentMode whether to run in quiet mode or not
56 @retval SHELL_SUCCESS The source file was copied to the destination
61 IN CONST CHAR16
*Source
,
62 IN CONST CHAR16
*Dest
,
69 SHELL_FILE_HANDLE SourceHandle
;
70 SHELL_FILE_HANDLE DestHandle
;
75 EFI_SHELL_FILE_INFO
*List
;
76 SHELL_STATUS ShellStatus
;
77 UINT64 SourceFileSize
;
79 EFI_FILE_PROTOCOL
*DestVolumeFP
;
80 EFI_FILE_SYSTEM_INFO
*DestVolumeInfo
;
81 UINTN DestVolumeInfoSize
;
89 DestVolumeInfo
= NULL
;
90 ShellStatus
= SHELL_SUCCESS
;
92 ReadSize
= PcdGet32(PcdShellFileOperationSize
);
93 // Why bother copying a file to itself
94 if (StrCmp(Source
, Dest
) == 0) {
95 return (SHELL_SUCCESS
);
99 // if the destination file existed check response and possibly prompt user
101 if (ShellFileExists(Dest
) == EFI_SUCCESS
) {
102 if (Response
== NULL
&& !SilentMode
) {
103 Status
= ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel
, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR
), gShellLevel2HiiHandle
, &Response
);
106 // possibly return based on response
109 switch (*(SHELL_PROMPT_RESPONSE
*)Response
) {
110 case ShellPromptResponseNo
:
112 // return success here so we dont stop the process
114 return (SHELL_SUCCESS
);
115 case ShellPromptResponseCancel
:
118 // indicate to stop everything
120 return (SHELL_ABORTED
);
121 case ShellPromptResponseAll
:
123 case ShellPromptResponseYes
:
126 return SHELL_ABORTED
;
131 if (ShellIsDirectory(Source
) == EFI_SUCCESS
) {
132 Status
= ShellCreateDirectory(Dest
, &DestHandle
);
133 if (EFI_ERROR(Status
)) {
134 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_DIR_FAIL
), gShellLevel2HiiHandle
, Dest
);
135 return (SHELL_ACCESS_DENIED
);
139 // Now copy all the files under the directory...
143 StrnCatGrow(&TempName
, &Size
, Source
, 0);
144 StrnCatGrow(&TempName
, &Size
, L
"\\*", 0);
145 if (TempName
!= NULL
) {
146 ShellOpenFileMetaArg((CHAR16
*)TempName
, EFI_FILE_MODE_READ
, &List
);
147 *TempName
= CHAR_NULL
;
148 StrnCatGrow(&TempName
, &Size
, Dest
, 0);
149 StrnCatGrow(&TempName
, &Size
, L
"\\", 0);
150 ShellStatus
= ValidateAndCopyFiles(List
, TempName
, SilentMode
, TRUE
, Resp
);
151 ShellCloseFileMetaArg(&List
);
152 SHELL_FREE_NON_NULL(TempName
);
156 Status
= ShellDeleteFileByName(Dest
);
159 // open file with create enabled
161 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
162 if (EFI_ERROR(Status
)) {
163 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL
), gShellLevel2HiiHandle
, Dest
);
164 return (SHELL_ACCESS_DENIED
);
170 Status
= ShellOpenFileByName(Source
, &SourceHandle
, EFI_FILE_MODE_READ
, 0);
171 ASSERT_EFI_ERROR(Status
);
174 //get file size of source file and freespace available on destination volume
176 ShellGetFileSize(SourceHandle
, &SourceFileSize
);
177 ShellGetFileSize(DestHandle
, &DestFileSize
);
180 //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space
182 if(DestFileSize
< SourceFileSize
){
183 SourceFileSize
-= DestFileSize
;
189 //get the system volume info to check the free space
191 DestVolumeFP
= ConvertShellHandleToEfiFileProtocol(DestHandle
);
192 DestVolumeInfo
= NULL
;
193 DestVolumeInfoSize
= 0;
194 Status
= DestVolumeFP
->GetInfo(
196 &gEfiFileSystemInfoGuid
,
201 if (Status
== EFI_BUFFER_TOO_SMALL
) {
202 DestVolumeInfo
= AllocateZeroPool(DestVolumeInfoSize
);
203 Status
= DestVolumeFP
->GetInfo(
205 &gEfiFileSystemInfoGuid
,
212 //check if enough space available on destination drive to complete copy
214 if (DestVolumeInfo
!= NULL
&& (DestVolumeInfo
->FreeSpace
< SourceFileSize
)) {
216 //not enough space on destination directory to copy file
218 SHELL_FREE_NON_NULL(DestVolumeInfo
);
219 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_FAIL
), gShellLevel2HiiHandle
);
220 return(SHELL_VOLUME_FULL
);
223 // copy data between files
225 Buffer
= AllocateZeroPool(ReadSize
);
226 ASSERT(Buffer
!= NULL
);
227 while (ReadSize
== PcdGet32(PcdShellFileOperationSize
) && !EFI_ERROR(Status
)) {
228 Status
= ShellReadFile(SourceHandle
, &ReadSize
, Buffer
);
229 if (!EFI_ERROR(Status
)) {
230 Status
= ShellWriteFile(DestHandle
, &ReadSize
, Buffer
);
231 if (EFI_ERROR(Status
)) {
232 ShellStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
233 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR
), gShellLevel2HiiHandle
, Dest
);
237 ShellStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
238 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_CPY_READ_ERROR
), gShellLevel2HiiHandle
, Source
);
243 SHELL_FREE_NON_NULL(DestVolumeInfo
);
249 if (DestHandle
!= NULL
) {
250 ShellCloseFile(&DestHandle
);
253 if (SourceHandle
!= NULL
) {
254 ShellCloseFile(&SourceHandle
);
265 function to take a list of files to copy and a destination location and do
266 the verification and copying of those files to that location. This function
267 will report any errors to the user and halt.
269 The key is to have this function called ONLY once. this allows for the parameter
270 verification to happen correctly.
272 @param[in] FileList A LIST_ENTRY* based list of files to move.
273 @param[in] DestDir The destination location.
274 @param[in] SilentMode TRUE to eliminate screen output.
275 @param[in] RecursiveMode TRUE to copy directories.
276 @param[in] Resp The response to the overwrite query (if always).
278 @retval SHELL_SUCCESS the files were all moved.
279 @retval SHELL_INVALID_PARAMETER a parameter was invalid
280 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
281 @retval SHELL_WRITE_PROTECTED the destination was write protected
282 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
286 ValidateAndCopyFiles(
287 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
288 IN CONST CHAR16
*DestDir
,
289 IN BOOLEAN SilentMode
,
290 IN BOOLEAN RecursiveMode
,
296 CONST EFI_SHELL_FILE_INFO
*Node
;
297 SHELL_STATUS ShellStatus
;
304 CHAR16
*CleanFilePathStr
;
313 ShellStatus
= SHELL_SUCCESS
;
315 Cwd
= ShellGetCurrentDir(NULL
);
316 CleanFilePathStr
= NULL
;
318 ASSERT(FileList
!= NULL
);
319 ASSERT(DestDir
!= NULL
);
322 Status
= ShellLevel2StripQuotes (DestDir
, &CleanFilePathStr
);
323 if (EFI_ERROR (Status
)) {
324 if (Status
== EFI_OUT_OF_RESOURCES
) {
325 return SHELL_OUT_OF_RESOURCES
;
327 return SHELL_INVALID_PARAMETER
;
332 // If we are trying to copy multiple files... make sure we got a directory for the target...
334 if (EFI_ERROR(ShellIsDirectory(CleanFilePathStr
)) && FileList
->Link
.ForwardLink
!= FileList
->Link
.BackLink
) {
336 // Error for destination not a directory
338 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, CleanFilePathStr
);
339 FreePool (CleanFilePathStr
);
340 return (SHELL_INVALID_PARAMETER
);
342 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
343 ; !IsNull(&FileList
->Link
, &Node
->Link
)
344 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
347 // skip the directory traversing stuff...
349 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
353 NewSize
= StrSize(CleanFilePathStr
);
354 NewSize
+= StrSize(Node
->FullName
);
355 NewSize
+= (Cwd
== NULL
)? 0 : StrSize(Cwd
);
356 if (NewSize
> PathSize
) {
361 // Make sure got -r if required
363 if (!RecursiveMode
&& !EFI_ERROR(ShellIsDirectory(Node
->FullName
))) {
364 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_REQ
), gShellLevel2HiiHandle
);
365 FreePool (CleanFilePathStr
);
366 return (SHELL_INVALID_PARAMETER
);
370 // make sure got dest as dir if needed
372 if (!EFI_ERROR(ShellIsDirectory(Node
->FullName
)) && EFI_ERROR(ShellIsDirectory(CleanFilePathStr
))) {
374 // Error for destination not a directory
376 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, CleanFilePathStr
);
377 FreePool (CleanFilePathStr
);
378 return (SHELL_INVALID_PARAMETER
);
382 HiiOutput
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_CP_OUTPUT
), NULL
);
383 HiiResultOk
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_GEN_RES_OK
), NULL
);
384 DestPath
= AllocateZeroPool(PathSize
);
386 if (DestPath
== NULL
|| HiiOutput
== NULL
|| HiiResultOk
== NULL
) {
387 SHELL_FREE_NON_NULL(DestPath
);
388 SHELL_FREE_NON_NULL(HiiOutput
);
389 SHELL_FREE_NON_NULL(HiiResultOk
);
390 FreePool (CleanFilePathStr
);
391 return (SHELL_OUT_OF_RESOURCES
);
395 // Go through the list of files to copy...
397 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
398 ; !IsNull(&FileList
->Link
, &Node
->Link
)
399 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
401 if (ShellGetExecutionBreakFlag()) {
404 ASSERT(Node
->FileName
!= NULL
);
405 ASSERT(Node
->FullName
!= NULL
);
408 // skip the directory traversing stuff...
410 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
414 if (FileList
->Link
.ForwardLink
== FileList
->Link
.BackLink
// 1 item
415 && EFI_ERROR(ShellIsDirectory(CleanFilePathStr
)) // not an existing directory
417 if (StrStr(CleanFilePathStr
, L
":") == NULL
) {
419 // simple copy of a single file
422 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
)-1);
424 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, CleanFilePathStr
);
425 FreePool (CleanFilePathStr
);
426 return (SHELL_INVALID_PARAMETER
);
428 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && CleanFilePathStr
[0] != L
'\\') {
429 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
430 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && CleanFilePathStr
[0] == L
'\\') {
431 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
433 StrnCat(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
435 StrnCpy(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) -1);
439 // we have multiple files or a directory in the DestDir
443 // Check for leading slash
445 if (CleanFilePathStr
[0] == L
'\\') {
447 // Copy to the root of CWD
450 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
) -1);
452 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, CleanFilePathStr
);
453 FreePool(CleanFilePathStr
);
454 return (SHELL_INVALID_PARAMETER
);
456 while (PathRemoveLastItem(DestPath
));
457 StrnCat(DestPath
, CleanFilePathStr
+1, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
458 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
459 } else if (StrStr(CleanFilePathStr
, L
":") == NULL
) {
461 StrnCpy(DestPath
, Cwd
, PathSize
/sizeof(CHAR16
) -1);
463 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, CleanFilePathStr
);
464 FreePool(CleanFilePathStr
);
465 return (SHELL_INVALID_PARAMETER
);
467 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && CleanFilePathStr
[0] != L
'\\') {
468 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
469 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && CleanFilePathStr
[0] == L
'\\') {
470 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
472 StrnCat(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
473 if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
474 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
475 } else if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
476 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
478 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
481 StrnCpy(DestPath
, CleanFilePathStr
, PathSize
/sizeof(CHAR16
) -1);
482 if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
483 StrnCat(DestPath
, L
"\\", PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
484 } else if (CleanFilePathStr
[StrLen(CleanFilePathStr
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
485 ((CHAR16
*)CleanFilePathStr
)[StrLen(CleanFilePathStr
)-1] = CHAR_NULL
;
487 StrnCat(DestPath
, Node
->FileName
, PathSize
/sizeof(CHAR16
) - StrLen(DestPath
) -1);
490 FreePool (CleanFilePathStr
);
492 // Make sure the path exists
494 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath
))) {
495 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_WNF
), gShellLevel2HiiHandle
);
496 ShellStatus
= SHELL_DEVICE_ERROR
;
500 if ( !EFI_ERROR(ShellIsDirectory(Node
->FullName
))
501 && !EFI_ERROR(ShellIsDirectory(DestPath
))
502 && StrniCmp(Node
->FullName
, DestPath
, StrLen(DestPath
)) == NULL
504 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_PARENT
), gShellLevel2HiiHandle
);
505 ShellStatus
= SHELL_INVALID_PARAMETER
;
508 if (StringNoCaseCompare(&Node
->FullName
, &DestPath
) == 0) {
509 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
510 ShellStatus
= SHELL_INVALID_PARAMETER
;
514 if ((StrniCmp(Node
->FullName
, DestPath
, StrLen(Node
->FullName
)) == 0)
515 && (DestPath
[StrLen(Node
->FullName
)] == CHAR_NULL
|| DestPath
[StrLen(Node
->FullName
)] == L
'\\')
517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
518 ShellStatus
= SHELL_INVALID_PARAMETER
;
522 PathCleanUpDirectories(DestPath
);
525 ShellPrintEx(-1, -1, HiiOutput
, Node
->FullName
, DestPath
);
529 // copy single file...
531 ShellStatus
= CopySingleFile(Node
->FullName
, DestPath
, &Response
, SilentMode
);
532 if (ShellStatus
!= SHELL_SUCCESS
) {
536 if (ShellStatus
== SHELL_SUCCESS
&& Resp
== NULL
) {
537 ShellPrintEx(-1, -1, L
"%s", HiiResultOk
);
540 SHELL_FREE_NON_NULL(DestPath
);
541 SHELL_FREE_NON_NULL(HiiOutput
);
542 SHELL_FREE_NON_NULL(HiiResultOk
);
544 SHELL_FREE_NON_NULL(Response
);
547 return (ShellStatus
);
552 Validate and if successful copy all the files from the list into
553 destination directory.
555 @param[in] FileList The list of files to copy.
556 @param[in] DestDir The directory to copy files to.
557 @param[in] SilentMode TRUE to eliminate screen output.
558 @param[in] RecursiveMode TRUE to copy directories.
560 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
561 @retval SHELL_SUCCESS The operation was successful.
565 ProcessValidateAndCopyFiles(
566 IN EFI_SHELL_FILE_INFO
*FileList
,
567 IN CONST CHAR16
*DestDir
,
568 IN BOOLEAN SilentMode
,
569 IN BOOLEAN RecursiveMode
572 SHELL_STATUS ShellStatus
;
573 EFI_SHELL_FILE_INFO
*List
;
574 EFI_FILE_INFO
*FileInfo
;
581 ShellOpenFileMetaArg((CHAR16
*)DestDir
, EFI_FILE_MODE_READ
, &List
);
582 if (List
!= NULL
&& List
->Link
.ForwardLink
!= List
->Link
.BackLink
) {
583 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_MARG_ERROR
), gShellLevel2HiiHandle
, DestDir
);
584 ShellStatus
= SHELL_INVALID_PARAMETER
;
585 ShellCloseFileMetaArg(&List
);
586 } else if (List
!= NULL
) {
587 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
) != NULL
);
588 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
!= NULL
);
589 FileInfo
= gEfiShellProtocol
->GetFileInfo(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->Handle
);
590 ASSERT(FileInfo
!= NULL
);
591 StrnCatGrow(&FullName
, NULL
, ((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
, 0);
592 ShellCloseFileMetaArg(&List
);
593 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) == 0) {
594 ShellStatus
= ValidateAndCopyFiles(FileList
, FullName
, SilentMode
, RecursiveMode
, NULL
);
596 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_ERROR
), gShellLevel2HiiHandle
);
597 ShellStatus
= SHELL_ACCESS_DENIED
;
600 ShellCloseFileMetaArg(&List
);
601 ShellStatus
= ValidateAndCopyFiles(FileList
, DestDir
, SilentMode
, RecursiveMode
, NULL
);
604 SHELL_FREE_NON_NULL(FileInfo
);
605 SHELL_FREE_NON_NULL(FullName
);
606 return (ShellStatus
);
609 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
616 Function for 'cp' command.
618 @param[in] ImageHandle Handle to the Image (NULL if Internal).
619 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
624 IN EFI_HANDLE ImageHandle
,
625 IN EFI_SYSTEM_TABLE
*SystemTable
630 CHAR16
*ProblemParam
;
631 SHELL_STATUS ShellStatus
;
634 EFI_SHELL_FILE_INFO
*FileList
;
636 BOOLEAN RecursiveMode
;
640 ShellStatus
= SHELL_SUCCESS
;
645 // initialize the shell lib (we must be in non-auto-init...)
647 Status
= ShellInitialize();
648 ASSERT_EFI_ERROR(Status
);
650 Status
= CommandInit();
651 ASSERT_EFI_ERROR(Status
);
654 // parse the command line
656 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
657 if (EFI_ERROR(Status
)) {
658 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
659 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
660 FreePool(ProblemParam
);
661 ShellStatus
= SHELL_INVALID_PARAMETER
;
669 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
674 // Initialize SilentMode and RecursiveMode
676 if (gEfiShellProtocol
->BatchIsActive()) {
679 SilentMode
= ShellCommandLineGetFlag(Package
, L
"-q");
681 RecursiveMode
= ShellCommandLineGetFlag(Package
, L
"-r");
683 switch (ParamCount
= ShellCommandLineGetCount(Package
)) {
687 // we have insufficient parameters
689 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
690 ShellStatus
= SHELL_INVALID_PARAMETER
;
694 // must have valid CWD for single parameter...
696 Cwd
= ShellGetCurrentDir(NULL
);
698 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
699 ShellStatus
= SHELL_INVALID_PARAMETER
;
701 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
702 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
) || EFI_ERROR(Status
)) {
703 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, 1));
704 ShellStatus
= SHELL_NOT_FOUND
;
706 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, Cwd
, SilentMode
, RecursiveMode
);
713 // Make a big list of all the files...
715 for (ParamCount
--, LoopCounter
= 1 ; LoopCounter
< ParamCount
&& ShellStatus
== SHELL_SUCCESS
; LoopCounter
++) {
716 if (ShellGetExecutionBreakFlag()) {
719 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, LoopCounter
), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
720 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
721 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, LoopCounter
));
722 ShellStatus
= SHELL_NOT_FOUND
;
725 if (ShellStatus
!= SHELL_SUCCESS
) {
726 Status
= ShellCloseFileMetaArg(&FileList
);
729 // now copy them all...
731 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
732 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, PathCleanUpDirectories((CHAR16
*)ShellCommandLineGetRawValue(Package
, ParamCount
)), SilentMode
, RecursiveMode
);
733 Status
= ShellCloseFileMetaArg(&FileList
);
734 if (EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
) {
735 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_FILE
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamCount
), ShellStatus
|MAX_BIT
);
736 ShellStatus
= SHELL_ACCESS_DENIED
;
741 } // switch on parameter count
743 if (FileList
!= NULL
) {
744 ShellCloseFileMetaArg(&FileList
);
748 // free the command line package
750 ShellCommandLineFreeVarList (Package
);
753 if (ShellGetExecutionBreakFlag()) {
754 return (SHELL_ABORTED
);
757 return (ShellStatus
);