summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-09-10 19:37:07 +0200
committerCarl Hetherington <cth@carlh.net>2025-09-10 23:24:26 +0200
commit60a0242e7c5275b73bf3007d3245b8f35bb77c18 (patch)
tree1f362ca696b695964e9123229b84f401b7b7580b /src
parentaf1cabad632b5ad811c1020f6084b5063a36b407 (diff)
Pass some more OS error codes up through Result_t.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/KM_error.h7
-rw-r--r--src/KM_fileio.cpp41
-rwxr-xr-xsrc/KM_util.cpp9
3 files changed, 37 insertions, 20 deletions
diff --git a/src/KM_error.h b/src/KM_error.h
index ad94f6c..81a01c9 100755
--- a/src/KM_error.h
+++ b/src/KM_error.h
@@ -49,6 +49,10 @@ namespace Kumu
{
int value;
std::string label, symbol, message;
+ /** operating-system-dependent error code, if known.
+ * 0 on success or if the OS error code is not known.
+ */
+ int os_error = 0;
Result_t();
public:
@@ -67,6 +71,8 @@ namespace Kumu
static const Result_t& Get(unsigned int);
Result_t(int v, const std::string& s, const std::string& l);
+ // Create a new Result_t from an existing one with a specified operating system error code
+ Result_t(const Result_t& other, int o);
Result_t(const Result_t& rhs);
const Result_t& operator=(const Result_t& rhs);
~Result_t();
@@ -86,6 +92,7 @@ namespace Kumu
inline operator const char*() const { return label.c_str(); }
inline const char* Symbol() const { return symbol.c_str(); }
inline const char* Message() const { return message.c_str(); }
+ inline int OsError() const { return os_error; }
};
KM_DECLARE_RESULT(FALSE, 1, "Successful but not true.");
diff --git a/src/KM_fileio.cpp b/src/KM_fileio.cpp
index fd170c1..de3948a 100644
--- a/src/KM_fileio.cpp
+++ b/src/KM_fileio.cpp
@@ -923,7 +923,7 @@ Kumu::FileReader::OpenRead(const std::string& filename) const
if (m_Handle == INVALID_HANDLE_VALUE)
{
DefaultLogSink().Error("CreateFileW failed: %lu", last_error);
- return Kumu::RESULT_FILEOPEN;
+ return Kumu::Result_t(Kumu::RESULT_FILEOPEN, last_error);
}
return Kumu::RESULT_OK;
@@ -962,7 +962,7 @@ Kumu::FileReader::Seek(Kumu::fpos_t position, SeekPos_t whence) const
if ( (LastError != NO_ERROR
&& (in.LowPart == INVALID_SET_FILE_POINTER
|| in.LowPart == ERROR_NEGATIVE_SEEK )) )
- return Kumu::RESULT_READFAIL;
+ return Kumu::Result_t(Kumu::RESULT_READFAIL, LastError);
return Kumu::RESULT_OK;
}
@@ -986,7 +986,7 @@ Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
if ( (LastError != NO_ERROR
&& (in.LowPart == INVALID_SET_FILE_POINTER
|| in.LowPart == ERROR_NEGATIVE_SEEK )) )
- return Kumu::RESULT_READFAIL;
+ return Kumu::Result_t(Kumu::RESULT_READFAIL, LastError);
*pos = (Kumu::fpos_t)in.QuadPart;
return Kumu::RESULT_OK;
@@ -1068,7 +1068,7 @@ Kumu::FileWriter::OpenWrite(const std::string& filename)
if (m_Handle == INVALID_HANDLE_VALUE)
{
DefaultLogSink().Error("CreateFileW failed: %lu\n", last_error);
- return Kumu::RESULT_FILEOPEN;
+ return Kumu::Result_t(Kumu::RESULT_FILEOPEN, last_error);
}
m_IOVec = new h__iovec;
@@ -1109,7 +1109,7 @@ Kumu::FileWriter::OpenModify(const std::string& filename)
if (m_Handle == INVALID_HANDLE_VALUE)
{
DefaultLogSink().Error("CreateFileW failed: %lu\n", last_error);
- return Kumu::RESULT_FILEOPEN;
+ return Kumu::Result_t(Kumu::RESULT_FILEOPEN, last_error);
}
m_IOVec = new h__iovec;
@@ -1145,10 +1145,11 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
(DWORD*)&tmp_count,
NULL);
+ const auto last_error = GetLastError();
if ( wr_result == 0 || tmp_count != iov->m_iovec[i].iov_len)
{
- DefaultLogSink().Error("Writev failed (%d) (%d)", wr_result, GetLastError());
- result = Kumu::RESULT_WRITEFAIL;
+ DefaultLogSink().Error("Writev failed (%d) (%d)", wr_result, last_error);
+ result = Kumu::Result_t(Kumu::RESULT_WRITEFAIL, last_error);
break;
}
@@ -1284,7 +1285,7 @@ Kumu::FileWriter::OpenWrite(const std::string& filename)
if ( m_Handle == -1L )
{
DefaultLogSink().Error("Error opening file %s: %s\n", filename.c_str(), strerror(errno));
- return RESULT_FILEOPEN;
+ return Kumu::Result_t(RESULT_FILEOPEN, errno);
}
m_IOVec = new h__iovec;
@@ -1301,7 +1302,7 @@ Kumu::FileWriter::OpenModify(const std::string& filename)
if ( m_Handle == -1L )
{
DefaultLogSink().Error("Error opening file %s: %s\n", filename.c_str(), strerror(errno));
- return RESULT_FILEOPEN;
+ return Kumu::Result_t(RESULT_FILEOPEN, errno);
}
m_IOVec = new h__iovec;
@@ -1331,7 +1332,7 @@ Kumu::FileWriter::Writev(ui32_t* bytes_written)
if ( write_size == -1L || write_size != total_size )
{
DefaultLogSink().Error("writev failed (%d)", errno);
- return RESULT_WRITEFAIL;
+ return Kumu::Result_t(RESULT_WRITEFAIL, errno);
}
for (int i = 0; i < iov->m_Count; ++i)
@@ -1362,7 +1363,7 @@ Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written
if ( write_size == -1L || (ui32_t)write_size != buf_len )
{
DefaultLogSink().Error("write failed (%d)", errno);
- return RESULT_WRITEFAIL;
+ return Kumu::Result_t(RESULT_WRITEFAIL, errno);
}
MaybeHash(buf, buf_len);
@@ -1588,7 +1589,7 @@ Kumu::DirScanner::Open(const std::string& dirname)
break;
default:
DefaultLogSink().Error("DirScanner::Open(%s): %s\n", dirname.c_str(), strerror(errno));
- result = RESULT_FAIL;
+ result = Kumu::Result_t(RESULT_FAIL, errno);
break;
}
}
@@ -1612,7 +1613,7 @@ Kumu::DirScanner::Close()
return RESULT_STATE;
default:
DefaultLogSink().Error("DirScanner::Close(): %s\n", strerror(errno));
- return RESULT_FAIL;
+ return Kumu::Result_t(RESULT_FAIL, errno);
}
}
@@ -1676,7 +1677,7 @@ Kumu::DirScannerEx::Open(const std::string& dirname)
break;
default:
DefaultLogSink().Error("DirScanner::Open(%s): %s\n", dirname.c_str(), strerror(errno));
- result = RESULT_FAIL;
+ result = Kumu::Result_t(RESULT_FAIL, errno);
break;
}
}
@@ -1706,7 +1707,7 @@ Kumu::DirScannerEx::Close()
default:
DefaultLogSink().Error("DirScanner::Close(): %s\n", strerror(errno));
- return RESULT_FAIL;
+ return Kumu::Result_t(RESULT_FAIL, errno);
}
}
@@ -1813,7 +1814,7 @@ Kumu::CreateDirectoriesInPath(const std::string& Path)
{
DefaultLogSink().Error("CreateDirectoriesInPath mkdir %s: %s\n",
tmp_path.c_str(), strerror(errno));
- return RESULT_DIR_CREATE;
+ return Kumu::Result_t(RESULT_DIR_CREATE, errno);
}
}
}
@@ -1841,7 +1842,7 @@ Kumu::DeleteFile(const std::string& filename)
}
DefaultLogSink().Error("DeleteFile %s: %s\n", filename.c_str(), strerror(errno));
- return RESULT_FAIL;
+ return Kumu::Result_t(RESULT_FAIL, errno);
}
namespace Kumu
@@ -1900,7 +1901,7 @@ h__DeletePath(const std::string& pathname)
default:
DefaultLogSink().Error("DeletePath %s: %s\n", pathname.c_str(), strerror(errno));
- result = RESULT_FAIL;
+ result = Kumu::Result_t(RESULT_FAIL, errno);
}
}
}
@@ -1967,7 +1968,7 @@ Kumu::FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu:
HRESULT last_error = ::GetLastError();
DefaultLogSink().Error("FreeSpaceForPath GetDiskFreeSpaceEx %s: %lu\n", path.c_str(), last_error);
- return RESULT_FAIL;
+ return Result_t(RESULT_FAIL, last_error);
#else // KM_WIN32
struct statfs s;
@@ -2007,7 +2008,7 @@ Kumu::FreeSpaceForPath(const std::string& path, Kumu::fsize_t& free_space, Kumu:
}
DefaultLogSink().Error("FreeSpaceForPath statfs %s: %s\n", path.c_str(), strerror(errno));
- return RESULT_FAIL;
+ return Kumu::Result_t(RESULT_FAIL, errno);
#endif // __sun
#endif // KM_WIN32
}
diff --git a/src/KM_util.cpp b/src/KM_util.cpp
index ec20374..198ba37 100755
--- a/src/KM_util.cpp
+++ b/src/KM_util.cpp
@@ -165,12 +165,20 @@ Kumu::Result_t::Result_t(int v, const std::string& s, const std::string& l) : va
}
+Kumu::Result_t::Result_t(const Result_t& other, int o)
+{
+ *this = other;
+ os_error = o;
+}
+
+
Kumu::Result_t::Result_t(const Result_t& rhs)
{
value = rhs.value;
symbol = rhs.symbol;
label = rhs.label;
message = rhs.message;
+ os_error = rhs.os_error;
}
Kumu::Result_t::~Result_t() {}
@@ -183,6 +191,7 @@ Kumu::Result_t::operator=(const Result_t& rhs)
symbol = rhs.symbol;
label = rhs.label;
message = rhs.message;
+ os_error = rhs.os_error;
return *this;
}