OWBI1_TempFileEnumerationImplBase.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
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; // "OWEN"
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    // now we have to read the sig so that the temp file stream is
00064    // positioned correctly
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    // now we have to read the sig so that the temp file stream is
00078    // positioned correctly
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    // Append the size onto the end of the stream so we can recover it
00116    // when constructing from the file
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    // open the file and read the size that is written to the end of it.
00139    File f = FileSystem::openFile(filename.c_str());
00140    if (!f)
00141    {
00142       OWBI1_THROW(EnumerationException, "Failed to open file");
00143    }
00144    
00145    // Check that the correct signature is on the file
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 } // end namespace OWBI1
00188 
00189 
00190 

Generated on Thu Feb 9 08:48:29 2006 for openwbem by  doxygen 1.4.6