OW_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 "OW_config.h"
00036 #include "OW_TempFileEnumerationImplBase.hpp"
00037 #include "OW_EnumerationException.hpp"
00038 #include "OW_File.hpp"
00039 #include "OW_FileSystem.hpp"
00040 
00041 namespace OW_NAMESPACE
00042 {
00043 
00044 namespace
00045 {
00046 const UInt32 TEMPFILE_ENUMERATION_SIG = 0x4f57454e; // "OWEN"
00047 }
00048 
00049 TempFileEnumerationImplBase::TempFileEnumerationImplBase()
00050    : m_size(0), m_Data()
00051 {
00052    UInt32 enumSig = TEMPFILE_ENUMERATION_SIG;
00053    m_Data.write(reinterpret_cast<const char*>(&enumSig), sizeof(enumSig));
00054    if (!m_Data.good())
00055    {
00056       OW_THROW(EnumerationException, "Failed to write signature to "
00057          "enumeration tempfile.");
00058    }
00059    // now we have to read the sig so that the temp file stream is
00060    // positioned correctly
00061    UInt32 tmpSig;
00062    m_Data.read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00063    if (!m_Data.good())
00064    {
00065       OW_THROW(EnumerationException, "Failed to read signature from "
00066          "enumeration tempfile.");
00067    }
00068 }
00069 
00070 TempFileEnumerationImplBase::TempFileEnumerationImplBase(String const& filename)
00071    : m_size(readSize(filename)), m_Data(filename)
00072 {
00073    // now we have to read the sig so that the temp file stream is
00074    // positioned correctly
00075    UInt32 tmpSig;
00076    m_Data.read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
00077    if (!m_Data.good())
00078    {
00079       OW_THROW(EnumerationException, "Failed to read signature of "
00080          "enumeration tempfile.");
00081    }
00082    if (tmpSig != TEMPFILE_ENUMERATION_SIG)
00083    {
00084       OW_THROW(EnumerationException, "Signature of enumeration tempfile is not valid.");
00085    }
00086 }
00087 TempFileEnumerationImplBase::~TempFileEnumerationImplBase()
00088 {
00089 }
00090 bool
00091 TempFileEnumerationImplBase::hasMoreElements() const
00092 {
00093    
00094    return m_size > 0;
00095 }
00096 size_t
00097 TempFileEnumerationImplBase::numberOfElements() const
00098 {
00099    return m_size;
00100 }
00101 void
00102 TempFileEnumerationImplBase::clear()
00103 {
00104    m_size = 0;
00105    m_Data.reset();
00106 }
00107 String
00108 TempFileEnumerationImplBase::releaseFile()
00109 {
00110    // Append the size onto the end of the stream so we can recover it
00111    // when constructing from the file
00112    m_Data.write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
00113    if (!m_Data.good())
00114    {
00115       OW_THROW(EnumerationException, "Failed to write size to "
00116          "enumeration tempfile.");
00117    }
00118    String rval = m_Data.releaseFile();
00119    clear();
00120    return rval;
00121 }
00122 
00123 bool
00124 TempFileEnumerationImplBase::usingTempFile() const
00125 {
00126    return m_Data.usingTempFile();
00127 }
00128 
00129 size_t 
00130 TempFileEnumerationImplBase::readSize(String const& filename)
00131 {
00132    size_t size;
00133    // open the file and read the size that is written to the end of it.
00134    File f = FileSystem::openFile(filename);
00135    if (!f)
00136    {
00137       OW_THROW(EnumerationException, "Failed to open file");
00138    }
00139    
00140    // Check that the correct signature is on the file
00141    UInt32 fileSig;
00142    if (f.read(reinterpret_cast<char*>(&fileSig), sizeof(fileSig)) != sizeof(fileSig))
00143    {
00144       OW_THROW(EnumerationException, "Failure to read enumeration "
00145          "signature");
00146    }
00147    if (fileSig != TEMPFILE_ENUMERATION_SIG)
00148    {
00149       OW_THROW(EnumerationException, "Attempted to construct an "
00150          "enumeration from a file that does not have the correct "
00151          "signature");
00152    }
00153    
00154    off_t whence = f.seek(-static_cast<off_t>(sizeof(size)), SEEK_END);
00155    if (whence == -1)
00156    {
00157       OW_THROW(EnumerationException, "Failure to seek");
00158    }
00159    if (f.read(reinterpret_cast<char*>(&size), sizeof(size), whence) != sizeof(size))
00160    {
00161       OW_THROW(EnumerationException, "Failure to read enumeration "
00162          "size");
00163    }
00164    if (f.close() == -1)
00165    {
00166       OW_THROW(EnumerationException, "Failure to close enumeration "
00167          "file");
00168    }
00169    return size;
00170 }
00171 
00172 void
00173 TempFileEnumerationImplBase::throwIfEmpty() const
00174 {
00175    if (!hasMoreElements())
00176    {
00177       OW_THROW (EnumerationException, "Attempt to Extract from empty Enum");
00178    }
00179 }
00180 
00181 
00182 } // end namespace OW_NAMESPACE
00183 
00184 
00185 

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