00001 #include "IOManagerArchiveFile.h"
00002 #include "gzstream.h"
00003 #include "IONameArchiveFile.h"
00004 #include "IOParamsArchive.h"
00005
00006 #include "../Streamer.h"
00007 #include "../Serializable.h"
00008 #include "../SctNames.h"
00009 #include "../XmlStyleStream.h"
00010 #include "../StdExceptionWrapper.h"
00011
00012 #include <cstdlib>
00013
00014 using std::string;
00015
00016 namespace Sct {
00017 namespace Archive {
00018
00019 IOManagerArchiveFile::IOManagerArchiveFile() throw() {
00020 }
00021
00022 IOManagerArchiveFile* IOManagerArchiveFile::s_man=0;
00023
00024 IOManagerArchiveFile& IOManagerArchiveFile::instance() {
00025 if (!s_man) s_man=new IOManagerArchiveFile();
00026 return *s_man;
00027 }
00028
00029 std::string IOManagerArchiveFile::status() const{
00030 std::ostringstream oss;
00031 oss << "IOManagerArchiveFile. Using Directory : "
00032 << SctNames::getPersistentDir() << std::endl;
00033 return oss.str();
00034 }
00035
00036 void IOManagerArchiveFile::write(const Serializable& ob, const IOParams* par) const {
00037 const IOParamsArchive* params = par ? dynamic_cast<const IOParamsArchive*>(par) : 0;
00038 boost::recursive_mutex::scoped_lock lock (getMutex());
00039 setReadMode(false);
00040 IONameArchiveFile name(ob.getUniqueID(), ob.getClassName());
00041 const std::string& file=name.getIOName();
00042 SctNames::Mrs() << "SCT_IO_MANAGER_ARCHIVE_FILE" << MRS_TEXT(std::string("Going to try to open file named [")+file+"] for writing.") << MRS_DIAGNOSTIC << ENDM;
00043
00044
00045 if (
00046 system( (string("test -w `dirname ")+file+"`").c_str() )
00047 ) throw FileException(file,"Directory not writable",__FILE__,__LINE__);
00048
00049 bool file_exists = !system( (string("test -f ")+file).c_str() );
00050 if ( file_exists ) {
00051 if (params && params->getOverWrite() ) {
00052 system( (string("chmod +w ")+file).c_str() );
00053 } else {
00054 throw FileException(file,"File already exists",__FILE__,__LINE__);
00055 }
00056 }
00057 ogzstream oz(name.getIOName().c_str(),std::ios::out, getCompressionLevel());
00058 if (!oz.good()) throw FileException(name.getIOName(),"Error opening file to write.",__FILE__,__LINE__);
00059
00060 XmlStyleOStream out_ad(oz);
00061
00062 out_ad << "SctRodDaq";
00063 writeImpl(out_ad, ob, false);
00064
00065 if (system( (string("chmod -w ")+file ).c_str() )) throw FileException(name.getIOName(),"Error making file read-only.",__FILE__,__LINE__);
00066 }
00067
00068 boost::shared_ptr<Serializable> IOManagerArchiveFile::read(const string& name, const IOParams* params) const {
00069 setReadMode(true);
00070 getReadVersionMap().clear();
00071 IONameArchiveFile ioname(name);
00072
00073 if ( system( (string("test -r ")+name).c_str() ) ) throw FileException(name,"Not readable",__FILE__,__LINE__);
00074
00075 igzstream iz(ioname.getIOName().c_str());
00076 if (!iz.good()) throw FileException(ioname.getIOName(),"Error opening file to read.",__FILE__,__LINE__);
00077
00078 XmlStyleIStream in_ad(iz);
00079 string tmp;
00080 in_ad >> tmp;
00081
00082 if (tmp.find("SctRodDaq")==string::npos) {
00083 std::ostringstream oss;
00084 oss << name << " does not look like a SctRodDaq archive file ... starts with " << tmp;
00085 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00086 }
00087
00088 return boost::dynamic_pointer_cast<Serializable>(readImpl(in_ad));
00089 }
00090 }
00091 }