summaryrefslogtreecommitdiff
path: root/store/source/filelckb.cxx
blob: 11be829121943ea3ecd6a8a53653112a082f2681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: filelckb.cxx,v $
 * $Revision: 1.17 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_store.hxx"

#include <store/filelckb.hxx>

#ifndef INCLUDED_STDDEF_H
#include <stddef.h>
#define INCLUDED_STDDEF_H
#endif

#ifndef INCLUDED_STRING_H
#include <string.h>
#define INCLUDED_STRING_H
#endif
#include <sal/types.h>
#include <sal/macros.h>
#include <rtl/alloc.h>
#include <rtl/ustring.hxx>
#include <osl/file.h>
#include <osl/thread.h>
#include <osl/mutex.hxx>
#include <store/types.h>
#include <store/object.hxx>
#include <store/lockbyte.hxx>

using namespace store;

/*========================================================================
 *
 * OFileLockBytes internals.
 *
 *======================================================================*/

/*
 * __store_memcpy.
 */
inline void __store_memcpy (void * dst, const void * src, sal_uInt32 n)
{
    ::memcpy (dst, src, n);
}

/*
 * __store_errcode_mapping.
 */
struct __store_errcode_mapping_st
{
    sal_uInt32 m_nErrno;
    storeError m_nCode;
};

static storeError __store_errnoToErrCode (sal_uInt32 nErrno);
#define ERROR_FROM_NATIVE(e) __store_errnoToErrCode((sal_uInt32)(e))

/*
 * __store_file (basic file I/O; platform specific; inline).
 */
#define store_File_OpenRead     0x01L
#define store_File_OpenWrite    0x02L
#define store_File_OpenNoCreate 0x04L
#define store_File_OpenTruncate 0x08L

#if defined(SAL_OS2)
#include <fileos2.hxx>
#elif defined(SAL_UNX)
#include <fileunx.hxx>
#elif defined(SAL_W32)
#include <filew32.hxx>
#else  /* !(OS2 | UNX | W32) */
#include <filestd.hxx>
#endif /* !(OS2 | UNX | W32) */

/*
 * __store_errnoToErrCode.
 */
static storeError __store_errnoToErrCode (sal_uInt32 nErrno)
{
    int i, n = sizeof(__store_errcode_map) / sizeof(__store_errcode_map[0]);
    for (i = 0; i < n; i++)
    {
        if (__store_errcode_map[i].m_nErrno == nErrno)
            return __store_errcode_map[i].m_nCode;
    }
    return store_E_Unknown;
}

/*========================================================================
 *
 * FileMapping_Impl.
 *
 *======================================================================*/
namespace // unnamed
{

struct FileMapping_Impl
{
    /** Representation.
     */
    sal_uInt32 m_nAlignment;
    sal_uInt32 m_nSize;
    HSTORE     m_hMap;

    /** Construction.
     */
    FileMapping_Impl (void);
    ~FileMapping_Impl (void);

    /** Create readonly file mapping.
     */
    storeError create (HSTORE hFile);
};

/*
 * FileMapping_Impl.
 */
inline FileMapping_Impl::FileMapping_Impl (void)
    : m_nAlignment (__store_malign()), m_nSize (0), m_hMap (0)
{
}

/*
 * ~FileMapping_Impl.
 */
inline FileMapping_Impl::~FileMapping_Impl (void)
{
    if (m_hMap != 0)
        __store_funmap (m_hMap);
}

/*
 * create.
 */
inline storeError FileMapping_Impl::create (HSTORE hFile)
{
    if (m_nAlignment == (sal_uInt32)(-1))
        return store_E_Unknown; // E_Unsupported

    if ((m_hMap = __store_fmap (hFile)) == 0)
        return ERROR_FROM_NATIVE(__store_errno());

    return __store_fsize (hFile, m_nSize);
}

} // unnamed namespace

/*========================================================================
 *
 * MemoryMapping_Impl.
 *
 *======================================================================*/
namespace // unnamed
{

struct MemoryMapping_Impl
{
    /** Representation.
     */
    sal_uInt32  m_nOffset;
    sal_uInt32  m_nSize;
    sal_uInt8  *m_pData;

    /** Construction.
     */
    MemoryMapping_Impl (void);
    ~MemoryMapping_Impl (void);

    /** Check for a valid memory mapping.
     */
    bool isValid (void) const { return (m_pData != 0); }

    /** Create a readonly memory mapping.
     */
    storeError create (
        const FileMapping_Impl & fmap,
        sal_uInt32               nOffset,
        sal_uInt32               nSize);

    /** Cleanup (unmap) memory mapping.
     */
    void cleanup (void);
};

/*
 * MemoryMapping_Impl.
 */
inline MemoryMapping_Impl::MemoryMapping_Impl (void)
    : m_nOffset (0), m_nSize (0), m_pData (0)
{
}

/*
 * ~MemoryMapping_Impl.
 */
inline MemoryMapping_Impl::~MemoryMapping_Impl (void)
{
    if (m_pData != 0)
    {
        __store_munmap (m_pData, m_nSize);
        m_pData = 0, m_nSize = 0;
    }
}

/*
 * create.
 */
inline storeError MemoryMapping_Impl::create (
    const FileMapping_Impl & fmap, sal_uInt32 nOffset, sal_uInt32 nSize)
{
    storeError result = store_E_None;
    sal_uInt32 nAlign = (nOffset % fmap.m_nAlignment);

    nOffset -= nAlign;
    nSize   += nAlign;

    if ((nOffset + nSize) > fmap.m_nSize)
        nSize = fmap.m_nSize - nOffset;

    m_pData = __store_mmap (fmap.m_hMap, nOffset, nSize);
    if (m_pData == 0)
        result = ERROR_FROM_NATIVE(__store_errno());
    else
        m_nOffset = nOffset, m_nSize = nSize;
    return (result);
}

/*
 * cleanup.
 */
inline void MemoryMapping_Impl::cleanup (void)
{
    if (m_pData != 0)
    {
        __store_munmap (m_pData, m_nSize);
        m_pData = 0, m_nSize = 0;
    }
}

} // unnamed namespace

/*========================================================================
 *
 * OFileLockBytes_Impl interface.
 *
 *======================================================================*/
namespace store
{

class OFileLockBytes_Impl
{
    /** Representation.
     */
    HSTORE              m_hFile;
    MemoryMapping_Impl  m_aMemmap;
    bool                m_bWriteable;

public:
    /** Allocation.
     */
    static void * operator new (size_t n) SAL_THROW(())
    {
        return rtl_allocateMemory (sal_uInt32(n));
    }
    static void operator delete (void * p, size_t) SAL_THROW(())
    {
        rtl_freeMemory (p);
    }

    /** Construction.
     */
    OFileLockBytes_Impl (void);
    ~OFileLockBytes_Impl (void);

    /** Check for a valid file handle.
     */
    bool isValid (void) const { return (m_hFile != 0); }

    /** Operation.
     */
    storeError close (void);

    storeError create (
        const sal_Char *pszFilename,
        storeAccessMode eAccessMode);
    storeError create (
        rtl_uString    *pFilename,
        storeAccessMode eAccessMode);

    storeError resize (sal_uInt32 nSize);

    storeError readAt (
        sal_uInt32  nOffset,
        void       *pBuffer,
        sal_uInt32  nBytes,
        sal_uInt32 &rnDone);
    storeError writeAt (
        sal_uInt32  nOffset,
        const void *pBuffer,
        sal_uInt32  nBytes,
        sal_uInt32 &rnDone);

    storeError sync (void);
    storeError stat (sal_uInt32 &rnSize);
};

} // namespace store

/*========================================================================
 *
 * OFileLockBytes_Impl (inline) implementation.
 *
 *======================================================================*/
/*
 * OFileLockBytes_Impl.
 */
inline OFileLockBytes_Impl::OFileLockBytes_Impl (void)
    : m_hFile       (0),
      m_bWriteable  (false)
{
}

/*
 * ~OFileLockBytes_Impl.
 */
inline OFileLockBytes_Impl::~OFileLockBytes_Impl (void)
{
    m_aMemmap.cleanup();
    if (m_hFile)
    {
        (void)__store_fclose (m_hFile);
        m_hFile = 0;
    }
}

/*
 * close.
 */
inline storeError OFileLockBytes_Impl::close (void)
{
    m_aMemmap.cleanup();
    if (m_hFile)
    {
        (void)__store_fclose (m_hFile);
        m_hFile = 0;
    }
    return store_E_None;
}

/*
 * create(sal_Char*).
 */
inline storeError OFileLockBytes_Impl::create (
    const sal_Char *pszFilename, storeAccessMode eAccessMode)
{
    m_aMemmap.cleanup();
    if (m_hFile)
    {
        (void)__store_fclose (m_hFile);
        m_hFile = 0;
    }

    m_bWriteable = (!(eAccessMode == store_AccessReadOnly));

    sal_uInt32 nMode = store_File_OpenRead;
    if (m_bWriteable)
        nMode |= store_File_OpenWrite;
    else
        nMode |= store_File_OpenNoCreate;

    if (eAccessMode == store_AccessCreate)
        nMode |= store_File_OpenTruncate;
    if (eAccessMode == store_AccessReadWrite)
        nMode |= store_File_OpenNoCreate;

    storeError eErrCode = __store_fopen (pszFilename, nMode, m_hFile);
    if (eErrCode == store_E_None)
    {
        if (!m_bWriteable)
        {
            // Readonly, try Memory mapped I/O, ignore errors.
            FileMapping_Impl fmap;
            if (fmap.create (m_hFile) == store_E_None)
            {
                // Try to map the entire file into memory.
                m_aMemmap.create (fmap, 0, fmap.m_nSize);
            }
        }
    }
    return eErrCode;
}

/*
 * create(rtl_uString*).
 */
inline storeError OFileLockBytes_Impl::create (
    rtl_uString *pFilename, storeAccessMode eAccessMode)
{
    // Path conversion result.
    oslFileError result;

    // Convert into system path.
    rtl::OUString aSystemPath;
    result = osl_getSystemPathFromFileURL (pFilename, &(aSystemPath.pData));
    if (!(result == osl_File_E_None))
    {
        // Not FileUrl. Maybe a system path, already.
        result = osl_getFileURLFromSystemPath (
            pFilename, &(aSystemPath.pData));
        if (!(result == osl_File_E_None))
        {
            // Invalid path.
            return store_E_InvalidParameter;
        }

        rtl_uString_assign (&(aSystemPath.pData), pFilename);
    }

    // Convert into system text encoding.
    rtl::OString aFilename (
        aSystemPath.pData->buffer,
        aSystemPath.pData->length,
        osl_getThreadTextEncoding());

    // Open file.
    return create (aFilename.pData->buffer, eAccessMode);
}

/*
 * readAt.
 */
inline storeError OFileLockBytes_Impl::readAt (
    sal_uInt32  nOffset,
    void       *pBuffer,
    sal_uInt32  nBytes,
    sal_uInt32 &rnDone)
{
    if (m_aMemmap.isValid())
    {
        // Memory mapped I/O.
        if (!(nOffset < m_aMemmap.m_nSize))
            return store_E_None;

        nBytes = SAL_MIN(nOffset + nBytes, m_aMemmap.m_nSize) - nOffset;
        if (!(nBytes > 0))
            return store_E_None;

        __store_memcpy (pBuffer, m_aMemmap.m_pData + nOffset, nBytes);
        rnDone = nBytes;

        return store_E_None;
    }
    else
    {
        // File I/O.
        return __store_fread (m_hFile, nOffset, pBuffer, nBytes, rnDone);
    }
}

/*
 * writeAt.
 */
inline storeError OFileLockBytes_Impl::writeAt (
    sal_uInt32  nOffset,
    const void *pBuffer,
    sal_uInt32  nBytes,
    sal_uInt32 &rnDone)
{
    if (m_bWriteable)
        return __store_fwrite (m_hFile, nOffset, pBuffer, nBytes, rnDone);
    else
        return store_E_AccessViolation;
}

/*
 * resize.
 */
inline storeError OFileLockBytes_Impl::resize (sal_uInt32 nSize)
{
    if (m_bWriteable)
        return __store_ftrunc (m_hFile, nSize);
    else
        return store_E_AccessViolation;
}

/*
 * sync.
 */
inline storeError OFileLockBytes_Impl::sync (void)
{
    if (m_bWriteable)
        return __store_fsync (m_hFile);
    else
        return store_E_None;
}

/*
 * stat.
 */
inline storeError OFileLockBytes_Impl::stat (sal_uInt32 &rnSize)
{
    if (m_aMemmap.isValid())
    {
        // Memory mapped I/O.
        rnSize = m_aMemmap.m_nSize;
        return store_E_None;
    }
    return __store_fsize (m_hFile, rnSize);
}

/*========================================================================
 *
 * OFileLockBytes implementation.
 *
 *======================================================================*/
/*
 * OFileLockBytes.
 */
OFileLockBytes::OFileLockBytes (void)
    : m_pImpl (new OFileLockBytes_Impl())
{
}

/*
 * ~OFileLockBytes.
 */
OFileLockBytes::~OFileLockBytes (void)
{
    delete m_pImpl;
}

/*
 * acquire.
 */
oslInterlockedCount SAL_CALL OFileLockBytes::acquire (void)
{
    return OStoreObject::acquire();
}

/*
 * release.
 */
oslInterlockedCount SAL_CALL OFileLockBytes::release (void)
{
    return OStoreObject::release();
}

/*
 * create.
 */
storeError OFileLockBytes::create (
    rtl_uString     *pFilename,
    storeAccessMode  eAccessMode)
{
    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);

    if (pFilename)
        return m_pImpl->create (pFilename, eAccessMode);
    else
        return store_E_InvalidParameter;
}

/*
 * readAt.
 */
storeError OFileLockBytes::readAt (
    sal_uInt32  nOffset,
    void       *pBuffer,
    sal_uInt32  nBytes,
    sal_uInt32 &rnDone)
{
    // Initialize [out] param.
    rnDone = 0;

    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return m_pImpl->readAt (nOffset, pBuffer, nBytes, rnDone);
    else
        return store_E_InvalidHandle;
}

/*
 * writeAt.
 */
storeError OFileLockBytes::writeAt (
    sal_uInt32  nOffset,
    const void *pBuffer,
    sal_uInt32  nBytes,
    sal_uInt32 &rnDone)
{
    // Initialize [out] param.
    rnDone = 0;

    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return m_pImpl->writeAt (nOffset, pBuffer, nBytes, rnDone);
    else
        return store_E_InvalidHandle;
}

/*
 * flush.
 */
storeError OFileLockBytes::flush (void)
{
    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return m_pImpl->sync();
    else
        return store_E_InvalidHandle;
}

/*
 * setSize.
 */
storeError OFileLockBytes::setSize (sal_uInt32 nSize)
{
    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return m_pImpl->resize (nSize);
    else
        return store_E_InvalidHandle;
}

/*
 * stat.
 */
storeError OFileLockBytes::stat (sal_uInt32 &rnSize)
{
    // Initialize [out] param.
    rnSize = 0;

    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return m_pImpl->stat (rnSize);
    else
        return store_E_InvalidHandle;
}

/*
 * lockRange.
 */
storeError OFileLockBytes::lockRange (
    sal_uInt32 /* nOffset */, sal_uInt32 /* nBytes */)
{
    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return store_E_None; // E_Unsupported
    else
        return store_E_InvalidHandle;
}

/*
 * unlockRange.
 */
storeError OFileLockBytes::unlockRange (
    sal_uInt32 /* nOffset */, sal_uInt32 /* nBytes */)
{
    // Acquire exclusive access.
    osl::MutexGuard aGuard (m_aMutex);
    if (m_pImpl->isValid())
        return store_E_None; // E_Unsupported
    else
        return store_E_InvalidHandle;
}