store.cxx 10.5 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Michael Meeks's avatar
Michael Meeks committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
19

20
#include <store/store.h>
21

Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
22 23
#include <sal/types.h>
#include <rtl/string.hxx>
24
#include <rtl/ref.hxx>
25 26 27 28 29 30 31 32

#include "object.hxx"
#include "lockbyte.hxx"

#include "storbase.hxx"
#include "storpage.hxx"
#include "stordir.hxx"
#include "storlckb.hxx"
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
33

34
using rtl::Reference;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
35

36 37 38 39 40 41 42 43
namespace store
{
/** Template helper class as type safe Reference to store_handle_type.
 */
template<class store_handle_type>
class OStoreHandle : public rtl::Reference<store_handle_type>
{
public:
44
    explicit OStoreHandle (store_handle_type * pHandle)
45 46 47 48 49 50
        : rtl::Reference<store_handle_type> (pHandle)
    {}

    static store_handle_type * SAL_CALL query (void * pHandle)
    {
        return store::query (
Noel Grandin's avatar
Noel Grandin committed
51
            static_cast<OStoreObject*>(pHandle),
52 53 54 55 56
            static_cast<store_handle_type*>(0));
    }
};
}

57
using namespace store;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
58 59 60 61 62 63 64 65 66

/*========================================================================
 *
 * storeHandle implementation.
 *
 *======================================================================*/
/*
 * store_acquireHandle.
 */
67
storeError store_acquireHandle (
68 69
    storeHandle Handle
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
70
{
Noel Grandin's avatar
Noel Grandin committed
71
    OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
72
    if (!pHandle)
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
73 74
        return store_E_InvalidHandle;

75
    pHandle->acquire();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
76 77 78 79 80 81
    return store_E_None;
}

/*
 * store_releaseHandle.
 */
82
storeError store_releaseHandle (
83 84
    storeHandle Handle
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
85
{
Noel Grandin's avatar
Noel Grandin committed
86
    OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
87
    if (!pHandle)
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
88 89
        return store_E_InvalidHandle;

90
    pHandle->release();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
91 92 93 94 95 96 97 98 99 100 101
    return store_E_None;
}

/*========================================================================
 *
 * storeFileHandle implementation.
 *
 *======================================================================*/
/*
 * store_createMemoryFile.
 */
102
storeError store_createMemoryFile (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
103
    sal_uInt16       nPageSize,
104 105
    storeFileHandle *phFile
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
106 107 108
{
    if (!phFile)
        return store_E_InvalidParameter;
109
    *phFile = nullptr;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
110

111 112 113 114 115
    Reference<ILockBytes> xLockBytes;

    storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
    if (eErrCode != store_E_None)
        return eErrCode;
116
    OSL_ASSERT(xLockBytes.is());
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
117

118 119
    Reference<OStorePageManager> xManager (new OStorePageManager());
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
120 121
        return store_E_OutOfMemory;

122
    eErrCode = xManager->initialize (
123
        &*xLockBytes, storeAccessMode::Create, nPageSize);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
124 125 126 127 128
    if (eErrCode != store_E_None)
        return eErrCode;

    xManager->acquire();

129
    *phFile = xManager.get();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
130 131 132 133 134 135
    return store_E_None;
}

/*
 * store_openFile.
 */
136
storeError store_openFile (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
137 138 139
    rtl_uString     *pFilename,
    storeAccessMode  eAccessMode,
    sal_uInt16       nPageSize,
140 141
    storeFileHandle *phFile
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
142 143
{
    if (phFile)
144
        *phFile = nullptr;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
145 146 147 148

    if (!(pFilename && phFile))
        return store_E_InvalidParameter;

149
    Reference<ILockBytes> xLockBytes;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
150

151
    storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
152 153
    if (eErrCode != store_E_None)
        return eErrCode;
154
    OSL_ASSERT(xLockBytes.is());
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
155

156 157
    Reference<OStorePageManager> xManager (new OStorePageManager());
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
158 159
        return store_E_OutOfMemory;

160
    eErrCode = xManager->initialize (
161
        &*xLockBytes, eAccessMode, nPageSize);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
162 163 164 165 166
    if (eErrCode != store_E_None)
        return eErrCode;

    xManager->acquire();

167
    *phFile = xManager.get();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
168 169 170 171 172 173
    return store_E_None;
}

/*
 * store_closeFile.
 */
174
storeError store_closeFile (
175 176
    storeFileHandle Handle
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
177
{
178 179 180
    OStorePageManager *pManager =
        OStoreHandle<OStorePageManager>::query (Handle);
    if (!pManager)
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
181 182
        return store_E_InvalidHandle;

183
    storeError eErrCode = pManager->close();
184
    pManager->release();
185
    return eErrCode;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
186 187 188 189 190
}

/*
 * store_flushFile.
 */
191
storeError store_flushFile (
192 193
    storeFileHandle Handle
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
194
{
195 196 197
    OStoreHandle<OStorePageManager> xManager (
        OStoreHandle<OStorePageManager>::query (Handle));
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
198 199 200 201 202 203 204 205 206 207 208 209 210
        return store_E_InvalidHandle;

    return xManager->flush();
}

/*========================================================================
 *
 * storeDirectoryHandle implementation.
 *
 *======================================================================*/
/*
 * store_openDirectory.
 */
211
storeError store_openDirectory (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
212
    storeFileHandle       hFile,
213 214
    rtl_uString const    *pPath,
    rtl_uString const    *pName,
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
215
    storeAccessMode       eAccessMode,
216 217
    storeDirectoryHandle *phDirectory
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
218 219 220
{
    storeError eErrCode = store_E_None;
    if (phDirectory)
221
        *phDirectory = nullptr;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
222

223 224 225
    OStoreHandle<OStorePageManager> xManager (
        OStoreHandle<OStorePageManager>::query (hFile));
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
226 227 228 229 230
        return store_E_InvalidHandle;

    if (!(pPath && pName && phDirectory))
        return store_E_InvalidParameter;

231
    Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
232
    if (!xDirectory.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
233 234
        return store_E_OutOfMemory;

235 236 237 238
    OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
    OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);

    eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
239 240 241 242 243
    if (eErrCode != store_E_None)
        return eErrCode;

    xDirectory->acquire();

244
    *phDirectory = xDirectory.get();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
245 246 247 248 249 250
    return store_E_None;
}

/*
 * store_findFirst.
 */
251
storeError store_findFirst (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
252
    storeDirectoryHandle  Handle,
253 254
    storeFindData        *pFindData
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
255
{
256 257
    OStoreHandle<OStoreDirectory_Impl> xDirectory (
        OStoreHandle<OStoreDirectory_Impl>::query (Handle));
258
    if (!xDirectory.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
259 260 261 262 263 264
        return store_E_InvalidHandle;

    if (!pFindData)
        return store_E_InvalidParameter;

    // Initialize FindData.
265
    memset (pFindData, 0, sizeof (storeFindData));
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
266 267

    // Find first.
268
    pFindData->m_nReserved = sal_uInt32(~0);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
269 270 271 272 273 274
    return xDirectory->iterate (*pFindData);
}

/*
 * store_findNext.
 */
275
storeError store_findNext (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
276
    storeDirectoryHandle  Handle,
277 278
    storeFindData        *pFindData
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
279
{
280 281
    OStoreHandle<OStoreDirectory_Impl> xDirectory (
        OStoreHandle<OStoreDirectory_Impl>::query (Handle));
282
    if (!xDirectory.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        return store_E_InvalidHandle;

    if (!pFindData)
        return store_E_InvalidParameter;

    // Check FindData.
    if (!pFindData->m_nReserved)
        return store_E_NoMoreFiles;

    // Find next.
    pFindData->m_nReserved -= 1;
    return xDirectory->iterate (*pFindData);
}

/*========================================================================
 *
 * storeStreamHandle implementation.
 *
 *======================================================================*/
/*
 * store_openStream
 */
305
storeError store_openStream (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
306
    storeFileHandle    hFile,
307 308
    rtl_uString const *pPath,
    rtl_uString const *pName,
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
309
    storeAccessMode    eAccessMode,
310 311
    storeStreamHandle *phStream
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
312 313 314
{
    storeError eErrCode = store_E_None;
    if (phStream)
315
        *phStream = nullptr;
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
316

317 318 319
    OStoreHandle<OStorePageManager> xManager (
        OStoreHandle<OStorePageManager>::query (hFile));
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
320 321 322 323 324
        return store_E_InvalidHandle;

    if (!(pPath && pName && phStream))
        return store_E_InvalidParameter;

325 326
    Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
    if (!xLockBytes.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
327 328
        return store_E_OutOfMemory;

329 330 331 332
    OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
    OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);

    eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
333 334 335 336 337
    if (eErrCode != store_E_None)
        return eErrCode;

    xLockBytes->acquire();

338
    *phStream = xLockBytes.get();
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
339 340 341 342 343 344
    return store_E_None;
}

/*
 * store_readStream.
 */
345
storeError store_readStream (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
346 347 348 349
    storeStreamHandle  Handle,
    sal_uInt32         nOffset,
    void              *pBuffer,
    sal_uInt32         nBytes,
350 351
    sal_uInt32        *pnDone
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
352
{
353 354 355
    OStoreHandle<OStoreLockBytes> xLockBytes (
        OStoreHandle<OStoreLockBytes>::query (Handle));
    if (!xLockBytes.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
356 357 358 359 360 361 362 363 364 365 366
        return store_E_InvalidHandle;

    if (!(pBuffer && pnDone))
        return store_E_InvalidParameter;

    return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
}

/*
 * store_writeStream.
 */
367
storeError store_writeStream (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
368 369 370 371
    storeStreamHandle  Handle,
    sal_uInt32         nOffset,
    const void        *pBuffer,
    sal_uInt32         nBytes,
372 373
    sal_uInt32        *pnDone
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
374
{
375 376 377
    OStoreHandle<OStoreLockBytes> xLockBytes (
        OStoreHandle<OStoreLockBytes>::query (Handle));
    if (!xLockBytes.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
378 379 380 381 382 383 384 385 386 387 388
        return store_E_InvalidHandle;

    if (!(pBuffer && pnDone))
        return store_E_InvalidParameter;

    return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
}

/*
 * store_remove.
 */
389
storeError store_remove (
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
390
    storeFileHandle Handle,
391 392
    rtl_uString const *pPath,
    rtl_uString const *pName
393
) SAL_THROW_EXTERN_C()
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
394 395 396
{
    storeError eErrCode = store_E_None;

397 398 399
    OStoreHandle<OStorePageManager> xManager (
        OStoreHandle<OStorePageManager>::query (Handle));
    if (!xManager.is())
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
400 401 402 403 404 405 406 407 408 409
        return store_E_InvalidHandle;

    if (!(pPath && pName))
        return store_E_InvalidParameter;

    // Setup page key.
    OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
    OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
    OStorePageKey aKey;

410
    eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
Jens-Heiner Rechtien's avatar
Jens-Heiner Rechtien committed
411 412 413 414 415 416
    if (eErrCode != store_E_None)
        return eErrCode;

    // Remove.
    return xManager->remove (aKey);
}
417 418

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */