blocxx
TempFileEnumerationImplBase.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
38 #include "blocxx/BLOCXX_config.h"
41 #include "blocxx/File.hpp"
42 #include "blocxx/FileSystem.hpp"
43 
44 namespace BLOCXX_NAMESPACE
45 {
46 
47 namespace
48 {
49 const UInt32 TEMPFILE_ENUMERATION_SIG = 0x4f57454e; // "OWEN"
50 }
51 
53  : m_size(0), m_Data()
54 {
55  UInt32 enumSig = TEMPFILE_ENUMERATION_SIG;
56  m_Data.write(reinterpret_cast<const char*>(&enumSig), sizeof(enumSig));
57  if (!m_Data.good())
58  {
59  BLOCXX_THROW(EnumerationException, "Failed to write signature to "
60  "enumeration tempfile.");
61  }
62  // now we have to read the sig so that the temp file stream is
63  // positioned correctly
64  UInt32 tmpSig;
65  m_Data.read(reinterpret_cast<char*>(&tmpSig), sizeof(tmpSig));
66  if (!m_Data.good())
67  {
68  BLOCXX_THROW(EnumerationException, "Failed to read signature from "
69  "enumeration tempfile.");
70  }
71 }
72 
74 {
75 }
76 bool
78 {
79 
80  return m_size > 0;
81 }
82 size_t
84 {
85  return m_size;
86 }
87 void
89 {
90  m_size = 0;
91  m_Data.reset();
92 }
93 bool
95 {
96  return m_Data.usingTempFile();
97 }
98 
99 size_t
101 {
102  size_t size;
103  // open the file and read the size that is written to the end of it.
104  File f = FileSystem::openFile(filename);
105  if (!f)
106  {
107  BLOCXX_THROW(EnumerationException, "Failed to open file");
108  }
109 
110  // Check that the correct signature is on the file
111  UInt32 fileSig;
112  if (f.read(reinterpret_cast<char*>(&fileSig), sizeof(fileSig)) != sizeof(fileSig))
113  {
114  BLOCXX_THROW(EnumerationException, "Failure to read enumeration "
115  "signature");
116  }
117  if (fileSig != TEMPFILE_ENUMERATION_SIG)
118  {
119  BLOCXX_THROW(EnumerationException, "Attempted to construct an "
120  "enumeration from a file that does not have the correct "
121  "signature");
122  }
123 
124  off_t whence = f.seek(-static_cast<off_t>(sizeof(size)), SEEK_END);
125  if (whence == -1)
126  {
127  BLOCXX_THROW(EnumerationException, "Failure to seek");
128  }
129  if (f.read(reinterpret_cast<char*>(&size), sizeof(size), whence) != sizeof(size))
130  {
131  BLOCXX_THROW(EnumerationException, "Failure to read enumeration "
132  "size");
133  }
134  if (f.close() == -1)
135  {
136  BLOCXX_THROW(EnumerationException, "Failure to close enumeration "
137  "file");
138  }
139  return size;
140 }
141 
142 void
144 {
145  if (!hasMoreElements())
146  {
147  BLOCXX_THROW (EnumerationException, "Attempt to Extract from empty Enum");
148  }
149 }
150 
151 
152 } // end namespace BLOCXX_NAMESPACE
153 
154 
155 
Taken from RFC 1321.
void reset()
reset puts the underlying stream object back into its initialized state.
This String class is an abstract data type that represents as NULL terminated string of characters...
Definition: String.hpp:66
Int64 seek(Int64 offset, int whence) const
Seek to a given offset within the file.
Definition: File.hpp:141
int close()
Close the underlying file object.
Definition: File.hpp:171
The purpose of the File class is to provide an abstraction layer over the platform dependant function...
Definition: File.hpp:54
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
Definition: Exception.hpp:263
size_t read(void *bfr, size_t numberOfBytes, Int64 offset=-1L) const
Read from the underlying file.
Definition: File.hpp:113
BLOCXX_COMMON_API File openFile(const String &path)
Open a file for read/write and return an File object that can be used for reading and writing...