1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dp_misc.h"
32*cdf0e10cSrcweir #include "dp_ucb.h"
33*cdf0e10cSrcweir #include "dp_persmap.h"
34*cdf0e10cSrcweir #include "rtl/strbuf.hxx"
35*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
36*cdf0e10cSrcweir #include "osl/file.hxx"
37*cdf0e10cSrcweir #include "osl/thread.h"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star;
41*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42*cdf0e10cSrcweir using namespace ::rtl;
43*cdf0e10cSrcweir using ::osl::File;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace dp_misc
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir //______________________________________________________________________________
49*cdf0e10cSrcweir void PersistentMap::throw_rtexc( int err, char const * pmsg ) const
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     OUStringBuffer buf;
52*cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("[") );
53*cdf0e10cSrcweir     buf.append( m_sysPath );
54*cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("] Berkeley Db error (") );
55*cdf0e10cSrcweir     buf.append( static_cast<sal_Int32>(err) );
56*cdf0e10cSrcweir     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("): ") );
57*cdf0e10cSrcweir     if (pmsg == 0)
58*cdf0e10cSrcweir         pmsg = DbEnv::strerror(err);
59*cdf0e10cSrcweir     const OString msg(pmsg);
60*cdf0e10cSrcweir     buf.append( OUString( msg.getStr(), msg.getLength(),
61*cdf0e10cSrcweir                           osl_getThreadTextEncoding() ) );
62*cdf0e10cSrcweir     const OUString msg_(buf.makeStringAndClear());
63*cdf0e10cSrcweir     OSL_ENSURE( 0, rtl::OUStringToOString(
64*cdf0e10cSrcweir                     msg_, RTL_TEXTENCODING_UTF8 ).getStr() );
65*cdf0e10cSrcweir     throw RuntimeException( msg_, Reference<XInterface>() );
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir //______________________________________________________________________________
69*cdf0e10cSrcweir PersistentMap::~PersistentMap()
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir     try {
72*cdf0e10cSrcweir         m_db.close(0);
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir     catch (DbException & exc) {
75*cdf0e10cSrcweir         (void) exc; // avoid warnings
76*cdf0e10cSrcweir         OSL_ENSURE( 0, DbEnv::strerror( exc.get_errno() ) );
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //______________________________________________________________________________
81*cdf0e10cSrcweir PersistentMap::PersistentMap( OUString const & url_, bool readOnly )
82*cdf0e10cSrcweir     : m_db( 0, 0 )
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir     try {
85*cdf0e10cSrcweir         OUString url( expandUnoRcUrl(url_) );
86*cdf0e10cSrcweir         if ( File::getSystemPathFromFileURL( url, m_sysPath ) != File::E_None )
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             OSL_ASSERT( false );
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir         OString cstr_sysPath(
91*cdf0e10cSrcweir             OUStringToOString( m_sysPath, RTL_TEXTENCODING_UTF8 ) );
92*cdf0e10cSrcweir         char const * pcstr_sysPath = cstr_sysPath.getStr();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         u_int32_t flags = DB_CREATE;
95*cdf0e10cSrcweir         if (readOnly) {
96*cdf0e10cSrcweir             flags = DB_RDONLY;
97*cdf0e10cSrcweir             if (! create_ucb_content(
98*cdf0e10cSrcweir                     0, url,
99*cdf0e10cSrcweir                     Reference<com::sun::star::ucb::XCommandEnvironment>(),
100*cdf0e10cSrcweir                     false /* no throw */ )) {
101*cdf0e10cSrcweir                 // ignore non-existent file in read-only mode: simulate empty db
102*cdf0e10cSrcweir                 pcstr_sysPath = 0;
103*cdf0e10cSrcweir                 flags = DB_CREATE;
104*cdf0e10cSrcweir             }
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         int err = m_db.open(
108*cdf0e10cSrcweir             // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
109*cdf0e10cSrcweir             0, pcstr_sysPath, 0, DB_HASH, flags/* | DB_THREAD*/, 0664 /* fs mode */ );
110*cdf0e10cSrcweir         if (err != 0)
111*cdf0e10cSrcweir             throw_rtexc(err);
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir     catch (DbException & exc) {
114*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir //______________________________________________________________________________
119*cdf0e10cSrcweir PersistentMap::PersistentMap()
120*cdf0e10cSrcweir     : m_db( 0, 0 )
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     try {
123*cdf0e10cSrcweir         // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
124*cdf0e10cSrcweir         int err = m_db.open( 0, 0, 0, DB_HASH, DB_CREATE/* | DB_THREAD*/, 0 );
125*cdf0e10cSrcweir         if (err != 0)
126*cdf0e10cSrcweir             throw_rtexc(err);
127*cdf0e10cSrcweir     }
128*cdf0e10cSrcweir     catch (DbException & exc) {
129*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir //______________________________________________________________________________
134*cdf0e10cSrcweir bool PersistentMap::has( OString const & key ) const
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     return get( 0, key );
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir //______________________________________________________________________________
140*cdf0e10cSrcweir bool PersistentMap::get( OString * value, OString const & key ) const
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir     try {
143*cdf0e10cSrcweir         Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
144*cdf0e10cSrcweir         Dbt dbData;
145*cdf0e10cSrcweir         int err = m_db.get( 0, &dbKey, &dbData, 0 );
146*cdf0e10cSrcweir         if (err == DB_NOTFOUND)
147*cdf0e10cSrcweir             return false;
148*cdf0e10cSrcweir         if (err == 0) {
149*cdf0e10cSrcweir             if (value != 0) {
150*cdf0e10cSrcweir                 *value = OString(
151*cdf0e10cSrcweir                     static_cast< sal_Char const * >(dbData.get_data()),
152*cdf0e10cSrcweir                     dbData.get_size() );
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir             return true;
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir         throw_rtexc(err);
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir     catch (DbException & exc) {
159*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir     return false; // avoiding warning
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir //______________________________________________________________________________
165*cdf0e10cSrcweir void PersistentMap::put( OString const & key, OString const & value )
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     try {
168*cdf0e10cSrcweir         Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
169*cdf0e10cSrcweir         Dbt dbData( const_cast< sal_Char * >(
170*cdf0e10cSrcweir                         value.getStr()), value.getLength() );
171*cdf0e10cSrcweir         int err = m_db.put( 0, &dbKey, &dbData, 0 );
172*cdf0e10cSrcweir         if (err == 0) {
173*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
174*cdf0e10cSrcweir             OString v;
175*cdf0e10cSrcweir             OSL_ASSERT( get( &v, key ) );
176*cdf0e10cSrcweir             OSL_ASSERT( v.equals( value ) );
177*cdf0e10cSrcweir #endif
178*cdf0e10cSrcweir             err = m_db.sync(0);
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir         if (err != 0)
181*cdf0e10cSrcweir             throw_rtexc(err);
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir     catch (DbException & exc) {
184*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir //______________________________________________________________________________
189*cdf0e10cSrcweir bool PersistentMap::erase( OString const & key, bool flush_immediately )
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir     try {
192*cdf0e10cSrcweir         Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
193*cdf0e10cSrcweir         int err = m_db.del( &dbKey, 0 );
194*cdf0e10cSrcweir         if (err == 0) {
195*cdf0e10cSrcweir             if (flush_immediately) {
196*cdf0e10cSrcweir                 err = m_db.sync(0);
197*cdf0e10cSrcweir                 if (err != 0)
198*cdf0e10cSrcweir                     throw_rtexc(err);
199*cdf0e10cSrcweir             }
200*cdf0e10cSrcweir             return true;
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir         if (err == DB_NOTFOUND)
203*cdf0e10cSrcweir             return false;
204*cdf0e10cSrcweir         throw_rtexc(err);
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir     catch (DbException & exc) {
207*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir     return false; // avoiding warning
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir //______________________________________________________________________________
213*cdf0e10cSrcweir t_string2string_map PersistentMap::getEntries() const
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     try {
216*cdf0e10cSrcweir         Dbc * pcurs = 0;
217*cdf0e10cSrcweir         int err = m_db.cursor( 0, &pcurs, 0 );
218*cdf0e10cSrcweir         if (err != 0)
219*cdf0e10cSrcweir             throw_rtexc(err);
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         t_string2string_map ret;
222*cdf0e10cSrcweir         for (;;) {
223*cdf0e10cSrcweir             Dbt dbKey, dbData;
224*cdf0e10cSrcweir             err = pcurs->get( &dbKey, &dbData, DB_NEXT );
225*cdf0e10cSrcweir             if (err == DB_NOTFOUND)
226*cdf0e10cSrcweir                 break;
227*cdf0e10cSrcweir             if (err != 0)
228*cdf0e10cSrcweir                 throw_rtexc(err);
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir             ::std::pair<t_string2string_map::iterator, bool > insertion(
231*cdf0e10cSrcweir                 ret.insert( t_string2string_map::value_type(
232*cdf0e10cSrcweir                                 t_string2string_map::value_type(
233*cdf0e10cSrcweir                                     OString( static_cast< sal_Char const * >(
234*cdf0e10cSrcweir                                                  dbKey.get_data()),
235*cdf0e10cSrcweir                                              dbKey.get_size() ),
236*cdf0e10cSrcweir                                     OString( static_cast< sal_Char const * >(
237*cdf0e10cSrcweir                                                  dbData.get_data()),
238*cdf0e10cSrcweir                                              dbData.get_size() ) ) ) ) );
239*cdf0e10cSrcweir             OSL_ASSERT( insertion.second );
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir         err = pcurs->close();
242*cdf0e10cSrcweir         if (err != 0)
243*cdf0e10cSrcweir             throw_rtexc(err);
244*cdf0e10cSrcweir         return ret;
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir     catch (DbException & exc) {
247*cdf0e10cSrcweir         throw_rtexc( exc.get_errno(), exc.what() );
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir     return t_string2string_map(); // avoiding warning
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254