From 031bd4f73402ada87556ee4655db6523ab6dbd75 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Mon, 19 Jun 2023 12:57:14 -0700 Subject: [PATCH] Provide separate filesystem.cpp --- UEFIExtract/CMakeLists.txt | 8 +++- UEFIFind/CMakeLists.txt | 1 + UEFIFind/uefifind_main.cpp | 2 - common/filesystem.cpp | 92 ++++++++++++++++++++++++++++++++++++++ common/filesystem.h | 84 +++------------------------------- common/meson.build | 1 + 6 files changed, 108 insertions(+), 80 deletions(-) create mode 100644 common/filesystem.cpp diff --git a/UEFIExtract/CMakeLists.txt b/UEFIExtract/CMakeLists.txt index 4db5457..bfcb773 100644 --- a/UEFIExtract/CMakeLists.txt +++ b/UEFIExtract/CMakeLists.txt @@ -12,6 +12,7 @@ SET(PROJECT_SOURCES uefidump.cpp ../common/guiddatabase.cpp ../common/types.cpp + ../common/filesystem.cpp ../common/descriptor.cpp ../common/ffs.cpp ../common/nvram.cpp @@ -61,7 +62,12 @@ SET(PROJECT_SOURCES ../common/zlib/zutil.c ) -ADD_DEFINITIONS(-DU_ENABLE_NVRAM_PARSING_SUPPORT -DU_ENABLE_ME_PARSING_SUPPORT -DU_ENABLE_FIT_PARSING_SUPPORT -DU_ENABLE_GUID_DATABASE_SUPPORT) +ADD_DEFINITIONS( + -DU_ENABLE_NVRAM_PARSING_SUPPORT + -DU_ENABLE_ME_PARSING_SUPPORT + -DU_ENABLE_FIT_PARSING_SUPPORT + -DU_ENABLE_GUID_DATABASE_SUPPORT +) ADD_EXECUTABLE(UEFIExtract ${PROJECT_SOURCES}) diff --git a/UEFIFind/CMakeLists.txt b/UEFIFind/CMakeLists.txt index b508422..477e38d 100644 --- a/UEFIFind/CMakeLists.txt +++ b/UEFIFind/CMakeLists.txt @@ -11,6 +11,7 @@ SET(PROJECT_SOURCES uefifind.cpp ../common/guiddatabase.cpp ../common/types.cpp + ../common/filesystem.cpp ../common/descriptor.cpp ../common/ffs.cpp ../common/nvram.cpp diff --git a/UEFIFind/uefifind_main.cpp b/UEFIFind/uefifind_main.cpp index 57c9eb3..8b54118 100644 --- a/UEFIFind/uefifind_main.cpp +++ b/UEFIFind/uefifind_main.cpp @@ -32,8 +32,6 @@ int main(int argc, char *argv[]) UEFIFind w; USTATUS result; - initGuidDatabase("guids.csv"); - if (argc == 1) { print_usage(); return U_SUCCESS; diff --git a/common/filesystem.cpp b/common/filesystem.cpp new file mode 100644 index 0000000..d59a47b --- /dev/null +++ b/common/filesystem.cpp @@ -0,0 +1,92 @@ +/* filesystem.c + +Copyright (c) 2023, Nikolaj Schlej. All rights reserved. +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +*/ + +#include "filesystem.h" +#include +#include + +USTATUS readFileIntoBuffer(const UString& inPath, UByteArray& buf) { + if (!isExistOnFs(inPath)) + return U_FILE_OPEN; + + std::ifstream inputFile(inPath.toLocal8Bit(), std::ios::in | std::ios::binary); + if (!inputFile) + return U_FILE_OPEN; + std::vector buffer(std::istreambuf_iterator(inputFile), + (std::istreambuf_iterator())); + inputFile.close(); + + buf = buffer; + + return U_SUCCESS; +} + + +#if defined(_WIN32) || defined(__MINGW32__) +#include +#include +bool isExistOnFs(const UString & path) { + struct _stat buf; + return (_stat(path.toLocal8Bit(), &buf) == 0); +} + +bool makeDirectory(const UString & dir) { + return (_mkdir(dir.toLocal8Bit()) == 0); +} + +bool changeDirectory(const UString & dir) { + return (_chdir(dir.toLocal8Bit()) == 0); +} + +void removeDirectory(const UString & dir) { + int r = _rmdir(dir.toLocal8Bit()); + // Hack: unlike *nix, Windows does not permit deleting current directories. + if (r < 0 && errno == EACCES && changeDirectory(dir + UString("/../"))) { + (void)_rmdir(dir.toLocal8Bit()); + } +} + +UString getAbsPath(const UString & path) { + char abs[_MAX_PATH] = {}; + if (_fullpath(abs, path.toLocal8Bit(), sizeof(abs))) + return UString(abs); + return path; +} +#else +#include +#include +bool isExistOnFs(const UString & path) { + struct stat buf; + return (stat(path.toLocal8Bit(), &buf) == 0); +} + +bool makeDirectory(const UString & dir) { + return (mkdir(dir.toLocal8Bit(), ACCESSPERMS) == 0); +} + +void removeDirectory(const UString & dir) { + rmdir(dir.toLocal8Bit()); +} + +bool changeDirectory(const UString & dir) { + return (chdir(dir.toLocal8Bit()) == 0); +} + +UString getAbsPath(const UString & path) { + char abs[PATH_MAX] = {}; + // Last is a non-standard extension for non-existent files. + if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0') + return UString(abs); + return path; +} +#endif \ No newline at end of file diff --git a/common/filesystem.h b/common/filesystem.h index c6afb35..2101809 100644 --- a/common/filesystem.h +++ b/common/filesystem.h @@ -1,6 +1,6 @@ /* filesystem.h -Copyright (c) 2015, Nikolaj Schlej. All rights reserved. +Copyright (c) 2023, Nikolaj Schlej. All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -17,82 +17,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "basetypes.h" #include "ustring.h" #include "ubytearray.h" -#include -#include -#if defined(_WIN32) || defined(__MINGW32__) -#include -#include -static inline bool isExistOnFs(const UString & path) { - struct _stat buf; - return (_stat(path.toLocal8Bit(), &buf) == 0); -} - -static inline bool makeDirectory(const UString & dir) { - return (_mkdir(dir.toLocal8Bit()) == 0); -} - -static inline bool changeDirectory(const UString & dir) { - return (_chdir(dir.toLocal8Bit()) == 0); -} - -static inline void removeDirectory(const UString & dir) { - int r = _rmdir(dir.toLocal8Bit()); - // Hack: unlike *nix, Windows does not permit deleting current directories. - if (r < 0 && errno == EACCES && changeDirectory(dir + UString("/../"))) { - _rmdir(dir.toLocal8Bit()); - } -} - -static inline UString getAbsPath(const UString & path) { - char abs[_MAX_PATH] = {}; - if (_fullpath(abs, path.toLocal8Bit(), sizeof(abs))) - return UString(abs); - return path; -} -#else -#include -#include -static inline bool isExistOnFs(const UString & path) { - struct stat buf; - return (stat(path.toLocal8Bit(), &buf) == 0); -} - -static inline bool makeDirectory(const UString & dir) { - return (mkdir(dir.toLocal8Bit(), ACCESSPERMS) == 0); -} - -static inline void removeDirectory(const UString & dir) { - rmdir(dir.toLocal8Bit()); -} - -static inline bool changeDirectory(const UString & dir) { - return (chdir(dir.toLocal8Bit()) == 0); -} - -static inline UString getAbsPath(const UString & path) { - char abs[PATH_MAX] = {}; - // Last is a non-standard extension for non-existent files. - if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0') - return UString(abs); - return path; -} -#endif - -static inline USTATUS readFileIntoBuffer(const UString & inPath, UByteArray &buf) { - if (!isExistOnFs(inPath)) - return U_FILE_OPEN; - - std::ifstream inputFile(inPath.toLocal8Bit(), std::ios::in | std::ios::binary); - if (!inputFile) - return U_FILE_OPEN; - std::vector buffer(std::istreambuf_iterator(inputFile), - (std::istreambuf_iterator())); - inputFile.close(); - - buf = buffer; - - return U_SUCCESS; -} +bool isExistOnFs(const UString& path); +bool makeDirectory(const UString& dir); +bool changeDirectory(const UString& dir); +void removeDirectory(const UString& dir); +UString getAbsPath(const UString& path); +USTATUS readFileIntoBuffer(const UString& inPath, UByteArray& buf); #endif diff --git a/common/meson.build b/common/meson.build index 2945df4..096b4de 100644 --- a/common/meson.build +++ b/common/meson.build @@ -19,6 +19,7 @@ uefitoolcommon = static_library('uefitoolcommon', 'guiddatabase.cpp', 'types.cpp', 'descriptor.cpp', + 'filesystem.cpp', 'ffs.cpp', 'nvram.cpp', 'nvramparser.cpp',