]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Follow spec to remove the last '\' char in return name of GetCurDir().
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
... / ...
CommitLineData
1/** @file\r
2 Main file for mv shell level 2 function.\r
3\r
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
5 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiShellLevel2CommandsLib.h"\r
17\r
18/**\r
19 function to determine if a move is between file systems.\r
20 \r
21 @param FullName [in] The name of the file to move.\r
22 @param Cwd [in] The current working directory\r
23 @param DestPath [in] The target location to move to\r
24\r
25 @retval TRUE The move is across file system.\r
26 @retval FALSE The move is within a file system.\r
27**/\r
28BOOLEAN\r
29EFIAPI\r
30IsBetweenFileSystem(\r
31 IN CONST CHAR16 *FullName,\r
32 IN CONST CHAR16 *Cwd,\r
33 IN CONST CHAR16 *DestPath\r
34 )\r
35{\r
36 CHAR16 *Test;\r
37 CHAR16 *Test1;\r
38 UINTN Result;\r
39\r
40 Test = StrStr(FullName, L":");\r
41 if (Test == NULL && Cwd != NULL) {\r
42 Test = StrStr(Cwd, L":");\r
43 }\r
44 Test1 = StrStr(DestPath, L":");\r
45 if (Test1 == NULL && Cwd != NULL) {\r
46 Test1 = StrStr(Cwd, L":");\r
47 }\r
48 if (Test1 != NULL && Test != NULL) {\r
49 *Test = CHAR_NULL;\r
50 *Test1 = CHAR_NULL;\r
51 Result = StringNoCaseCompare(&FullName, &DestPath);\r
52 *Test = L':';\r
53 *Test1 = L':';\r
54 if (Result != 0) {\r
55 return (TRUE);\r
56 }\r
57 }\r
58 return (FALSE);\r
59}\r
60\r
61/**\r
62 Function to validate that moving a specific file (FileName) to a specific\r
63 location (DestPath) is valid.\r
64\r
65 This function will verify that the destination is not a subdirectory of\r
66 FullName, that the Current working Directory is not being moved, and that\r
67 the directory is not read only.\r
68\r
69 if the move is invalid this function will report the error to StdOut.\r
70\r
71 @param SourcePath [in] The name of the file to move.\r
72 @param Cwd [in] The current working directory\r
73 @param DestPath [in] The target location to move to\r
74 @param Attribute [in] The Attribute of the file\r
75 @param DestAttr [in] The Attribute of the destination\r
76 @param FileStatus [in] The Status of the file when opened\r
77\r
78 @retval TRUE The move is valid\r
79 @retval FALSE The move is not\r
80**/\r
81BOOLEAN\r
82EFIAPI\r
83IsValidMove(\r
84 IN CONST CHAR16 *SourcePath,\r
85 IN CONST CHAR16 *Cwd,\r
86 IN CONST CHAR16 *DestPath,\r
87 IN CONST UINT64 Attribute,\r
88 IN CONST UINT64 DestAttr,\r
89 IN CONST EFI_STATUS FileStatus\r
90 )\r
91{\r
92 CHAR16 *DestPathCopy;\r
93 CHAR16 *DestPathWalker;\r
94\r
95 if (Cwd != NULL && StrCmp(SourcePath, Cwd) == 0) {\r
96 //\r
97 // Invalid move\r
98 //\r
99 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_CWD), gShellLevel2HiiHandle);\r
100 return (FALSE);\r
101 }\r
102\r
103 //\r
104 // invalid to move read only or move to a read only destination\r
105 //\r
106 if (((Attribute & EFI_FILE_READ_ONLY) != 0) \r
107 || (FileStatus == EFI_WRITE_PROTECTED)\r
108 || ((DestAttr & EFI_FILE_READ_ONLY) != 0)\r
109 ) {\r
110 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle, SourcePath);\r
111 return (FALSE);\r
112 } \r
113 \r
114 DestPathCopy = AllocateCopyPool(StrSize(DestPath), DestPath);\r
115 if (DestPathCopy == NULL) {\r
116 return (FALSE);\r
117 }\r
118\r
119 for (DestPathWalker = DestPathCopy; *DestPathWalker == L'\\'; DestPathWalker++) ;\r
120\r
121 while(DestPathWalker != NULL && DestPathWalker[StrLen(DestPathWalker)-1] == L'\\') {\r
122 DestPathWalker[StrLen(DestPathWalker)-1] = CHAR_NULL;\r
123 }\r
124\r
125 ASSERT(DestPathWalker != NULL);\r
126 ASSERT(SourcePath != NULL);\r
127\r
128 //\r
129 // If they're the same, or if source is "above" dest on file path tree\r
130 //\r
131 if ( StringNoCaseCompare (&DestPathWalker, &SourcePath) == 0 ||\r
132 ((StrStr(DestPathWalker, SourcePath) == DestPathWalker) && \r
133 (DestPathWalker[StrLen(SourcePath)] == '\\')\r
134 )\r
135 ) {\r
136 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_SUB), gShellLevel2HiiHandle);\r
137 FreePool(DestPathCopy);\r
138 return (FALSE);\r
139 }\r
140 FreePool(DestPathCopy);\r
141\r
142 return (TRUE);\r
143}\r
144\r
145/**\r
146 Function to take a destination path that might contain wildcards and verify\r
147 that there is only a single possible target (IE we cant have wildcards that\r
148 have 2 possible destination).\r
149\r
150 if the result is sucessful the caller must free *DestPathPointer.\r
151\r
152 @param[in] DestParameter The original path to the destination.\r
153 @param[in, out] DestPathPointer A pointer to the callee allocated final path.\r
154 @param[in] Cwd A pointer to the current working directory.\r
155 @param[in] SingleSource TRUE to have only one source file.\r
156 @param[in, out] DestAttr A pointer to the destination information attribute.\r
157\r
158 @retval SHELL_INVALID_PARAMETER The DestParameter could not be resolved to a location.\r
159 @retval SHELL_INVALID_PARAMETER The DestParameter could be resolved to more than 1 location.\r
160 @retval SHELL_INVALID_PARAMETER Cwd is required and is NULL.\r
161 @retval SHELL_SUCCESS The operation was sucessful.\r
162**/\r
163SHELL_STATUS\r
164EFIAPI\r
165GetDestinationLocation(\r
166 IN CONST CHAR16 *DestParameter,\r
167 IN OUT CHAR16 **DestPathPointer,\r
168 IN CONST CHAR16 *Cwd,\r
169 IN CONST BOOLEAN SingleSource,\r
170 IN OUT UINT64 *DestAttr\r
171 )\r
172{\r
173 EFI_SHELL_FILE_INFO *DestList;\r
174 EFI_SHELL_FILE_INFO *Node;\r
175 CHAR16 *DestPath;\r
176 UINTN NewSize;\r
177 UINTN CurrentSize;\r
178\r
179 DestList = NULL;\r
180 DestPath = NULL;\r
181\r
182 ASSERT(DestAttr != NULL);\r
183\r
184 if (StrStr(DestParameter, L"\\") == DestParameter) {\r
185 if (Cwd == NULL) {\r
186 return SHELL_INVALID_PARAMETER;\r
187 }\r
188 DestPath = AllocateZeroPool(StrSize(Cwd));\r
189 if (DestPath == NULL) {\r
190 return (SHELL_OUT_OF_RESOURCES);\r
191 }\r
192 StrCpyS(DestPath, StrSize(Cwd) / sizeof(CHAR16), Cwd);\r
193 while (PathRemoveLastItem(DestPath)) ;\r
194\r
195 //\r
196 // Append DestParameter beyond '\' which may be present\r
197 //\r
198 CurrentSize = StrSize(DestPath);\r
199 StrnCatGrow(&DestPath, &CurrentSize, &DestParameter[1], 0);\r
200\r
201 *DestPathPointer = DestPath;\r
202 return (SHELL_SUCCESS);\r
203 }\r
204 //\r
205 // get the destination path\r
206 //\r
207 ShellOpenFileMetaArg((CHAR16*)DestParameter, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);\r
208 if (DestList == NULL || IsListEmpty(&DestList->Link)) {\r
209 //\r
210 // Not existing... must be renaming\r
211 //\r
212 if (StrStr(DestParameter, L":") == NULL) {\r
213 if (Cwd == NULL) {\r
214 ShellCloseFileMetaArg(&DestList);\r
215 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
216 return (SHELL_INVALID_PARAMETER);\r
217 }\r
218 NewSize = StrSize(Cwd);\r
219 NewSize += StrSize(DestParameter);\r
220 DestPath = AllocateZeroPool(NewSize);\r
221 if (DestPath == NULL) {\r
222 ShellCloseFileMetaArg(&DestList);\r
223 return (SHELL_OUT_OF_RESOURCES);\r
224 }\r
225 StrCpyS(DestPath, NewSize / sizeof(CHAR16), Cwd);\r
226 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestParameter[0] != L'\\') {\r
227 StrCatS(DestPath, NewSize / sizeof(CHAR16), L"\\");\r
228 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestParameter[0] == L'\\') {\r
229 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
230 }\r
231 StrCatS(DestPath, NewSize / sizeof(CHAR16), DestParameter);\r
232 } else {\r
233 ASSERT(DestPath == NULL);\r
234 DestPath = StrnCatGrow(&DestPath, NULL, DestParameter, 0);\r
235 if (DestPath == NULL) {\r
236 ShellCloseFileMetaArg(&DestList);\r
237 return (SHELL_OUT_OF_RESOURCES);\r
238 }\r
239 }\r
240 } else {\r
241 Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&DestList->Link);\r
242 *DestAttr = Node->Info->Attribute;\r
243 //\r
244 // Make sure there is only 1 node in the list.\r
245 //\r
246 if (!IsNodeAtEnd(&DestList->Link, &Node->Link)) {\r
247 ShellCloseFileMetaArg(&DestList);\r
248 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter); \r
249 return (SHELL_INVALID_PARAMETER);\r
250 }\r
251\r
252 //\r
253 // If we are a directory or a single file, then one node is fine.\r
254 //\r
255 if (ShellIsDirectory(Node->FullName)==EFI_SUCCESS || SingleSource) {\r
256 DestPath = AllocateZeroPool(StrSize(Node->FullName)+sizeof(CHAR16));\r
257 if (DestPath == NULL) {\r
258 ShellCloseFileMetaArg(&DestList);\r
259 return (SHELL_OUT_OF_RESOURCES);\r
260 }\r
261 StrCpyS(DestPath, (StrSize(Node->FullName)+sizeof(CHAR16)) / sizeof(CHAR16), Node->FullName);\r
262 StrCatS(DestPath, (StrSize(Node->FullName)+sizeof(CHAR16)) / sizeof(CHAR16), L"\\");\r
263 } else {\r
264 //\r
265 // cant move multiple files onto a single file.\r
266 //\r
267 ShellCloseFileMetaArg(&DestList);\r
268 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_ERROR), gShellLevel2HiiHandle, L"mv", DestParameter); \r
269 return (SHELL_INVALID_PARAMETER);\r
270 }\r
271 }\r
272\r
273 *DestPathPointer = DestPath;\r
274 ShellCloseFileMetaArg(&DestList);\r
275\r
276 return (SHELL_SUCCESS);\r
277}\r
278\r
279/**\r
280 Function to do a move across file systems.\r
281\r
282 @param[in] Node A pointer to the file to be removed.\r
283 @param[in] DestPath A pointer to the destination file path.\r
284 @param[out] Resp A pointer to response from question. Pass back on looped calling\r
285\r
286 @retval SHELL_SUCCESS The source file was moved to the destination.\r
287**/\r
288EFI_STATUS\r
289EFIAPI\r
290MoveBetweenFileSystems(\r
291 IN EFI_SHELL_FILE_INFO *Node,\r
292 IN CONST CHAR16 *DestPath,\r
293 OUT VOID **Resp\r
294 )\r
295{\r
296 SHELL_STATUS ShellStatus;\r
297\r
298 //\r
299 // First we copy the file\r
300 //\r
301 ShellStatus = CopySingleFile (Node->FullName, DestPath, Resp, TRUE, L"mv");\r
302\r
303 //\r
304 // Check our result\r
305 //\r
306 if (ShellStatus == SHELL_SUCCESS) {\r
307 //\r
308 // The copy was successful. delete the source file.\r
309 //\r
310 CascadeDelete(Node, TRUE);\r
311 Node->Handle = NULL;\r
312 } else if (ShellStatus == SHELL_ABORTED) {\r
313 return EFI_ABORTED;\r
314 } else if (ShellStatus == SHELL_ACCESS_DENIED) {\r
315 return EFI_ACCESS_DENIED;\r
316 } else if (ShellStatus == SHELL_VOLUME_FULL) {\r
317 return EFI_VOLUME_FULL;\r
318 } else {\r
319 return EFI_UNSUPPORTED;\r
320 }\r
321\r
322 return (EFI_SUCCESS);\r
323}\r
324\r
325/**\r
326 Function to take the destination path and target file name to generate the full destination path.\r
327\r
328 @param[in] DestPath A pointer to the destination file path string.\r
329 @param[out] FullDestPath A pointer to the full destination path string.\r
330 @param[in] FileName Name string of the targe file.\r
331\r
332 @retval SHELL_SUCCESS the files were all moved.\r
333 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
334 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
335**/\r
336EFI_STATUS\r
337EFIAPI\r
338CreateFullDestPath(\r
339 IN CONST CHAR16 **DestPath,\r
340 OUT CHAR16 **FullDestPath, \r
341 IN CONST CHAR16 *FileName\r
342 )\r
343{\r
344 UINTN Size;\r
345 if (FullDestPath == NULL || FileName == NULL || DestPath == NULL || *DestPath == NULL){\r
346 return (EFI_INVALID_PARAMETER);\r
347 }\r
348\r
349 Size = StrSize(*DestPath) + StrSize(FileName);\r
350\r
351 *FullDestPath = AllocateZeroPool(Size);\r
352 if (*FullDestPath == NULL){\r
353 return (EFI_OUT_OF_RESOURCES);\r
354 }\r
355\r
356 StrCpyS(*FullDestPath, Size / sizeof(CHAR16), *DestPath);\r
357 if ((*FullDestPath)[StrLen(*FullDestPath)-1] != L'\\' && FileName[0] != L'\\') {\r
358 StrCatS(*FullDestPath, Size / sizeof(CHAR16), L"\\");\r
359 }\r
360 StrCatS(*FullDestPath, Size / sizeof(CHAR16), FileName);\r
361\r
362 return (EFI_SUCCESS);\r
363}\r
364\r
365/**\r
366 Function to do a move within a file system.\r
367\r
368 @param[in] Node A pointer to the file to be removed.\r
369 @param[in] DestPath A pointer to the destination file path.\r
370 @param[out] Resp A pointer to response from question. Pass back on looped calling.\r
371\r
372 @retval SHELL_SUCCESS The source file was moved to the destination.\r
373 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
374**/\r
375EFI_STATUS\r
376EFIAPI\r
377MoveWithinFileSystems(\r
378 IN EFI_SHELL_FILE_INFO *Node,\r
379 IN CHAR16 *DestPath,\r
380 OUT VOID **Resp\r
381 )\r
382{\r
383 EFI_FILE_INFO *NewFileInfo;\r
384 CHAR16 *TempLocation;\r
385 UINTN NewSize;\r
386 UINTN Length;\r
387 EFI_STATUS Status;\r
388\r
389 //\r
390 // Chop off map info from DestPath\r
391 //\r
392 if ((TempLocation = StrStr(DestPath, L":")) != NULL) {\r
393 CopyMem(DestPath, TempLocation+1, StrSize(TempLocation+1));\r
394 }\r
395\r
396 //\r
397 // construct the new file info block\r
398 //\r
399 NewSize = StrSize(DestPath);\r
400 NewSize += StrSize(Node->FileName) + SIZE_OF_EFI_FILE_INFO + sizeof(CHAR16);\r
401 NewFileInfo = AllocateZeroPool(NewSize);\r
402 if (NewFileInfo == NULL) {\r
403 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
404 Status = EFI_OUT_OF_RESOURCES;\r
405 } else {\r
406 CopyMem(NewFileInfo, Node->Info, SIZE_OF_EFI_FILE_INFO);\r
407 if (DestPath[0] != L'\\') {\r
408 StrCpyS(NewFileInfo->FileName, (NewSize - SIZE_OF_EFI_FILE_INFO) / sizeof(CHAR16), L"\\");\r
409 StrCatS(NewFileInfo->FileName, (NewSize - SIZE_OF_EFI_FILE_INFO) / sizeof(CHAR16), DestPath);\r
410 } else {\r
411 StrCpyS(NewFileInfo->FileName, (NewSize - SIZE_OF_EFI_FILE_INFO) / sizeof(CHAR16), DestPath);\r
412 }\r
413 Length = StrLen(NewFileInfo->FileName);\r
414 if (Length > 0) {\r
415 Length--;\r
416 }\r
417 if (NewFileInfo->FileName[Length] == L'\\') {\r
418 if (Node->FileName[0] == L'\\') {\r
419 //\r
420 // Don't allow for double slashes. Eliminate one of them.\r
421 //\r
422 NewFileInfo->FileName[Length] = CHAR_NULL;\r
423 }\r
424 StrCatS(NewFileInfo->FileName, (NewSize - SIZE_OF_EFI_FILE_INFO) / sizeof(CHAR16), Node->FileName);\r
425 }\r
426 NewFileInfo->Size = SIZE_OF_EFI_FILE_INFO + StrSize(NewFileInfo->FileName);\r
427\r
428 //\r
429 // Perform the move operation\r
430 //\r
431 Status = ShellSetFileInfo(Node->Handle, NewFileInfo);\r
432\r
433 //\r
434 // Free the info object we used...\r
435 //\r
436 FreePool(NewFileInfo);\r
437 }\r
438\r
439 return (Status);\r
440}\r
441/**\r
442 function to take a list of files to move and a destination location and do\r
443 the verification and moving of those files to that location. This function\r
444 will report any errors to the user and continue to move the rest of the files.\r
445\r
446 @param[in] FileList A LIST_ENTRY* based list of files to move\r
447 @param[out] Resp pointer to response from question. Pass back on looped calling\r
448 @param[in] DestParameter the originally specified destination location\r
449\r
450 @retval SHELL_SUCCESS the files were all moved.\r
451 @retval SHELL_INVALID_PARAMETER a parameter was invalid\r
452 @retval SHELL_SECURITY_VIOLATION a security violation ocurred\r
453 @retval SHELL_WRITE_PROTECTED the destination was write protected\r
454 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed\r
455**/\r
456SHELL_STATUS\r
457EFIAPI\r
458ValidateAndMoveFiles(\r
459 IN EFI_SHELL_FILE_INFO *FileList,\r
460 OUT VOID **Resp,\r
461 IN CONST CHAR16 *DestParameter\r
462 )\r
463{\r
464 EFI_STATUS Status;\r
465 CHAR16 *HiiOutput;\r
466 CHAR16 *HiiResultOk;\r
467 CHAR16 *DestPath;\r
468 CHAR16 *FullDestPath;\r
469 CONST CHAR16 *Cwd;\r
470 CHAR16 *FullCwd;\r
471 SHELL_STATUS ShellStatus;\r
472 EFI_SHELL_FILE_INFO *Node;\r
473 VOID *Response;\r
474 UINT64 Attr;\r
475 CHAR16 *CleanFilePathStr;\r
476\r
477 ASSERT(FileList != NULL);\r
478 ASSERT(DestParameter != NULL);\r
479\r
480 DestPath = NULL;\r
481 FullDestPath = NULL;\r
482 Cwd = ShellGetCurrentDir(NULL);\r
483 Response = *Resp;\r
484 Attr = 0;\r
485 CleanFilePathStr = NULL;\r
486\r
487 FullCwd = AllocateZeroPool(StrSize(Cwd) + sizeof(CHAR16));\r
488 if (FullCwd == NULL) {\r
489 return SHELL_OUT_OF_RESOURCES;\r
490 } else {\r
491 StrCpyS(FullCwd, StrSize(Cwd)/sizeof(CHAR16)+1, Cwd);\r
492 StrCatS(FullCwd, StrSize(Cwd)/sizeof(CHAR16)+1, L"\\");\r
493 }\r
494\r
495 Status = ShellLevel2StripQuotes (DestParameter, &CleanFilePathStr);\r
496 if (EFI_ERROR (Status)) {\r
497 FreePool (FullCwd);\r
498 if (Status == EFI_OUT_OF_RESOURCES) {\r
499 return SHELL_OUT_OF_RESOURCES;\r
500 } else {\r
501 return SHELL_INVALID_PARAMETER;\r
502 }\r
503 }\r
504\r
505 ASSERT (CleanFilePathStr != NULL);\r
506\r
507 //\r
508 // Get and validate the destination location\r
509 //\r
510 ShellStatus = GetDestinationLocation(CleanFilePathStr, &DestPath, FullCwd, (BOOLEAN)(FileList->Link.ForwardLink == FileList->Link.BackLink), &Attr);\r
511 FreePool (CleanFilePathStr);\r
512\r
513 if (ShellStatus != SHELL_SUCCESS) {\r
514 FreePool (FullCwd);\r
515 return (ShellStatus);\r
516 }\r
517 DestPath = PathCleanUpDirectories(DestPath);\r
518 if (DestPath == NULL) {\r
519 FreePool (FullCwd);\r
520 return (SHELL_OUT_OF_RESOURCES);\r
521 }\r
522\r
523 HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_MV_OUTPUT), NULL);\r
524 HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
525 if (HiiOutput == NULL || HiiResultOk == NULL) {\r
526 SHELL_FREE_NON_NULL(DestPath);\r
527 SHELL_FREE_NON_NULL(HiiOutput);\r
528 SHELL_FREE_NON_NULL(HiiResultOk);\r
529 FreePool (FullCwd);\r
530 return (SHELL_OUT_OF_RESOURCES);\r
531 }\r
532\r
533 //\r
534 // Go through the list of files and directories to move...\r
535 //\r
536 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
537 ; !IsNull(&FileList->Link, &Node->Link)\r
538 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
539 ){\r
540 if (ShellGetExecutionBreakFlag()) {\r
541 break;\r
542 }\r
543\r
544 //\r
545 // These should never be NULL\r
546 //\r
547 ASSERT(Node->FileName != NULL);\r
548 ASSERT(Node->FullName != NULL);\r
549 ASSERT(Node->Info != NULL);\r
550\r
551 //\r
552 // skip the directory traversing stuff...\r
553 //\r
554 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {\r
555 continue;\r
556 }\r
557\r
558 SHELL_FREE_NON_NULL(FullDestPath);\r
559 FullDestPath = NULL;\r
560 if (ShellIsDirectory(DestPath)==EFI_SUCCESS) {\r
561 CreateFullDestPath((CONST CHAR16 **)&DestPath, &FullDestPath, Node->FileName);\r
562 }\r
563\r
564 //\r
565 // Validate that the move is valid\r
566 //\r
567 if (!IsValidMove(Node->FullName, FullCwd, FullDestPath!=NULL? FullDestPath:DestPath, Node->Info->Attribute, Attr, Node->Status)) {\r
568 ShellStatus = SHELL_INVALID_PARAMETER;\r
569 continue;\r
570 }\r
571\r
572 ShellPrintEx(-1, -1, HiiOutput, Node->FullName, FullDestPath!=NULL? FullDestPath:DestPath);\r
573\r
574 //\r
575 // See if destination exists\r
576 //\r
577 if (!EFI_ERROR(ShellFileExists(FullDestPath!=NULL? FullDestPath:DestPath))) {\r
578 if (Response == NULL) {\r
579 ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);\r
580 }\r
581 switch (*(SHELL_PROMPT_RESPONSE*)Response) {\r
582 case ShellPromptResponseNo:\r
583 FreePool(Response);\r
584 Response = NULL;\r
585 continue;\r
586 case ShellPromptResponseCancel:\r
587 *Resp = Response;\r
588 //\r
589 // indicate to stop everything\r
590 //\r
591 FreePool(FullCwd);\r
592 return (SHELL_ABORTED);\r
593 case ShellPromptResponseAll:\r
594 *Resp = Response;\r
595 break;\r
596 case ShellPromptResponseYes:\r
597 FreePool(Response);\r
598 Response = NULL;\r
599 break;\r
600 default:\r
601 FreePool(Response);\r
602 FreePool(FullCwd);\r
603 return SHELL_ABORTED;\r
604 }\r
605 Status = ShellDeleteFileByName(FullDestPath!=NULL? FullDestPath:DestPath);\r
606 }\r
607\r
608 if (IsBetweenFileSystem(Node->FullName, FullCwd, DestPath)) {\r
609 while (FullDestPath == NULL && DestPath != NULL && DestPath[0] != CHAR_NULL && DestPath[StrLen(DestPath) - 1] == L'\\') {\r
610 DestPath[StrLen(DestPath) - 1] = CHAR_NULL;\r
611 }\r
612 Status = MoveBetweenFileSystems(Node, FullDestPath!=NULL? FullDestPath:DestPath, &Response);\r
613 } else {\r
614 Status = MoveWithinFileSystems(Node, DestPath, &Response);\r
615 //\r
616 // Display error status\r
617 //\r
618 if (EFI_ERROR(Status)) {\r
619 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, L"mv", Status);\r
620 }\r
621 }\r
622\r
623 //\r
624 // Check our result\r
625 //\r
626 if (EFI_ERROR(Status)) {\r
627 ShellStatus = SHELL_INVALID_PARAMETER;\r
628 if (Status == EFI_SECURITY_VIOLATION) {\r
629 ShellStatus = SHELL_SECURITY_VIOLATION;\r
630 } else if (Status == EFI_WRITE_PROTECTED) {\r
631 ShellStatus = SHELL_WRITE_PROTECTED;\r
632 } else if (Status == EFI_OUT_OF_RESOURCES) {\r
633 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
634 } else if (Status == EFI_DEVICE_ERROR) {\r
635 ShellStatus = SHELL_DEVICE_ERROR;\r
636 } else if (Status == EFI_ACCESS_DENIED) {\r
637 ShellStatus = SHELL_ACCESS_DENIED;\r
638 }\r
639 } else {\r
640 ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
641 }\r
642\r
643 } // main for loop\r
644\r
645 SHELL_FREE_NON_NULL(FullDestPath);\r
646 SHELL_FREE_NON_NULL(DestPath);\r
647 SHELL_FREE_NON_NULL(HiiOutput);\r
648 SHELL_FREE_NON_NULL(HiiResultOk);\r
649 FreePool (FullCwd);\r
650 return (ShellStatus);\r
651}\r
652\r
653/**\r
654 Function for 'mv' command.\r
655\r
656 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
657 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
658**/\r
659SHELL_STATUS\r
660EFIAPI\r
661ShellCommandRunMv (\r
662 IN EFI_HANDLE ImageHandle,\r
663 IN EFI_SYSTEM_TABLE *SystemTable\r
664 )\r
665{\r
666 EFI_STATUS Status;\r
667 LIST_ENTRY *Package;\r
668 CHAR16 *ProblemParam;\r
669 CHAR16 *Cwd;\r
670 UINTN CwdSize;\r
671 SHELL_STATUS ShellStatus;\r
672 UINTN ParamCount;\r
673 UINTN LoopCounter;\r
674 EFI_SHELL_FILE_INFO *FileList;\r
675 VOID *Response;\r
676\r
677 ProblemParam = NULL;\r
678 ShellStatus = SHELL_SUCCESS;\r
679 ParamCount = 0;\r
680 FileList = NULL;\r
681 Response = NULL;\r
682\r
683 //\r
684 // initialize the shell lib (we must be in non-auto-init...)\r
685 //\r
686 Status = ShellInitialize();\r
687 ASSERT_EFI_ERROR(Status);\r
688\r
689 //\r
690 // parse the command line\r
691 //\r
692 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r
693 if (EFI_ERROR(Status)) {\r
694 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
695 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"mv", ProblemParam); \r
696 FreePool(ProblemParam);\r
697 ShellStatus = SHELL_INVALID_PARAMETER;\r
698 } else {\r
699 ASSERT(FALSE);\r
700 }\r
701 } else {\r
702 //\r
703 // check for "-?"\r
704 //\r
705 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
706 ASSERT(FALSE);\r
707 }\r
708\r
709 switch (ParamCount = ShellCommandLineGetCount(Package)) {\r
710 case 0:\r
711 case 1:\r
712 //\r
713 // we have insufficient parameters\r
714 //\r
715 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mv"); \r
716 ShellStatus = SHELL_INVALID_PARAMETER;\r
717 break;\r
718 case 2:\r
719 //\r
720 // must have valid CWD for single parameter...\r
721 //\r
722 if (ShellGetCurrentDir(NULL) == NULL){\r
723 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"mv"); \r
724 ShellStatus = SHELL_INVALID_PARAMETER;\r
725 } else {\r
726 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
727 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
728 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, 1)); \r
729 ShellStatus = SHELL_NOT_FOUND;\r
730 } else {\r
731 //\r
732 // ValidateAndMoveFiles will report errors to the screen itself\r
733 //\r
734 CwdSize = StrSize(ShellGetCurrentDir(NULL)) + 1;\r
735 Cwd = AllocateZeroPool(CwdSize);\r
736 ASSERT (Cwd != NULL);\r
737 StrCpyS(Cwd, CwdSize/sizeof(CHAR16), ShellGetCurrentDir(NULL));\r
738 StrCatS(Cwd, CwdSize/sizeof(CHAR16), L"\\");\r
739 ShellStatus = ValidateAndMoveFiles(FileList, &Response, Cwd);\r
740 FreePool(Cwd);\r
741 }\r
742 }\r
743\r
744 break;\r
745 default:\r
746 ///@todo make sure this works with error half way through and continues...\r
747 for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount ; LoopCounter++) {\r
748 if (ShellGetExecutionBreakFlag()) {\r
749 break;\r
750 }\r
751 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
752 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
753 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, LoopCounter)); \r
754 ShellStatus = SHELL_NOT_FOUND;\r
755 } else {\r
756 //\r
757 // ValidateAndMoveFiles will report errors to the screen itself\r
758 // Only change ShellStatus if it's sucessful\r
759 //\r
760 if (ShellStatus == SHELL_SUCCESS) {\r
761 ShellStatus = ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));\r
762 } else {\r
763 ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));\r
764 }\r
765 }\r
766 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
767 Status = ShellCloseFileMetaArg(&FileList);\r
768 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
769 ShellStatus = SHELL_ACCESS_DENIED;\r
770 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"mv", ShellCommandLineGetRawValue(Package, 1), ShellStatus|MAX_BIT); \r
771 }\r
772 }\r
773 }\r
774 break;\r
775 } // switch on parameter count\r
776\r
777 if (FileList != NULL) {\r
778 ShellCloseFileMetaArg(&FileList);\r
779 }\r
780\r
781 //\r
782 // free the command line package\r
783 //\r
784 ShellCommandLineFreeVarList (Package);\r
785 }\r
786\r
787 SHELL_FREE_NON_NULL(Response);\r
788\r
789 if (ShellGetExecutionBreakFlag()) {\r
790 return (SHELL_ABORTED);\r
791 }\r
792\r
793 return (ShellStatus);\r
794}\r