summaryrefslogtreecommitdiffstats
path: root/src/plugins/sqldrivers/sqlite/qsql_sqlite_vfs.cpp
blob: bbba3cd14f2d3e03a8762fffa663905efed68edb (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qsql_sqlite_vfs_p.h"

#include <QFile>

#include <limits.h> // defines PATH_MAX on unix
#include <sqlite3.h>
#include <stdio.h> // defines FILENAME_MAX everywhere

#ifndef PATH_MAX
#  define PATH_MAX FILENAME_MAX
#endif
#if SQLITE_VERSION_NUMBER < 3040000
typedef const char *sqlite3_filename;
#endif

namespace {
struct Vfs : sqlite3_vfs {
    sqlite3_vfs *pVfs;
    sqlite3_io_methods ioMethods;
};

struct File : sqlite3_file {
    class QtFile : public QFile {
    public:
        QtFile(const QString &name, bool removeOnClose)
            : QFile(name)
            , removeOnClose(removeOnClose)
        {}

        ~QtFile() override
        {
            if (removeOnClose)
                remove();
        }
    private:
        bool removeOnClose;
    };
    QtFile *pFile;
};


int xClose(sqlite3_file *sfile)
{
    auto file = static_cast<File *>(sfile);
    delete file->pFile;
    file->pFile = nullptr;
    return SQLITE_OK;
}

int xRead(sqlite3_file *sfile, void *ptr, int iAmt, sqlite3_int64 iOfst)
{
    auto file = static_cast<File *>(sfile);
    if (!file->pFile->seek(iOfst))
        return SQLITE_IOERR_READ;

    auto sz = file->pFile->read(static_cast<char *>(ptr), iAmt);
    if (sz < iAmt) {
        memset(static_cast<char *>(ptr) + sz, 0, size_t(iAmt - sz));
        return SQLITE_IOERR_SHORT_READ;
    }
    return SQLITE_OK;
}

int xWrite(sqlite3_file *sfile, const void *data, int iAmt, sqlite3_int64 iOfst)
{
    auto file = static_cast<File *>(sfile);
    if (!file->pFile->seek(iOfst))
        return SQLITE_IOERR_SEEK;
    return file->pFile->write(reinterpret_cast<const char*>(data), iAmt) == iAmt ? SQLITE_OK : SQLITE_IOERR_WRITE;
}

int xTruncate(sqlite3_file *sfile, sqlite3_int64 size)
{
    auto file = static_cast<File *>(sfile);
    return file->pFile->resize(size) ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
}

int xSync(sqlite3_file *sfile, int /*flags*/)
{
    static_cast<File *>(sfile)->pFile->flush();
    return SQLITE_OK;
}

int xFileSize(sqlite3_file *sfile, sqlite3_int64 *pSize)
{
    auto file = static_cast<File *>(sfile);
    *pSize = file->pFile->size();
    return SQLITE_OK;
}

// No lock/unlock for QFile, QLockFile doesn't work for me

int xLock(sqlite3_file *, int) { return SQLITE_OK; }

int xUnlock(sqlite3_file *, int) { return SQLITE_OK; }

int xCheckReservedLock(sqlite3_file *, int *pResOut)
{
    *pResOut = 0;
    return SQLITE_OK;
}

int xFileControl(sqlite3_file *, int, void *) { return SQLITE_NOTFOUND; }

int xSectorSize(sqlite3_file *)
{
    return 4096;
}

int xDeviceCharacteristics(sqlite3_file *)
{
    return 0; // no SQLITE_IOCAP_XXX
}

int xOpen(sqlite3_vfs *svfs, sqlite3_filename zName, sqlite3_file *sfile,
          int flags, int *pOutFlags)
{
    auto vfs = static_cast<Vfs *>(svfs);
    auto file = static_cast<File *>(sfile);
    memset(file, 0, sizeof(File));
    QIODeviceBase::OpenMode mode = QIODeviceBase::NotOpen;
    if (!zName || (flags & SQLITE_OPEN_MEMORY))
        return SQLITE_PERM;
    if ((flags & SQLITE_OPEN_READONLY) &&
        !(flags & SQLITE_OPEN_READWRITE) &&
        !(flags & SQLITE_OPEN_CREATE) &&
        !(flags & SQLITE_OPEN_DELETEONCLOSE)) {
        mode |= QIODeviceBase::OpenModeFlag::ReadOnly;
    } else {
        /*
            ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
            ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
            ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
            ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
            ** SQLITE_OPEN_CREATE, is used to indicate that file should always
            ** be created, and that it is an error if it already exists.
            ** It is <i>not</i> used to indicate the file should be opened
            ** for exclusive access.
         */
        if ((flags & SQLITE_OPEN_CREATE) && (flags & SQLITE_OPEN_EXCLUSIVE))
            mode |= QIODeviceBase::OpenModeFlag::NewOnly;

        if (flags & SQLITE_OPEN_READWRITE)
            mode |= QIODeviceBase::OpenModeFlag::ReadWrite;
    }

    file->pMethods = &vfs->ioMethods;
    file->pFile = new File::QtFile(QString::fromUtf8(zName), bool(flags & SQLITE_OPEN_DELETEONCLOSE));
    if (!file->pFile->open(mode))
        return SQLITE_CANTOPEN;
    if (pOutFlags)
        *pOutFlags = flags;

    return SQLITE_OK;
}

int xDelete(sqlite3_vfs *, const char *zName, int)
{
    return QFile::remove(QString::fromUtf8(zName)) ? SQLITE_OK : SQLITE_ERROR;
}

int xAccess(sqlite3_vfs */*svfs*/, const char *zName, int flags, int *pResOut)
{
    *pResOut = 0;
    switch (flags) {
    case SQLITE_ACCESS_EXISTS:
    case SQLITE_ACCESS_READ:
        *pResOut = QFile::exists(QString::fromUtf8(zName));
        break;
    default:
        break;
    }
    return SQLITE_OK;
}

int xFullPathname(sqlite3_vfs *, const char *zName, int nOut, char *zOut)
{
    if (!zName)
        return SQLITE_ERROR;

    int i = 0;
    for (;zName[i] && i < nOut; ++i)
        zOut[i] = zName[i];

    if (i >= nOut)
        return SQLITE_ERROR;

    zOut[i] = '\0';
    return SQLITE_OK;
}

int xRandomness(sqlite3_vfs *svfs, int nByte, char *zOut)
{
    auto vfs = static_cast<Vfs *>(svfs)->pVfs;
    return vfs->xRandomness(vfs, nByte, zOut);
}

int xSleep(sqlite3_vfs *svfs, int microseconds)
{
    auto vfs = static_cast<Vfs *>(svfs)->pVfs;
    return vfs->xSleep(vfs, microseconds);
}

int xCurrentTime(sqlite3_vfs *svfs, double *zOut)
{
    auto vfs = static_cast<Vfs *>(svfs)->pVfs;
    return vfs->xCurrentTime(vfs, zOut);
}

int xGetLastError(sqlite3_vfs *, int, char *)
{
   return 0;
}

int xCurrentTimeInt64(sqlite3_vfs *svfs, sqlite3_int64 *zOut)
{
   auto vfs = static_cast<Vfs *>(svfs)->pVfs;
   return vfs->xCurrentTimeInt64(vfs, zOut);
}
} // namespace {

void register_qt_vfs()
{
    static Vfs vfs;
    memset(&vfs, 0, sizeof(Vfs));
    vfs.iVersion = 1;
    vfs.szOsFile = sizeof(File);
    vfs.mxPathname = PATH_MAX;
    vfs.zName = "QtVFS";
    vfs.xOpen = &xOpen;
    vfs.xDelete = &xDelete;
    vfs.xAccess = &xAccess;
    vfs.xFullPathname = &xFullPathname;
    vfs.xRandomness = &xRandomness;
    vfs.xSleep = &xSleep;
    vfs.xCurrentTime = &xCurrentTime;
    vfs.xGetLastError = &xGetLastError;
    vfs.xCurrentTimeInt64 = &xCurrentTimeInt64;
    vfs.pVfs = sqlite3_vfs_find(nullptr);
    vfs.ioMethods.iVersion = 1;
    vfs.ioMethods.xClose = &xClose;
    vfs.ioMethods.xRead = &xRead;
    vfs.ioMethods.xWrite = &xWrite;
    vfs.ioMethods.xTruncate = &xTruncate;
    vfs.ioMethods.xSync = &xSync;
    vfs.ioMethods.xFileSize = &xFileSize;
    vfs.ioMethods.xLock = &xLock;
    vfs.ioMethods.xUnlock = &xUnlock;
    vfs.ioMethods.xCheckReservedLock = &xCheckReservedLock;
    vfs.ioMethods.xFileControl = &xFileControl;
    vfs.ioMethods.xSectorSize = &xSectorSize;
    vfs.ioMethods.xDeviceCharacteristics = &xDeviceCharacteristics;

    sqlite3_vfs_register(&vfs, 0);
}