From b8f13e45be884dc12884ebe8f3dcd9eecb23a106 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:20:29 +0200 Subject: 5.5 --- lib/dir_lock.cpp | 77 ++++++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 47 deletions(-) (limited to 'lib/dir_lock.cpp') diff --git a/lib/dir_lock.cpp b/lib/dir_lock.cpp index f41bbfa8..682612a7 100644 --- a/lib/dir_lock.cpp +++ b/lib/dir_lock.cpp @@ -9,17 +9,15 @@ #include //#include #include -#include #include #include //includes #include #include -#include #include #include -#include #include #include +#include #ifdef FFS_WIN #include @@ -161,27 +159,6 @@ Zstring deleteAbandonedLockName(const Zstring& lockfilename) //make sure to NOT } -class CheckedLockReader : public CheckedReader -{ -public: - CheckedLockReader(wxInputStream& stream, const Zstring& errorObjName) : CheckedReader(stream), errorObjName_(errorObjName) {} - virtual void throwException() const { throw FileError(replaceCpy(_("Cannot read file %x."), L"%x", fmtFileName(errorObjName_))); } - -private: - const Zstring errorObjName_; -}; - -class CheckedLockWriter : public CheckedWriter -{ -public: - CheckedLockWriter(wxOutputStream& stream, const Zstring& errorObjName) : CheckedWriter(stream), errorObjName_(errorObjName) {} - virtual void throwException() const { throw FileError(replaceCpy(_("Cannot write file %x."), L"%x", fmtFileName(errorObjName_))); } - -private: - const Zstring errorObjName_; -}; - - #ifdef FFS_WIN std::wstring getLoginSid() //throw FileError { @@ -273,35 +250,35 @@ struct LockInformation //throw FileError } #endif - explicit LockInformation(CheckedLockReader& reader) + explicit LockInformation(BinStreamIn& stream) //throw UnexpectedEndOfStreamError { char tmp[sizeof(LOCK_FORMAT_DESCR)] = {}; - reader.readArray(&tmp, sizeof(tmp)); //file format header - const int lockFileVersion = reader.readPOD(); // + readArray(stream, &tmp, sizeof(tmp)); //file format header + const int lockFileVersion = readNumber(stream); // if (!std::equal(std::begin(tmp), std::end(tmp), std::begin(LOCK_FORMAT_DESCR)) || lockFileVersion != LOCK_FORMAT_VER) - reader.throwException(); + throw UnexpectedEndOfStreamError(); //well, not really...!? - reader.readString(lockId); - reader.readString(computerName); - reader.readString(userId); - reader.readString(sessionId); - processId = static_cast(reader.readPOD()); //[!] conversion + lockId = readContainer(stream); // + computerName = readContainer(stream); //UnexpectedEndOfStreamError + userId = readContainer(stream); // + sessionId = readContainer(stream); // + processId = static_cast(readNumber(stream)); //[!] conversion } - void toStream(CheckedLockWriter& writer) const + void toStream(BinStreamOut& stream) const //throw () { - writer.writeArray(LOCK_FORMAT_DESCR, sizeof(LOCK_FORMAT_DESCR)); - writer.writePOD(LOCK_FORMAT_VER); + writeArray(stream, LOCK_FORMAT_DESCR, sizeof(LOCK_FORMAT_DESCR)); + writeNumber(stream, LOCK_FORMAT_VER); assert_static(sizeof(processId) <= sizeof(std::uint64_t)); //ensure portability - writer.writeString(lockId); - writer.writeString(computerName); - writer.writeString(userId); - writer.writeString(sessionId); - writer.writePOD(processId); + writeContainer(stream, lockId); + writeContainer(stream, computerName); + writeContainer(stream, userId); + writeContainer(stream, sessionId); + writeNumber(stream, processId); } std::string lockId; //16 byte GUID - a universal identifier for this lock (no matter what the path is, considering symlinks, distributed network, etc.) @@ -322,17 +299,23 @@ struct LockInformation //throw FileError void writeLockInfo(const Zstring& lockfilename) //throw FileError { - FileOutputStream stream(lockfilename); //throw FileError - CheckedLockWriter writer(stream, lockfilename); - LockInformation(FromCurrentProcess()).toStream(writer); //throw FileError + BinStreamOut streamOut; + LockInformation(FromCurrentProcess()).toStream(streamOut); + saveBinStream(lockfilename, streamOut.get()); //throw FileError } LockInformation retrieveLockInfo(const Zstring& lockfilename) //throw FileError, ErrorNotExisting { - FileInputStream stream(lockfilename); //throw FileError, ErrorNotExisting - CheckedLockReader reader(stream, lockfilename); - return LockInformation(reader); //throw FileError + BinStreamIn streamIn = loadBinStream(lockfilename); //throw FileError, ErrorNotExisting + try + { + return LockInformation(streamIn); //throw UnexpectedEndOfStreamError + } + catch (UnexpectedEndOfStreamError&) + { + throw FileError(replaceCpy(_("Cannot read file %x."), L"%x", fmtFileName(lockfilename))); + } } -- cgit