From 1a0922b860b8a7648c232ac80e2fa66446dbdbb2 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Sat, 23 Nov 2024 11:52:18 +0100 Subject: [PATCH] Core: Fixed problems with handling non-existent files --- uppsrc/Core/BlockStream.cpp | 10 +++++++-- uppsrc/Core/Path.h | 3 ++- uppsrc/Core/Stream.h | 1 + uppsrc/Core/TimeDate.cpp | 14 +++++++----- upptst/InvalidFile/InvalidFile.cpp | 35 ++++++++++++++++++++++++++++++ upptst/InvalidFile/InvalidFile.upp | 10 +++++++++ upptst/InvalidFile/etalon.log | 32 +++++++++++++++++++++++++++ 7 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 upptst/InvalidFile/InvalidFile.cpp create mode 100644 upptst/InvalidFile/InvalidFile.upp create mode 100644 upptst/InvalidFile/etalon.log diff --git a/uppsrc/Core/BlockStream.cpp b/uppsrc/Core/BlockStream.cpp index 0798dca30..657c01394 100644 --- a/uppsrc/Core/BlockStream.cpp +++ b/uppsrc/Core/BlockStream.cpp @@ -28,6 +28,11 @@ void BlockStream::SetBufferSize(dword size) Seek(p); } +void BlockStream::Reset() +{ + streamsize = pos = 0; +} + BlockStream::BlockStream() { buffer = NULL; @@ -40,13 +45,13 @@ BlockStream::~BlockStream() } int64 BlockStream::GetSize() const { - if(IsError()) return 0; + if(IsError() || !ptr) return 0; return max(streamsize, ptr - buffer + pos); } void BlockStream::SyncSize() { - streamsize = max(streamsize, ptr - buffer + pos); + streamsize = ptr ? max(streamsize, ptr - buffer + pos) : 0; } void BlockStream::Flush() { @@ -367,6 +372,7 @@ void FileStream::Close() { SetLastError(); } handle = INVALID_HANDLE_VALUE; + Reset(); } bool FileStream::IsOpen() const { diff --git a/uppsrc/Core/Path.h b/uppsrc/Core/Path.h index f9dc60893..43348ef56 100644 --- a/uppsrc/Core/Path.h +++ b/uppsrc/Core/Path.h @@ -51,7 +51,8 @@ int Compare_FileTime(const FileTime& fa, const FileTime& fb); #ifdef PLATFORM_WIN32 struct FileTime : FILETIME, CompareRelOps { - FileTime() {} + operator bool() const { return dwLowDateTime || dwHighDateTime; } + FileTime() { dwLowDateTime = 0; dwHighDateTime = 0; } FileTime(const FILETIME& ft) { dwLowDateTime = ft.dwLowDateTime; dwHighDateTime = ft.dwHighDateTime; } }; diff --git a/uppsrc/Core/Stream.h b/uppsrc/Core/Stream.h index ce422d98b..9e0478c13 100644 --- a/uppsrc/Core/Stream.h +++ b/uppsrc/Core/Stream.h @@ -383,6 +383,7 @@ public: virtual ~BlockStream(); protected: + void Reset(); void OpenInit(dword mode, int64 file_size); }; diff --git a/uppsrc/Core/TimeDate.cpp b/uppsrc/Core/TimeDate.cpp index 234074a77..994d9fd69 100644 --- a/uppsrc/Core/TimeDate.cpp +++ b/uppsrc/Core/TimeDate.cpp @@ -437,11 +437,15 @@ void Time::Serialize(Stream& s) #ifdef PLATFORM_WIN32 Time::Time(FileTime filetime) { - SYSTEMTIME tm, tml; - FileTime ft; - FileTimeToSystemTime(&filetime, &tm); - SystemTimeToTzSpecificLocalTime(NULL, &tm, &tml); - *this = Time(tml.wYear, tml.wMonth, tml.wDay, tml.wHour, tml.wMinute, tml.wSecond); + if(filetime) { + SYSTEMTIME tm, tml; + FileTime ft; + FileTimeToSystemTime(&filetime, &tm); + SystemTimeToTzSpecificLocalTime(NULL, &tm, &tml); + *this = Time(tml.wYear, tml.wMonth, tml.wDay, tml.wHour, tml.wMinute, tml.wSecond); + } + else + *this = Null; } FileTime Time::AsFileTime() const diff --git a/upptst/InvalidFile/InvalidFile.cpp b/upptst/InvalidFile/InvalidFile.cpp new file mode 100644 index 000000000..45cecc8eb --- /dev/null +++ b/upptst/InvalidFile/InvalidFile.cpp @@ -0,0 +1,35 @@ +#include + +using namespace Upp; + +CONSOLE_APP_MAIN +{ + StdLogSetup(LOG_COUT|LOG_FILE); + DDUMP(FileGetTime("asfasdfasdfasdf")); + DDUMP(IsNull(FileGetTime("asfasdfasdfasdf"))); + DLOG("================"); + for(int pass = 0; pass < 2; pass++) { + FileIn in; + if(pass) { + DLOG("==== Opening nonexistent file"); + in.Open("aklsdflkasljkdf"); + } + + bool second = false; + for(;;) { + DDUMP((bool)in); + DDUMP(in.IsEof()); + DDUMP(in.IsError()); + DDUMP(in.GetPos()); + DDUMP(in.GetSize()); + DDUMP(in.Get()); + if(second) + break; + second = true; + DLOG("--- Seek 0"); + in.Seek(0); + } + } + + CheckLogEtalon(); +} diff --git a/upptst/InvalidFile/InvalidFile.upp b/upptst/InvalidFile/InvalidFile.upp new file mode 100644 index 000000000..08d7885ce --- /dev/null +++ b/upptst/InvalidFile/InvalidFile.upp @@ -0,0 +1,10 @@ +uses + Core; + +file + InvalidFile.cpp, + etalon.log; + +mainconfig + "" = ""; + diff --git a/upptst/InvalidFile/etalon.log b/upptst/InvalidFile/etalon.log new file mode 100644 index 000000000..903097749 --- /dev/null +++ b/upptst/InvalidFile/etalon.log @@ -0,0 +1,32 @@ +* C:\upp\out\upptst\CLANGx64.Debug.Debug_Full\InvalidFile.exe 23.11.2024 11:51:22, user: cxl + +FileGetTime("asfasdfasdfasdf") = +IsNull(FileGetTime("asfasdfasdfasdf")) = true +================ +(bool)in = false +in.IsEof() = true +in.IsError() = false +in.GetPos() = 0 +in.GetSize() = 0 +in.Get() = -1 +--- Seek 0 +(bool)in = false +in.IsEof() = true +in.IsError() = false +in.GetPos() = 0 +in.GetSize() = 0 +in.Get() = -1 +==== Opening nonexistent file +(bool)in = false +in.IsEof() = true +in.IsError() = true +in.GetPos() = 0 +in.GetSize() = 0 +in.Get() = -1 +--- Seek 0 +(bool)in = false +in.IsEof() = true +in.IsError() = true +in.GetPos() = 0 +in.GetSize() = 0 +in.Get() = -1