CommonFuncs: LastStrerrorString added
This commit is contained in:
parent
4e40fad248
commit
4e5fe6366a
|
@ -22,27 +22,35 @@
|
|||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#define strerror_r(err, buf, len) strerror_s(buf, len, err)
|
||||
#endif
|
||||
|
||||
// Generic function to get last error message.
|
||||
// Call directly after the command or use the error num.
|
||||
// This function might change the error code.
|
||||
std::string GetLastErrorMsg()
|
||||
{
|
||||
const size_t buff_size = 256;
|
||||
char err_str[buff_size];
|
||||
constexpr size_t BUFFER_SIZE = 256;
|
||||
|
||||
// Wrapper function to get last strerror(errno) string.
|
||||
// This function might change the error code.
|
||||
std::string LastStrerrorString()
|
||||
{
|
||||
char error_message[BUFFER_SIZE];
|
||||
|
||||
#ifdef _WIN32
|
||||
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr);
|
||||
#else
|
||||
// We assume that the XSI-compliant version of strerror_r (returns int) is used
|
||||
// rather than the GNU version (returns char*). The returned value is stored to
|
||||
// an int variable to get a compile-time check that the return type is not char*.
|
||||
const int result = strerror_r(errno, err_str, buff_size);
|
||||
const int result = strerror_r(errno, error_message, BUFFER_SIZE);
|
||||
if (result != 0)
|
||||
return "";
|
||||
#endif
|
||||
|
||||
return std::string(err_str);
|
||||
return std::string(error_message);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
// Wrapper function to get GetLastError() string.
|
||||
// This function might change the error code.
|
||||
std::string GetLastErrorString()
|
||||
{
|
||||
char error_message[BUFFER_SIZE];
|
||||
|
||||
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr);
|
||||
return std::string(error_message);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -86,7 +86,12 @@ __declspec(dllimport) void __stdcall DebugBreak(void);
|
|||
}
|
||||
#endif // WIN32 ndef
|
||||
|
||||
// Generic function to get last error message.
|
||||
// Call directly after the command or use the error num.
|
||||
// Wrapper function to get last strerror(errno) string.
|
||||
// This function might change the error code.
|
||||
std::string GetLastErrorMsg();
|
||||
std::string LastStrerrorString();
|
||||
|
||||
#ifdef _WIN32
|
||||
// Wrapper function to get GetLastError() string.
|
||||
// This function might change the error code.
|
||||
std::string GetLastErrorString();
|
||||
#endif
|
||||
|
|
|
@ -141,14 +141,14 @@ bool Delete(const std::string& filename)
|
|||
if (!DeleteFile(UTF8ToTStr(filename).c_str()))
|
||||
{
|
||||
WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", filename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
GetLastErrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
if (unlink(filename.c_str()) == -1)
|
||||
{
|
||||
WARN_LOG(COMMON, "Delete: unlink failed on %s: %s", filename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
LastStrerrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
@ -241,11 +241,14 @@ bool DeleteDir(const std::string& filename)
|
|||
#ifdef _WIN32
|
||||
if (::RemoveDirectory(UTF8ToTStr(filename).c_str()))
|
||||
return true;
|
||||
ERROR_LOG(COMMON, "DeleteDir: RemoveDirectory failed on %s: %s", filename.c_str(),
|
||||
GetLastErrorString().c_str());
|
||||
#else
|
||||
if (rmdir(filename.c_str()) == 0)
|
||||
return true;
|
||||
ERROR_LOG(COMMON, "DeleteDir: rmdir failed on %s: %s", filename.c_str(),
|
||||
LastStrerrorString().c_str());
|
||||
#endif
|
||||
ERROR_LOG(COMMON, "DeleteDir: %s: %s", filename.c_str(), GetLastErrorMsg().c_str());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -268,12 +271,14 @@ bool Rename(const std::string& srcFilename, const std::string& destFilename)
|
|||
if (MoveFile(sf.c_str(), df.c_str()))
|
||||
return true;
|
||||
}
|
||||
ERROR_LOG(COMMON, "Rename: MoveFile failed on %s --> %s: %s", srcFilename.c_str(),
|
||||
destFilename.c_str(), GetLastErrorString().c_str());
|
||||
#else
|
||||
if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
|
||||
return true;
|
||||
ERROR_LOG(COMMON, "Rename: rename failed on %s --> %s: %s", srcFilename.c_str(),
|
||||
destFilename.c_str(), LastStrerrorString().c_str());
|
||||
#endif
|
||||
ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -321,7 +326,7 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
|||
return true;
|
||||
|
||||
ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
GetLastErrorString().c_str());
|
||||
return false;
|
||||
#else
|
||||
|
||||
|
@ -335,8 +340,7 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
|||
OpenFStream(input, srcFilename, std::ifstream::in | std::ifstream::binary);
|
||||
if (!input.is_open())
|
||||
{
|
||||
ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s", srcFilename.c_str(), destFilename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "Copy: failed to open %s", srcFilename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -346,7 +350,7 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
|||
if (!output.IsOpen())
|
||||
{
|
||||
ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s", srcFilename.c_str(),
|
||||
destFilename.c_str(), GetLastErrorMsg().c_str());
|
||||
destFilename.c_str(), LastStrerrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -357,8 +361,8 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
|||
input.read(buffer, BSIZE);
|
||||
if (!input)
|
||||
{
|
||||
ERROR_LOG(COMMON, "Copy: failed reading from source, %s --> %s: %s", srcFilename.c_str(),
|
||||
destFilename.c_str(), GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "Copy: failed reading from source, %s --> %s", srcFilename.c_str(),
|
||||
destFilename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -366,7 +370,7 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename)
|
|||
if (!output.WriteBytes(buffer, BSIZE))
|
||||
{
|
||||
ERROR_LOG(COMMON, "Copy: failed writing to output, %s --> %s: %s", srcFilename.c_str(),
|
||||
destFilename.c_str(), GetLastErrorMsg().c_str());
|
||||
destFilename.c_str(), LastStrerrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -394,14 +398,14 @@ u64 GetSize(FILE* f)
|
|||
u64 pos = ftello(f);
|
||||
if (fseeko(f, 0, SEEK_END) != 0)
|
||||
{
|
||||
ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", f, GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", f, LastStrerrorString().c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
u64 size = ftello(f);
|
||||
if ((size != pos) && (fseeko(f, pos, SEEK_SET) != 0))
|
||||
{
|
||||
ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", f, GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", f, LastStrerrorString().c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -416,7 +420,7 @@ bool CreateEmptyFile(const std::string& filename)
|
|||
if (!File::IOFile(filename, "wb"))
|
||||
{
|
||||
ERROR_LOG(COMMON, "CreateEmptyFile: failed %s: %s", filename.c_str(),
|
||||
GetLastErrorMsg().c_str());
|
||||
LastStrerrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -620,7 +624,7 @@ std::string GetCurrentDir()
|
|||
char* dir = __getcwd(nullptr, 0);
|
||||
if (!dir)
|
||||
{
|
||||
ERROR_LOG(COMMON, "GetCurrentDirectory failed: %s", GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "GetCurrentDirectory failed: %s", LastStrerrorString().c_str());
|
||||
return nullptr;
|
||||
}
|
||||
std::string strDir = dir;
|
||||
|
|
|
@ -136,7 +136,7 @@ u8* MemArena::FindMemoryBase()
|
|||
u8* base = static_cast<u8*>(VirtualAlloc(nullptr, memory_size, MEM_RESERVE, PAGE_READWRITE));
|
||||
if (!base)
|
||||
{
|
||||
PanicAlert("Failed to map enough memory space: %s", GetLastErrorMsg().c_str());
|
||||
PanicAlert("Failed to map enough memory space: %s", GetLastErrorString().c_str());
|
||||
return nullptr;
|
||||
}
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
|
@ -153,7 +153,7 @@ u8* MemArena::FindMemoryBase()
|
|||
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
||||
if (base == MAP_FAILED)
|
||||
{
|
||||
PanicAlert("Failed to map enough memory space: %s", GetLastErrorMsg().c_str());
|
||||
PanicAlert("Failed to map enough memory space: %s", LastStrerrorString().c_str());
|
||||
return nullptr;
|
||||
}
|
||||
munmap(base, memory_size);
|
||||
|
|
|
@ -88,20 +88,13 @@ void FreeMemoryPages(void* ptr, size_t size)
|
|||
{
|
||||
if (ptr)
|
||||
{
|
||||
bool error_occurred = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
if (!VirtualFree(ptr, 0, MEM_RELEASE))
|
||||
error_occurred = true;
|
||||
PanicAlert("FreeMemoryPages failed!\nVirtualFree: %s", GetLastErrorString().c_str());
|
||||
#else
|
||||
int retval = munmap(ptr, size);
|
||||
|
||||
if (retval != 0)
|
||||
error_occurred = true;
|
||||
if (munmap(ptr, size) != 0)
|
||||
PanicAlert("FreeMemoryPages failed!\nmunmap: %s", LastStrerrorString().c_str());
|
||||
#endif
|
||||
|
||||
if (error_occurred)
|
||||
PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,60 +112,41 @@ void FreeAlignedMemory(void* ptr)
|
|||
|
||||
void ReadProtectMemory(void* ptr, size_t size)
|
||||
{
|
||||
bool error_occurred = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD oldValue;
|
||||
if (!VirtualProtect(ptr, size, PAGE_NOACCESS, &oldValue))
|
||||
error_occurred = true;
|
||||
PanicAlert("ReadProtectMemory failed!\nVirtualProtect: %s", GetLastErrorString().c_str());
|
||||
#else
|
||||
int retval = mprotect(ptr, size, PROT_NONE);
|
||||
|
||||
if (retval != 0)
|
||||
error_occurred = true;
|
||||
if (mprotect(ptr, size, PROT_NONE) != 0)
|
||||
PanicAlert("ReadProtectMemory failed!\nmprotect: %s", LastStrerrorString().c_str());
|
||||
#endif
|
||||
|
||||
if (error_occurred)
|
||||
PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg().c_str());
|
||||
}
|
||||
|
||||
void WriteProtectMemory(void* ptr, size_t size, bool allowExecute)
|
||||
{
|
||||
bool error_occurred = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD oldValue;
|
||||
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue))
|
||||
error_occurred = true;
|
||||
PanicAlert("WriteProtectMemory failed!\nVirtualProtect: %s", GetLastErrorString().c_str());
|
||||
#else
|
||||
int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ);
|
||||
|
||||
if (retval != 0)
|
||||
error_occurred = true;
|
||||
if (mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ) != 0)
|
||||
PanicAlert("WriteProtectMemory failed!\nmprotect: %s", LastStrerrorString().c_str());
|
||||
#endif
|
||||
|
||||
if (error_occurred)
|
||||
PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());
|
||||
}
|
||||
|
||||
void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute)
|
||||
{
|
||||
bool error_occurred = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
DWORD oldValue;
|
||||
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue))
|
||||
error_occurred = true;
|
||||
PanicAlert("UnWriteProtectMemory failed!\nVirtualProtect: %s", GetLastErrorString().c_str());
|
||||
#else
|
||||
int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) :
|
||||
PROT_WRITE | PROT_READ);
|
||||
|
||||
if (retval != 0)
|
||||
error_occurred = true;
|
||||
if (mprotect(ptr, size,
|
||||
allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ) != 0)
|
||||
{
|
||||
PanicAlert("UnWriteProtectMemory failed!\nmprotect: %s", LastStrerrorString().c_str());
|
||||
}
|
||||
#endif
|
||||
|
||||
if (error_occurred)
|
||||
PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());
|
||||
}
|
||||
|
||||
size_t MemPhysical()
|
||||
|
|
|
@ -291,7 +291,7 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename)
|
|||
FailWrite:
|
||||
ERROR_LOG(COMMON, "Could not write to '%s', aborting...", filename.c_str());
|
||||
if (unlink(filename.c_str()) < 0)
|
||||
ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), GetLastErrorMsg().c_str());
|
||||
ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), LastStrerrorString().c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue