]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg: Remove redundant quotes in file path string for Shell command parameters.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Mv.c
1 /** @file
2 Main file for mv shell level 2 function.
3
4 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.
5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellLevel2CommandsLib.h"
17
18 /**
19 Function to validate that moving a specific file (FileName) to a specific
20 location (DestPath) is valid.
21
22 This function will verify that the destination is not a subdirectory of
23 FullName, that the Current working Directory is not being moved, and that
24 the directory is not read only.
25
26 if the move is invalid this function will report the error to StdOut.
27
28 @param FullName [in] The name of the file to move.
29 @param Cwd [in] The current working directory
30 @param DestPath [in] The target location to move to
31 @param Attribute[in] The Attribute of the file
32 @param FileStatus[in] The Status of the file when opened
33
34 @retval TRUE The move is valid
35 @retval FALSE The move is not
36 **/
37 BOOLEAN
38 EFIAPI
39 IsValidMove(
40 IN CONST CHAR16 *FullName,
41 IN CONST CHAR16 *Cwd,
42 IN CONST CHAR16 *DestPath,
43 IN CONST UINT64 Attribute,
44 IN CONST EFI_STATUS FileStatus
45 )
46 {
47 CHAR16 *Test;
48 CHAR16 *Test1;
49 CHAR16 *TestWalker;
50 INTN Result;
51 UINTN TempLen;
52 if (Cwd != NULL && StrCmp(FullName, Cwd) == 0) {
53 //
54 // Invalid move
55 //
56 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_CWD), gShellLevel2HiiHandle);
57 return (FALSE);
58 }
59 Test = NULL;
60 Test = StrnCatGrow(&Test, NULL, DestPath, 0);
61 TestWalker = Test;
62 ASSERT(TestWalker != NULL);
63 while(*TestWalker == L'\\') {
64 TestWalker++;
65 }
66 while(TestWalker != NULL && TestWalker[StrLen(TestWalker)-1] == L'\\') {
67 TestWalker[StrLen(TestWalker)-1] = CHAR_NULL;
68 }
69 ASSERT(TestWalker != NULL);
70 ASSERT(FullName != NULL);
71 if (StrStr(FullName, TestWalker) != 0) {
72 TempLen = StrLen(FullName);
73 if (StrStr(FullName, TestWalker) != FullName // not the first items... (could below it)
74 && TempLen <= (StrLen(TestWalker) + 1)
75 && StrStr(FullName+StrLen(TestWalker) + 1, L"\\") == NULL) {
76 //
77 // Invalid move
78 //
79 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_SUB), gShellLevel2HiiHandle);
80 FreePool(Test);
81 return (FALSE);
82 }
83 }
84 FreePool(Test);
85 if (StrStr(DestPath, FullName) != 0 && StrStr(DestPath, FullName) != DestPath) {
86 //
87 // Invalid move
88 //
89 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_SUB), gShellLevel2HiiHandle);
90 return (FALSE);
91 }
92 if (((Attribute & EFI_FILE_READ_ONLY) != 0) || (FileStatus == EFI_WRITE_PROTECTED)) {
93 //
94 // invalid to move read only
95 //
96 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_RO), gShellLevel2HiiHandle, FullName);
97 return (FALSE);
98 }
99 Test = StrStr(FullName, L":");
100 Test1 = StrStr(DestPath, L":");
101 if (Test1 != NULL && Test != NULL) {
102 *Test = CHAR_NULL;
103 *Test1 = CHAR_NULL;
104 Result = StringNoCaseCompare(&FullName, &DestPath);
105 *Test = L':';
106 *Test1 = L':';
107 if (Result != 0) {
108 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_FS), gShellLevel2HiiHandle);
109 return (FALSE);
110 }
111 }
112 return (TRUE);
113 }
114
115 /**
116 Function to take a destination path that might contain wildcards and verify
117 that there is only a single possible target (IE we cant have wildcards that
118 have 2 possible destination).
119
120 if the result is sucessful the caller must free *DestPathPointer.
121
122 @param[in] DestDir The original path to the destination.
123 @param[in, out] DestPathPointer A pointer to the callee allocated final path.
124 @param[in] Cwd A pointer to the current working directory.
125
126 @retval SHELL_INVALID_PARAMETER The DestDir could not be resolved to a location.
127 @retval SHELL_INVALID_PARAMETER The DestDir could be resolved to more than 1 location.
128 @retval SHELL_INVALID_PARAMETER Cwd is required and is NULL.
129 @retval SHELL_SUCCESS The operation was sucessful.
130 **/
131 SHELL_STATUS
132 EFIAPI
133 GetDestinationLocation(
134 IN CONST CHAR16 *DestDir,
135 IN OUT CHAR16 **DestPathPointer,
136 IN CONST CHAR16 *Cwd
137 )
138 {
139 EFI_SHELL_FILE_INFO *DestList;
140 EFI_SHELL_FILE_INFO *Node;
141 CHAR16 *DestPath;
142 UINTN NewSize;
143 UINTN CurrentSize;
144
145 DestList = NULL;
146 DestPath = NULL;
147
148 if (StrStr(DestDir, L"\\") == DestDir) {
149 if (Cwd == NULL) {
150 return SHELL_INVALID_PARAMETER;
151 }
152 DestPath = AllocateZeroPool(StrSize(Cwd));
153 if (DestPath == NULL) {
154 return (SHELL_OUT_OF_RESOURCES);
155 }
156 StrCpy(DestPath, Cwd);
157 while (PathRemoveLastItem(DestPath)) ;
158
159 //
160 // Append DestDir beyond '\' which may be present
161 //
162 CurrentSize = StrSize(DestPath);
163 StrnCatGrow(&DestPath, &CurrentSize, &DestDir[1], 0);
164
165 *DestPathPointer = DestPath;
166 return (SHELL_SUCCESS);
167 }
168 //
169 // get the destination path
170 //
171 ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE, &DestList);
172 if (DestList == NULL || IsListEmpty(&DestList->Link)) {
173 //
174 // Not existing... must be renaming
175 //
176 if (StrStr(DestDir, L":") == NULL) {
177 if (Cwd == NULL) {
178 ShellCloseFileMetaArg(&DestList);
179 return (SHELL_INVALID_PARAMETER);
180 }
181 NewSize = StrSize(Cwd);
182 NewSize += StrSize(DestDir);
183 DestPath = AllocateZeroPool(NewSize);
184 if (DestPath == NULL) {
185 ShellCloseFileMetaArg(&DestList);
186 return (SHELL_OUT_OF_RESOURCES);
187 }
188 StrCpy(DestPath, Cwd);
189 if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {
190 StrCat(DestPath, L"\\");
191 } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {
192 ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
193 }
194 StrCat(DestPath, DestDir);
195 } else {
196 ASSERT(DestPath == NULL);
197 DestPath = StrnCatGrow(&DestPath, NULL, DestDir, 0);
198 if (DestPath == NULL) {
199 ShellCloseFileMetaArg(&DestList);
200 return (SHELL_OUT_OF_RESOURCES);
201 }
202 }
203 } else {
204 Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&DestList->Link);
205 //
206 // Make sure there is only 1 node in the list.
207 //
208 if (!IsNodeAtEnd(&DestList->Link, &Node->Link)) {
209 ShellCloseFileMetaArg(&DestList);
210 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, DestDir);
211 return (SHELL_INVALID_PARAMETER);
212 }
213 if (ShellIsDirectory(Node->FullName)==EFI_SUCCESS) {
214 DestPath = AllocateZeroPool(StrSize(Node->FullName)+sizeof(CHAR16));
215 if (DestPath == NULL) {
216 ShellCloseFileMetaArg(&DestList);
217 return (SHELL_OUT_OF_RESOURCES);
218 }
219 StrCpy(DestPath, Node->FullName);
220 StrCat(DestPath, L"\\");
221 } else {
222 //
223 // cant move onto another file.
224 //
225 ShellCloseFileMetaArg(&DestList);
226 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_ERROR), gShellLevel2HiiHandle, DestDir);
227 return (SHELL_INVALID_PARAMETER);
228 }
229 }
230
231 *DestPathPointer = DestPath;
232 ShellCloseFileMetaArg(&DestList);
233
234 return (SHELL_SUCCESS);
235 }
236
237 /**
238 function to take a list of files to move and a destination location and do
239 the verification and moving of those files to that location. This function
240 will report any errors to the user and continue to move the rest of the files.
241
242 @param[in] FileList A LIST_ENTRY* based list of files to move
243 @param[out] Resp pointer to response from question. Pass back on looped calling
244 @param[in] DestDir the destination location
245
246 @retval SHELL_SUCCESS the files were all moved.
247 @retval SHELL_INVALID_PARAMETER a parameter was invalid
248 @retval SHELL_SECURITY_VIOLATION a security violation ocurred
249 @retval SHELL_WRITE_PROTECTED the destination was write protected
250 @retval SHELL_OUT_OF_RESOURCES a memory allocation failed
251 **/
252 SHELL_STATUS
253 EFIAPI
254 ValidateAndMoveFiles(
255 IN CONST EFI_SHELL_FILE_INFO *FileList,
256 OUT VOID **Resp,
257 IN CONST CHAR16 *DestDir
258 )
259 {
260 EFI_STATUS Status;
261 CHAR16 *HiiOutput;
262 CHAR16 *HiiResultOk;
263 CHAR16 *DestPath;
264 CONST CHAR16 *Cwd;
265 SHELL_STATUS ShellStatus;
266 CONST EFI_SHELL_FILE_INFO *Node;
267 EFI_FILE_INFO *NewFileInfo;
268 CHAR16 *TempLocation;
269 UINTN NewSize;
270 UINTN Length;
271 VOID *Response;
272 SHELL_FILE_HANDLE DestHandle;
273 CHAR16 *CleanFilePathStr;
274
275 ASSERT(FileList != NULL);
276 ASSERT(DestDir != NULL);
277
278 DestPath = NULL;
279 Cwd = ShellGetCurrentDir(NULL);
280 Response = *Resp;
281
282 Status = ShellLevel2StripQuotes (DestDir, &CleanFilePathStr);
283 if (EFI_ERROR (Status)) {
284 if (Status == EFI_OUT_OF_RESOURCES) {
285 return SHELL_OUT_OF_RESOURCES;
286 } else {
287 return SHELL_INVALID_PARAMETER;
288 }
289 }
290
291 //
292 // Get and validate the destination location
293 //
294 ShellStatus = GetDestinationLocation(CleanFilePathStr, &DestPath, Cwd);
295 FreePool (CleanFilePathStr);
296 if (ShellStatus != SHELL_SUCCESS) {
297 return (ShellStatus);
298 }
299 DestPath = PathCleanUpDirectories(DestPath);
300
301 HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_MV_OUTPUT), NULL);
302 HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);
303 ASSERT (DestPath != NULL);
304 ASSERT (HiiResultOk != NULL);
305 ASSERT (HiiOutput != NULL);
306
307 //
308 // Go through the list of files and directories to move...
309 //
310 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
311 ; !IsNull(&FileList->Link, &Node->Link)
312 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)
313 ){
314 if (ShellGetExecutionBreakFlag()) {
315 break;
316 }
317
318 //
319 // These should never be NULL
320 //
321 ASSERT(Node->FileName != NULL);
322 ASSERT(Node->FullName != NULL);
323 ASSERT(Node->Info != NULL);
324
325 //
326 // skip the directory traversing stuff...
327 //
328 if (StrCmp(Node->FileName, L".") == 0 || StrCmp(Node->FileName, L"..") == 0) {
329 continue;
330 }
331
332 //
333 // Validate that the move is valid
334 //
335 if (!IsValidMove(Node->FullName, Cwd, DestPath, Node->Info->Attribute, Node->Status)) {
336 ShellStatus = SHELL_INVALID_PARAMETER;
337 continue;
338 }
339
340 //
341 // Chop off map info from "DestPath"
342 //
343 if ((TempLocation = StrStr(DestPath, L":")) != NULL) {
344 CopyMem(DestPath, TempLocation+1, StrSize(TempLocation+1));
345 }
346
347 //
348 // construct the new file info block
349 //
350 NewSize = StrSize(DestPath);
351 NewSize += StrSize(Node->FileName) + SIZE_OF_EFI_FILE_INFO + sizeof(CHAR16);
352 NewFileInfo = AllocateZeroPool(NewSize);
353 if (NewFileInfo == NULL) {
354 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);
355 ShellStatus = SHELL_OUT_OF_RESOURCES;
356 } else {
357 CopyMem(NewFileInfo, Node->Info, SIZE_OF_EFI_FILE_INFO);
358 if (DestPath[0] != L'\\') {
359 StrCpy(NewFileInfo->FileName, L"\\");
360 StrCat(NewFileInfo->FileName, DestPath);
361 } else {
362 StrCpy(NewFileInfo->FileName, DestPath);
363 }
364 Length = StrLen(NewFileInfo->FileName);
365 if (Length > 0) {
366 Length--;
367 }
368 if (NewFileInfo->FileName[Length] == L'\\') {
369 if (Node->FileName[0] == L'\\') {
370 //
371 // Don't allow for double slashes. Eliminate one of them.
372 //
373 NewFileInfo->FileName[Length] = CHAR_NULL;
374 }
375 StrCat(NewFileInfo->FileName, Node->FileName);
376 }
377 NewFileInfo->Size = SIZE_OF_EFI_FILE_INFO + StrSize(NewFileInfo->FileName);
378 ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);
379
380 if (!EFI_ERROR(ShellFileExists(NewFileInfo->FileName))) {
381 if (Response == NULL) {
382 ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);
383 }
384 switch (*(SHELL_PROMPT_RESPONSE*)Response) {
385 case ShellPromptResponseNo:
386 FreePool(NewFileInfo);
387 continue;
388 case ShellPromptResponseCancel:
389 *Resp = Response;
390 //
391 // indicate to stop everything
392 //
393 FreePool(NewFileInfo);
394 FreePool(DestPath);
395 FreePool(HiiOutput);
396 FreePool(HiiResultOk);
397 return (SHELL_ABORTED);
398 case ShellPromptResponseAll:
399 *Resp = Response;
400 break;
401 case ShellPromptResponseYes:
402 FreePool(Response);
403 break;
404 default:
405 FreePool(Response);
406 FreePool(NewFileInfo);
407 FreePool(DestPath);
408 FreePool(HiiOutput);
409 FreePool(HiiResultOk);
410 return SHELL_ABORTED;
411 }
412 Status = ShellOpenFileByName(NewFileInfo->FileName, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);
413 ShellDeleteFile(&DestHandle);
414 }
415
416
417 //
418 // Perform the move operation
419 //
420 Status = ShellSetFileInfo(Node->Handle, NewFileInfo);
421
422 //
423 // Free the info object we used...
424 //
425 FreePool(NewFileInfo);
426
427 //
428 // Check our result
429 //
430 if (EFI_ERROR(Status)) {
431 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);
432 ShellStatus = SHELL_INVALID_PARAMETER;
433 if (Status == EFI_SECURITY_VIOLATION) {
434 ShellStatus = SHELL_SECURITY_VIOLATION;
435 } else if (Status == EFI_WRITE_PROTECTED) {
436 ShellStatus = SHELL_WRITE_PROTECTED;
437 } else if (Status == EFI_OUT_OF_RESOURCES) {
438 ShellStatus = SHELL_OUT_OF_RESOURCES;
439 } else if (Status == EFI_DEVICE_ERROR) {
440 ShellStatus = SHELL_DEVICE_ERROR;
441 } else if (Status == EFI_ACCESS_DENIED) {
442 ShellStatus = SHELL_ACCESS_DENIED;
443 }
444 } else {
445 ShellPrintEx(-1, -1, L"%s", HiiResultOk);
446 }
447 }
448 } // for loop
449
450 FreePool(DestPath);
451 FreePool(HiiOutput);
452 FreePool(HiiResultOk);
453 return (ShellStatus);
454 }
455
456 /**
457 Function for 'mv' command.
458
459 @param[in] ImageHandle Handle to the Image (NULL if Internal).
460 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
461 **/
462 SHELL_STATUS
463 EFIAPI
464 ShellCommandRunMv (
465 IN EFI_HANDLE ImageHandle,
466 IN EFI_SYSTEM_TABLE *SystemTable
467 )
468 {
469 EFI_STATUS Status;
470 LIST_ENTRY *Package;
471 CHAR16 *ProblemParam;
472 SHELL_STATUS ShellStatus;
473 UINTN ParamCount;
474 UINTN LoopCounter;
475 EFI_SHELL_FILE_INFO *FileList;
476 VOID *Response;
477
478 ProblemParam = NULL;
479 ShellStatus = SHELL_SUCCESS;
480 ParamCount = 0;
481 FileList = NULL;
482 Response = NULL;
483
484 //
485 // initialize the shell lib (we must be in non-auto-init...)
486 //
487 Status = ShellInitialize();
488 ASSERT_EFI_ERROR(Status);
489
490 //
491 // parse the command line
492 //
493 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
494 if (EFI_ERROR(Status)) {
495 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
496 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
497 FreePool(ProblemParam);
498 ShellStatus = SHELL_INVALID_PARAMETER;
499 } else {
500 ASSERT(FALSE);
501 }
502 } else {
503 //
504 // check for "-?"
505 //
506 if (ShellCommandLineGetFlag(Package, L"-?")) {
507 ASSERT(FALSE);
508 }
509
510 switch (ParamCount = ShellCommandLineGetCount(Package)) {
511 case 0:
512 case 1:
513 //
514 // we have insufficient parameters
515 //
516 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
517 ShellStatus = SHELL_INVALID_PARAMETER;
518 break;
519 case 2:
520 //
521 // must have valid CWD for single parameter...
522 //
523 if (ShellGetCurrentDir(NULL) == NULL){
524 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);
525 ShellStatus = SHELL_INVALID_PARAMETER;
526 } else {
527 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);
528 if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {
529 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));
530 ShellStatus = SHELL_NOT_FOUND;
531 } else {
532 //
533 // ValidateAndMoveFiles will report errors to the screen itself
534 //
535 ShellStatus = ValidateAndMoveFiles(FileList, &Response, ShellGetCurrentDir(NULL));
536 }
537 }
538
539 break;
540 default:
541 ///@todo make sure this works with error half way through and continues...
542 for (ParamCount--, LoopCounter = 1 ; LoopCounter < ParamCount ; LoopCounter++) {
543 if (ShellGetExecutionBreakFlag()) {
544 break;
545 }
546 Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), 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, LoopCounter));
549 ShellStatus = SHELL_NOT_FOUND;
550 } else {
551 //
552 // ValidateAndMoveFiles will report errors to the screen itself
553 // Only change ShellStatus if it's sucessful
554 //
555 if (ShellStatus == SHELL_SUCCESS) {
556 ShellStatus = ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));
557 } else {
558 ValidateAndMoveFiles(FileList, &Response, ShellCommandLineGetRawValue(Package, ParamCount));
559 }
560 }
561 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
562 Status = ShellCloseFileMetaArg(&FileList);
563 if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {
564 ShellStatus = SHELL_ACCESS_DENIED;
565 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1), ShellStatus|MAX_BIT);
566 }
567 }
568 }
569 break;
570 } // switch on parameter count
571
572 if (FileList != NULL) {
573 ShellCloseFileMetaArg(&FileList);
574 }
575
576 //
577 // free the command line package
578 //
579 ShellCommandLineFreeVarList (Package);
580 }
581
582 SHELL_FREE_NON_NULL(Response);
583
584 if (ShellGetExecutionBreakFlag()) {
585 return (SHELL_ABORTED);
586 }
587
588 return (ShellStatus);
589 }