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
00043
00044 if (
00045 system( (string("test -w `dirname ")+file+"`").c_str() )
00046 ) throw FileException(file,"Directory not writable",__FILE__,__LINE__);
00047
00048 bool file_exists = !system( (string("test -f ")+file).c_str() );
00049 if ( file_exists ) {
00050 if (params && params->getOverWrite() ) {
00051 system( (string("chmod +w ")+file).c_str() );
00052 } else {
00053 throw FileException(file,"File already exists",__FILE__,__LINE__);
00054 }
00055 }
00056 ogzstream oz(name.getIOName().c_str(),std::ios::out, getCompressionLevel());
00057 if (!oz.good()) throw FileException(name.getIOName(),"Error opening file to write.",__FILE__,__LINE__);
00058
00059 XmlStyleOStream out_ad(oz);
00060
00061 out_ad << "SctRodDaq";
00062 writeImpl(out_ad, ob, false);
00063
00064 if (system( (string("chmod -w ")+file ).c_str() )) throw FileException(name.getIOName(),"Error making file read-only.",__FILE__,__LINE__);
00065 }
00066
00067 boost::shared_ptr<Serializable> IOManagerArchiveFile::read(const string& name, const IOParams* params) const {
00068 setReadMode(true);
00069 getReadVersionMap().clear();
00070 IONameArchiveFile ioname(name);
00071
00072 if ( system( (string("test -r ")+name).c_str() ) ) throw FileException(name,"Not readable",__FILE__,__LINE__);
00073
00074 igzstream iz(ioname.getIOName().c_str());
00075 if (!iz.good()) throw FileException(ioname.getIOName(),"Error opening file to read.",__FILE__,__LINE__);
00076
00077 XmlStyleIStream in_ad(iz);
00078 string tmp;
00079 in_ad >> tmp;
00080
00081 if (tmp.find("SctRodDaq")==string::npos) {
00082 std::ostringstream oss;
00083 oss << name << " does not look like a SctRodDaq archive file ... starts with " << tmp;
00084 throw StreamCorruptedException(oss.str(), __FILE__, __LINE__);
00085 }
00086
00087 return boost::dynamic_pointer_cast<Serializable>(readImpl(in_ad));
00088 }
00089 }
00090 }