diff --git a/include/nod/Util.hpp b/include/nod/Util.hpp index 6825070..69c649b 100644 --- a/include/nod/Util.hpp +++ b/include/nod/Util.hpp @@ -4,14 +4,8 @@ #include #include #include -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif -#ifndef NOMINMAX -#define NOMINMAX -#endif #include -#include +#include #if defined(WINAPI_FAMILY) && WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP #define WINDOWS_STORE 1 #else @@ -181,32 +175,7 @@ static inline uint64_t SBig(uint64_t val) { return val; } #endif enum class FileLockType { None = 0, Read, Write }; -static inline FILE* Fopen(const char* path, const char* mode, FileLockType lock = FileLockType::None) { -#if _MSC_VER - const nowide::wstackstring wpath(path); - const nowide::wshort_stackstring wmode(mode); - FILE* fp = _wfopen(wpath.get(), wmode.get()); - if (!fp) - return nullptr; -#else - FILE* fp = fopen(path, mode); - if (!fp) - return nullptr; -#endif - - if (lock != FileLockType::None) { -#if _WIN32 - OVERLAPPED ov = {}; - LockFileEx((HANDLE)(uintptr_t)_fileno(fp), (lock == FileLockType::Write) ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1, - &ov); -#else - if (flock(fileno(fp), ((lock == FileLockType::Write) ? LOCK_EX : LOCK_SH) | LOCK_NB)) - LogModule.report(logvisor::Error, FMT_STRING("flock {}: {}"), path, strerror(errno)); -#endif - } - - return fp; -} +FILE* Fopen(const char* path, const char* mode, FileLockType lock = FileLockType::None); static inline int FSeek(FILE* fp, int64_t offset, int whence) { #if _WIN32 @@ -228,33 +197,6 @@ static inline int64_t FTell(FILE* fp) { #endif } -static inline bool CheckFreeSpace(const char* path, size_t reqSz) { -#if _WIN32 - ULARGE_INTEGER freeBytes; - const nowide::wstackstring wpath(path); - std::array buf{}; - wchar_t* end = nullptr; - DWORD ret = GetFullPathNameW(wpath.get(), 1024, buf.data(), &end); - if (ret == 0 || ret > 1024) { - LogModule.report(logvisor::Error, FMT_STRING("GetFullPathNameW {}"), path); - return false; - } - if (end != nullptr) { - end[0] = L'\0'; - } - if (!GetDiskFreeSpaceExW(buf.data(), &freeBytes, nullptr, nullptr)) { - LogModule.report(logvisor::Error, FMT_STRING("GetDiskFreeSpaceExW {}: {}"), path, GetLastError()); - return false; - } - return reqSz < freeBytes.QuadPart; -#else - struct statvfs svfs; - if (statvfs(path, &svfs)) { - LogModule.report(logvisor::Error, FMT_STRING("statvfs {}: {}"), path, strerror(errno)); - return false; - } - return reqSz < svfs.f_frsize * svfs.f_bavail; -#endif -} +bool CheckFreeSpace(const char* path, size_t reqSz); } // namespace nod diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 825b842..ed4277a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -11,6 +11,7 @@ add_library(nod DiscWii.cpp nod.cpp OSUTF.c + Util.cpp ../include/nod/aes.hpp ../include/nod/DirectoryEnumerator.hpp @@ -20,9 +21,9 @@ add_library(nod ../include/nod/IDiscIO.hpp ../include/nod/IFileIO.hpp ../include/nod/nod.hpp + ../include/nod/OSUTF.h ../include/nod/sha1.h ../include/nod/Util.hpp - ../include/nod/OSUTF.h ) target_include_directories(nod PUBLIC diff --git a/lib/FileIOWin32.cpp b/lib/FileIOWin32.cpp index beece52..7841a55 100644 --- a/lib/FileIOWin32.cpp +++ b/lib/FileIOWin32.cpp @@ -1,7 +1,13 @@ -#include #include -#include -#include +#if _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif #include "nod/IFileIO.hpp" #include "nod/Util.hpp" diff --git a/lib/Util.cpp b/lib/Util.cpp new file mode 100644 index 0000000..990b2e8 --- /dev/null +++ b/lib/Util.cpp @@ -0,0 +1,69 @@ +#include + +#if _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + +namespace nod { +FILE* Fopen(const char* path, const char* mode, FileLockType lock) { +#if _WIN32 + const nowide::wstackstring wpath(path); + const nowide::wshort_stackstring wmode(mode); + FILE* fp = _wfopen(wpath.get(), wmode.get()); + if (!fp) + return nullptr; +#else + FILE* fp = fopen(path, mode); + if (!fp) + return nullptr; +#endif + + if (lock != FileLockType::None) { +#if _WIN32 + OVERLAPPED ov = {}; + LockFileEx((HANDLE)(uintptr_t)_fileno(fp), (lock == FileLockType::Write) ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1, + &ov); +#else + if (flock(fileno(fp), ((lock == FileLockType::Write) ? LOCK_EX : LOCK_SH) | LOCK_NB)) + LogModule.report(logvisor::Error, FMT_STRING("flock {}: {}"), path, strerror(errno)); +#endif + } + + return fp; +} + +bool CheckFreeSpace(const char* path, size_t reqSz) { +#if _WIN32 + ULARGE_INTEGER freeBytes; + const nowide::wstackstring wpath(path); + std::array buf{}; + wchar_t* end = nullptr; + DWORD ret = GetFullPathNameW(wpath.get(), 1024, buf.data(), &end); + if (ret == 0 || ret > 1024) { + LogModule.report(logvisor::Error, FMT_STRING("GetFullPathNameW {}"), path); + return false; + } + if (end != nullptr) { + end[0] = L'\0'; + } + if (!GetDiskFreeSpaceExW(buf.data(), &freeBytes, nullptr, nullptr)) { + LogModule.report(logvisor::Error, FMT_STRING("GetDiskFreeSpaceExW {}: {}"), path, GetLastError()); + return false; + } + return reqSz < freeBytes.QuadPart; +#else + struct statvfs svfs; + if (statvfs(path, &svfs)) { + LogModule.report(logvisor::Error, FMT_STRING("statvfs {}: {}"), path, strerror(errno)); + return false; + } + return reqSz < svfs.f_frsize * svfs.f_bavail; +#endif +} +} // namespace nod