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