summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/store/FSDirectory.cpp
blob: e9659cff1b00d46ac24c8fedcc6d9bf18ca5eadf (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
/*
 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 *
 * Distributable under the terms of either the Apache License (Version 2.0) or 
 * the GNU Lesser General Public License, as specified in the COPYING file.
 *
 * Changes are Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
*/
#include <QtCore/QDir>
#include <QtCore/QDateTime>
#include <QtCore/QFileInfo>
#include <QtCore/QByteArray>
#include <QtCore/QCryptographicHash>

#include "CLucene/StdHeader.h"
#include "FSDirectory.h"
#include "CLucene/index/IndexReader.h"
#include "CLucene/util/Misc.h"
#include "CLucene/debug/condition.h"

CL_NS_DEF(store)
CL_NS_USE(util)

bool FSDirectory::disableLocks = false;

// This cache of directories ensures that there is a unique Directory instance
// per path, so that synchronization on the Directory can be used to synchronize
// access between readers and writers.
static CL_NS(util)::CLHashMap<QString, FSDirectory*,
    CL_NS(util)::Compare::Qstring, CL_NS(util)::Equals::Qstring,
    CL_NS(util)::Deletor::DummyQString> DIRECTORIES(false, false);
 
// # pragma mark -- FSDirectory::FSLock

FSDirectory::FSLock::FSLock(const QString& _lockDir, const QString& name)
    : lockDir(_lockDir)
    , lockFile(_lockDir + QDir::separator() + name)
{
}

FSDirectory::FSLock::~FSLock()
{
}

bool FSDirectory::FSLock::obtain()
{
    if (disableLocks)
        return true;

    if (QFile::exists(lockFile))
        return false;

    QDir dir(lockDir);
    if (!dir.exists()) {
        if (!dir.mkpath(lockDir)) {
            // 34: len of "Couldn't create lock directory: "
            char* err = _CL_NEWARRAY(
                char, 34 + strlen(lockDir.toLocal8Bit().constData()) + 1);
            strcpy(err, "Couldn't create lock directory: ");
            strcat(err, lockDir.toLocal8Bit().constData());
            _CLTHROWA_DEL(CL_ERR_IO, err);
        }
    }

    QFile file(lockFile);
    return file.open(QIODevice::ReadWrite);
}

void FSDirectory::FSLock::release()
{
    if (disableLocks)
        return;
    
    QFile file(lockFile);
    file.remove();
}

bool FSDirectory::FSLock::isLocked()
{
    if (disableLocks)
        return false;
    return QFile::exists(lockFile);
}

QString FSDirectory::FSLock::toString() const
{
    QString ret(QLatin1String("Lock@"));
    return ret.append(lockFile);
}

// # pragma mark -- FSDirectory::FSIndexInput

FSDirectory::FSIndexInput::FSIndexInput(const QString& path, int32_t bufferSize)
    : BufferedIndexInput(bufferSize)	
{
    CND_PRECONDITION(!path.isEmpty(), "path is NULL");

    handle = _CLNEW SharedHandle();
    handle->fhandle.setFileName(path);
    handle->fhandle.open(QIODevice::ReadOnly);
    
    if (handle->fhandle.error() != QFile::NoError) {
        switch(handle->fhandle.error()) {
            case 1:
                _CLTHROWA(CL_ERR_IO, "An error occurred when reading from the file");
                break;
            case 2:
                _CLTHROWA(CL_ERR_IO, "An error occurred when writing to the file.");
                break;
            case 5:
                _CLTHROWA(CL_ERR_IO, "The file could not be opened.");
                break;
            case 6:
                _CLTHROWA(CL_ERR_IO, "The operation was aborted.");
                break;
            case 7:
                _CLTHROWA(CL_ERR_IO, "A timeout occurred.");
                break;
            case 8:
                _CLTHROWA(CL_ERR_IO, "An unspecified error occurred.");
                break;
            case 9:
                _CLTHROWA(CL_ERR_IO, "The file could not be removed.");
                break;
            case 10:
                _CLTHROWA(CL_ERR_IO, "The file could not be renamed.");
                break;
            case 11:
                _CLTHROWA(CL_ERR_IO, "The position in the file could not be changed.");
                break;
            case 12:
                _CLTHROWA(CL_ERR_IO, "The file could not be resized.e");
                break;
            case 13:
                _CLTHROWA(CL_ERR_IO, "The file could not be accessed.");
                break;
            case 14:
                _CLTHROWA(CL_ERR_IO, "The file could not be copied.");
                break;
            case 4:
            default:
                _CLTHROWA(CL_ERR_IO, "A fatal error occurred.");
        }
    }

    //Store the file length
    handle->_length = handle->fhandle.size();
    handle->_fpos = 0;
    this->_pos = 0;
}

FSDirectory::FSIndexInput::FSIndexInput(const FSIndexInput& other)
    : BufferedIndexInput(other)
{
    if (other.handle == NULL)
        _CLTHROWA(CL_ERR_NullPointer, "other handle is null");

    SCOPED_LOCK_MUTEX(other.handle->THIS_LOCK)

    _pos = other.handle->_fpos;
    handle = _CL_POINTER(other.handle);
}

FSDirectory::FSIndexInput::~FSIndexInput()
{
    FSIndexInput::close();
}

void FSDirectory::FSIndexInput::close()
{
    BufferedIndexInput::close();
    _CLDECDELETE(handle);
}

IndexInput* FSDirectory::FSIndexInput::clone() const
{
    return _CLNEW FSDirectory::FSIndexInput(*this);
}

void FSDirectory::FSIndexInput::seekInternal(const int64_t position)
{
    CND_PRECONDITION(position >= 0 && position < handle->_length,
        "Seeking out of range")
    _pos = position;
}

void FSDirectory::FSIndexInput::readInternal(uint8_t* b, const int32_t len)
{
    SCOPED_LOCK_MUTEX(handle->THIS_LOCK)

    CND_PRECONDITION(handle != NULL, "shared file handle has closed");
    CND_PRECONDITION(handle->fhandle.isOpen(), "file is not open");

    if (handle->_fpos != _pos) {
        handle->fhandle.seek(_pos);
        if (handle->fhandle.pos() != _pos)
            _CLTHROWA( CL_ERR_IO, "File IO Seek error");
        handle->_fpos = _pos;
    }

    bufferLength = (int32_t)handle->fhandle.read((char*)b, len);
    if (bufferLength == 0)
        _CLTHROWA(CL_ERR_IO, "read past EOF");

    if (bufferLength == -1)
        _CLTHROWA(CL_ERR_IO, "read error");

    _pos += bufferLength;
    handle->_fpos =_pos;
}

// # pragma mark -- FSDirectory::FSIndexInput::SharedHandle

FSDirectory::FSIndexInput::SharedHandle::SharedHandle()
    : _fpos(0)
    , _length(0)

{
}

FSDirectory::FSIndexInput::SharedHandle::~SharedHandle()
{
    if (fhandle.isOpen())
        fhandle.close();
}

// # pragma mark -- FSDirectory::FSIndexOutput

FSDirectory::FSIndexOutput::FSIndexOutput(const QString& path)
{
    //O_BINARY - Opens file in binary (untranslated) mode
    //O_CREAT - Creates and opens new file for writing. Has no effect if file specified by filename exists
    //O_RANDOM - Specifies that caching is optimized for, but not restricted to, random access from disk.
    //O_WRONLY - Opens file for writing only;
    fhandle.setFileName(path);
    fhandle.open(QIODevice::ReadWrite | QIODevice::Truncate);
    
    if (fhandle.error() != QFile::NoError) {
        switch(fhandle.error()) {
            case 1:
                _CLTHROWA(CL_ERR_IO, "An error occurred when reading from the file");
                break;
            case 2:
                _CLTHROWA(CL_ERR_IO, "An error occurred when writing to the file.");
                break;
            case 5:
                _CLTHROWA(CL_ERR_IO, "The file could not be opened.");
                break;
            case 6:
                _CLTHROWA(CL_ERR_IO, "The operation was aborted.");
                break;
            case 7:
                _CLTHROWA(CL_ERR_IO, "A timeout occurred.");
                break;
            case 8:
                _CLTHROWA(CL_ERR_IO, "An unspecified error occurred.");
                break;
            case 9:
                _CLTHROWA(CL_ERR_IO, "The file could not be removed.");
                break;
            case 10:
                _CLTHROWA(CL_ERR_IO, "The file could not be renamed.");
                break;
            case 11:
                _CLTHROWA(CL_ERR_IO, "The position in the file could not be changed.");
                break;
            case 12:
                _CLTHROWA(CL_ERR_IO, "The file could not be resized.e");
                break;
            case 13:
                _CLTHROWA(CL_ERR_IO, "The file could not be accessed.");
                break;
            case 14:
                _CLTHROWA(CL_ERR_IO, "The file could not be copied.");
                break;
            case 4:
            default:
                _CLTHROWA(CL_ERR_IO, "A fatal error occurred.");
        }
    }
}

FSDirectory::FSIndexOutput::~FSIndexOutput()
{
    if (fhandle.isOpen()) {
        try {
            FSIndexOutput::close();
        } catch (CLuceneError& err) {
            //ignore IO errors...
            if (err.number() != CL_ERR_IO)
                throw;
        }
    }
}

void FSDirectory::FSIndexOutput::close()
{
    try {
        BufferedIndexOutput::close();
    } catch (CLuceneError& err) {
        //ignore IO errors...
        if (err.number() != CL_ERR_IO)
            throw;
    }
    fhandle.close();
}

int64_t FSDirectory::FSIndexOutput::length()
{
    CND_PRECONDITION(fhandle.isOpen(), "file is not open");
    return fhandle.size();
}

void FSDirectory::FSIndexOutput::seek(const int64_t pos)
{
    CND_PRECONDITION(fhandle.isOpen(), "file is not open");
    
    BufferedIndexOutput::seek(pos);
    fhandle.seek(pos);
    if (fhandle.pos() != pos)
        _CLTHROWA(CL_ERR_IO, "File IO Seek error");
}

void FSDirectory::FSIndexOutput::flushBuffer(const uint8_t* b, const int32_t size)
{
    CND_PRECONDITION(fhandle.isOpen(), "file is not open");

    if (size > 0 && fhandle.write((const char*)b, size) != size)
        _CLTHROWA(CL_ERR_IO, "File IO Write error");
}

// # pragma mark -- FSDirectory

FSDirectory::FSDirectory(const QString& path, const bool createDir)
    : Directory()
    , refCount(0)
    , useMMap(false)
{
    //set a realpath so that if we change directory, we can still function
    directory = QFileInfo(path).absoluteFilePath();
    lockDir = directory;

    QDir dir(lockDir);
    if (!dir.exists()) {
        if (!dir.mkpath(lockDir))
            _CLTHROWA_DEL(CL_ERR_IO, "Cannot create temp directory");
    }

    QFileInfo info(lockDir);
    if (info.isFile() || info.isSymLink())
        _CLTHROWA(CL_ERR_IO, "Found regular file where directory expected");

    if (createDir)
        create();

    dir.setPath(directory);
    if (!dir.exists()) {
         //19: len of " is not a directory"
        char* err = 
            _CL_NEWARRAY(char, 19 + strlen(path.toLocal8Bit().constData()) + 1);
        strcpy(err, path.toLocal8Bit().constData());
        strcat(err, " is not a directory");
        _CLTHROWA_DEL(CL_ERR_IO, err);
    }
}

void FSDirectory::create()
{
    SCOPED_LOCK_MUTEX(THIS_LOCK)

    bool clear = false;
    QDir dir(directory);
    if (!dir.exists()) {
        if (!dir.mkpath(directory)) {
            char* err = _CL_NEWARRAY( // 27 len of "Couldn't create directory:"
                char, 27 + strlen(directory.toLocal8Bit().constData()) + 1);
            strcpy(err, "Couldn't create directory: ");
            strcat(err, directory.toLocal8Bit().constData());
            _CLTHROWA_DEL(CL_ERR_IO, err);
        }
    } else {
        clear = true;
    }

    QFileInfo info(directory);
    if (info.isFile() || info.isSymLink()) {
        char tmp[1024];
        _snprintf(tmp, 1024, "%s not a directory",
            directory.toLocal8Bit().constData());
        _CLTHROWA(CL_ERR_IO, tmp);
    }

    if (clear) {
        dir.setPath(directory);
        // clear probably existing lucene index files 
        QStringList fileList = dir.entryList(QDir::Files | QDir::Hidden
            | QDir::NoSymLinks);
        foreach(const QString file, fileList) {
            if (CL_NS(index)::IndexReader::isLuceneFile(file)) {
                if (!dir.remove(file))
                    _CLTHROWA(CL_ERR_IO, "Couldn't delete file ");
            }
        }

        // clear probably existing file locks
        QFileInfo dirInfo(lockDir);
        if (dirInfo.exists() && dirInfo.isReadable() && dirInfo.isWritable()
            && !dirInfo.isFile() && !dirInfo.isSymLink()) {
            QDir lockDirectory(lockDir);
            fileList = dir.entryList(QStringList() << getLockPrefix()
                + QLatin1Char('*'), QDir::Files | QDir::Hidden | QDir::NoSymLinks);

            foreach(const QString file, fileList) {
                if (!lockDirectory.remove(file))
                    _CLTHROWA(CL_ERR_IO, "Couldn't delete file ");
            }
        }
        else {
             //todo: richer error: + lockDir.getAbsolutePath());
            _CLTHROWA(CL_ERR_IO, "Cannot read lock directory");
        }
    }
}

void FSDirectory::priv_getFN(QString& buffer, const QString& name) const
{
    buffer.clear();
    buffer.append(directory);
    buffer.append(QDir::separator());
    buffer.append(name);
}

FSDirectory::~FSDirectory()
{
}

QStringList FSDirectory::list() const
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QDir dir(directory);
    return dir.entryList(QDir::Files | QDir::Hidden);
}

bool FSDirectory::fileExists(const QString& name) const
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QDir dir(directory);
    return dir.entryList().contains(name);
}

QString FSDirectory::getDirName() const
{
    return directory;
}

//static
FSDirectory* FSDirectory::getDirectory(const QString& file, const bool _create)
{
    FSDirectory* dir = NULL;
    {
        if (file.isEmpty())
            _CLTHROWA(CL_ERR_IO, "Invalid directory");

        SCOPED_LOCK_MUTEX(DIRECTORIES.THIS_LOCK)
            dir = DIRECTORIES.get(file);
        if ( dir == NULL  ){
            dir = _CLNEW FSDirectory(file, _create);
            DIRECTORIES.put(dir->directory, dir);
        } else if (_create) {
            dir->create();
        }

        {
            SCOPED_LOCK_MUTEX(dir->THIS_LOCK)
            dir->refCount++;
        }
    }    

    return _CL_POINTER(dir);
}

int64_t FSDirectory::fileModified(const QString& name) const
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QFileInfo fInfo(directory + QDir::separator() + name);
    return fInfo.lastModified().toTime_t();
}

//static
int64_t FSDirectory::fileModified(const QString& dir, const QString& name)
{
    QFileInfo fInfo(dir + QDir::separator() + name);
    return fInfo.lastModified().toTime_t();
}

void FSDirectory::touchFile(const QString& name)
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QFile file(directory + QDir::separator() + name);
    if (!file.open(QIODevice::ReadWrite))
        _CLTHROWA(CL_ERR_IO, "IO Error while touching file");
}

int64_t FSDirectory::fileLength(const QString& name) const
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QFileInfo fInfo(directory + QDir::separator() + name);
    return fInfo.size();
}

IndexInput* FSDirectory::openInput(const QString& name)
{
    return openInput(name, CL_NS(store)::BufferedIndexOutput::BUFFER_SIZE);
}

IndexInput* FSDirectory::openInput(const QString& name, int32_t bufferSize )
{
    CND_PRECONDITION(directory[0]!=0,"directory is not open")

    return _CLNEW FSIndexInput(directory + QDir::separator() + name, bufferSize);
}

void FSDirectory::close()
{
    SCOPED_LOCK_MUTEX(DIRECTORIES.THIS_LOCK)
    {
        SCOPED_LOCK_MUTEX(THIS_LOCK)

        CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

        //refcount starts at 1
        if (--refCount <= 0) {
            Directory* dir = DIRECTORIES.get(getDirName());
            if (dir) {
                //this will be removed in ~FSDirectory
                DIRECTORIES.remove(getDirName());
                _CLDECDELETE(dir);
            }
        }
    }
}

QString FSDirectory::getLockPrefix() const
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QString dirName(QFileInfo(directory).absoluteFilePath());
    if (dirName.isEmpty())
        _CLTHROWA(CL_ERR_Runtime, "Invalid directory path");

    // to be compatible with jlucene,
    // we need to make some changes ...
    if (dirName.at(1) == QLatin1Char(':'))
        dirName[0] = dirName.at(0).toUpper();

    TCHAR tBuffer[2048] = { 0 };
    dirName.toWCharArray(tBuffer);
    
    char aBuffer[4096] = { 0 };
    STRCPY_TtoA(aBuffer, tBuffer, 4096);

    QString string(QLatin1String("lucene-"));
    QByteArray hash(QCryptographicHash::hash(aBuffer, QCryptographicHash::Md5));

    // TODO: verify this !!!
    return string.append(QLatin1String(hash.toHex().constData()));
}

bool FSDirectory::doDeleteFile(const QString& name)
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QDir dir(directory);
    return dir.remove(name);
}

void FSDirectory::renameFile(const QString& from, const QString& to)
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");
    SCOPED_LOCK_MUTEX(THIS_LOCK)
    
    if (fileExists(to))
        deleteFile(to, false);

    QFile file(directory + QDir::separator() + from);
    QString newFile(directory + QDir::separator() + to);
    if (!file.rename(newFile)) {
        // try a second time if we fail
        if (fileExists(to))
            deleteFile(to, false);

        if (!file.rename(newFile)) {
            QString error(QLatin1String("Could not rename: %1 to %2!!!!"));
            error.arg(from).arg(newFile);
            QByteArray bArray(error.toLocal8Bit());
            _CLTHROWA(CL_ERR_IO, bArray.constData());
        }
    }
}

IndexOutput* FSDirectory::createOutput(const QString& name)
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");

    QString file = directory + QDir::separator() + name;
    if (QFileInfo(file).exists()) {
        if (!QFile::remove(file)) {
            QByteArray bArray("Cannot overwrite: ");
            bArray.append(name.toLocal8Bit());
            _CLTHROWA(CL_ERR_IO, bArray.constData());
        }
    }
    return _CLNEW FSIndexOutput(file);
}

LuceneLock* FSDirectory::makeLock(const QString& name)
{
    CND_PRECONDITION(!directory.isEmpty(), "directory is not open");


    QString lockFile(getLockPrefix());
    lockFile.append(QLatin1Char('-')).append(name);

    return _CLNEW FSLock(lockDir, lockFile);
}

QString FSDirectory::toString() const
{
    return QString::fromLatin1("FSDirectory@").append(directory);
}

CL_NS_END