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