2 Main file for cp shell level 2 function.
4 Copyright (c) 2009 - 2013, 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"
18 Function to take a list of files to copy and a destination location and do
19 the verification and copying of those files to that location. This function
20 will report any errors to the user and halt.
22 @param[in] FileList A LIST_ENTRY* based list of files to move.
23 @param[in] DestDir The destination location.
24 @param[in] SilentMode TRUE to eliminate screen output.
25 @param[in] RecursiveMode TRUE to copy directories.
26 @param[in] Resp The response to the overwrite query (if always).
28 @retval SHELL_SUCCESS the files were all moved.
29 @retval SHELL_INVALID_PARAMETER a parameter was invalid
30 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
31 @retval SHELL_WRITE_PROTECTED the destination was write protected
32 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
37 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
38 IN CONST CHAR16
*DestDir
,
39 IN BOOLEAN SilentMode
,
40 IN BOOLEAN RecursiveMode
,
45 Function to Copy one file to another location
47 If the destination exists the user will be prompted and the result put into *resp
49 @param[in] Source pointer to source file name
50 @param[in] Dest pointer to destination file name
51 @param[out] Resp pointer to response from question. Pass back on looped calling
52 @param[in] SilentMode whether to run in quiet mode or not
54 @retval SHELL_SUCCESS The source file was copied to the destination
59 IN CONST CHAR16
*Source
,
60 IN CONST CHAR16
*Dest
,
67 SHELL_FILE_HANDLE SourceHandle
;
68 SHELL_FILE_HANDLE DestHandle
;
73 EFI_SHELL_FILE_INFO
*List
;
74 SHELL_STATUS ShellStatus
;
84 ReadSize
= PcdGet16(PcdShellFileOperationSize
);
85 // Why bother copying a file to itself
86 if (StrCmp(Source
, Dest
) == 0) {
87 return (SHELL_SUCCESS
);
91 // Open destination file without create
93 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
98 if (DestHandle
!= NULL
) {
99 ShellCloseFile(&DestHandle
);
104 // if the destination file existed check response and possibly prompt user
106 if (!EFI_ERROR(Status
)) {
107 if (Response
== NULL
&& !SilentMode
) {
108 Status
= ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel
, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR
), gShellLevel2HiiHandle
, &Response
);
111 // possibly return based on response
114 switch (*(SHELL_PROMPT_RESPONSE
*)Response
) {
115 case ShellPromptResponseNo
:
117 // return success here so we dont stop the process
119 return (SHELL_SUCCESS
);
120 case ShellPromptResponseCancel
:
123 // indicate to stop everything
125 return (SHELL_ABORTED
);
126 case ShellPromptResponseAll
:
128 case ShellPromptResponseYes
:
131 return SHELL_ABORTED
;
136 if (ShellIsDirectory(Source
) == EFI_SUCCESS
) {
137 Status
= ShellCreateDirectory(Dest
, &DestHandle
);
138 if (EFI_ERROR(Status
)) {
139 return (SHELL_ACCESS_DENIED
);
143 // Now copy all the files under the directory...
147 StrnCatGrow(&TempName
, &Size
, Source
, 0);
148 StrnCatGrow(&TempName
, &Size
, L
"\\*", 0);
149 if (TempName
!= NULL
) {
150 ShellOpenFileMetaArg((CHAR16
*)TempName
, EFI_FILE_MODE_READ
, &List
);
151 *TempName
= CHAR_NULL
;
152 StrnCatGrow(&TempName
, &Size
, Dest
, 0);
153 StrnCatGrow(&TempName
, &Size
, L
"\\", 0);
154 ShellStatus
= ValidateAndCopyFiles(List
, TempName
, SilentMode
, TRUE
, Resp
);
155 ShellCloseFileMetaArg(&List
);
156 SHELL_FREE_NON_NULL(TempName
);
161 // open file with create enabled
163 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
164 if (EFI_ERROR(Status
)) {
165 return (SHELL_ACCESS_DENIED
);
171 Status
= ShellOpenFileByName(Source
, &SourceHandle
, EFI_FILE_MODE_READ
, 0);
172 ASSERT_EFI_ERROR(Status
);
175 // copy data between files
177 Buffer
= AllocateZeroPool(ReadSize
);
178 ASSERT(Buffer
!= NULL
);
179 while (ReadSize
== PcdGet16(PcdShellFileOperationSize
) && !EFI_ERROR(Status
)) {
180 Status
= ShellReadFile(SourceHandle
, &ReadSize
, Buffer
);
181 ASSERT_EFI_ERROR(Status
);
182 Status
= ShellWriteFile(DestHandle
, &ReadSize
, Buffer
);
189 if (DestHandle
!= NULL
) {
190 ShellCloseFile(&DestHandle
);
193 if (SourceHandle
!= NULL
) {
194 ShellCloseFile(&SourceHandle
);
201 return (SHELL_SUCCESS
);
205 function to take a list of files to copy and a destination location and do
206 the verification and copying of those files to that location. This function
207 will report any errors to the user and halt.
209 The key is to have this function called ONLY once. this allows for the parameter
210 verification to happen correctly.
212 @param[in] FileList A LIST_ENTRY* based list of files to move.
213 @param[in] DestDir The destination location.
214 @param[in] SilentMode TRUE to eliminate screen output.
215 @param[in] RecursiveMode TRUE to copy directories.
216 @param[in] Resp The response to the overwrite query (if always).
218 @retval SHELL_SUCCESS the files were all moved.
219 @retval SHELL_INVALID_PARAMETER a parameter was invalid
220 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
221 @retval SHELL_WRITE_PROTECTED the destination was write protected
222 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
226 ValidateAndCopyFiles(
227 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
228 IN CONST CHAR16
*DestDir
,
229 IN BOOLEAN SilentMode
,
230 IN BOOLEAN RecursiveMode
,
236 CONST EFI_SHELL_FILE_INFO
*Node
;
237 SHELL_STATUS ShellStatus
;
242 CONST CHAR16
*TempLocation
;
252 ShellStatus
= SHELL_SUCCESS
;
254 Cwd
= ShellGetCurrentDir(NULL
);
256 ASSERT(FileList
!= NULL
);
257 ASSERT(DestDir
!= NULL
);
260 // We already verified that this was present.
265 // If we are trying to copy multiple files... make sure we got a directory for the target...
267 if (EFI_ERROR(ShellIsDirectory(DestDir
)) && FileList
->Link
.ForwardLink
!= FileList
->Link
.BackLink
) {
269 // Error for destination not a directory
271 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, DestDir
);
272 return (SHELL_INVALID_PARAMETER
);
274 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
275 ; !IsNull(&FileList
->Link
, &Node
->Link
)
276 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
279 // skip the directory traversing stuff...
281 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
285 NewSize
= StrSize(DestDir
);
286 NewSize
+= StrSize(Node
->FullName
);
287 NewSize
+= StrSize(Cwd
);
288 if (NewSize
> PathLen
) {
293 // Make sure got -r if required
295 if (!RecursiveMode
&& !EFI_ERROR(ShellIsDirectory(Node
->FullName
))) {
296 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_REQ
), gShellLevel2HiiHandle
);
297 return (SHELL_INVALID_PARAMETER
);
301 // make sure got dest as dir if needed
303 if (!EFI_ERROR(ShellIsDirectory(Node
->FullName
)) && EFI_ERROR(ShellIsDirectory(DestDir
))) {
305 // Error for destination not a directory
307 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, DestDir
);
308 return (SHELL_INVALID_PARAMETER
);
312 HiiOutput
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_CP_OUTPUT
), NULL
);
313 HiiResultOk
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_GEN_RES_OK
), NULL
);
314 DestPath
= AllocateZeroPool(PathLen
);
316 if (DestPath
== NULL
|| HiiOutput
== NULL
|| HiiResultOk
== NULL
) {
317 SHELL_FREE_NON_NULL(DestPath
);
318 SHELL_FREE_NON_NULL(HiiOutput
);
319 SHELL_FREE_NON_NULL(HiiResultOk
);
320 return (SHELL_OUT_OF_RESOURCES
);
324 // Go through the list of files to copy...
326 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
327 ; !IsNull(&FileList
->Link
, &Node
->Link
)
328 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
330 if (ShellGetExecutionBreakFlag()) {
333 ASSERT(Node
->FileName
!= NULL
);
334 ASSERT(Node
->FullName
!= NULL
);
337 // skip the directory traversing stuff...
339 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
343 if (FileList
->Link
.ForwardLink
== FileList
->Link
.BackLink
// 1 item
344 && EFI_ERROR(ShellIsDirectory(DestDir
)) // not an existing directory
346 if (StrStr(DestDir
, L
":") == NULL
) {
348 // simple copy of a single file
350 StrCpy(DestPath
, Cwd
);
351 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && DestDir
[0] != L
'\\') {
352 StrCat(DestPath
, L
"\\");
353 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && DestDir
[0] == L
'\\') {
354 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
356 StrCat(DestPath
, DestDir
);
358 StrCpy(DestPath
, DestDir
);
362 // we have multiple files or a directory in the DestDir
366 // Check for leading slash
368 if (DestDir
[0] == L
'\\') {
370 // Copy to the root of CWD
372 StrCpy(DestPath
, Cwd
);
373 while (PathRemoveLastItem(DestPath
));
374 StrCat(DestPath
, DestDir
+1);
375 StrCat(DestPath
, Node
->FileName
);
376 } else if (StrStr(DestDir
, L
":") == NULL
) {
377 StrCpy(DestPath
, Cwd
);
378 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && DestDir
[0] != L
'\\') {
379 StrCat(DestPath
, L
"\\");
380 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && DestDir
[0] == L
'\\') {
381 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
383 StrCat(DestPath
, DestDir
);
384 if (DestDir
[StrLen(DestDir
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
385 StrCat(DestPath
, L
"\\");
386 } else if (DestDir
[StrLen(DestDir
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
387 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
389 StrCat(DestPath
, Node
->FileName
);
392 StrCpy(DestPath
, DestDir
);
393 if (DestDir
[StrLen(DestDir
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
394 StrCat(DestPath
, L
"\\");
395 } else if (DestDir
[StrLen(DestDir
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
396 ((CHAR16
*)DestDir
)[StrLen(DestDir
)-1] = CHAR_NULL
;
398 StrCat(DestPath
, Node
->FileName
);
403 // Make sure the path exists
405 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath
))) {
406 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_WNF
), gShellLevel2HiiHandle
);
407 ShellStatus
= SHELL_DEVICE_ERROR
;
411 if ( !EFI_ERROR(ShellIsDirectory(Node
->FullName
))
412 && !EFI_ERROR(ShellIsDirectory(DestPath
))
413 && StrniCmp(Node
->FullName
, DestPath
, StrLen(DestPath
)) == NULL
415 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_PARENT
), gShellLevel2HiiHandle
);
416 ShellStatus
= SHELL_INVALID_PARAMETER
;
419 if (StringNoCaseCompare(&Node
->FullName
, &DestPath
) == 0) {
420 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
421 ShellStatus
= SHELL_INVALID_PARAMETER
;
425 if ((TempLocation
= StrniCmp(Node
->FullName
, DestPath
, StrLen(Node
->FullName
))) == 0
426 && (DestPath
[StrLen(Node
->FullName
)] == CHAR_NULL
|| DestPath
[StrLen(Node
->FullName
)] == L
'\\')
428 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
429 ShellStatus
= SHELL_INVALID_PARAMETER
;
433 PathCleanUpDirectories(DestPath
);
435 ShellPrintEx(-1, -1, HiiOutput
, Node
->FullName
, DestPath
);
438 // copy single file...
440 ShellStatus
= CopySingleFile(Node
->FullName
, DestPath
, &Response
, SilentMode
);
441 if (ShellStatus
!= SHELL_SUCCESS
) {
445 if (ShellStatus
== SHELL_SUCCESS
&& Resp
== NULL
) {
446 ShellPrintEx(-1, -1, L
"%s", HiiResultOk
);
449 SHELL_FREE_NON_NULL(DestPath
);
450 SHELL_FREE_NON_NULL(HiiOutput
);
451 SHELL_FREE_NON_NULL(HiiResultOk
);
453 SHELL_FREE_NON_NULL(Response
);
456 return (ShellStatus
);
460 Validate and if successful copy all the files from the list into
461 destination directory.
463 @param[in] FileList The list of files to copy.
464 @param[in] DestDir The directory to copy files to.
465 @param[in] SilentMode TRUE to eliminate screen output.
466 @param[in] RecursiveMode TRUE to copy directories.
468 @retval SHELL_INVALID_PARAMETER A parameter was invalid.
469 @retval SHELL_SUCCESS The operation was successful.
473 ProcessValidateAndCopyFiles(
474 IN EFI_SHELL_FILE_INFO
*FileList
,
475 IN CONST CHAR16
*DestDir
,
476 IN BOOLEAN SilentMode
,
477 IN BOOLEAN RecursiveMode
480 SHELL_STATUS ShellStatus
;
481 EFI_SHELL_FILE_INFO
*List
;
483 EFI_FILE_INFO
*FileInfo
;
487 Status
= ShellOpenFileMetaArg((CHAR16
*)DestDir
, EFI_FILE_MODE_READ
, &List
);
488 if (List
!= NULL
&& List
->Link
.ForwardLink
!= List
->Link
.BackLink
) {
489 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_MARG_ERROR
), gShellLevel2HiiHandle
, DestDir
);
490 ShellStatus
= SHELL_INVALID_PARAMETER
;
491 ShellCloseFileMetaArg(&List
);
492 } else if (List
!= NULL
) {
493 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
) != NULL
);
494 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
!= NULL
);
496 FileInfo
= gEfiShellProtocol
->GetFileInfo(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->Handle
);
497 ASSERT(FileInfo
!= NULL
);
498 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) == 0) {
499 ShellStatus
= ValidateAndCopyFiles(FileList
, ((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
, SilentMode
, RecursiveMode
, NULL
);
501 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_ERROR
), gShellLevel2HiiHandle
);
502 ShellStatus
= SHELL_ACCESS_DENIED
;
504 SHELL_FREE_NON_NULL(FileInfo
);
505 ShellCloseFileMetaArg(&List
);
507 ShellStatus
= ValidateAndCopyFiles(FileList
, DestDir
, SilentMode
, RecursiveMode
, NULL
);
510 return (ShellStatus
);
513 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
520 Function for 'cp' command.
522 @param[in] ImageHandle Handle to the Image (NULL if Internal).
523 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
528 IN EFI_HANDLE ImageHandle
,
529 IN EFI_SYSTEM_TABLE
*SystemTable
534 CHAR16
*ProblemParam
;
535 SHELL_STATUS ShellStatus
;
538 EFI_SHELL_FILE_INFO
*FileList
;
540 BOOLEAN RecursiveMode
;
544 ShellStatus
= SHELL_SUCCESS
;
549 // initialize the shell lib (we must be in non-auto-init...)
551 Status
= ShellInitialize();
552 ASSERT_EFI_ERROR(Status
);
554 Status
= CommandInit();
555 ASSERT_EFI_ERROR(Status
);
558 // parse the command line
560 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
561 if (EFI_ERROR(Status
)) {
562 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
563 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
564 FreePool(ProblemParam
);
565 ShellStatus
= SHELL_INVALID_PARAMETER
;
573 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
578 // Initialize SilentMode and RecursiveMode
580 if (gEfiShellProtocol
->BatchIsActive()) {
583 SilentMode
= ShellCommandLineGetFlag(Package
, L
"-q");
585 RecursiveMode
= ShellCommandLineGetFlag(Package
, L
"-r");
587 switch (ParamCount
= ShellCommandLineGetCount(Package
)) {
591 // we have insufficient parameters
593 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
594 ShellStatus
= SHELL_INVALID_PARAMETER
;
598 // must have valid CWD for single parameter...
600 Cwd
= ShellGetCurrentDir(NULL
);
602 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
603 ShellStatus
= SHELL_INVALID_PARAMETER
;
605 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
606 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
) || EFI_ERROR(Status
)) {
607 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, 1));
608 ShellStatus
= SHELL_NOT_FOUND
;
610 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, Cwd
, SilentMode
, RecursiveMode
);
617 // Make a big list of all the files...
619 for (ParamCount
--, LoopCounter
= 1 ; LoopCounter
< ParamCount
&& ShellStatus
== SHELL_SUCCESS
; LoopCounter
++) {
620 if (ShellGetExecutionBreakFlag()) {
623 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, LoopCounter
), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
624 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
625 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, LoopCounter
));
626 ShellStatus
= SHELL_NOT_FOUND
;
629 if (ShellStatus
!= SHELL_SUCCESS
) {
630 Status
= ShellCloseFileMetaArg(&FileList
);
633 // now copy them all...
635 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
636 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, PathCleanUpDirectories((CHAR16
*)ShellCommandLineGetRawValue(Package
, ParamCount
)), SilentMode
, RecursiveMode
);
637 Status
= ShellCloseFileMetaArg(&FileList
);
638 if (EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
) {
639 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_FILE
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, ParamCount
), ShellStatus
|MAX_BIT
);
640 ShellStatus
= SHELL_ACCESS_DENIED
;
645 } // switch on parameter count
647 if (FileList
!= NULL
) {
648 ShellCloseFileMetaArg(&FileList
);
652 // free the command line package
654 ShellCommandLineFreeVarList (Package
);
657 if (ShellGetExecutionBreakFlag()) {
658 return (SHELL_ABORTED
);
661 return (ShellStatus
);