From afd916c38fda0d70b83015ed867c30bc1578fc14 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Sun, 12 Dec 2021 11:05:07 +0300 Subject: add creation of one or more directories --- Library/UefiShellUfmCommandLib/cmds.c | 65 +++++++++++++++++++++++++++++++++++ Library/UefiShellUfmCommandLib/cmds.h | 20 +++++++++++ 2 files changed, 85 insertions(+) create mode 100644 Library/UefiShellUfmCommandLib/cmds.c create mode 100644 Library/UefiShellUfmCommandLib/cmds.h diff --git a/Library/UefiShellUfmCommandLib/cmds.c b/Library/UefiShellUfmCommandLib/cmds.c new file mode 100644 index 0000000..93c5666 --- /dev/null +++ b/Library/UefiShellUfmCommandLib/cmds.c @@ -0,0 +1,65 @@ +#include +#include +#include + +#include "cmds.h" + +EFI_STATUS make_directory(CONST CHAR16 *dir_name) +{ + EFI_STATUS status = EFI_SUCCESS; + CHAR16 *tmp_str = NULL; + CHAR16 split_char, *split_name = NULL; + SHELL_FILE_HANDLE file_handle = NULL; + + // check if dir already exists + status = ShellOpenFileByName(dir_name, + &file_handle, + EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE, + EFI_FILE_DIRECTORY + ); + if(!EFI_ERROR(status)) { + ShellCloseFile(&file_handle); + return EFI_INVALID_PARAMETER; + } + + // create the nested directory from parent to child: + // if dir_name = test1\test2\test3, first create "test1\" directory, then "test1\test2\", finally "test1\test2\test3". + tmp_str = AllocateCopyPool(StrSize(dir_name), dir_name); + tmp_str = PathCleanUpDirectories(tmp_str); + if(tmp_str == NULL) + return EFI_OUT_OF_RESOURCES; + + split_name = tmp_str; + while(split_name != NULL) { + split_name = StrStr(split_name + 1, L"\\"); + if(split_name != NULL) { + split_char = *(split_name + 1); + *(split_name + 1) = '\0'; + } + + // check if current nested directory already exists + status = ShellOpenFileByName(tmp_str, + &file_handle, + EFI_FILE_MODE_READ, + EFI_FILE_DIRECTORY + ); + if(!EFI_ERROR(status)) { + ShellCloseFile(&file_handle); + } + else { + status = ShellCreateDirectory(tmp_str, &file_handle); + if(EFI_ERROR(status)) + break; + if(file_handle != NULL) + gEfiShellProtocol->CloseFile(file_handle); + } + + if(split_name != NULL) + *(split_name + 1) = split_char; + } + if(EFI_ERROR(status)) + status = EFI_ACCESS_DENIED; + + SHELL_FREE_NON_NULL(tmp_str); + return status; +} diff --git a/Library/UefiShellUfmCommandLib/cmds.h b/Library/UefiShellUfmCommandLib/cmds.h new file mode 100644 index 0000000..d819d47 --- /dev/null +++ b/Library/UefiShellUfmCommandLib/cmds.h @@ -0,0 +1,20 @@ +#ifndef UFM_CMDS_H +#define UFM_CMDS_H + +#include +#include + +/* + * Creates one or more directories. + * + * dir_name: the name of a directory or directories to create + * + * return: EFI_SUCCESS Directory(-ies) was created + EFI_INVALID_PARAMETER Parameter has an invalid value + EFI_ACCESS_DENIED Error while creating directory(-ies) + EFI_OUT_OF_RESOURCES Not enough resources were available to open the + file +*/ +EFI_STATUS make_directory(CONST CHAR16 *dir_name); + +#endif /* UFM_CMDS_H */ -- cgit v1.2.3-18-g5258