]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Remove the redundant quotes around the parameter for 'alias'.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for cp shell level 2 function.\r
3\r
7282b505 4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
a405b86d 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellLevel2CommandsLib.h"\r
f06be00e 16#include <Guid/FileSystemInfo.h>\r
17#include <Guid/FileSystemVolumeLabelInfo.h>\r
a405b86d 18\r
b54fd049 19/**\r
20 Function to take a list of files to copy and a destination location and do\r
21 the verification and copying of those files to that location. This function\r
22 will report any errors to the user and halt.\r
23\r
24 @param[in] FileList A LIST_ENTRY* based list of files to move.\r
25 @param[in] DestDir The destination location.\r
26 @param[in] SilentMode TRUE to eliminate screen output.\r
27 @param[in] RecursiveMode TRUE to copy directories.\r
28 @param[in] Resp The response to the overwrite query (if always).\r
29\r
30 @retval SHELL_SUCCESS the files were all moved.\r
31 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
32 @retval SHELL_SECURITY_VIOLATION a security violation ocurred\r
33 @retval SHELL_WRITE_PROTECTED the destination was write protected\r
34 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
35**/\r
a405b86d 36SHELL_STATUS\r
37EFIAPI\r
38ValidateAndCopyFiles(\r
39 IN CONST EFI_SHELL_FILE_INFO *FileList,\r
40 IN CONST CHAR16 *DestDir,\r
41 IN BOOLEAN SilentMode,\r
42 IN BOOLEAN RecursiveMode,\r
43 IN VOID **Resp\r
44 );\r
45\r
46/**\r
47 Function to Copy one file to another location\r
48\r
49 If the destination exists the user will be prompted and the result put into *resp\r
50\r
51 @param[in] Source pointer to source file name\r
52 @param[in] Dest pointer to destination file name\r
53 @param[out] Resp pointer to response from question. Pass back on looped calling\r
54 @param[in] SilentMode whether to run in quiet mode or not\r
55\r
56 @retval SHELL_SUCCESS The source file was copied to the destination\r
57**/\r
58SHELL_STATUS\r
59EFIAPI\r
60CopySingleFile(\r
61 IN CONST CHAR16 *Source,\r
62 IN CONST CHAR16 *Dest,\r
63 OUT VOID **Resp,\r
64 IN BOOLEAN SilentMode\r
65 )\r
66{\r
f06be00e 67 VOID *Response;\r
68 UINTN ReadSize;\r
69 SHELL_FILE_HANDLE SourceHandle;\r
70 SHELL_FILE_HANDLE DestHandle;\r
71 EFI_STATUS Status;\r
72 VOID *Buffer;\r
73 CHAR16 *TempName;\r
74 UINTN Size;\r
75 EFI_SHELL_FILE_INFO *List;\r
76 SHELL_STATUS ShellStatus;\r
77 UINT64 SourceFileSize;\r
78 UINT64 DestFileSize;\r
79 EFI_FILE_PROTOCOL *DestVolumeFP;\r
80 EFI_FILE_SYSTEM_INFO *DestVolumeInfo;\r
81 UINTN DestVolumeInfoSize;\r
a405b86d 82\r
83 ASSERT(Resp != NULL);\r
84\r
f06be00e 85 SourceHandle = NULL;\r
86 DestHandle = NULL;\r
87 Response = *Resp;\r
88 List = NULL;\r
89 DestVolumeInfo = NULL;\r
e755a4ca 90 ShellStatus = SHELL_SUCCESS;\r
a405b86d 91\r
433a21cb 92 ReadSize = PcdGet32(PcdShellFileOperationSize);\r
a405b86d 93 // Why bother copying a file to itself\r
94 if (StrCmp(Source, Dest) == 0) {\r
95 return (SHELL_SUCCESS);\r
96 }\r
97\r
a405b86d 98 //\r
99 // if the destination file existed check response and possibly prompt user\r
100 //\r
ac8783c8 101 if (ShellFileExists(Dest) == EFI_SUCCESS) {\r
a405b86d 102 if (Response == NULL && !SilentMode) {\r
b54fd049 103 Status = ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);\r
a405b86d 104 }\r
105 //\r
106 // possibly return based on response\r
107 //\r
108 if (!SilentMode) {\r
109 switch (*(SHELL_PROMPT_RESPONSE*)Response) {\r
110 case ShellPromptResponseNo:\r
111 //\r
112 // return success here so we dont stop the process\r
113 //\r
114 return (SHELL_SUCCESS);\r
115 case ShellPromptResponseCancel:\r
116 *Resp = Response;\r
117 //\r
118 // indicate to stop everything\r
119 //\r
120 return (SHELL_ABORTED);\r
121 case ShellPromptResponseAll:\r
122 *Resp = Response;\r
123 case ShellPromptResponseYes:\r
124 break;\r
e9723321 125 default:\r
126 return SHELL_ABORTED;\r
a405b86d 127 }\r
128 }\r
129 }\r
130\r
131 if (ShellIsDirectory(Source) == EFI_SUCCESS) {\r
132 Status = ShellCreateDirectory(Dest, &DestHandle);\r
133 if (EFI_ERROR(Status)) {\r
7282b505 134 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_DIR_FAIL), gShellLevel2HiiHandle, Dest);\r
a405b86d 135 return (SHELL_ACCESS_DENIED);\r
136 }\r
137\r
138 //\r
139 // Now copy all the files under the directory...\r
140 //\r
141 TempName = NULL;\r
f06be00e 142 Size = 0;\r
a405b86d 143 StrnCatGrow(&TempName, &Size, Source, 0);\r
144 StrnCatGrow(&TempName, &Size, L"\\*", 0);\r
7dd05623 145 if (TempName != NULL) {\r
146 ShellOpenFileMetaArg((CHAR16*)TempName, EFI_FILE_MODE_READ, &List);\r
147 *TempName = CHAR_NULL;\r
148 StrnCatGrow(&TempName, &Size, Dest, 0);\r
149 StrnCatGrow(&TempName, &Size, L"\\", 0);\r
150 ShellStatus = ValidateAndCopyFiles(List, TempName, SilentMode, TRUE, Resp);\r
151 ShellCloseFileMetaArg(&List);\r
152 SHELL_FREE_NON_NULL(TempName);\r
153 Size = 0;\r
154 }\r
a405b86d 155 } else {\r
ac8783c8 156 Status = ShellDeleteFileByName(Dest);\r
a405b86d 157\r
ac8783c8
JC
158 //\r
159 // open file with create enabled\r
160 //\r
161 Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
162 if (EFI_ERROR(Status)) {\r
7282b505 163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL), gShellLevel2HiiHandle, Dest);\r
ac8783c8
JC
164 return (SHELL_ACCESS_DENIED);\r
165 }\r
f06be00e 166\r
ac8783c8
JC
167 //\r
168 // open source file\r
169 //\r
170 Status = ShellOpenFileByName(Source, &SourceHandle, EFI_FILE_MODE_READ, 0);\r
171 ASSERT_EFI_ERROR(Status);\r
f06be00e 172\r
ac8783c8
JC
173 //\r
174 //get file size of source file and freespace available on destination volume\r
175 //\r
176 ShellGetFileSize(SourceHandle, &SourceFileSize);\r
177 ShellGetFileSize(DestHandle, &DestFileSize);\r
f06be00e 178\r
ac8783c8
JC
179 //\r
180 //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space\r
181 //\r
182 if(DestFileSize < SourceFileSize){\r
183 SourceFileSize -= DestFileSize;\r
184 } else {\r
185 SourceFileSize = 0;\r
186 }\r
187\r
188 //\r
189 //get the system volume info to check the free space\r
190 //\r
191 DestVolumeFP = ConvertShellHandleToEfiFileProtocol(DestHandle);\r
192 DestVolumeInfo = NULL;\r
193 DestVolumeInfoSize = 0;\r
194 Status = DestVolumeFP->GetInfo(\r
195 DestVolumeFP,\r
196 &gEfiFileSystemInfoGuid,\r
197 &DestVolumeInfoSize,\r
198 DestVolumeInfo\r
199 );\r
200\r
201 if (Status == EFI_BUFFER_TOO_SMALL) {\r
202 DestVolumeInfo = AllocateZeroPool(DestVolumeInfoSize);\r
f06be00e 203 Status = DestVolumeFP->GetInfo(\r
204 DestVolumeFP,\r
205 &gEfiFileSystemInfoGuid,\r
206 &DestVolumeInfoSize,\r
207 DestVolumeInfo\r
208 );\r
ac8783c8 209 }\r
f06be00e 210\r
ac8783c8
JC
211 //\r
212 //check if enough space available on destination drive to complete copy\r
213 //\r
214 if (DestVolumeInfo!= NULL && (DestVolumeInfo->FreeSpace < SourceFileSize)) {\r
f06be00e 215 //\r
ac8783c8 216 //not enough space on destination directory to copy file\r
f06be00e 217 //\r
f06be00e 218 SHELL_FREE_NON_NULL(DestVolumeInfo);\r
ac8783c8
JC
219 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle);\r
220 return(SHELL_VOLUME_FULL);\r
221 } else {\r
222 //\r
223 // copy data between files\r
224 //\r
225 Buffer = AllocateZeroPool(ReadSize);\r
226 ASSERT(Buffer != NULL);\r
227 while (ReadSize == PcdGet32(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
228 Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);\r
f3a14a0f
SQ
229 if (!EFI_ERROR(Status)) {\r
230 Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
231 if (EFI_ERROR(Status)) {\r
232 ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
233 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR), gShellLevel2HiiHandle, Dest);\r
234 break;\r
235 }\r
236 } else {\r
237 ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
238 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_READ_ERROR), gShellLevel2HiiHandle, Source);\r
239 break;\r
240 }\r
ac8783c8 241 }\r
a405b86d 242 }\r
ac8783c8
JC
243 SHELL_FREE_NON_NULL(DestVolumeInfo);\r
244 }\r
f3a14a0f 245 \r
a405b86d 246 //\r
247 // close files\r
248 //\r
249 if (DestHandle != NULL) {\r
250 ShellCloseFile(&DestHandle);\r
251 DestHandle = NULL;\r
252 }\r
253 if (SourceHandle != NULL) {\r
254 ShellCloseFile(&SourceHandle);\r
255 SourceHandle = NULL;\r
256 }\r
257\r
258 //\r
259 // return\r
260 //\r
e755a4ca 261 return ShellStatus;\r
a405b86d 262}\r
263\r
264/**\r
265 function to take a list of files to copy and a destination location and do\r
266 the verification and copying of those files to that location. This function\r
267 will report any errors to the user and halt.\r
268\r
269 The key is to have this function called ONLY once. this allows for the parameter\r
270 verification to happen correctly.\r
271\r
b54fd049 272 @param[in] FileList A LIST_ENTRY* based list of files to move.\r
273 @param[in] DestDir The destination location.\r
274 @param[in] SilentMode TRUE to eliminate screen output.\r
275 @param[in] RecursiveMode TRUE to copy directories.\r
276 @param[in] Resp The response to the overwrite query (if always).\r
a405b86d 277\r
278 @retval SHELL_SUCCESS the files were all moved.\r
279 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
280 @retval SHELL_SECURITY_VIOLATION a security violation ocurred\r
281 @retval SHELL_WRITE_PROTECTED the destination was write protected\r
282 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
283**/\r
284SHELL_STATUS\r
285EFIAPI\r
286ValidateAndCopyFiles(\r
287 IN CONST EFI_SHELL_FILE_INFO *FileList,\r
288 IN CONST CHAR16 *DestDir,\r
289 IN BOOLEAN SilentMode,\r
290 IN BOOLEAN RecursiveMode,\r
291 IN VOID **Resp\r
292 )\r
293{\r
294 CHAR16 *HiiOutput;\r
295 CHAR16 *HiiResultOk;\r
296 CONST EFI_SHELL_FILE_INFO *Node;\r
297 SHELL_STATUS ShellStatus;\r
0960ba17 298 EFI_STATUS Status;\r
a405b86d 299 CHAR16 *DestPath;\r
300 VOID *Response;\r
e1044f80 301 UINTN PathSize;\r
a405b86d 302 CONST CHAR16 *Cwd;\r
a405b86d 303 UINTN NewSize;\r
0960ba17 304 CHAR16 *CleanFilePathStr;\r
a405b86d 305\r
306 if (Resp == NULL) {\r
307 Response = NULL;\r
308 } else {\r
309 Response = *Resp;\r
310 }\r
311\r
715096c2
QS
312 DestPath = NULL;\r
313 ShellStatus = SHELL_SUCCESS;\r
314 PathSize = 0;\r
315 Cwd = ShellGetCurrentDir(NULL);\r
316 CleanFilePathStr = NULL;\r
a405b86d 317\r
318 ASSERT(FileList != NULL);\r
319 ASSERT(DestDir != NULL);\r
320\r
0960ba17
QS
321 \r
322 Status = ShellLevel2StripQuotes (DestDir, &CleanFilePathStr);\r
323 if (EFI_ERROR (Status)) {\r
324 if (Status == EFI_OUT_OF_RESOURCES) {\r
325 return SHELL_OUT_OF_RESOURCES;\r
326 } else {\r
327 return SHELL_INVALID_PARAMETER;\r
328 }\r
329 } \r
330\r
a405b86d 331 //\r
332 // If we are trying to copy multiple files... make sure we got a directory for the target...\r
333 //\r
0960ba17 334 if (EFI_ERROR(ShellIsDirectory(CleanFilePathStr)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
a405b86d 335 //\r
336 // Error for destination not a directory\r
337 //\r
0960ba17
QS
338 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, CleanFilePathStr);\r
339 FreePool (CleanFilePathStr);\r
a405b86d 340 return (SHELL_INVALID_PARAMETER);\r
341 }\r
342 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
343 ; !IsNull(&FileList->Link, &Node->Link)\r
344 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
f06be00e 345 ){\r
a405b86d 346 //\r
347 // skip the directory traversing stuff...\r
348 //\r
349 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
350 continue;\r
351 }\r
352\r
0960ba17 353 NewSize = StrSize(CleanFilePathStr);\r
b54fd049 354 NewSize += StrSize(Node->FullName);\r
ed053afe 355 NewSize += (Cwd == NULL)? 0 : StrSize(Cwd);\r
e1044f80
JC
356 if (NewSize > PathSize) {\r
357 PathSize = NewSize;\r
a405b86d 358 }\r
359\r
360 //\r
361 // Make sure got -r if required\r
362 //\r
363 if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {\r
364 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle);\r
0960ba17 365 FreePool (CleanFilePathStr);\r
a405b86d 366 return (SHELL_INVALID_PARAMETER);\r
367 }\r
368\r
369 //\r
370 // make sure got dest as dir if needed\r
371 //\r
0960ba17 372 if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(CleanFilePathStr))) {\r
a405b86d 373 //\r
374 // Error for destination not a directory\r
375 //\r
0960ba17
QS
376 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, CleanFilePathStr);\r
377 FreePool (CleanFilePathStr);\r
a405b86d 378 return (SHELL_INVALID_PARAMETER);\r
379 }\r
380 }\r
381\r
382 HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);\r
383 HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
e1044f80 384 DestPath = AllocateZeroPool(PathSize);\r
a405b86d 385\r
3e082d58 386 if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {\r
9ea69f8a 387 SHELL_FREE_NON_NULL(DestPath);\r
388 SHELL_FREE_NON_NULL(HiiOutput);\r
389 SHELL_FREE_NON_NULL(HiiResultOk);\r
0960ba17 390 FreePool (CleanFilePathStr);\r
9ea69f8a 391 return (SHELL_OUT_OF_RESOURCES);\r
392 }\r
393\r
a405b86d 394 //\r
395 // Go through the list of files to copy...\r
396 //\r
397 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
398 ; !IsNull(&FileList->Link, &Node->Link)\r
399 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
f06be00e 400 ){\r
a405b86d 401 if (ShellGetExecutionBreakFlag()) {\r
402 break;\r
403 }\r
404 ASSERT(Node->FileName != NULL);\r
405 ASSERT(Node->FullName != NULL);\r
406\r
407 //\r
408 // skip the directory traversing stuff...\r
409 //\r
410 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
411 continue;\r
412 }\r
413\r
414 if (FileList->Link.ForwardLink == FileList->Link.BackLink // 1 item\r
0960ba17 415 && EFI_ERROR(ShellIsDirectory(CleanFilePathStr)) // not an existing directory\r
f06be00e 416 ) {\r
0960ba17 417 if (StrStr(CleanFilePathStr, L":") == NULL) {\r
b54fd049 418 //\r
419 // simple copy of a single file\r
420 //\r
ed053afe 421 if (Cwd != NULL) {\r
e1044f80 422 StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16)-1);\r
ed053afe 423 } else {\r
0960ba17
QS
424 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
425 FreePool (CleanFilePathStr);\r
ed053afe
ED
426 return (SHELL_INVALID_PARAMETER);\r
427 }\r
0960ba17 428 if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
e1044f80 429 StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
0960ba17 430 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
b54fd049 431 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
432 }\r
0960ba17 433 StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
b54fd049 434 } else {\r
0960ba17 435 StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
a405b86d 436 }\r
a405b86d 437 } else {\r
438 //\r
439 // we have multiple files or a directory in the DestDir\r
440 //\r
1fc3749d 441 \r
442 //\r
443 // Check for leading slash\r
444 //\r
0960ba17 445 if (CleanFilePathStr[0] == L'\\') {\r
ed053afe
ED
446 //\r
447 // Copy to the root of CWD\r
448 //\r
449 if (Cwd != NULL) {\r
e1044f80 450 StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
ed053afe 451 } else {\r
0960ba17
QS
452 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
453 FreePool(CleanFilePathStr);\r
ed053afe
ED
454 return (SHELL_INVALID_PARAMETER);\r
455 }\r
1fc3749d 456 while (PathRemoveLastItem(DestPath));\r
0960ba17 457 StrnCat(DestPath, CleanFilePathStr+1, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
e1044f80 458 StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
0960ba17 459 } else if (StrStr(CleanFilePathStr, L":") == NULL) {\r
ed053afe 460 if (Cwd != NULL) {\r
e1044f80 461 StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
ed053afe 462 } else {\r
0960ba17
QS
463 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, CleanFilePathStr);\r
464 FreePool(CleanFilePathStr);\r
ed053afe
ED
465 return (SHELL_INVALID_PARAMETER);\r
466 }\r
0960ba17 467 if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
e1044f80 468 StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
0960ba17 469 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
a405b86d 470 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
471 }\r
0960ba17
QS
472 StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
473 if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
e1044f80 474 StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
0960ba17 475 } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
a405b86d 476 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
477 }\r
e1044f80 478 StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
a405b86d 479\r
480 } else {\r
0960ba17
QS
481 StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
482 if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
e1044f80 483 StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
0960ba17
QS
484 } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
485 ((CHAR16*)CleanFilePathStr)[StrLen(CleanFilePathStr)-1] = CHAR_NULL;\r
a405b86d 486 }\r
e1044f80 487 StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
a405b86d 488 }\r
489 }\r
0960ba17 490 FreePool (CleanFilePathStr);\r
a405b86d 491 //\r
492 // Make sure the path exists\r
493 //\r
494 if (EFI_ERROR(VerifyIntermediateDirectories(DestPath))) {\r
495 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle);\r
496 ShellStatus = SHELL_DEVICE_ERROR;\r
497 break;\r
498 }\r
499\r
500 if ( !EFI_ERROR(ShellIsDirectory(Node->FullName))\r
501 && !EFI_ERROR(ShellIsDirectory(DestPath))\r
502 && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == NULL\r
f06be00e 503 ){\r
a405b86d 504 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle);\r
505 ShellStatus = SHELL_INVALID_PARAMETER;\r
506 break;\r
507 }\r
508 if (StringNoCaseCompare(&Node->FullName, &DestPath) == 0) {\r
509 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
510 ShellStatus = SHELL_INVALID_PARAMETER;\r
511 break;\r
512 }\r
513\r
5051c287 514 if ((StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName)) == 0)\r
a405b86d 515 && (DestPath[StrLen(Node->FullName)] == CHAR_NULL || DestPath[StrLen(Node->FullName)] == L'\\')\r
f06be00e 516 ) {\r
a405b86d 517 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
518 ShellStatus = SHELL_INVALID_PARAMETER;\r
519 break;\r
520 }\r
521\r
ab94587a 522 PathCleanUpDirectories(DestPath);\r
a405b86d 523\r
a737ea73
JC
524 if (!SilentMode) {\r
525 ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
526 }\r
a405b86d 527\r
528 //\r
529 // copy single file...\r
530 //\r
531 ShellStatus = CopySingleFile(Node->FullName, DestPath, &Response, SilentMode);\r
532 if (ShellStatus != SHELL_SUCCESS) {\r
533 break;\r
534 }\r
535 }\r
536 if (ShellStatus == SHELL_SUCCESS && Resp == NULL) {\r
537 ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
538 }\r
539\r
540 SHELL_FREE_NON_NULL(DestPath);\r
541 SHELL_FREE_NON_NULL(HiiOutput);\r
542 SHELL_FREE_NON_NULL(HiiResultOk);\r
590c3cb1 543 if (Resp == NULL) {\r
a405b86d 544 SHELL_FREE_NON_NULL(Response);\r
545 }\r
546\r
547 return (ShellStatus);\r
f06be00e 548\r
a405b86d 549}\r
550\r
b54fd049 551/**\r
552 Validate and if successful copy all the files from the list into \r
553 destination directory.\r
554\r
555 @param[in] FileList The list of files to copy.\r
556 @param[in] DestDir The directory to copy files to.\r
557 @param[in] SilentMode TRUE to eliminate screen output.\r
558 @param[in] RecursiveMode TRUE to copy directories.\r
559\r
560 @retval SHELL_INVALID_PARAMETER A parameter was invalid.\r
561 @retval SHELL_SUCCESS The operation was successful.\r
562**/\r
a405b86d 563SHELL_STATUS\r
564EFIAPI\r
565ProcessValidateAndCopyFiles(\r
566 IN EFI_SHELL_FILE_INFO *FileList,\r
567 IN CONST CHAR16 *DestDir,\r
568 IN BOOLEAN SilentMode,\r
569 IN BOOLEAN RecursiveMode\r
570 )\r
571{\r
572 SHELL_STATUS ShellStatus;\r
573 EFI_SHELL_FILE_INFO *List;\r
a405b86d 574 EFI_FILE_INFO *FileInfo;\r
ac8783c8 575 CHAR16 *FullName;\r
a405b86d 576\r
ac8783c8
JC
577 List = NULL;\r
578 FullName = NULL;\r
9f56625f 579 FileInfo = NULL;\r
a405b86d 580\r
e755a4ca 581 ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
a405b86d 582 if (List != NULL && List->Link.ForwardLink != List->Link.BackLink) {\r
583 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, DestDir);\r
584 ShellStatus = SHELL_INVALID_PARAMETER;\r
585 ShellCloseFileMetaArg(&List);\r
586 } else if (List != NULL) {\r
a405b86d 587 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink) != NULL);\r
588 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName != NULL);\r
a405b86d 589 FileInfo = gEfiShellProtocol->GetFileInfo(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->Handle);\r
590 ASSERT(FileInfo != NULL);\r
ac8783c8
JC
591 StrnCatGrow(&FullName, NULL, ((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName, 0);\r
592 ShellCloseFileMetaArg(&List);\r
a405b86d 593 if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) == 0) {\r
ac8783c8 594 ShellStatus = ValidateAndCopyFiles(FileList, FullName, SilentMode, RecursiveMode, NULL);\r
a405b86d 595 } else {\r
596 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle);\r
597 ShellStatus = SHELL_ACCESS_DENIED;\r
598 }\r
a405b86d 599 } else {\r
ac8783c8 600 ShellCloseFileMetaArg(&List);\r
ed053afe 601 ShellStatus = ValidateAndCopyFiles(FileList, DestDir, SilentMode, RecursiveMode, NULL);\r
a405b86d 602 }\r
603\r
ac8783c8
JC
604 SHELL_FREE_NON_NULL(FileInfo);\r
605 SHELL_FREE_NON_NULL(FullName);\r
a405b86d 606 return (ShellStatus);\r
607}\r
608\r
609STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
610 {L"-r", TypeFlag},\r
611 {L"-q", TypeFlag},\r
612 {NULL, TypeMax}\r
613 };\r
614\r
615/**\r
616 Function for 'cp' command.\r
617\r
618 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
619 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
620**/\r
621SHELL_STATUS\r
622EFIAPI\r
623ShellCommandRunCp (\r
624 IN EFI_HANDLE ImageHandle,\r
625 IN EFI_SYSTEM_TABLE *SystemTable\r
626 )\r
627{\r
628 EFI_STATUS Status;\r
629 LIST_ENTRY *Package;\r
630 CHAR16 *ProblemParam;\r
631 SHELL_STATUS ShellStatus;\r
632 UINTN ParamCount;\r
633 UINTN LoopCounter;\r
634 EFI_SHELL_FILE_INFO *FileList;\r
635 BOOLEAN SilentMode;\r
636 BOOLEAN RecursiveMode;\r
637 CONST CHAR16 *Cwd;\r
638\r
639 ProblemParam = NULL;\r
640 ShellStatus = SHELL_SUCCESS;\r
641 ParamCount = 0;\r
642 FileList = NULL;\r
643\r
644 //\r
645 // initialize the shell lib (we must be in non-auto-init...)\r
646 //\r
647 Status = ShellInitialize();\r
648 ASSERT_EFI_ERROR(Status);\r
649\r
650 Status = CommandInit();\r
651 ASSERT_EFI_ERROR(Status);\r
652\r
653 //\r
654 // parse the command line\r
655 //\r
656 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
657 if (EFI_ERROR(Status)) {\r
658 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
659 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
660 FreePool(ProblemParam);\r
661 ShellStatus = SHELL_INVALID_PARAMETER;\r
662 } else {\r
663 ASSERT(FALSE);\r
664 }\r
665 } else {\r
666 //\r
667 // check for "-?"\r
668 //\r
669 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
670 ASSERT(FALSE);\r
671 }\r
672\r
673 //\r
674 // Initialize SilentMode and RecursiveMode\r
675 //\r
676 if (gEfiShellProtocol->BatchIsActive()) {\r
677 SilentMode = TRUE;\r
678 } else {\r
679 SilentMode = ShellCommandLineGetFlag(Package, L"-q");\r
680 }\r
681 RecursiveMode = ShellCommandLineGetFlag(Package, L"-r");\r
682\r
683 switch (ParamCount = ShellCommandLineGetCount(Package)) {\r
684 case 0:\r
685 case 1:\r
686 //\r
687 // we have insufficient parameters\r
688 //\r
689 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
690 ShellStatus = SHELL_INVALID_PARAMETER;\r
691 break;\r
692 case 2:\r
693 //\r
694 // must have valid CWD for single parameter...\r
695 //\r
696 Cwd = ShellGetCurrentDir(NULL);\r
697 if (Cwd == NULL){\r
698 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
699 ShellStatus = SHELL_INVALID_PARAMETER;\r
700 } else {\r
701 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
702 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
703 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
704 ShellStatus = SHELL_NOT_FOUND;\r
705 } else {\r
706 ShellStatus = ProcessValidateAndCopyFiles(FileList, Cwd, SilentMode, RecursiveMode);\r
707 }\r
708 }\r
709\r
710 break;\r
711 default:\r
712 //\r
713 // Make a big list of all the files...\r
714 //\r
715 for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount && ShellStatus == SHELL_SUCCESS ; LoopCounter++) {\r
716 if (ShellGetExecutionBreakFlag()) {\r
717 break;\r
718 }\r
719 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
720 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
b2bf9735 721 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, LoopCounter));\r
a405b86d 722 ShellStatus = SHELL_NOT_FOUND;\r
723 }\r
724 }\r
b2bf9735 725 if (ShellStatus != SHELL_SUCCESS) {\r
a405b86d 726 Status = ShellCloseFileMetaArg(&FileList);\r
b2bf9735 727 } else {\r
728 //\r
729 // now copy them all...\r
730 //\r
731 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
ab94587a 732 ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
b2bf9735 733 Status = ShellCloseFileMetaArg(&FileList);\r
734 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
735 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);\r
736 ShellStatus = SHELL_ACCESS_DENIED;\r
737 }\r
a405b86d 738 }\r
739 }\r
a405b86d 740 break;\r
741 } // switch on parameter count\r
742\r
743 if (FileList != NULL) {\r
744 ShellCloseFileMetaArg(&FileList);\r
745 }\r
746\r
747 //\r
748 // free the command line package\r
749 //\r
750 ShellCommandLineFreeVarList (Package);\r
751 }\r
752\r
753 if (ShellGetExecutionBreakFlag()) {\r
754 return (SHELL_ABORTED);\r
755 }\r
756\r
757 return (ShellStatus);\r
758}\r
759\r