2 Main file for cp shell level 2 function.
4 Copyright (c) 2009 - 2010, 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"
17 // this is later in the file.
21 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
22 IN CONST CHAR16
*DestDir
,
23 IN BOOLEAN SilentMode
,
24 IN BOOLEAN RecursiveMode
,
29 Function to Copy one file to another location
31 If the destination exists the user will be prompted and the result put into *resp
33 @param[in] Source pointer to source file name
34 @param[in] Dest pointer to destination file name
35 @param[out] Resp pointer to response from question. Pass back on looped calling
36 @param[in] SilentMode whether to run in quiet mode or not
38 @retval SHELL_SUCCESS The source file was copied to the destination
43 IN CONST CHAR16
*Source
,
44 IN CONST CHAR16
*Dest
,
51 SHELL_FILE_HANDLE SourceHandle
;
52 SHELL_FILE_HANDLE DestHandle
;
57 EFI_SHELL_FILE_INFO
*List
;
58 SHELL_STATUS ShellStatus
;
68 ReadSize
= PcdGet16(PcdShellFileOperationSize
);
69 // Why bother copying a file to itself
70 if (StrCmp(Source
, Dest
) == 0) {
71 return (SHELL_SUCCESS
);
75 // Open destination file without create
77 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
82 if (DestHandle
!= NULL
) {
83 ShellCloseFile(&DestHandle
);
88 // if the destination file existed check response and possibly prompt user
90 if (!EFI_ERROR(Status
)) {
91 if (Response
== NULL
&& !SilentMode
) {
92 Status
= ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel
, STRING_TOKEN (STR_CP_PROMPT
), gShellLevel2HiiHandle
, &Response
);
95 // possibly return based on response
98 switch (*(SHELL_PROMPT_RESPONSE
*)Response
) {
99 case ShellPromptResponseNo
:
101 // return success here so we dont stop the process
103 return (SHELL_SUCCESS
);
104 case ShellPromptResponseCancel
:
107 // indicate to stop everything
109 return (SHELL_ABORTED
);
110 case ShellPromptResponseAll
:
112 case ShellPromptResponseYes
:
115 return SHELL_ABORTED
;
120 if (ShellIsDirectory(Source
) == EFI_SUCCESS
) {
121 Status
= ShellCreateDirectory(Dest
, &DestHandle
);
122 if (EFI_ERROR(Status
)) {
123 return (SHELL_ACCESS_DENIED
);
127 // Now copy all the files under the directory...
131 StrnCatGrow(&TempName
, &Size
, Source
, 0);
132 StrnCatGrow(&TempName
, &Size
, L
"\\*", 0);
133 ShellOpenFileMetaArg((CHAR16
*)TempName
, EFI_FILE_MODE_READ
, &List
);
135 StrnCatGrow(&TempName
, &Size
, Dest
, 0);
136 StrnCatGrow(&TempName
, &Size
, L
"\\", 0);
137 ShellStatus
= ValidateAndCopyFiles(List
, TempName
, SilentMode
, TRUE
, Resp
);
138 ShellCloseFileMetaArg(&List
);
143 // open file with create enabled
145 Status
= ShellOpenFileByName(Dest
, &DestHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
146 if (EFI_ERROR(Status
)) {
147 return (SHELL_ACCESS_DENIED
);
153 Status
= ShellOpenFileByName(Source
, &SourceHandle
, EFI_FILE_MODE_READ
, 0);
154 ASSERT_EFI_ERROR(Status
);
157 // copy data between files
159 Buffer
= AllocateZeroPool(ReadSize
);
160 ASSERT(Buffer
!= NULL
);
161 while (ReadSize
== PcdGet16(PcdShellFileOperationSize
) && !EFI_ERROR(Status
)) {
162 Status
= ShellReadFile(SourceHandle
, &ReadSize
, Buffer
);
163 ASSERT_EFI_ERROR(Status
);
164 Status
= ShellWriteFile(DestHandle
, &ReadSize
, Buffer
);
171 if (DestHandle
!= NULL
) {
172 ShellCloseFile(&DestHandle
);
175 if (SourceHandle
!= NULL
) {
176 ShellCloseFile(&SourceHandle
);
183 return (SHELL_SUCCESS
);
187 function to take a list of files to copy and a destination location and do
188 the verification and copying of those files to that location. This function
189 will report any errors to the user and halt.
191 The key is to have this function called ONLY once. this allows for the parameter
192 verification to happen correctly.
194 @param[in] FileList A LIST_ENTRY* based list of files to move
195 @param[in] DestDir the destination location
197 @retval SHELL_SUCCESS the files were all moved.
198 @retval SHELL_INVALID_PARAMETER a parameter was invalid
199 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
200 @retval SHELL_WRITE_PROTECTED the destination was write protected
201 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
205 ValidateAndCopyFiles(
206 IN CONST EFI_SHELL_FILE_INFO
*FileList
,
207 IN CONST CHAR16
*DestDir
,
208 IN BOOLEAN SilentMode
,
209 IN BOOLEAN RecursiveMode
,
215 CONST EFI_SHELL_FILE_INFO
*Node
;
216 SHELL_STATUS ShellStatus
;
221 CONST CHAR16
*TempLocation
;
231 ShellStatus
= SHELL_SUCCESS
;
233 Cwd
= ShellGetCurrentDir(NULL
);
235 ASSERT(FileList
!= NULL
);
236 ASSERT(DestDir
!= NULL
);
239 // If we are trying to copy multiple files... make sure we got a directory for the target...
241 if (EFI_ERROR(ShellIsDirectory(DestDir
)) && FileList
->Link
.ForwardLink
!= FileList
->Link
.BackLink
) {
243 // Error for destination not a directory
245 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, DestDir
);
246 return (SHELL_INVALID_PARAMETER
);
248 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
249 ; !IsNull(&FileList
->Link
, &Node
->Link
)
250 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
253 // skip the directory traversing stuff...
255 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
259 NewSize
= StrSize(DestDir
);
260 NewSize
+= StrSize(Node
->FileName
);
261 NewSize
+= StrSize(Cwd
);
262 if (NewSize
> PathLen
) {
267 // Make sure got -r if required
269 if (!RecursiveMode
&& !EFI_ERROR(ShellIsDirectory(Node
->FullName
))) {
270 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_REQ
), gShellLevel2HiiHandle
);
271 return (SHELL_INVALID_PARAMETER
);
275 // make sure got dest as dir if needed
277 if (!EFI_ERROR(ShellIsDirectory(Node
->FullName
)) && EFI_ERROR(ShellIsDirectory(DestDir
))) {
279 // Error for destination not a directory
281 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, DestDir
);
282 return (SHELL_INVALID_PARAMETER
);
286 HiiOutput
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_CP_OUTPUT
), NULL
);
287 HiiResultOk
= HiiGetString (gShellLevel2HiiHandle
, STRING_TOKEN (STR_GEN_RES_OK
), NULL
);
288 DestPath
= AllocatePool(PathLen
);
291 // Go through the list of files to copy...
293 for (Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
294 ; !IsNull(&FileList
->Link
, &Node
->Link
)
295 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
297 if (ShellGetExecutionBreakFlag()) {
300 ASSERT(Node
->FileName
!= NULL
);
301 ASSERT(Node
->FullName
!= NULL
);
304 // skip the directory traversing stuff...
306 if (StrCmp(Node
->FileName
, L
".") == 0 || StrCmp(Node
->FileName
, L
"..") == 0) {
310 if (FileList
->Link
.ForwardLink
== FileList
->Link
.BackLink
// 1 item
311 && EFI_ERROR(ShellIsDirectory(DestDir
)) // not an existing directory
313 ASSERT(StrStr(DestDir
, L
":") == NULL
);
315 // simple copy of a single file
317 StrCpy(DestPath
, Cwd
);
318 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && DestDir
[0] != L
'\\') {
319 StrCat(DestPath
, L
"\\");
320 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && DestDir
[0] == L
'\\') {
321 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
323 StrCat(DestPath
, DestDir
);
326 // we have multiple files or a directory in the DestDir
328 if (StrStr(DestDir
, L
":") == NULL
) {
329 StrCpy(DestPath
, Cwd
);
330 if (DestPath
[StrLen(DestPath
)-1] != L
'\\' && DestDir
[0] != L
'\\') {
331 StrCat(DestPath
, L
"\\");
332 } else if (DestPath
[StrLen(DestPath
)-1] == L
'\\' && DestDir
[0] == L
'\\') {
333 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
335 StrCat(DestPath
, DestDir
);
336 if (DestDir
[StrLen(DestDir
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
337 StrCat(DestPath
, L
"\\");
338 } else if (DestDir
[StrLen(DestDir
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
339 ((CHAR16
*)DestPath
)[StrLen(DestPath
)-1] = CHAR_NULL
;
341 StrCat(DestPath
, Node
->FileName
);
344 StrCpy(DestPath
, DestDir
);
345 if (DestDir
[StrLen(DestDir
)-1] != L
'\\' && Node
->FileName
[0] != L
'\\') {
346 StrCat(DestPath
, L
"\\");
347 } else if (DestDir
[StrLen(DestDir
)-1] == L
'\\' && Node
->FileName
[0] == L
'\\') {
348 ((CHAR16
*)DestDir
)[StrLen(DestDir
)-1] = CHAR_NULL
;
350 StrCat(DestPath
, Node
->FileName
);
355 // Make sure the path exists
357 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath
))) {
358 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DIR_WNF
), gShellLevel2HiiHandle
);
359 ShellStatus
= SHELL_DEVICE_ERROR
;
363 if ( !EFI_ERROR(ShellIsDirectory(Node
->FullName
))
364 && !EFI_ERROR(ShellIsDirectory(DestPath
))
365 && StrniCmp(Node
->FullName
, DestPath
, StrLen(DestPath
)) == NULL
367 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_PARENT
), gShellLevel2HiiHandle
);
368 ShellStatus
= SHELL_INVALID_PARAMETER
;
371 if (StringNoCaseCompare(&Node
->FullName
, &DestPath
) == 0) {
372 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
373 ShellStatus
= SHELL_INVALID_PARAMETER
;
377 if ((TempLocation
= StrniCmp(Node
->FullName
, DestPath
, StrLen(Node
->FullName
))) == 0
378 && (DestPath
[StrLen(Node
->FullName
)] == CHAR_NULL
|| DestPath
[StrLen(Node
->FullName
)] == L
'\\')
380 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_SD_SAME
), gShellLevel2HiiHandle
);
381 ShellStatus
= SHELL_INVALID_PARAMETER
;
387 ShellPrintEx(-1, -1, HiiOutput
, Node
->FullName
, DestPath
);
390 // copy single file...
392 ShellStatus
= CopySingleFile(Node
->FullName
, DestPath
, &Response
, SilentMode
);
393 if (ShellStatus
!= SHELL_SUCCESS
) {
397 if (ShellStatus
== SHELL_SUCCESS
&& Resp
== NULL
) {
398 ShellPrintEx(-1, -1, L
"%s", HiiResultOk
);
401 SHELL_FREE_NON_NULL(DestPath
);
402 SHELL_FREE_NON_NULL(HiiOutput
);
403 SHELL_FREE_NON_NULL(HiiResultOk
);
405 SHELL_FREE_NON_NULL(Response
);
408 return (ShellStatus
);
413 ProcessValidateAndCopyFiles(
414 IN EFI_SHELL_FILE_INFO
*FileList
,
415 IN CONST CHAR16
*DestDir
,
416 IN BOOLEAN SilentMode
,
417 IN BOOLEAN RecursiveMode
420 SHELL_STATUS ShellStatus
;
421 EFI_SHELL_FILE_INFO
*List
;
423 EFI_FILE_INFO
*FileInfo
;
427 Status
= ShellOpenFileMetaArg((CHAR16
*)DestDir
, EFI_FILE_MODE_READ
, &List
);
428 if (List
!= NULL
&& List
->Link
.ForwardLink
!= List
->Link
.BackLink
) {
429 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_MARG_ERROR
), gShellLevel2HiiHandle
, DestDir
);
430 ShellStatus
= SHELL_INVALID_PARAMETER
;
431 ShellCloseFileMetaArg(&List
);
432 } else if (List
!= NULL
) {
433 ASSERT(List
!= NULL
);
434 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
) != NULL
);
435 ASSERT(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
!= NULL
);
437 FileInfo
= gEfiShellProtocol
->GetFileInfo(((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->Handle
);
438 ASSERT(FileInfo
!= NULL
);
439 if ((FileInfo
->Attribute
& EFI_FILE_READ_ONLY
) == 0) {
440 ShellStatus
= ValidateAndCopyFiles(FileList
, ((EFI_SHELL_FILE_INFO
*)List
->Link
.ForwardLink
)->FullName
, SilentMode
, RecursiveMode
, NULL
);
442 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CP_DEST_ERROR
), gShellLevel2HiiHandle
);
443 ShellStatus
= SHELL_ACCESS_DENIED
;
445 SHELL_FREE_NON_NULL(FileInfo
);
446 ShellCloseFileMetaArg(&List
);
448 ShellStatus
= ValidateAndCopyFiles(FileList
, DestDir
, SilentMode
, RecursiveMode
, NULL
);
451 return (ShellStatus
);
454 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
461 Function for 'cp' command.
463 @param[in] ImageHandle Handle to the Image (NULL if Internal).
464 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
469 IN EFI_HANDLE ImageHandle
,
470 IN EFI_SYSTEM_TABLE
*SystemTable
475 CHAR16
*ProblemParam
;
476 SHELL_STATUS ShellStatus
;
479 EFI_SHELL_FILE_INFO
*FileList
;
481 BOOLEAN RecursiveMode
;
485 ShellStatus
= SHELL_SUCCESS
;
490 // initialize the shell lib (we must be in non-auto-init...)
492 Status
= ShellInitialize();
493 ASSERT_EFI_ERROR(Status
);
495 Status
= CommandInit();
496 ASSERT_EFI_ERROR(Status
);
499 // parse the command line
501 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
502 if (EFI_ERROR(Status
)) {
503 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
504 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
505 FreePool(ProblemParam
);
506 ShellStatus
= SHELL_INVALID_PARAMETER
;
514 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
519 // Initialize SilentMode and RecursiveMode
521 if (gEfiShellProtocol
->BatchIsActive()) {
524 SilentMode
= ShellCommandLineGetFlag(Package
, L
"-q");
526 RecursiveMode
= ShellCommandLineGetFlag(Package
, L
"-r");
528 switch (ParamCount
= ShellCommandLineGetCount(Package
)) {
532 // we have insufficient parameters
534 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
535 ShellStatus
= SHELL_INVALID_PARAMETER
;
539 // must have valid CWD for single parameter...
541 Cwd
= ShellGetCurrentDir(NULL
);
543 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
544 ShellStatus
= SHELL_INVALID_PARAMETER
;
546 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, 1), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
547 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
) || EFI_ERROR(Status
)) {
548 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, 1));
549 ShellStatus
= SHELL_NOT_FOUND
;
551 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, Cwd
, SilentMode
, RecursiveMode
);
558 // Make a big list of all the files...
560 for (ParamCount
--, LoopCounter
= 1 ; LoopCounter
< ParamCount
&& ShellStatus
== SHELL_SUCCESS
; LoopCounter
++) {
561 if (ShellGetExecutionBreakFlag()) {
564 Status
= ShellOpenFileMetaArg((CHAR16
*)ShellCommandLineGetRawValue(Package
, LoopCounter
), EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_READ
, &FileList
);
565 if (EFI_ERROR(Status
) || FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
566 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, 1));
567 ShellStatus
= SHELL_NOT_FOUND
;
571 // now copy them all...
573 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
574 ShellStatus
= ProcessValidateAndCopyFiles(FileList
, ShellCommandCleanPath((CHAR16
*)ShellCommandLineGetRawValue(Package
, ParamCount
)), SilentMode
, RecursiveMode
);
575 Status
= ShellCloseFileMetaArg(&FileList
);
576 if (EFI_ERROR(Status
) && ShellStatus
== SHELL_SUCCESS
) {
577 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_ERR_FILE
), gShellLevel2HiiHandle
, ShellCommandLineGetRawValue(Package
, 1), ShellStatus
|MAX_BIT
);
578 ShellStatus
= SHELL_ACCESS_DENIED
;
583 } // switch on parameter count
585 if (FileList
!= NULL
) {
586 ShellCloseFileMetaArg(&FileList
);
590 // free the command line package
592 ShellCommandLineFreeVarList (Package
);
595 if (ShellGetExecutionBreakFlag()) {
596 return (SHELL_ABORTED
);
599 return (ShellStatus
);