diff --git a/src/libsrc++/Writer.cpp b/src/libsrc++/Writer.cpp index 2048958c..2ac91255 100644 --- a/src/libsrc++/Writer.cpp +++ b/src/libsrc++/Writer.cpp @@ -459,12 +459,15 @@ namespace evio { * the dictionary and/or first event will be placed in its * own record and written as the user header. * @param userLen length of valid data (bytes) in userHdr (starting at off). + * @param overwrite if true, overwrite existing file. * @throws EvioException filename arg is null, if constructor specified writing to a buffer, * if open() was already called without being followed by reset(), - * if file cannot be found, if IO error writing to file, - * or if filename is empty. + * if IO error writing to file, + * if filename is empty, + * or if overwrite is false and file exists. + */ - void Writer::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen) { + void Writer::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite) { if (opened) { throw EvioException("currently open, call reset() first"); @@ -503,7 +506,14 @@ namespace evio { // Write this to file fileName = filename; - // TODO: what flags??? instead of "rw" + if (!overwrite) { + // Check if the file exists using std::ifstream + std::ifstream fileCheck(filename); + if (fileCheck.good()) { + throw EvioException("file already exists: " + filename); + } + } + outFile.open(filename, std::ios::binary); if (outFile.fail()) { throw EvioException("error opening file " + filename); diff --git a/src/libsrc++/Writer.h b/src/libsrc++/Writer.h index 571e9ceb..ebf6b1fe 100644 --- a/src/libsrc++/Writer.h +++ b/src/libsrc++/Writer.h @@ -191,7 +191,7 @@ namespace evio { void addTrailerWithIndex(bool addTrailingIndex); void open(const std::string & filename); - void open(const std::string & filename, uint8_t* userHdr, uint32_t len); + void open(const std::string & filename, uint8_t* userHdr, uint32_t len, bool overwrite = true); void open(std::shared_ptr buf, uint8_t* userHdr, uint32_t len); static std::shared_ptr createRecord(const std::string & dictionary, diff --git a/src/libsrc++/WriterMT.cpp b/src/libsrc++/WriterMT.cpp index 32847be2..062e2011 100644 --- a/src/libsrc++/WriterMT.cpp +++ b/src/libsrc++/WriterMT.cpp @@ -268,11 +268,14 @@ namespace evio { * the dictionary and/or first event will be placed in its * own record and written as the user header. * @param userLen length of userHdr in bytes. + * @param overwrite if true, overwrite existing file. * @throws EvioException if filename arg is bad, - * or if open() was already called without being followed by reset(). - * @throws IOException if file cannot be found or IO error writing to file + * if open() was already called without being followed by reset(), + * if IO error writing to file, + * if filename is empty, + * if overwrite is false and file exists. */ - void WriterMT::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen) { + void WriterMT::open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite) { if (opened) { throw EvioException("currently open, call reset() first"); @@ -307,7 +310,14 @@ namespace evio { // Write this to file fileName = filename; - // TODO: what flags??? instead of "rw" + if (!overwrite) { + // Check if the file exists using std::ifstream + std::ifstream fileCheck(filename); + if (fileCheck.good()) { + throw EvioException("file already exists: " + filename); + } + } + outFile.open(filename, std::ios::binary); outFile.write(reinterpret_cast(fileHeaderBuffer->array()), fileHeaderBuffer->remaining()); if (outFile.fail()) { diff --git a/src/libsrc++/WriterMT.h b/src/libsrc++/WriterMT.h index 3d8c7423..2d1081f7 100644 --- a/src/libsrc++/WriterMT.h +++ b/src/libsrc++/WriterMT.h @@ -365,7 +365,7 @@ namespace evio { void addTrailerWithIndex(bool addTrailingIndex); void open(const std::string & filename); - void open(const std::string & filename, uint8_t* userHdr, uint32_t userLen); + void open(const std::string & filename, uint8_t* userHdr, uint32_t userLen, bool overwrite = true); std::shared_ptr createHeader(uint8_t* userHdr, uint32_t userLen); std::shared_ptr createHeader(ByteBuffer & userHdr);