1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><meta http-equiv=
"Content-Type" content=
"text/html;charset=UTF-8">
3 <title>Barry: restore.cc Source File
</title>
4 <link href=
"doxygen.css" rel=
"stylesheet" type=
"text/css">
5 <link href=
"tabs.css" rel=
"stylesheet" type=
"text/css">
7 <!-- Generated by Doxygen 1.5.6 -->
8 <div class=
"navigation" id=
"top">
11 <li><a href=
"index.html"><span>Main
Page
</span></a></li>
12 <li><a href=
"modules.html"><span>Modules
</span></a></li>
13 <li><a href=
"namespaces.html"><span>Namespaces
</span></a></li>
14 <li><a href=
"classes.html"><span>Classes
</span></a></li>
15 <li class=
"current"><a href=
"files.html"><span>Files
</span></a></li>
16 <li><a href=
"dirs.html"><span>Directories
</span></a></li>
19 <div class=
"navpath"><a class=
"el" href=
"dir_edc0f428e3eefcfdce9c25befd901c57.html">src
</a>
22 <div class=
"contents">
23 <h1>restore.cc
</h1><a href=
"restore_8cc.html">Go to the documentation of this file.
</a><div class=
"fragment"><pre class=
"fragment"><a name=
"l00001"></a>00001 <span class=
"comment">///
</span>
24 <a name=
"l00002"></a>00002 <span class=
"comment"></span><span class=
"comment">/// \file restore.cc
</span>
25 <a name=
"l00003"></a>00003 <span class=
"comment"></span><span class=
"comment">/// Builder class for restoring from Barry Backup files
</span>
26 <a name=
"l00004"></a>00004 <span class=
"comment"></span><span class=
"comment">///
</span>
27 <a name=
"l00005"></a>00005 <span class=
"comment"></span>
28 <a name=
"l00006"></a>00006 <span class=
"comment">/*
</span>
29 <a name=
"l00007"></a>00007 <span class=
"comment"> Copyright (C)
2010-
2011, Net Direct Inc. (http://www.netdirect.ca/)
</span>
30 <a name=
"l00008"></a>00008 <span class=
"comment"></span>
31 <a name=
"l00009"></a>00009 <span class=
"comment"> This program is free software; you can redistribute it and/or modify
</span>
32 <a name=
"l00010"></a>00010 <span class=
"comment"> it under the terms of the GNU General Public License as published by
</span>
33 <a name=
"l00011"></a>00011 <span class=
"comment"> the Free Software Foundation; either version
2 of the License, or
</span>
34 <a name=
"l00012"></a>00012 <span class=
"comment"> (at your option) any later version.
</span>
35 <a name=
"l00013"></a>00013 <span class=
"comment"></span>
36 <a name=
"l00014"></a>00014 <span class=
"comment"> This program is distributed in the hope that it will be useful,
</span>
37 <a name=
"l00015"></a>00015 <span class=
"comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of
</span>
38 <a name=
"l00016"></a>00016 <span class=
"comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</span>
39 <a name=
"l00017"></a>00017 <span class=
"comment"></span>
40 <a name=
"l00018"></a>00018 <span class=
"comment"> See the GNU General Public License in the COPYING file at the
</span>
41 <a name=
"l00019"></a>00019 <span class=
"comment"> root directory of this project for more details.
</span>
42 <a name=
"l00020"></a>00020 <span class=
"comment">*/
</span>
43 <a name=
"l00021"></a>00021
44 <a name=
"l00022"></a>00022 <span class=
"preprocessor">#include
"<a class="code
" href="restore_8h.html
" title="Builder class for restoring from Barry Backup files.
">restore.h</a>"</span>
45 <a name=
"l00023"></a>00023 <span class=
"preprocessor">#include
"<a class="code
" href="tarfile_8h.html
" title="API for reading and writing sequentially from compressed tar files.
">tarfile.h</a>"</span>
46 <a name=
"l00024"></a>00024 <span class=
"preprocessor">#include
"<a class="code
" href="error_8h.html
" title="Common exception classes for the Barry library.
">error.h</a>"</span>
47 <a name=
"l00025"></a>00025 <span class=
"preprocessor">#include
<sstream
></span>
48 <a name=
"l00026"></a>00026 <span class=
"preprocessor">#include
<iomanip
></span>
49 <a name=
"l00027"></a>00027 <span class=
"preprocessor">#include
<iostream
></span>
50 <a name=
"l00028"></a>00028 <span class=
"preprocessor">#include
<string.h
></span>
51 <a name=
"l00029"></a>00029 <span class=
"preprocessor">#include
<algorithm
></span>
52 <a name=
"l00030"></a>00030
53 <a name=
"l00031"></a>00031 <span class=
"keyword">using namespace
</span>std;
54 <a name=
"l00032"></a>00032
55 <a name=
"l00033"></a>00033 <span class=
"keyword">namespace
</span>Barry {
56 <a name=
"l00034"></a>00034
57 <a name=
"l00035"></a>00035 <span class=
"keyword">namespace
</span>{
58 <a name=
"l00036"></a>00036
59 <a name=
"l00037"></a>00037 <span class=
"keywordtype">int
</span> CountFiles(reuse::TarFile
&tar,
60 <a name=
"l00038"></a>00038 <span class=
"keyword">const
</span> Barry::Restore::DBListType
&restoreList,
61 <a name=
"l00039"></a>00039 <span class=
"keywordtype">bool
</span> default_all_db)
62 <a name=
"l00040"></a>00040 {
63 <a name=
"l00041"></a>00041 <span class=
"keywordtype">int
</span> count =
0;
64 <a name=
"l00042"></a>00042 std::string name, last_name;
65 <a name=
"l00043"></a>00043 <span class=
"keywordtype">bool
</span> good =
<span class=
"keyword">false
</span>;
66 <a name=
"l00044"></a>00044
67 <a name=
"l00045"></a>00045 <span class=
"keywordflow">while
</span>( tar.ReadNextFilenameOnly(name) ) {
68 <a name=
"l00046"></a>00046 std::string::size_type pos = name.rfind(
<span class=
"charliteral">'/'
</span>);
69 <a name=
"l00047"></a>00047 <span class=
"keywordflow">if
</span>( pos == std::string::npos )
70 <a name=
"l00048"></a>00048 <span class=
"keywordflow">continue
</span>;
<span class=
"comment">// bad name
</span>
71 <a name=
"l00049"></a>00049 std::string dbname = name.substr(
0, pos);
72 <a name=
"l00050"></a>00050
73 <a name=
"l00051"></a>00051 <span class=
"keywordflow">if
</span>( dbname != last_name ) {
74 <a name=
"l00052"></a>00052 last_name = dbname;
75 <a name=
"l00053"></a>00053 good = (default_all_db
&& restoreList.size() ==
0) ||
76 <a name=
"l00054"></a>00054 restoreList.IsSelected(dbname);
77 <a name=
"l00055"></a>00055 }
78 <a name=
"l00056"></a>00056 <span class=
"keywordflow">if
</span>( good )
79 <a name=
"l00057"></a>00057 count++;
80 <a name=
"l00058"></a>00058 }
81 <a name=
"l00059"></a>00059 <span class=
"keywordflow">return
</span> count;
82 <a name=
"l00060"></a>00060 }
83 <a name=
"l00061"></a>00061
84 <a name=
"l00062"></a>00062 }
85 <a name=
"l00063"></a>00063 <span class=
"comment"></span>
86 <a name=
"l00064"></a>00064 <span class=
"comment">//////////////////////////////////////////////////////////////////////////////
</span>
87 <a name=
"l00065"></a>00065 <span class=
"comment"></span><span class=
"comment">// Static Restore members
</span>
88 <a name=
"l00066"></a>00066 <span class=
"comment"></span>
89 <a name=
"l00067"></a>00067 <span class=
"comment">/// Splits a tarpath of the form
"DBName/DBID" into separate string values.
</span>
90 <a name=
"l00068"></a>00068 <span class=
"comment">/// Returns true if successful, false if tarpath is a bad name.
</span>
91 <a name=
"l00069"></a><a class=
"code" href=
"classBarry_1_1Restore.html#eed51b73f7d5125ae655af0a965a8113">00069</a> <span class=
"comment"></span><span class=
"keywordtype">bool
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#eed51b73f7d5125ae655af0a965a8113" title=
"Splits a tarpath of the form &quot;DBName/DBID&quot; into separate string values...">Restore::SplitTarPath
</a>(
<span class=
"keyword">const
</span> std::string
&tarpath,
92 <a name=
"l00070"></a>00070 std::string
&dbname,
93 <a name=
"l00071"></a>00071 std::string
&dbid_text,
94 <a name=
"l00072"></a>00072 uint8_t
&dbrectype,
95 <a name=
"l00073"></a>00073 uint32_t
&dbid)
96 <a name=
"l00074"></a>00074 {
97 <a name=
"l00075"></a>00075 std::string::size_type pos = tarpath.rfind(
<span class=
"charliteral">'/'
</span>);
98 <a name=
"l00076"></a>00076 <span class=
"keywordflow">if
</span>( pos == std::string::npos )
99 <a name=
"l00077"></a>00077 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
<span class=
"comment">// bad name
</span>
100 <a name=
"l00078"></a>00078
101 <a name=
"l00079"></a>00079 dbname = tarpath.substr(
0, pos);
102 <a name=
"l00080"></a>00080 dbid_text = tarpath.substr(pos +
1);
103 <a name=
"l00081"></a>00081 <span class=
"keywordflow">if
</span>( dbname.size() ==
0 || dbid_text.size() ==
0 )
104 <a name=
"l00082"></a>00082 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
<span class=
"comment">// bad name
</span>
105 <a name=
"l00083"></a>00083
106 <a name=
"l00084"></a>00084 std::istringstream iss(dbid_text);
107 <a name=
"l00085"></a>00085 <span class=
"keywordtype">unsigned
</span> <span class=
"keywordtype">int
</span> temp;
108 <a name=
"l00086"></a>00086 iss
>> std::hex
>> dbid
>> temp;
109 <a name=
"l00087"></a>00087 dbrectype = (uint8_t) temp;
110 <a name=
"l00088"></a>00088
111 <a name=
"l00089"></a>00089 <span class=
"keywordflow">return
</span> <span class=
"keyword">true
</span>;
112 <a name=
"l00090"></a>00090 }
113 <a name=
"l00091"></a>00091
114 <a name=
"l00092"></a>00092 <span class=
"comment"></span>
115 <a name=
"l00093"></a>00093 <span class=
"comment">//////////////////////////////////////////////////////////////////////////////
</span>
116 <a name=
"l00094"></a>00094 <span class=
"comment"></span><span class=
"comment">// Restore - constructors
</span>
117 <a name=
"l00095"></a>00095
118 <a name=
"l00096"></a><a class=
"code" href=
"classBarry_1_1Restore.html#d23e005d6fadc20cde4c6f12e2bfa892">00096</a> Restore::Restore(
<span class=
"keyword">const
</span> std::string
&tarpath,
<span class=
"keywordtype">bool
</span> default_all_db)
119 <a name=
"l00097"></a>00097 : m_tarpath(tarpath)
120 <a name=
"l00098"></a>00098 , m_default_all_db(default_all_db)
121 <a name=
"l00099"></a>00099 , m_tar_record_state(RS_EMPTY)
122 <a name=
"l00100"></a>00100 , m_rec_type(
0)
123 <a name=
"l00101"></a>00101 , m_unique_id(
0)
124 <a name=
"l00102"></a>00102 {
125 <a name=
"l00103"></a>00103 <span class=
"keywordflow">try
</span> {
126 <a name=
"l00104"></a>00104 m_tar.reset(
<span class=
"keyword">new
</span> reuse::TarFile(tarpath.c_str(),
<span class=
"keyword">false
</span>,
127 <a name=
"l00105"></a>00105 &reuse::gztar_ops_nonthread,
<span class=
"keyword">true
</span>) );
128 <a name=
"l00106"></a>00106 }
129 <a name=
"l00107"></a>00107 <span class=
"keywordflow">catch
</span>( reuse::TarFile::TarError
&te ) {
130 <a name=
"l00108"></a>00108 <span class=
"keywordflow">throw
</span> <a class=
"code" href=
"classBarry_1_1RestoreError.html" title=
"Thrown by the Restore builder class when there is a problem with the low level file...">Barry::RestoreError
</a>(te.what());
131 <a name=
"l00109"></a>00109 }
132 <a name=
"l00110"></a>00110 }
133 <a name=
"l00111"></a>00111
134 <a name=
"l00112"></a>00112 Restore::~Restore()
135 <a name=
"l00113"></a>00113 {
136 <a name=
"l00114"></a>00114 }
137 <a name=
"l00115"></a>00115
138 <a name=
"l00116"></a>00116 <span class=
"comment"></span>
139 <a name=
"l00117"></a>00117 <span class=
"comment">//////////////////////////////////////////////////////////////////////////////
</span>
140 <a name=
"l00118"></a>00118 <span class=
"comment"></span><span class=
"comment">// Restore - Protected helpers
</span>
141 <a name=
"l00119"></a>00119
142 <a name=
"l00120"></a>00120 <span class=
"keywordtype">bool
</span> Restore::IsSelected(
<span class=
"keyword">const
</span> std::string
&dbName)
<span class=
"keyword"> const
</span>
143 <a name=
"l00121"></a>00121 <span class=
"keyword"></span>{
144 <a name=
"l00122"></a>00122 <span class=
"comment">// if nothing is in the list, use default
</span>
145 <a name=
"l00123"></a>00123 <span class=
"keywordflow">if
</span>( m_dbList.size() ==
0 )
146 <a name=
"l00124"></a>00124 <span class=
"keywordflow">return
</span> m_default_all_db;
147 <a name=
"l00125"></a>00125 <span class=
"keywordflow">else
</span>
148 <a name=
"l00126"></a>00126 <span class=
"keywordflow">return
</span> m_dbList.IsSelected(dbName);
149 <a name=
"l00127"></a>00127 }
150 <a name=
"l00128"></a>00128
151 <a name=
"l00129"></a>00129 <span class=
"comment"></span>
152 <a name=
"l00130"></a>00130 <span class=
"comment">//////////////////////////////////////////////////////////////////////////////
</span>
153 <a name=
"l00131"></a>00131 <span class=
"comment"></span><span class=
"comment">// Restore - Public API
</span>
154 <a name=
"l00132"></a>00132
155 <a name=
"l00133"></a><a class=
"code" href=
"classBarry_1_1Restore.html#e8d23b32bbbb26d4bd02961cd9b32262">00133</a> <span class=
"keywordtype">void
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#e8d23b32bbbb26d4bd02961cd9b32262" title=
"Add database name to the read filter.">Restore::AddDB
</a>(
<span class=
"keyword">const
</span> std::string
&dbName)
156 <a name=
"l00134"></a>00134 {
157 <a name=
"l00135"></a>00135 <span class=
"keywordflow">if
</span>( find(m_dbList.begin(), m_dbList.end(), dbName) == m_dbList.end() ) {
158 <a name=
"l00136"></a>00136 <span class=
"comment">// only add it if it is not already in the list
</span>
159 <a name=
"l00137"></a>00137 m_dbList.push_back(dbName);
160 <a name=
"l00138"></a>00138 }
161 <a name=
"l00139"></a>00139 }
162 <a name=
"l00140"></a>00140
163 <a name=
"l00141"></a><a class=
"code" href=
"classBarry_1_1Restore.html#210fba48d39754019bbbb893e404a849">00141</a> <span class=
"keywordtype">void
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#210fba48d39754019bbbb893e404a849" title=
"Add all database names in dblist to the read filter This function is additive.">Restore::Add
</a>(
<span class=
"keyword">const
</span> DBListType
&dbList)
164 <a name=
"l00142"></a>00142 {
165 <a name=
"l00143"></a>00143 <span class=
"keywordflow">for
</span>( DBListType::const_iterator i = dbList.begin();
166 <a name=
"l00144"></a>00144 i != dbList.end();
167 <a name=
"l00145"></a>00145 ++i )
168 <a name=
"l00146"></a>00146 {
169 <a name=
"l00147"></a>00147 <a class=
"code" href=
"classBarry_1_1Restore.html#e8d23b32bbbb26d4bd02961cd9b32262" title=
"Add database name to the read filter.">AddDB
</a>(*i);
170 <a name=
"l00148"></a>00148 }
171 <a name=
"l00149"></a>00149 }
172 <a name=
"l00150"></a>00150
173 <a name=
"l00151"></a>00151 <span class=
"keywordtype">void
</span> Restore::SkipCurrentDB()
174 <a name=
"l00152"></a>00152 {
175 <a name=
"l00153"></a>00153 <span class=
"comment">// skip all records until next DB
</span>
176 <a name=
"l00154"></a>00154 <span class=
"keywordflow">try
</span> {
177 <a name=
"l00155"></a>00155 <span class=
"keywordflow">while
</span>( Retrieve(m_record_data) == RS_NEXT ) {
178 <a name=
"l00156"></a>00156 std::cerr
<< <span class=
"stringliteral">"Skipping: "</span>
179 <a name=
"l00157"></a>00157 << m_current_dbname
<< <span class=
"stringliteral">"/"</span>
180 <a name=
"l00158"></a>00158 << m_tar_id_text
<< std::endl;
181 <a name=
"l00159"></a>00159 m_tar_record_state = RS_EMPTY;
182 <a name=
"l00160"></a>00160 }
183 <a name=
"l00161"></a>00161 }
184 <a name=
"l00162"></a>00162 <span class=
"keywordflow">catch
</span>( reuse::TarFile::TarError
& ) {
185 <a name=
"l00163"></a>00163 m_tar_record_state = RS_EOF;
186 <a name=
"l00164"></a>00164 }
187 <a name=
"l00165"></a>00165 }
188 <a name=
"l00166"></a>00166
189 <a name=
"l00167"></a><a class=
"code" href=
"classBarry_1_1Restore.html#97e837397dbfe9e027bd5745a42011d5">00167</a> <span class=
"keywordtype">unsigned
</span> <span class=
"keywordtype">int
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#97e837397dbfe9e027bd5745a42011d5" title=
"Loads the given file, and counts all records according to the current read filter...">Restore::GetRecordTotal
</a>()
<span class=
"keyword"> const
</span>
190 <a name=
"l00168"></a>00168 <span class=
"keyword"></span>{
191 <a name=
"l00169"></a>00169 <span class=
"keywordflow">return
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#97e837397dbfe9e027bd5745a42011d5" title=
"Loads the given file, and counts all records according to the current read filter...">GetRecordTotal
</a>(m_tarpath, m_dbList, m_default_all_db);
192 <a name=
"l00170"></a>00170 }
193 <a name=
"l00171"></a>00171
194 <a name=
"l00172"></a><a class=
"code" href=
"classBarry_1_1Restore.html#2c57a7bbe16c2c37ff38ccb6f3d42a39">00172</a> <span class=
"keywordtype">unsigned
</span> <span class=
"keywordtype">int
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#97e837397dbfe9e027bd5745a42011d5" title=
"Loads the given file, and counts all records according to the current read filter...">Restore::GetRecordTotal
</a>(
<span class=
"keyword">const
</span> std::string
&tarpath,
195 <a name=
"l00173"></a>00173 <span class=
"keyword">const
</span> DBListType
&dbList,
196 <a name=
"l00174"></a>00174 <span class=
"keywordtype">bool
</span> default_all_db)
197 <a name=
"l00175"></a>00175 {
198 <a name=
"l00176"></a>00176 <span class=
"keywordtype">unsigned
</span> <span class=
"keywordtype">int
</span> count =
0;
199 <a name=
"l00177"></a>00177
200 <a name=
"l00178"></a>00178 std::auto_ptr
<reuse::TarFile
> tar;
201 <a name=
"l00179"></a>00179
202 <a name=
"l00180"></a>00180 <span class=
"keywordflow">try
</span> {
203 <a name=
"l00181"></a>00181 <span class=
"comment">// do a scan through the tar file
</span>
204 <a name=
"l00182"></a>00182 tar.reset(
<span class=
"keyword">new
</span> reuse::TarFile(tarpath.c_str(),
<span class=
"keyword">false
</span>,
205 <a name=
"l00183"></a>00183 &reuse::gztar_ops_nonthread,
<span class=
"keyword">true
</span>) );
206 <a name=
"l00184"></a>00184 count = CountFiles(*tar, dbList, default_all_db);
207 <a name=
"l00185"></a>00185 }
208 <a name=
"l00186"></a>00186 <span class=
"keywordflow">catch
</span>( reuse::TarFile::TarError
&te ) {
209 <a name=
"l00187"></a>00187 <span class=
"keywordflow">throw
</span> <a class=
"code" href=
"classBarry_1_1RestoreError.html" title=
"Thrown by the Restore builder class when there is a problem with the low level file...">Barry::RestoreError
</a>(te.what());
210 <a name=
"l00188"></a>00188 }
211 <a name=
"l00189"></a>00189 <span class=
"keywordflow">return
</span> count;
212 <a name=
"l00190"></a>00190 }
213 <a name=
"l00191"></a>00191
214 <a name=
"l00192"></a><a class=
"code" href=
"classBarry_1_1Restore.html#ca46f9c51a68eb3872f98a73bdcd226c">00192</a> <span class=
"keywordtype">bool
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#ca46f9c51a68eb3872f98a73bdcd226c" title=
"If this function returns true, it fills data with the meta data that the next call...">Restore::GetNextMeta
</a>(
<a class=
"code" href=
"classBarry_1_1DBData.html" title=
"Database record data class.">DBData
</a> &data)
215 <a name=
"l00193"></a>00193 {
216 <a name=
"l00194"></a>00194 <span class=
"comment">// always use m_record_data here, so that we don't lose access
</span>
217 <a name=
"l00195"></a>00195 <span class=
"comment">// to the actual record data for future calls to BuildRecord()
</span>
218 <a name=
"l00196"></a>00196 <span class=
"comment">// and FetchRecord()
</span>
219 <a name=
"l00197"></a>00197 <span class=
"keywordflow">if
</span>( m_tar_record_state == RS_EMPTY ) {
220 <a name=
"l00198"></a>00198 Retrieve(m_record_data);
221 <a name=
"l00199"></a>00199 }
222 <a name=
"l00200"></a>00200
223 <a name=
"l00201"></a>00201 <span class=
"comment">// fill in the meta data that will be returned in the next call
</span>
224 <a name=
"l00202"></a>00202 <span class=
"comment">// to BuildRecord() or FetchRecord()... this is only valid if
</span>
225 <a name=
"l00203"></a>00203 <span class=
"comment">// the state is RS_NEXT
</span>
226 <a name=
"l00204"></a>00204 <span class=
"keywordflow">switch
</span>( m_tar_record_state )
227 <a name=
"l00205"></a>00205 {
228 <a name=
"l00206"></a>00206 <span class=
"keywordflow">case
</span> RS_NEXT:
229 <a name=
"l00207"></a>00207 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#d1a3e69cabea68e0a309459cd7c58e78">SetVersion
</a>(Barry::DBData::REC_VERSION_1);
230 <a name=
"l00208"></a>00208 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#55d6bde680fe0b48d07882ade96ef9c3">SetDBName
</a>(m_current_dbname);
231 <a name=
"l00209"></a>00209 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#a46147fced1b122cb6a0e6d1cee3cb07">SetIds
</a>(m_rec_type, m_unique_id);
232 <a name=
"l00210"></a>00210 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#cb569139ee2d44442d4709ccf2a4ab3c">SetOffset
</a>(
0);
233 <a name=
"l00211"></a>00211 <span class=
"keywordflow">return
</span> <span class=
"keyword">true
</span>;
234 <a name=
"l00212"></a>00212
235 <a name=
"l00213"></a>00213 <span class=
"keywordflow">default
</span>:
236 <a name=
"l00214"></a>00214 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
237 <a name=
"l00215"></a>00215 }
238 <a name=
"l00216"></a>00216 }
239 <a name=
"l00217"></a>00217
240 <a name=
"l00218"></a>00218 <span class=
"comment"></span>
241 <a name=
"l00219"></a>00219 <span class=
"comment">//////////////////////////////////////////////////////////////////////////////
</span>
242 <a name=
"l00220"></a>00220 <span class=
"comment"></span><span class=
"comment">// Barry::Builder overrides
</span>
243 <a name=
"l00221"></a>00221
244 <a name=
"l00222"></a>00222 Restore::RetrievalState Restore::Retrieve(Data
&record_data)
245 <a name=
"l00223"></a>00223 {
246 <a name=
"l00224"></a>00224 <span class=
"comment">// don't do anything unless we're empty
</span>
247 <a name=
"l00225"></a>00225 <span class=
"keywordflow">if
</span>( m_tar_record_state != RS_EMPTY )
248 <a name=
"l00226"></a>00226 <span class=
"keywordflow">return
</span> m_tar_record_state;
249 <a name=
"l00227"></a>00227
250 <a name=
"l00228"></a>00228 <span class=
"comment">// search for a valid record
</span>
251 <a name=
"l00229"></a>00229 <span class=
"keywordflow">for
</span>(;;) {
252 <a name=
"l00230"></a>00230 <span class=
"comment">// load record data from tar file
</span>
253 <a name=
"l00231"></a>00231 std::string filename;
254 <a name=
"l00232"></a>00232 <span class=
"keywordflow">if
</span>( !m_tar-
>ReadNextFile(filename, record_data) ) {
255 <a name=
"l00233"></a>00233 <span class=
"comment">// assume end of file
</span>
256 <a name=
"l00234"></a>00234 <span class=
"keywordflow">return
</span> m_tar_record_state = RS_EOF;
257 <a name=
"l00235"></a>00235 }
258 <a name=
"l00236"></a>00236 m_tar_record_state = RS_UNKNOWN;
259 <a name=
"l00237"></a>00237
260 <a name=
"l00238"></a>00238 <span class=
"comment">// split record filename into dbname and ID
</span>
261 <a name=
"l00239"></a>00239 std::string dbname;
262 <a name=
"l00240"></a>00240 <span class=
"keywordflow">if
</span>( !
<a class=
"code" href=
"classBarry_1_1Restore.html#eed51b73f7d5125ae655af0a965a8113" title=
"Splits a tarpath of the form &quot;DBName/DBID&quot; into separate string values...">SplitTarPath
</a>(filename, dbname, m_tar_id_text, m_rec_type, m_unique_id) ) {
263 <a name=
"l00241"></a>00241 <span class=
"comment">// invalid filename, skip it
</span>
264 <a name=
"l00242"></a>00242 std::cerr
<< <span class=
"stringliteral">"Skipping invalid tar record: "</span> << filename
<< std::endl;
265 <a name=
"l00243"></a>00243 <span class=
"keywordflow">continue
</span>;
266 <a name=
"l00244"></a>00244 }
267 <a name=
"l00245"></a>00245
268 <a name=
"l00246"></a>00246 <span class=
"comment">// are we working on the same dbname as last time?
</span>
269 <a name=
"l00247"></a>00247 <span class=
"comment">// if so, go ahead!
</span>
270 <a name=
"l00248"></a>00248 <span class=
"keywordflow">if
</span>( m_current_dbname == dbname ) {
271 <a name=
"l00249"></a>00249 <span class=
"keywordflow">return
</span> m_tar_record_state = RS_NEXT;
272 <a name=
"l00250"></a>00250 }
273 <a name=
"l00251"></a>00251
274 <a name=
"l00252"></a>00252 <span class=
"comment">// DIFFERENT DBNAME from here on down!
</span>
275 <a name=
"l00253"></a>00253 m_tar_record_state = RS_DBEND;
276 <a name=
"l00254"></a>00254
277 <a name=
"l00255"></a>00255 <span class=
"comment">// does the filter allow this record?
</span>
278 <a name=
"l00256"></a>00256 <span class=
"comment">// if not, skip it and continue looking
</span>
279 <a name=
"l00257"></a>00257 <span class=
"keywordflow">if
</span>( !IsSelected(dbname) ) {
280 <a name=
"l00258"></a>00258 <span class=
"keywordflow">continue
</span>;
281 <a name=
"l00259"></a>00259 }
282 <a name=
"l00260"></a>00260
283 <a name=
"l00261"></a>00261 <span class=
"comment">// all checks pass, load the new dbname, and return DBEND
</span>
284 <a name=
"l00262"></a>00262 <span class=
"comment">// if we are on a dbname boundary
</span>
285 <a name=
"l00263"></a>00263 <span class=
"keywordflow">if
</span>( m_current_dbname.size() ==
0 ) {
286 <a name=
"l00264"></a>00264 <span class=
"comment">// this is the first time through Retrieve, so ok
</span>
287 <a name=
"l00265"></a>00265 m_tar_record_state = RS_NEXT;
288 <a name=
"l00266"></a>00266 }
289 <a name=
"l00267"></a>00267
290 <a name=
"l00268"></a>00268 m_current_dbname = dbname;
291 <a name=
"l00269"></a>00269 <span class=
"keywordflow">return
</span> m_tar_record_state;
292 <a name=
"l00270"></a>00270 }
293 <a name=
"l00271"></a>00271 }
294 <a name=
"l00272"></a>00272
295 <a name=
"l00273"></a><a class=
"code" href=
"classBarry_1_1Restore.html#59f9e2d2e9dd3d7c40588416b27b5ee7">00273</a> <span class=
"keywordtype">bool
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#59f9e2d2e9dd3d7c40588416b27b5ee7" title=
"Called to build the record field data.">Restore::BuildRecord
</a>(
<a class=
"code" href=
"classBarry_1_1DBData.html" title=
"Database record data class.">Barry::DBData
</a> &data,
296 <a name=
"l00274"></a>00274 <span class=
"keywordtype">size_t
</span> &offset,
297 <a name=
"l00275"></a>00275 <span class=
"keyword">const
</span> <a class=
"code" href=
"classBarry_1_1IConverter.html" title=
"Main charset conversion class, primarily focused on converting between the Blackberry...">Barry::IConverter
</a> *ic)
298 <a name=
"l00276"></a>00276 {
299 <a name=
"l00277"></a>00277 <span class=
"comment">// in this case, we are loading into m_record_data anyway,
</span>
300 <a name=
"l00278"></a>00278 <span class=
"comment">// so no special handling is needed, like FetchRecord() needs.
</span>
301 <a name=
"l00279"></a>00279 <span class=
"keywordflow">switch
</span>( Retrieve(m_record_data) )
302 <a name=
"l00280"></a>00280 {
303 <a name=
"l00281"></a>00281 <span class=
"keywordflow">case
</span> RS_NEXT:
304 <a name=
"l00282"></a>00282 {
305 <a name=
"l00283"></a>00283 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#d1a3e69cabea68e0a309459cd7c58e78">SetVersion
</a>(Barry::DBData::REC_VERSION_1);
306 <a name=
"l00284"></a>00284 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#55d6bde680fe0b48d07882ade96ef9c3">SetDBName
</a>(m_current_dbname);
307 <a name=
"l00285"></a>00285 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#a46147fced1b122cb6a0e6d1cee3cb07">SetIds
</a>(m_rec_type, m_unique_id);
308 <a name=
"l00286"></a>00286 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#cb569139ee2d44442d4709ccf2a4ab3c">SetOffset
</a>(offset);
309 <a name=
"l00287"></a>00287
310 <a name=
"l00288"></a>00288 <span class=
"keywordtype">int
</span> packet_size = offset + m_record_data.GetSize();
311 <a name=
"l00289"></a>00289 <span class=
"keywordtype">unsigned
</span> <span class=
"keywordtype">char
</span> *buf = data.
<a class=
"code" href=
"classBarry_1_1DBData.html#3c1c4ad5ce85cef0ea85002a0fe07881">UseData
</a>().GetBuffer(packet_size);
312 <a name=
"l00290"></a>00290 memcpy(buf + offset, m_record_data.GetData(), m_record_data.GetSize());
313 <a name=
"l00291"></a>00291 offset += m_record_data.GetSize();
314 <a name=
"l00292"></a>00292 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#3c1c4ad5ce85cef0ea85002a0fe07881">UseData
</a>().ReleaseBuffer(packet_size);
315 <a name=
"l00293"></a>00293
316 <a name=
"l00294"></a>00294 <span class=
"comment">// clear loaded flag, as it has now been used
</span>
317 <a name=
"l00295"></a>00295 m_tar_record_state = RS_EMPTY;
318 <a name=
"l00296"></a>00296 <span class=
"keywordflow">return
</span> <span class=
"keyword">true
</span>;
319 <a name=
"l00297"></a>00297 }
320 <a name=
"l00298"></a>00298
321 <a name=
"l00299"></a>00299 <span class=
"keywordflow">case
</span> RS_EMPTY:
322 <a name=
"l00300"></a>00300 <span class=
"keywordflow">case
</span> RS_UNKNOWN:
323 <a name=
"l00301"></a>00301 <span class=
"keywordflow">default
</span>:
324 <a name=
"l00302"></a>00302 <span class=
"keywordflow">throw
</span> std::logic_error(
<span class=
"stringliteral">"Invalid state in Restore::BuildRecord()"</span>);
325 <a name=
"l00303"></a>00303
326 <a name=
"l00304"></a>00304 <span class=
"keywordflow">case
</span> RS_DBEND:
327 <a name=
"l00305"></a>00305 <span class=
"comment">// process the end of database by returning false
</span>
328 <a name=
"l00306"></a>00306 <span class=
"comment">// the next round will be valid, so set to RS_NEXT
</span>
329 <a name=
"l00307"></a>00307 m_tar_record_state = RS_NEXT;
330 <a name=
"l00308"></a>00308 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
331 <a name=
"l00309"></a>00309
332 <a name=
"l00310"></a>00310 <span class=
"keywordflow">case
</span> RS_EOF:
333 <a name=
"l00311"></a>00311 <span class=
"comment">// always return false at end of file
</span>
334 <a name=
"l00312"></a>00312 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
335 <a name=
"l00313"></a>00313 }
336 <a name=
"l00314"></a>00314 }
337 <a name=
"l00315"></a>00315
338 <a name=
"l00316"></a><a class=
"code" href=
"classBarry_1_1Restore.html#fc3e4162a0ae1c19d338126d920abb57">00316</a> <span class=
"keywordtype">bool
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#fc3e4162a0ae1c19d338126d920abb57" title=
"Same as BuildRecord, but does not care about any offsets.">Restore::FetchRecord
</a>(
<a class=
"code" href=
"classBarry_1_1DBData.html" title=
"Database record data class.">Barry::DBData
</a> &data,
<span class=
"keyword">const
</span> <a class=
"code" href=
"classBarry_1_1IConverter.html" title=
"Main charset conversion class, primarily focused on converting between the Blackberry...">Barry::IConverter
</a> *ic)
339 <a name=
"l00317"></a>00317 {
340 <a name=
"l00318"></a>00318 <span class=
"comment">// if the record has not yet been loaded, we can optimize
</span>
341 <a name=
"l00319"></a>00319 <span class=
"comment">// the buffer, and pass in our own... otherwise, just copy
</span>
342 <a name=
"l00320"></a>00320 <span class=
"comment">// the current buffer from m_record_data
</span>
343 <a name=
"l00321"></a>00321 <span class=
"comment">//
</span>
344 <a name=
"l00322"></a>00322 <span class=
"comment">// it is assumed here that Builder users will not alternate
</span>
345 <a name=
"l00323"></a>00323 <span class=
"comment">// between calls to BuildRecord() and FetchRecord()
</span>
346 <a name=
"l00324"></a>00324 <span class=
"comment">//
</span>
347 <a name=
"l00325"></a>00325 <span class=
"keywordflow">if
</span>( m_tar_record_state == RS_EMPTY ) {
348 <a name=
"l00326"></a>00326 <span class=
"comment">// BUT, if RS_DBEND is the next value, then we need
</span>
349 <a name=
"l00327"></a>00327 <span class=
"comment">// to save the data for the next round... this
</span>
350 <a name=
"l00328"></a>00328 <span class=
"comment">// optimization is almost more bother than it's worth :-)
</span>
351 <a name=
"l00329"></a>00329 <span class=
"keywordflow">if
</span>( Retrieve(data.
<a class=
"code" href=
"classBarry_1_1DBData.html#3c1c4ad5ce85cef0ea85002a0fe07881">UseData
</a>()) == RS_DBEND ) {
352 <a name=
"l00330"></a>00330 m_record_data = data.
<a class=
"code" href=
"classBarry_1_1DBData.html#a7cacc9655376447ffbf3aa4d664a03b">GetData
</a>();
353 <a name=
"l00331"></a>00331 m_tar_record_state = RS_NEXT;
354 <a name=
"l00332"></a>00332 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
355 <a name=
"l00333"></a>00333 }
356 <a name=
"l00334"></a>00334 }
357 <a name=
"l00335"></a>00335 <span class=
"keywordflow">else
</span> {
358 <a name=
"l00336"></a>00336 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#3c1c4ad5ce85cef0ea85002a0fe07881">UseData
</a>() = m_record_data;
359 <a name=
"l00337"></a>00337 }
360 <a name=
"l00338"></a>00338
361 <a name=
"l00339"></a>00339 <span class=
"keywordflow">switch
</span>( m_tar_record_state )
362 <a name=
"l00340"></a>00340 {
363 <a name=
"l00341"></a>00341 <span class=
"keywordflow">case
</span> RS_NEXT:
364 <a name=
"l00342"></a>00342 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#d1a3e69cabea68e0a309459cd7c58e78">SetVersion
</a>(Barry::DBData::REC_VERSION_1);
365 <a name=
"l00343"></a>00343 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#55d6bde680fe0b48d07882ade96ef9c3">SetDBName
</a>(m_current_dbname);
366 <a name=
"l00344"></a>00344 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#a46147fced1b122cb6a0e6d1cee3cb07">SetIds
</a>(m_rec_type, m_unique_id);
367 <a name=
"l00345"></a>00345 data.
<a class=
"code" href=
"classBarry_1_1DBData.html#cb569139ee2d44442d4709ccf2a4ab3c">SetOffset
</a>(
0);
368 <a name=
"l00346"></a>00346
369 <a name=
"l00347"></a>00347 <span class=
"comment">// clear loaded flag, as it has now been used
</span>
370 <a name=
"l00348"></a>00348 m_tar_record_state = RS_EMPTY;
371 <a name=
"l00349"></a>00349 <span class=
"keywordflow">return
</span> <span class=
"keyword">true
</span>;
372 <a name=
"l00350"></a>00350
373 <a name=
"l00351"></a>00351 <span class=
"keywordflow">case
</span> RS_EMPTY:
374 <a name=
"l00352"></a>00352 <span class=
"keywordflow">case
</span> RS_UNKNOWN:
375 <a name=
"l00353"></a>00353 <span class=
"keywordflow">default
</span>:
376 <a name=
"l00354"></a>00354 <span class=
"keywordflow">throw
</span> std::logic_error(
<span class=
"stringliteral">"Invalid state in Restore::FetchRecord()"</span>);
377 <a name=
"l00355"></a>00355
378 <a name=
"l00356"></a>00356 <span class=
"keywordflow">case
</span> RS_DBEND:
379 <a name=
"l00357"></a>00357 <span class=
"comment">// process the end of database by returning false
</span>
380 <a name=
"l00358"></a>00358 <span class=
"comment">// the next round will be valid, so set to RS_NEXT
</span>
381 <a name=
"l00359"></a>00359 m_tar_record_state = RS_NEXT;
382 <a name=
"l00360"></a>00360 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
383 <a name=
"l00361"></a>00361
384 <a name=
"l00362"></a>00362 <span class=
"keywordflow">case
</span> RS_EOF:
385 <a name=
"l00363"></a>00363 <span class=
"comment">// always return false at end of file
</span>
386 <a name=
"l00364"></a>00364 <span class=
"keywordflow">return
</span> <span class=
"keyword">false
</span>;
387 <a name=
"l00365"></a>00365 }
388 <a name=
"l00366"></a>00366 }
389 <a name=
"l00367"></a>00367
390 <a name=
"l00368"></a><a class=
"code" href=
"classBarry_1_1Restore.html#4d8f64fce3b905a756185e1a9ca0c09e">00368</a> <span class=
"keywordtype">bool
</span> <a class=
"code" href=
"classBarry_1_1Restore.html#4d8f64fce3b905a756185e1a9ca0c09e" title=
"Sometimes a builder can have multiple databases stored in it, so when Build/Fetch...">Restore::EndOfFile
</a>()
<span class=
"keyword"> const
</span>
391 <a name=
"l00369"></a>00369 <span class=
"keyword"></span>{
392 <a name=
"l00370"></a>00370 <span class=
"keywordflow">return
</span> m_tar_record_state == RS_EOF;
393 <a name=
"l00371"></a>00371 }
394 <a name=
"l00372"></a>00372
395 <a name=
"l00373"></a>00373 }
<span class=
"comment">// namespace Barry
</span>
396 <a name=
"l00374"></a>00374
398 <hr size=
"1"><address style=
"text-align: right;"><small>Generated on Thu Feb
10 16:
13:
59 2011 for Barry by
399 <a href=
"http://www.doxygen.org/index.html">
400 <img src=
"doxygen.png" alt=
"doxygen" align=
"middle" border=
"0"></a> 1.5.6 </small></address>