TianoCore EDK2 master
Loading...
Searching...
No Matches
MkDir.c
Go to the documentation of this file.
1
11
19EFIAPI
21 IN EFI_HANDLE ImageHandle,
22 IN EFI_SYSTEM_TABLE *SystemTable
23 )
24{
25 EFI_STATUS Status;
26 CONST CHAR16 *NewDirName;
27 CHAR16 *NewDirNameCopy;
28 CHAR16 *SplitName;
29 CHAR16 SaveSplitChar;
30 UINTN DirCreateCount;
31 LIST_ENTRY *Package;
32 CHAR16 *ProblemParam;
33 SHELL_FILE_HANDLE FileHandle;
34 SHELL_STATUS ShellStatus;
35
36 ShellStatus = SHELL_SUCCESS;
37 NewDirNameCopy = NULL;
38 SplitName = NULL;
39 SaveSplitChar = CHAR_NULL;
40 //
41 // initialize the shell lib (we must be in non-auto-init...)
42 //
43 Status = ShellInitialize ();
44 ASSERT_EFI_ERROR (Status);
45
46 //
47 // parse the command line
48 //
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;
55 } else {
56 ASSERT (FALSE);
57 }
58 } else {
59 //
60 // check for "-?"
61 //
62 if (ShellCommandLineGetFlag (Package, L"-?")) {
63 ASSERT (FALSE);
64 }
65
66 //
67 // create a set of directories
68 //
69 if (ShellCommandLineGetRawValue (Package, 1) == NULL) {
70 //
71 // we didnt get a single parameter
72 //
73 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mkdir");
74 ShellStatus = SHELL_INVALID_PARAMETER;
75 } else {
76 for ( DirCreateCount = 1
77 ;
78 ; DirCreateCount++
79 )
80 {
81 //
82 // loop through each directory specified
83 //
84
85 NewDirName = ShellCommandLineGetRawValue (Package, DirCreateCount);
86 if (NewDirName == NULL) {
87 break;
88 }
89
90 //
91 // check if that already exists... if yes fail
92 //
93 FileHandle = NULL;
94 Status = ShellOpenFileByName (
95 NewDirName,
96 &FileHandle,
97 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
98 EFI_FILE_DIRECTORY
99 );
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;
104 } else {
105 ASSERT (FileHandle == NULL);
106 //
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".
109 //
110 NewDirNameCopy = AllocateCopyPool (StrSize (NewDirName), NewDirName);
111 NewDirNameCopy = PathCleanUpDirectories (NewDirNameCopy);
112 if (NewDirNameCopy == NULL) {
113 ShellStatus = SHELL_OUT_OF_RESOURCES;
114 break;
115 }
116
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';
123 }
124
125 //
126 // check if current nested directory already exists... continue to create the child directory.
127 //
128 Status = ShellOpenFileByName (
129 NewDirNameCopy,
130 &FileHandle,
131 EFI_FILE_MODE_READ,
132 EFI_FILE_DIRECTORY
133 );
134 if (!EFI_ERROR (Status)) {
135 ShellCloseFile (&FileHandle);
136 } else {
137 Status = ShellCreateDirectory (NewDirNameCopy, &FileHandle);
138 if (EFI_ERROR (Status)) {
139 break;
140 }
141
142 if (FileHandle != NULL) {
143 gEfiShellProtocol->CloseFile (FileHandle);
144 }
145 }
146
147 if (SplitName != NULL) {
148 *(SplitName + 1) = SaveSplitChar;
149 }
150 }
151
152 if (EFI_ERROR (Status)) {
153 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MKDIR_CREATEFAIL), gShellLevel2HiiHandle, NewDirName);
154 ShellStatus = SHELL_ACCESS_DENIED;
155 break;
156 }
157
158 SHELL_FREE_NON_NULL (NewDirNameCopy);
159 }
160 }
161 }
162 }
163
164 //
165 // free the command line package
166 //
168
169 return (ShellStatus);
170}
UINT64 UINTN
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
Definition: String.c:72
CHAR16 *EFIAPI PathCleanUpDirectories(IN CHAR16 *Path)
Definition: FilePaths.c:68
CHAR16 *EFIAPI StrStr(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString)
Definition: String.c:224
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ACCESS_DENIED
Definition: Shell.h:106
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
SHELL_STATUS EFIAPI ShellCommandRunMkDir(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: MkDir.c:20
EFI_STATUS EFIAPI ShellCreateDirectory(IN CONST CHAR16 *DirectoryName, OUT SHELL_FILE_HANDLE *FileHandle)
Definition: UefiShellLib.c:857
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellCommandLineGetFlag(IN CONST LIST_ENTRY *CONST CheckPackage, IN CONST CHAR16 *CONST KeyString)
EFI_STATUS EFIAPI ShellOpenFileByName(IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes)
Definition: UefiShellLib.c:720
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
SHELL_PARAM_ITEM EmptyParamList[]
Helper structure for no parameters (besides -? and -b)
Definition: UefiShellLib.c:19
EFI_STATUS EFIAPI ShellCloseFile(IN SHELL_FILE_HANDLE *FileHandle)
Definition: UefiShellLib.c:969
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define STRING_TOKEN(t)