]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/MkDir.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / MkDir.c
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
26 ShellCommandRunMkDir (
27 IN EFI_HANDLE ImageHandle,
28 IN EFI_SYSTEM_TABLE *SystemTable
29 )
30 {
31 EFI_STATUS Status;
32 CONST CHAR16 *NewDirName;
33 CHAR16 *NewDirNameCopy;
34 CHAR16 *SplitName;
35 CHAR16 SaveSplitChar;
36 UINTN DirCreateCount;
37 LIST_ENTRY *Package;
38 CHAR16 *ProblemParam;
39 SHELL_FILE_HANDLE FileHandle;
40 SHELL_STATUS ShellStatus;
41
42 ShellStatus = SHELL_SUCCESS;
43 NewDirNameCopy = NULL;
44 SplitName = NULL;
45 SaveSplitChar = CHAR_NULL;
46 //
47 // initialize the shell lib (we must be in non-auto-init...)
48 //
49 Status = ShellInitialize();
50 ASSERT_EFI_ERROR(Status);
51
52 //
53 // parse the command line
54 //
55 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
56 if (EFI_ERROR(Status)) {
57 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
58 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"mkdir", ProblemParam);
59 FreePool(ProblemParam);
60 ShellStatus = SHELL_INVALID_PARAMETER;
61 } else {
62 ASSERT(FALSE);
63 }
64 } else {
65 //
66 // check for "-?"
67 //
68 if (ShellCommandLineGetFlag(Package, L"-?")) {
69 ASSERT(FALSE);
70 }
71
72 //
73 // create a set of directories
74 //
75 if (ShellCommandLineGetRawValue(Package, 1) == NULL) {
76 //
77 // we didnt get a single parameter
78 //
79 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"mkdir");
80 ShellStatus = SHELL_INVALID_PARAMETER;
81 } else {
82 for ( DirCreateCount = 1
83 ;
84 ; DirCreateCount++
85 ){
86 //
87 // loop through each directory specified
88 //
89
90 NewDirName = ShellCommandLineGetRawValue(Package, DirCreateCount);
91 if (NewDirName == NULL) {
92 break;
93 }
94 //
95 // check if that already exists... if yes fail
96 //
97 FileHandle = NULL;
98 Status = ShellOpenFileByName(NewDirName,
99 &FileHandle,
100 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE,
101 EFI_FILE_DIRECTORY
102 );
103 if (!EFI_ERROR(Status)) {
104 ShellCloseFile(&FileHandle);
105 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MKDIR_ALREADY), gShellLevel2HiiHandle, NewDirName);
106 ShellStatus = SHELL_INVALID_PARAMETER;
107 } else {
108 ASSERT(FileHandle == NULL);
109 //
110 // create the nested directory from parent to child.
111 // if NewDirName = test1\test2\test3, first create "test1\" directory, then "test1\test2\", finally "test1\test2\test3".
112 //
113 NewDirNameCopy = AllocateCopyPool (StrSize(NewDirName), NewDirName);
114 NewDirNameCopy = PathCleanUpDirectories (NewDirNameCopy);
115 if(NewDirNameCopy == NULL) {
116 ShellStatus = SHELL_OUT_OF_RESOURCES;
117 break;
118 }
119 SplitName = NewDirNameCopy;
120 while (SplitName != NULL) {
121 SplitName = StrStr (SplitName + 1, L"\\");
122 if (SplitName != NULL) {
123 SaveSplitChar = *(SplitName + 1);
124 *(SplitName + 1) = '\0';
125 }
126 //
127 // check if current nested directory already exists... continue to create the child directory.
128 //
129 Status = ShellOpenFileByName (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 if (FileHandle != NULL) {
142 gEfiShellProtocol->CloseFile (FileHandle);
143 }
144 }
145 if (SplitName != NULL) {
146 *(SplitName + 1) = SaveSplitChar;
147 }
148 }
149 if (EFI_ERROR(Status)) {
150 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_MKDIR_CREATEFAIL), gShellLevel2HiiHandle, NewDirName);
151 ShellStatus = SHELL_ACCESS_DENIED;
152 break;
153 }
154 SHELL_FREE_NON_NULL (NewDirNameCopy);
155 }
156 }
157 }
158 }
159
160 //
161 // free the command line package
162 //
163 ShellCommandLineFreeVarList (Package);
164
165 return (ShellStatus);
166 }
167