]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
BaseTools: Update nmake Makefile to handle the file path with “:\\”.
[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
a6e84d95 490 \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
a6e84d95 543 SHELL_FREE_NON_NULL(CleanFilePathStr);\r
590c3cb1 544 if (Resp == NULL) {\r
a405b86d 545 SHELL_FREE_NON_NULL(Response);\r
546 }\r
547\r
548 return (ShellStatus);\r
f06be00e 549\r
a405b86d 550}\r
551\r
b54fd049 552/**\r
553 Validate and if successful copy all the files from the list into \r
554 destination directory.\r
555\r
556 @param[in] FileList The list of files to copy.\r
557 @param[in] DestDir The directory to copy files to.\r
558 @param[in] SilentMode TRUE to eliminate screen output.\r
559 @param[in] RecursiveMode TRUE to copy directories.\r
560\r
561 @retval SHELL_INVALID_PARAMETER A parameter was invalid.\r
562 @retval SHELL_SUCCESS The operation was successful.\r
563**/\r
a405b86d 564SHELL_STATUS\r
565EFIAPI\r
566ProcessValidateAndCopyFiles(\r
567 IN EFI_SHELL_FILE_INFO *FileList,\r
568 IN CONST CHAR16 *DestDir,\r
569 IN BOOLEAN SilentMode,\r
570 IN BOOLEAN RecursiveMode\r
571 )\r
572{\r
573 SHELL_STATUS ShellStatus;\r
574 EFI_SHELL_FILE_INFO *List;\r
a405b86d 575 EFI_FILE_INFO *FileInfo;\r
ac8783c8 576 CHAR16 *FullName;\r
a405b86d 577\r
ac8783c8
JC
578 List = NULL;\r
579 FullName = NULL;\r
9f56625f 580 FileInfo = NULL;\r
a405b86d 581\r
e755a4ca 582 ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
a405b86d 583 if (List != NULL && List->Link.ForwardLink != List->Link.BackLink) {\r
584 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, DestDir);\r
585 ShellStatus = SHELL_INVALID_PARAMETER;\r
586 ShellCloseFileMetaArg(&List);\r
587 } else if (List != NULL) {\r
a405b86d 588 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink) != NULL);\r
589 ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName != NULL);\r
a405b86d 590 FileInfo = gEfiShellProtocol->GetFileInfo(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->Handle);\r
591 ASSERT(FileInfo != NULL);\r
ac8783c8
JC
592 StrnCatGrow(&FullName, NULL, ((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName, 0);\r
593 ShellCloseFileMetaArg(&List);\r
a405b86d 594 if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) == 0) {\r
ac8783c8 595 ShellStatus = ValidateAndCopyFiles(FileList, FullName, SilentMode, RecursiveMode, NULL);\r
a405b86d 596 } else {\r
597 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle);\r
598 ShellStatus = SHELL_ACCESS_DENIED;\r
599 }\r
a405b86d 600 } else {\r
ac8783c8 601 ShellCloseFileMetaArg(&List);\r
ed053afe 602 ShellStatus = ValidateAndCopyFiles(FileList, DestDir, SilentMode, RecursiveMode, NULL);\r
a405b86d 603 }\r
604\r
ac8783c8
JC
605 SHELL_FREE_NON_NULL(FileInfo);\r
606 SHELL_FREE_NON_NULL(FullName);\r
a405b86d 607 return (ShellStatus);\r
608}\r
609\r
610STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
611 {L"-r", TypeFlag},\r
612 {L"-q", TypeFlag},\r
613 {NULL, TypeMax}\r
614 };\r
615\r
616/**\r
617 Function for 'cp' command.\r
618\r
619 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
620 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
621**/\r
622SHELL_STATUS\r
623EFIAPI\r
624ShellCommandRunCp (\r
625 IN EFI_HANDLE ImageHandle,\r
626 IN EFI_SYSTEM_TABLE *SystemTable\r
627 )\r
628{\r
629 EFI_STATUS Status;\r
630 LIST_ENTRY *Package;\r
631 CHAR16 *ProblemParam;\r
632 SHELL_STATUS ShellStatus;\r
633 UINTN ParamCount;\r
634 UINTN LoopCounter;\r
635 EFI_SHELL_FILE_INFO *FileList;\r
636 BOOLEAN SilentMode;\r
637 BOOLEAN RecursiveMode;\r
638 CONST CHAR16 *Cwd;\r
639\r
640 ProblemParam = NULL;\r
641 ShellStatus = SHELL_SUCCESS;\r
642 ParamCount = 0;\r
643 FileList = NULL;\r
644\r
645 //\r
646 // initialize the shell lib (we must be in non-auto-init...)\r
647 //\r
648 Status = ShellInitialize();\r
649 ASSERT_EFI_ERROR(Status);\r
650\r
651 Status = CommandInit();\r
652 ASSERT_EFI_ERROR(Status);\r
653\r
654 //\r
655 // parse the command line\r
656 //\r
657 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
658 if (EFI_ERROR(Status)) {\r
659 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
660 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
661 FreePool(ProblemParam);\r
662 ShellStatus = SHELL_INVALID_PARAMETER;\r
663 } else {\r
664 ASSERT(FALSE);\r
665 }\r
666 } else {\r
667 //\r
668 // check for "-?"\r
669 //\r
670 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
671 ASSERT(FALSE);\r
672 }\r
673\r
674 //\r
675 // Initialize SilentMode and RecursiveMode\r
676 //\r
677 if (gEfiShellProtocol->BatchIsActive()) {\r
678 SilentMode = TRUE;\r
679 } else {\r
680 SilentMode = ShellCommandLineGetFlag(Package, L"-q");\r
681 }\r
682 RecursiveMode = ShellCommandLineGetFlag(Package, L"-r");\r
683\r
684 switch (ParamCount = ShellCommandLineGetCount(Package)) {\r
685 case 0:\r
686 case 1:\r
687 //\r
688 // we have insufficient parameters\r
689 //\r
690 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
691 ShellStatus = SHELL_INVALID_PARAMETER;\r
692 break;\r
693 case 2:\r
694 //\r
695 // must have valid CWD for single parameter...\r
696 //\r
697 Cwd = ShellGetCurrentDir(NULL);\r
698 if (Cwd == NULL){\r
699 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
700 ShellStatus = SHELL_INVALID_PARAMETER;\r
701 } else {\r
702 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
703 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
704 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
705 ShellStatus = SHELL_NOT_FOUND;\r
706 } else {\r
707 ShellStatus = ProcessValidateAndCopyFiles(FileList, Cwd, SilentMode, RecursiveMode);\r
708 }\r
709 }\r
710\r
711 break;\r
712 default:\r
713 //\r
714 // Make a big list of all the files...\r
715 //\r
716 for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount && ShellStatus == SHELL_SUCCESS ; LoopCounter++) {\r
717 if (ShellGetExecutionBreakFlag()) {\r
718 break;\r
719 }\r
720 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
721 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
b2bf9735 722 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, LoopCounter));\r
a405b86d 723 ShellStatus = SHELL_NOT_FOUND;\r
724 }\r
725 }\r
b2bf9735 726 if (ShellStatus != SHELL_SUCCESS) {\r
a405b86d 727 Status = ShellCloseFileMetaArg(&FileList);\r
b2bf9735 728 } else {\r
729 //\r
730 // now copy them all...\r
731 //\r
732 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
ab94587a 733 ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
b2bf9735 734 Status = ShellCloseFileMetaArg(&FileList);\r
735 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
736 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);\r
737 ShellStatus = SHELL_ACCESS_DENIED;\r
738 }\r
a405b86d 739 }\r
740 }\r
a405b86d 741 break;\r
742 } // switch on parameter count\r
743\r
744 if (FileList != NULL) {\r
745 ShellCloseFileMetaArg(&FileList);\r
746 }\r
747\r
748 //\r
749 // free the command line package\r
750 //\r
751 ShellCommandLineFreeVarList (Package);\r
752 }\r
753\r
754 if (ShellGetExecutionBreakFlag()) {\r
755 return (SHELL_ABORTED);\r
756 }\r
757\r
758 return (ShellStatus);\r
759}\r
760\r