aboutsummaryrefslogtreecommitdiff
path: root/src/serialize.h
diff options
context:
space:
mode:
authorPhilip Kaufmann <phil.kaufmann@t-online.de>2014-09-25 08:53:43 +0200
committerPhilip Kaufmann <phil.kaufmann@t-online.de>2014-10-02 10:46:31 +0200
commitc9fb27da0a72135417956dca8dafa959ebb67c10 (patch)
tree7676f91bda9f87762ff5cdd2cbee18eba50dd854 /src/serialize.h
parent00eef5d6e9b08b173492599ec41c7feb445e2139 (diff)
CBufferedFile: convert into a non-refcounted RAII wrapper
- it now takes over the passed file descriptor and closes it in the destructor - this fixes a leak in LoadExternalBlockFile(), where an exception could cause the file to not getting closed - disallow copies (like recently added for CAutoFile) - make nType and nVersion private
Diffstat (limited to 'src/serialize.h')
-rw-r--r--src/serialize.h30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/serialize.h b/src/serialize.h
index 68501facf2..f56dce1481 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -1256,13 +1256,19 @@ public:
}
};
-/** Wrapper around a FILE* that implements a ring buffer to
- * deserialize from. It guarantees the ability to rewind
- * a given number of bytes. */
+/** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
+ * deserialize from. It guarantees the ability to rewind a given number of bytes. */
class CBufferedFile
{
private:
- FILE *src; // source file
+ // Disallow copies
+ CBufferedFile(const CBufferedFile&);
+ CBufferedFile& operator=(const CBufferedFile&);
+
+ int nType;
+ int nVersion;
+
+ FILE *src; // source file
uint64_t nSrcPos; // how many bytes have been read from source
uint64_t nReadPos; // how many bytes have been read from this
uint64_t nReadLimit; // up to which position we're allowed to read
@@ -1289,12 +1295,18 @@ protected:
}
public:
- int nType;
- int nVersion;
-
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
- src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0),
- nType(nTypeIn), nVersion(nVersionIn) {
+ nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
+ {
+ src = fileIn;
+ nType = nTypeIn;
+ nVersion = nVersionIn;
+ }
+
+ ~CBufferedFile()
+ {
+ if (src)
+ fclose(src);
}
// check whether we're at the end of the source file