static Kumu::Result_t
do_stat(const char* path, fstat_t* stat_info)
{
- KM_TEST_NULL_STR(path);
- KM_TEST_NULL(stat_info);
+ KM_TEST_NULL_STR_L(path);
+ KM_TEST_NULL_L(stat_info);
Kumu::Result_t result = Kumu::RESULT_OK;
static Kumu::Result_t
do_fstat(HANDLE handle, fstat_t* stat_info)
{
- KM_TEST_NULL(stat_info);
+ KM_TEST_NULL_L(stat_info);
Kumu::Result_t result = Kumu::RESULT_OK;
bool
Kumu::PathIsFile(const char* pathname)
{
- assert(pathname);
+ if ( pathname == 0 || *pathname == 0 )
+ return false;
+
fstat_t info;
if ( KM_SUCCESS(do_stat(pathname, &info)) )
bool
Kumu::PathIsDirectory(const char* pathname)
{
- assert(pathname);
+ if ( pathname == 0 || *pathname == 0 )
+ return false;
+
fstat_t info;
if ( KM_SUCCESS(do_stat(pathname, &info)) )
Kumu::fsize_t
Kumu::FileSize(const char* pathname)
{
- assert(pathname);
+ if ( pathname == 0 || *pathname == 0 )
+ return false;
+
fstat_t info;
if ( KM_SUCCESS(do_stat(pathname, &info)) )
{
assert( ! m_IOVec.empty() );
register h__iovec* iov = m_IOVec;
- KM_TEST_NULL(buf);
+ KM_TEST_NULL_L(buf);
if ( iov->m_Count >= IOVecMaxEntries )
{
DefaultLogSink().Error("The iovec is full! Only %u entries allowed before a flush.\n",
IOVecMaxEntries);
- return RESULT_FAIL;
+ return RESULT_WRITEFAIL;
}
iov->m_iovec[iov->m_Count].iov_base = (char*)buf; // stupid iovec uses char*
Kumu::Result_t
Kumu::FileReader::OpenRead(const char* filename) const
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
const_cast<FileReader*>(this)->m_Filename = filename;
// suppress popup window on error
Kumu::Result_t
Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
{
- KM_TEST_NULL(pos);
+ KM_TEST_NULL_L(pos);
if ( m_Handle == (HANDLE)-1L )
return Kumu::RESULT_FILEOPEN;
Kumu::Result_t
Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
{
- KM_TEST_NULL(buf);
+ KM_TEST_NULL_L(buf);
Result_t result = Kumu::RESULT_OK;
DWORD tmp_count;
ui32_t tmp_int;
Kumu::Result_t
Kumu::FileWriter::OpenWrite(const char* filename)
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
m_Filename = filename;
// suppress popup window on error
(DWORD*)&tmp_count,
NULL);
- if ( wr_result == 0 )
+ if ( wr_result == 0 || tmp_count != iov->m_iovec[i].iov_len)
{
result = Kumu::RESULT_WRITEFAIL;
break;
}
- assert(iov->m_iovec[i].iov_len == tmp_count);
*bytes_written += tmp_count;
}
Kumu::Result_t
Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written)
{
- KM_TEST_NULL(buf);
+ KM_TEST_NULL_L(buf);
ui32_t tmp_int;
if ( bytes_written == 0 )
BOOL result = ::WriteFile(m_Handle, buf, buf_len, (DWORD*)bytes_written, NULL);
::SetErrorMode(prev);
- return ( result == 0 ) ? Kumu::RESULT_WRITEFAIL : Kumu::RESULT_OK;
+ if ( result == 0 || bytes_written != buf_len )
+ return Kumu::RESULT_WRITEFAIL;
+
+ return Kumu::RESULT_OK;
}
#else // KM_WIN32
Kumu::Result_t
Kumu::FileReader::OpenRead(const char* filename) const
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
const_cast<FileReader*>(this)->m_Filename = filename;
const_cast<FileReader*>(this)->m_Handle = open(filename, O_RDONLY, 0);
return ( m_Handle == -1L ) ? RESULT_FILEOPEN : RESULT_OK;
Kumu::Result_t
Kumu::FileReader::Tell(Kumu::fpos_t* pos) const
{
- KM_TEST_NULL(pos);
+ KM_TEST_NULL_L(pos);
if ( m_Handle == -1L )
return RESULT_FILEOPEN;
Kumu::Result_t
Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const
{
- KM_TEST_NULL(buf);
+ KM_TEST_NULL_L(buf);
i32_t tmp_count = 0;
ui32_t tmp_int = 0;
Kumu::Result_t
Kumu::FileWriter::OpenWrite(const char* filename)
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
m_Filename = filename;
m_Handle = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0664);
Kumu::Result_t
Kumu::FileWriter::OpenModify(const char* filename)
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
m_Filename = filename;
m_Handle = open(filename, O_RDWR|O_CREAT, 0664);
if ( m_Handle == -1L )
return RESULT_STATE;
- int read_size = writev(m_Handle, iov->m_iovec, iov->m_Count);
+ int total_size = 0;
+ for ( int i = 0; i < iov->m_Count; i++ )
+ total_size += iov->m_iovec[i].iov_len;
+
+ int write_size = writev(m_Handle, iov->m_iovec, iov->m_Count);
- if ( read_size == -1L )
+ if ( write_size == -1L || write_size != total_size )
return RESULT_WRITEFAIL;
iov->m_Count = 0;
- *bytes_written = read_size;
+ *bytes_written = write_size;
return RESULT_OK;
}
Kumu::Result_t
Kumu::FileWriter::Write(const byte_t* buf, ui32_t buf_len, ui32_t* bytes_written)
{
- KM_TEST_NULL(buf);
+ KM_TEST_NULL_L(buf);
ui32_t tmp_int;
if ( bytes_written == 0 )
bytes_written = &tmp_int;
- // TODO: flush iovec
-
-
if ( m_Handle == -1L )
return RESULT_STATE;
- int read_size = write(m_Handle, buf, buf_len);
-
- if ( read_size == -1L )
+ int write_size = write(m_Handle, buf, buf_len);
+
+ if ( write_size == -1L || (ui32_t)write_size != buf_len )
return RESULT_WRITEFAIL;
- *bytes_written = read_size;
+ *bytes_written = write_size;
return RESULT_OK;
}
FileReader File;
ByteString ReadBuf;
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
Result_t result = File.OpenRead(filename);
if ( fsize > (Kumu::fpos_t)max_size )
{
- DefaultLogSink().Error("%s: exceeds available buffer size (%u)", filename, max_size);
+ DefaultLogSink().Error("%s: exceeds available buffer size (%u)\n", filename, max_size);
return RESULT_ALLOC;
}
+ if ( fsize == 0 )
+ {
+ DefaultLogSink().Error("%s: zero file size\n", filename);
+ return RESULT_READFAIL;
+ }
+
result = ReadBuf.Capacity((ui32_t)fsize);
}
{
FileWriter File;
ui32_t write_count = 0;
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
Result_t result = File.OpenWrite(filename);
if ( KM_SUCCESS(result) )
result = File.Write((byte_t*)inString.c_str(), inString.length(), &write_count);
- if ( KM_SUCCESS(result) && write_count != inString.length() )
- return RESULT_WRITEFAIL;
-
- return RESULT_OK;
+ return result;
}
Result_t
Kumu::DirScanner::Open(const char* filename)
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
// we need to append a '*' to read the entire directory
ui32_t fn_len = strlen(filename);
Result_t
Kumu::DirScanner::GetNext(char* filename)
{
- KM_TEST_NULL(filename);
+ KM_TEST_NULL_L(filename);
if ( m_Handle == -1 )
return RESULT_FILEOPEN;
Result_t
Kumu::DirScanner::Open(const char* filename)
{
- KM_TEST_NULL_STR(filename);
+ KM_TEST_NULL_STR_L(filename);
Result_t result = RESULT_OK;
Result_t
Kumu::DirScanner::GetNext(char* filename)
{
- KM_TEST_NULL(filename);
+ KM_TEST_NULL_L(filename);
if ( m_Handle == NULL )
return RESULT_FILEOPEN;