1 /** @file
2   Main file for attrib shell level 2 function.
3 
4   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5   Copyright (c) 2009 - 2010, 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 for 'mkdir' command.
20 
21   @param[in] ImageHandle  Handle to the Image (NULL if Internal).
22   @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
23 **/
24 SHELL_STATUS
25 EFIAPI
ShellCommandRunMkDir(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)26 ShellCommandRunMkDir (
27   IN EFI_HANDLE        ImageHandle,
28   IN EFI_SYSTEM_TABLE  *SystemTable
29   )
30 {
31   EFI_STATUS      Status;
32   CONST CHAR16    *NewDirName;
33   UINTN           DirCreateCount;
34   LIST_ENTRY      *Package;
35   CHAR16          *ProblemParam;
36   SHELL_FILE_HANDLE          FileHandle;
37   SHELL_STATUS    ShellStatus;
38 
39   ShellStatus  = SHELL_SUCCESS;
40 
41   //
42   // initialize the shell lib (we must be in non-auto-init...)
43   //
44   Status = ShellInitialize();
45   ASSERT_EFI_ERROR(Status);
46 
47   //
48   // parse the command line
49   //
50   Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
51   if (EFI_ERROR(Status)) {
52     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
53       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"mkdir", ProblemParam);
54       FreePool(ProblemParam);
55       ShellStatus = SHELL_INVALID_PARAMETER;
56     } else {
57       ASSERT(FALSE);
58     }
59   } else {
60     //
61     // check for "-?"
62     //
63     if (ShellCommandLineGetFlag(Package, L"-?")) {
64       ASSERT(FALSE);
65     }
66 
67     //
68     // create a set of directories
69     //
70     if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
71       //
72       // we didnt get a single parameter
73       //
74       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mkdir");
75       ShellStatus = SHELL_INVALID_PARAMETER;
76     } else {
77       for ( DirCreateCount = 1
78           ;
79           ; DirCreateCount++
80        ){
81         //
82         // loop through each directory specified
83         //
84 
85         NewDirName = ShellCommandLineGetRawValue(Package, DirCreateCount);
86         if (NewDirName == NULL) {
87           break;
88         }
89         //
90         // check if that already exists... if yes fail
91         //
92         FileHandle = NULL;
93         Status = ShellOpenFileByName(NewDirName,
94                                     &FileHandle,
95                                     EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
96                                     EFI_FILE_DIRECTORY
97                                    );
98         if (!EFI_ERROR(Status)) {
99           ShellCloseFile(&FileHandle);
100           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MKDIR_ALREADY), gShellLevel2HiiHandle, NewDirName);
101           ShellStatus = SHELL_INVALID_PARAMETER;
102           break;
103         } else {
104           ASSERT(FileHandle == NULL);
105           //
106           // create the directory named NewDirName
107           //
108           Status = ShellCreateDirectory(NewDirName, &FileHandle);
109           if (FileHandle != NULL) {
110             gEfiShellProtocol->CloseFile(FileHandle);
111           }
112           if (EFI_ERROR(Status)) {
113             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MKDIR_CREATEFAIL), gShellLevel2HiiHandle, NewDirName);
114             ShellStatus = SHELL_ACCESS_DENIED;
115             break;
116           }
117         }
118       }
119     }
120   }
121 
122   //
123   // free the command line package
124   //
125   ShellCommandLineFreeVarList (Package);
126 
127   return (ShellStatus);
128 }
129 
130