LCOV - code coverage report
Current view: top level - desktop/source/deployment - dp_persmap.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 81 154 52.6 %
Date: 2015-06-13 12:38:46 Functions: 9 13 69.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "dp_misc.h"
      21             : #include "dp_persmap.h"
      22             : #include <rtl/strbuf.hxx>
      23             : 
      24             : using namespace ::rtl;
      25             : 
      26             : // the persistent map is used to manage a handful of key-value string pairs
      27             : // this implementation replaces a rather heavy-weight berkeleydb integration
      28             : 
      29             : // the file backing up a persistent map consists of line pairs with
      30             : // - a key string   (encoded with chars 0x00..0x0F being escaped)
      31             : // - a value string (encoded with chars 0x00..0x0F being escaped)
      32             : 
      33             : namespace dp_misc
      34             : {
      35             : 
      36             : static const char PmapMagic[4] = {'P','m','p','1'};
      37             : 
      38         339 : PersistentMap::PersistentMap( OUString const & url_, bool readOnly )
      39             : :    m_MapFile( expandUnoRcUrl(url_) )
      40             : ,    m_bReadOnly( readOnly )
      41             : ,    m_bIsOpen( false )
      42         339 : ,    m_bToBeCreated( !readOnly )
      43         678 : ,    m_bIsDirty( false )
      44             : {
      45         339 :     open();
      46         339 : }
      47             : 
      48           0 : PersistentMap::PersistentMap()
      49             : :    m_MapFile( OUString() )
      50             : ,    m_bReadOnly( false )
      51             : ,    m_bIsOpen( false )
      52             : ,    m_bToBeCreated( false )
      53           0 : ,    m_bIsDirty( false )
      54           0 : {}
      55             : 
      56         678 : PersistentMap::~PersistentMap()
      57             : {
      58         339 :     if( m_bIsDirty )
      59           0 :         flush();
      60         339 :     if( m_bIsOpen )
      61           2 :         m_MapFile.close();
      62         339 : }
      63             : 
      64             : 
      65             : // replace 0x00..0x0F with "%0".."%F"
      66             : // replace "%" with "%%"
      67           6 : static OString encodeString( const OString& rStr)
      68             : {
      69           6 :     const sal_Char* pChar = rStr.getStr();
      70           6 :     const sal_Int32 nLen = rStr.getLength();
      71           6 :     sal_Int32 i = nLen;
      72             :     // short circuit for the simple non-encoded case
      73         366 :     while( --i >= 0)
      74             :     {
      75         354 :         const unsigned char c = (unsigned char) *(pChar++);
      76         354 :         if( c <= 0x0F )
      77           0 :             break;
      78         354 :         if( c == '%')
      79           0 :             break;
      80             :     }
      81           6 :     if( i < 0)
      82           6 :         return rStr;
      83             : 
      84             :     // escape chars 0x00..0x0F with "%0".."%F"
      85           0 :     OStringBuffer aEncStr( nLen + 32);
      86           0 :     aEncStr.append( pChar - (nLen-i), nLen - i);
      87           0 :     while( --i >= 0)
      88             :     {
      89           0 :         unsigned char c = (unsigned char) *(pChar++);
      90           0 :         if( c <= 0x0F )
      91             :         {
      92           0 :             aEncStr.append( '%');
      93           0 :             c += (c <= 0x09) ? '0' : 'A'-10;
      94           0 :         } else if( c == '%')
      95           0 :             aEncStr.append( '%');
      96           0 :         aEncStr.append( c);
      97             :     }
      98             : 
      99           0 :     return aEncStr.makeStringAndClear();
     100             : }
     101             : 
     102             : // replace "%0".."%F" with 0x00..0x0F
     103             : // replace "%%" with "%"
     104           0 : static OString decodeString( const sal_Char* pEncChars, int nLen)
     105             : {
     106           0 :     const char* pChar = pEncChars;
     107           0 :     sal_Int32 i = nLen;
     108             :     // short circuit for the simple non-encoded case
     109           0 :     while( --i >= 0)
     110           0 :         if( *(pChar++) == '%')
     111           0 :             break;
     112           0 :     if( i < 0)
     113           0 :         return OString( pEncChars, nLen);
     114             : 
     115             :     // replace escaped chars with their decoded counterparts
     116           0 :     OStringBuffer aDecStr( nLen);
     117           0 :     pChar = pEncChars;
     118           0 :     for( i = nLen; --i >= 0;)
     119             :     {
     120           0 :         sal_Char c = *(pChar++);
     121             :         // handle escaped character
     122           0 :         if( c == '%')
     123             :         {
     124           0 :             --i;
     125             :             OSL_ASSERT( i >= 0);
     126           0 :             c = *(pChar++);
     127           0 :             if( ('0' <= c) && (c <= '9'))
     128           0 :                 c -= '0';
     129             :             else
     130             :             {
     131             :                 OSL_ASSERT( ('A' <= c) && (c <= 'F'));
     132           0 :                 c -= ('A'-10);
     133             :             }
     134             :         }
     135           0 :         aDecStr.append( c);
     136             :     }
     137             : 
     138           0 :     return aDecStr.makeStringAndClear();
     139             : }
     140             : 
     141         339 : bool PersistentMap::open()
     142             : {
     143             :     // open the existing file
     144         339 :     sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read;
     145         339 :     if( !m_bReadOnly)
     146         339 :         nOpenFlags |= osl_File_OpenFlag_Write;
     147             : 
     148         339 :     const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
     149         339 :     m_bIsOpen = (rcOpen == osl::File::E_None);
     150             : 
     151             :     // or create later if needed
     152         339 :     m_bToBeCreated &= (rcOpen == osl::File::E_NOENT) && !m_bIsOpen;
     153             : 
     154         339 :     if( !m_bIsOpen)
     155         339 :         return m_bToBeCreated;
     156             : 
     157           0 :     return readAll();
     158             : }
     159             : 
     160             : 
     161           0 : bool PersistentMap::readAll()
     162             : {
     163             :     // prepare for re-reading the map-file
     164           0 :     const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
     165             :     (void)nRes;
     166           0 :     m_entries.clear();
     167             : 
     168             :     // read header and check magic
     169             :     char aHeaderBytes[ sizeof(PmapMagic)];
     170           0 :     sal_uInt64 nBytesRead = 0;
     171           0 :     m_MapFile.read( aHeaderBytes, sizeof(aHeaderBytes), nBytesRead);
     172             :     OSL_ASSERT( nBytesRead == sizeof(aHeaderBytes));
     173           0 :     if( nBytesRead != sizeof(aHeaderBytes))
     174           0 :         return false;
     175             :     // check header magic
     176           0 :     for( int i = 0; i < (int)sizeof(PmapMagic); ++i)
     177           0 :         if( aHeaderBytes[i] != PmapMagic[i])
     178           0 :             return false;
     179             : 
     180             :     // read key value pairs and add them to the map
     181           0 :     ByteSequence aKeyLine;
     182           0 :     ByteSequence aValLine;
     183             :     for(;;)
     184             :     {
     185             :         // read key-value line pair
     186             :         // an empty key name indicates the end of the line pairs
     187           0 :         if( m_MapFile.readLine( aKeyLine) != osl::File::E_None)
     188           0 :             return false;
     189           0 :         if( !aKeyLine.getLength())
     190           0 :             break;
     191           0 :         if( m_MapFile.readLine( aValLine) != osl::File::E_None)
     192           0 :             return false;
     193             :         // decode key and value strings
     194           0 :         const OString aKeyName = decodeString( reinterpret_cast<char const *>(aKeyLine.getConstArray()), aKeyLine.getLength());
     195           0 :         const OString aValName = decodeString( reinterpret_cast<char const *>(aValLine.getConstArray()), aValLine.getLength());
     196             :         // insert key-value pair into map
     197           0 :         add( aKeyName, aValName );
     198             :         // check end-of-file status
     199           0 :         sal_Bool bIsEOF = true;
     200           0 :         if( m_MapFile.isEndOfFile( &bIsEOF) != osl::File::E_None )
     201           0 :             return false;
     202           0 :         if( bIsEOF )
     203           0 :             break;
     204           0 :     }
     205             : 
     206           0 :     m_bIsDirty = false;
     207           0 :     return true;
     208             : }
     209             : 
     210           6 : void PersistentMap::flush()
     211             : {
     212           6 :     if( !m_bIsDirty)
     213           0 :         return;
     214             :     OSL_ASSERT( !m_bReadOnly);
     215           6 :     if( m_bToBeCreated && !m_entries.empty())
     216             :     {
     217           2 :         const sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
     218           2 :         const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
     219           2 :         m_bIsOpen = (rcOpen == osl::File::E_None);
     220           2 :         m_bToBeCreated = !m_bIsOpen;
     221             :     }
     222           6 :     if( !m_bIsOpen)
     223           0 :         return;
     224             : 
     225             :     // write header magic
     226           6 :     const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
     227             :     (void)nRes;
     228           6 :     sal_uInt64 nBytesWritten = 0;
     229           6 :     m_MapFile.write( PmapMagic, sizeof(PmapMagic), nBytesWritten);
     230             : 
     231             :     // write key value pairs
     232           6 :     t_string2string_map::const_iterator it = m_entries.begin();
     233           9 :     for(; it != m_entries.end(); ++it) {
     234             :         // write line for key
     235           3 :         const OString aKeyString = encodeString( (*it).first);
     236           3 :         const sal_Int32 nKeyLen = aKeyString.getLength();
     237           3 :         m_MapFile.write( aKeyString.getStr(), nKeyLen, nBytesWritten);
     238             :         OSL_ASSERT( nKeyLen == (sal_Int32)nBytesWritten);
     239           3 :         m_MapFile.write( "\n", 1, nBytesWritten);
     240             :         // write line for value
     241           6 :         const OString& rValString = encodeString( (*it).second);
     242           3 :         const sal_Int32 nValLen = rValString.getLength();
     243           3 :         m_MapFile.write( rValString.getStr(), nValLen, nBytesWritten);
     244             :         OSL_ASSERT( nValLen == (sal_Int32)nBytesWritten);
     245           3 :         m_MapFile.write( "\n", 1, nBytesWritten);
     246           3 :     }
     247             : 
     248             :     // write a file delimiter (an empty key-string)
     249           6 :     m_MapFile.write( "\n", 1, nBytesWritten);
     250             :     // truncate file here
     251             :     sal_uInt64 nNewFileSize;
     252           6 :     if( m_MapFile.getPos( nNewFileSize) == osl::File::E_None)
     253           6 :         m_MapFile.setSize( nNewFileSize);
     254             :     // flush to disk
     255           6 :     m_MapFile.sync();
     256             :     // the in-memory map now matches to the file on disk
     257           6 :     m_bIsDirty = false;
     258             : }
     259             : 
     260           0 : bool PersistentMap::has( OString const & key ) const
     261             : {
     262           0 :     return get( NULL, key );
     263             : }
     264             : 
     265          35 : bool PersistentMap::get( OString * value, OString const & key ) const
     266             : {
     267          35 :     t_string2string_map::const_iterator it = m_entries.find( key);
     268          35 :     if( it == m_entries.end())
     269          28 :         return false;
     270           7 :     if( value)
     271           7 :         *value = it->second;
     272           7 :     return true;
     273             : }
     274             : 
     275           3 : void PersistentMap::add( OString const & key, OString const & value )
     276             : {
     277           3 :     if( m_bReadOnly)
     278           3 :         return;
     279             :     typedef std::pair<t_string2string_map::iterator,bool> InsertRC;
     280           3 :     InsertRC r = m_entries.insert( t_string2string_map::value_type(key,value));
     281           3 :     m_bIsDirty = r.second;
     282             : }
     283             : 
     284             : 
     285           3 : void PersistentMap::put( OString const & key, OString const & value )
     286             : {
     287           3 :     add( key, value);
     288             :     // HACK: flush now as the extension manager does not seem
     289             :     //       to properly destruct this object in some situations
     290           3 :     if(m_bIsDirty)
     291           3 :         flush();
     292           3 : }
     293             : 
     294           3 : bool PersistentMap::erase( OString const & key, bool flush_immediately )
     295             : {
     296           3 :     if( m_bReadOnly)
     297           0 :         return false;
     298           3 :     size_t nCount = m_entries.erase( key);
     299           3 :     if( !nCount)
     300           0 :         return false;
     301           3 :     m_bIsDirty = true;
     302           3 :     if( flush_immediately)
     303           3 :         flush();
     304           3 :     return true;
     305             : }
     306             : 
     307             : }
     308             : 
     309             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11