2 Main file for attrib shell level 2 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellLevel2CommandsLib.h"
13 Function for 'mkdir' command.
15 @param[in] ImageHandle Handle to the Image (NULL if Internal).
16 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
20 ShellCommandRunMkDir (
21 IN EFI_HANDLE ImageHandle
,
22 IN EFI_SYSTEM_TABLE
*SystemTable
26 CONST CHAR16
*NewDirName
;
27 CHAR16
*NewDirNameCopy
;
33 SHELL_FILE_HANDLE FileHandle
;
34 SHELL_STATUS ShellStatus
;
36 ShellStatus
= SHELL_SUCCESS
;
37 NewDirNameCopy
= NULL
;
39 SaveSplitChar
= CHAR_NULL
;
41 // initialize the shell lib (we must be in non-auto-init...)
43 Status
= ShellInitialize ();
44 ASSERT_EFI_ERROR (Status
);
47 // parse the command line
49 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
50 if (EFI_ERROR (Status
)) {
51 if ((Status
== EFI_VOLUME_CORRUPTED
) && (ProblemParam
!= NULL
)) {
52 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, L
"mkdir", ProblemParam
);
53 FreePool (ProblemParam
);
54 ShellStatus
= SHELL_INVALID_PARAMETER
;
62 if (ShellCommandLineGetFlag (Package
, L
"-?")) {
67 // create a set of directories
69 if (ShellCommandLineGetRawValue (Package
, 1) == NULL
) {
71 // we didnt get a single parameter
73 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
, L
"mkdir");
74 ShellStatus
= SHELL_INVALID_PARAMETER
;
76 for ( DirCreateCount
= 1
82 // loop through each directory specified
85 NewDirName
= ShellCommandLineGetRawValue (Package
, DirCreateCount
);
86 if (NewDirName
== NULL
) {
91 // check if that already exists... if yes fail
94 Status
= ShellOpenFileByName (
97 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
,
100 if (!EFI_ERROR (Status
)) {
101 ShellCloseFile (&FileHandle
);
102 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MKDIR_ALREADY
), gShellLevel2HiiHandle
, NewDirName
);
103 ShellStatus
= SHELL_INVALID_PARAMETER
;
105 ASSERT (FileHandle
== NULL
);
107 // create the nested directory from parent to child.
108 // if NewDirName = test1\test2\test3, first create "test1\" directory, then "test1\test2\", finally "test1\test2\test3".
110 NewDirNameCopy
= AllocateCopyPool (StrSize (NewDirName
), NewDirName
);
111 NewDirNameCopy
= PathCleanUpDirectories (NewDirNameCopy
);
112 if (NewDirNameCopy
== NULL
) {
113 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
117 SplitName
= NewDirNameCopy
;
118 while (SplitName
!= NULL
) {
119 SplitName
= StrStr (SplitName
+ 1, L
"\\");
120 if (SplitName
!= NULL
) {
121 SaveSplitChar
= *(SplitName
+ 1);
122 *(SplitName
+ 1) = '\0';
126 // check if current nested directory already exists... continue to create the child directory.
128 Status
= ShellOpenFileByName (
134 if (!EFI_ERROR (Status
)) {
135 ShellCloseFile (&FileHandle
);
137 Status
= ShellCreateDirectory (NewDirNameCopy
, &FileHandle
);
138 if (EFI_ERROR (Status
)) {
142 if (FileHandle
!= NULL
) {
143 gEfiShellProtocol
->CloseFile (FileHandle
);
147 if (SplitName
!= NULL
) {
148 *(SplitName
+ 1) = SaveSplitChar
;
152 if (EFI_ERROR (Status
)) {
153 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_MKDIR_CREATEFAIL
), gShellLevel2HiiHandle
, NewDirName
);
154 ShellStatus
= SHELL_ACCESS_DENIED
;
158 SHELL_FREE_NON_NULL (NewDirNameCopy
);
165 // free the command line package
167 ShellCommandLineFreeVarList (Package
);
169 return (ShellStatus
);