00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00035 #include "OWBI1_config.h"
00036 #include "OWBI1_TempFileEnumerationImplBase.hpp"
00037 #include "OWBI1_EnumerationException.hpp"
00038 #include "OW_File.hpp"
00039 #include "OW_FileSystem.hpp"
00040 #include "OWBI1_Types.hpp"
00041 #include "OW_TempFileStream.hpp"
00042 #include "OWBI1_String.hpp"
00043
00044 namespace OWBI1
00045 {
00046 using namespace OpenWBEM;
00047
00048 namespace
00049 {
00050 const UInt32 TEMPFILE_ENUMERATION_SIG = 0x4f57454e;
00051 }
00052
00053 TempFileEnumerationImplBase::TempFileEnumerationImplBase()
00054 : m_size(0), m_data(new OpenWBEM::TempFileStream())
00055 {
00056 UInt32 enumSig = TEMPFILE_ENUMERATION_SIG;
00057 m_data->write(reinterpret_cast<const char*>(&enumSig), sizeof(enumSig));
00058 if (!m_data->good())
00059 {
00060 OWBI1_THROW(EnumerationException, "Failed to write signature to "
00061 "enumeration tempfile.");
00062 }
00063
00064
00065 UInt32 tmpSig;
00066 m_data->read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00067 if (!m_data->good())
00068 {
00069 OWBI1_THROW(EnumerationException, "Failed to read signature from "
00070 "enumeration tempfile.");
00071 }
00072 }
00073
00074 TempFileEnumerationImplBase::TempFileEnumerationImplBase(String const& filename)
00075 : m_size(readSize(filename)), m_data(new OpenWBEM::TempFileStream(filename.c_str()))
00076 {
00077
00078
00079 UInt32 tmpSig;
00080 m_data->read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00081 if (!m_data->good())
00082 {
00083 OWBI1_THROW(EnumerationException, "Failed to read signature of "
00084 "enumeration tempfile.");
00085 }
00086 if (tmpSig != TEMPFILE_ENUMERATION_SIG)
00087 {
00088 OWBI1_THROW(EnumerationException, "Signature of enumeration tempfile is not valid.");
00089 }
00090 }
00091 TempFileEnumerationImplBase::~TempFileEnumerationImplBase()
00092 {
00093 delete m_data;
00094 }
00095 bool
00096 TempFileEnumerationImplBase::hasMoreElements() const
00097 {
00098
00099 return m_size > 0;
00100 }
00101 size_t
00102 TempFileEnumerationImplBase::numberOfElements() const
00103 {
00104 return m_size;
00105 }
00106 void
00107 TempFileEnumerationImplBase::clear()
00108 {
00109 m_size = 0;
00110 m_data->reset();
00111 }
00112 String
00113 TempFileEnumerationImplBase::releaseFile()
00114 {
00115
00116
00117 m_data->write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
00118 if (!m_data->good())
00119 {
00120 OWBI1_THROW(EnumerationException, "Failed to write size to "
00121 "enumeration tempfile.");
00122 }
00123 String rval = m_data->releaseFile().c_str();
00124 clear();
00125 return rval;
00126 }
00127
00128 bool
00129 TempFileEnumerationImplBase::usingTempFile() const
00130 {
00131 return m_data->usingTempFile();
00132 }
00133
00134 size_t
00135 TempFileEnumerationImplBase::readSize(String const& filename)
00136 {
00137 size_t size;
00138
00139 File f = FileSystem::openFile(filename.c_str());
00140 if (!f)
00141 {
00142 OWBI1_THROW(EnumerationException, "Failed to open file");
00143 }
00144
00145
00146 UInt32 fileSig;
00147 if (f.read(reinterpret_cast<char*>(&fileSig), sizeof(fileSig)) != sizeof(fileSig))
00148 {
00149 OWBI1_THROW(EnumerationException, "Failure to read enumeration "
00150 "signature");
00151 }
00152 if (fileSig != TEMPFILE_ENUMERATION_SIG)
00153 {
00154 OWBI1_THROW(EnumerationException, "Attempted to construct an "
00155 "enumeration from a file that does not have the correct "
00156 "signature");
00157 }
00158
00159 off_t whence = f.seek(-static_cast<off_t>(sizeof(size)), SEEK_END);
00160 if (whence == -1)
00161 {
00162 OWBI1_THROW(EnumerationException, "Failure to seek");
00163 }
00164 if (f.read(reinterpret_cast<char*>(&size), sizeof(size), whence) != sizeof(size))
00165 {
00166 OWBI1_THROW(EnumerationException, "Failure to read enumeration "
00167 "size");
00168 }
00169 if (f.close() == -1)
00170 {
00171 OWBI1_THROW(EnumerationException, "Failure to close enumeration "
00172 "file");
00173 }
00174 return size;
00175 }
00176
00177 void
00178 TempFileEnumerationImplBase::throwIfEmpty() const
00179 {
00180 if (!hasMoreElements())
00181 {
00182 OWBI1_THROW (EnumerationException, "Attempt to Extract from empty Enum");
00183 }
00184 }
00185
00186
00187 }
00188
00189
00190