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
|
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file contains code that is specific to Symbian.
// Differently from the rest of SQLite, it is implemented in C++ as this is
// the native language of the OS and all interfaces we need to use are C++.
//
// This file follows the Gears code style guidelines.
#ifdef OS_SYMBIAN
#include <coemain.h>
#include <e32math.h>
#include <f32file.h>
#include <utf.h>
extern "C" {
#include "sqliteInt.h"
#include "os_common.h"
}
const TInt kFileLockAttempts = 3;
// The global file system session.
RFs g_fs_session;
static TInt UTF8ToUTF16(const char *in, TDes *out16) {
assert(in);
TPtrC8 in_des(reinterpret_cast<const unsigned char*>(in));
return CnvUtfConverter::ConvertToUnicodeFromUtf8(*out16, in_des);
}
static TInt UTF16ToUTF8(const TDesC16& in16, TDes8 *out8) {
return CnvUtfConverter::ConvertFromUnicodeToUtf8(*out8, in16);
}
// The SymbianFile structure is a subclass of sqlite3_file* specific to the
// Symbian portability layer.
struct SymbianFile {
const sqlite3_io_methods *methods;
RFile handle; // The file handle
TUint8 lock_type; // Type of lock currently held on this file
TUint16 shared_lock_byte; // Randomly chosen byte used as a shared lock
};
static SymbianFile* ConvertToSymbianFile(sqlite3_file* const id) {
assert(id);
return reinterpret_cast<SymbianFile*>(id);
}
static int SymbianClose(sqlite3_file *id) {
SymbianFile *file_id = ConvertToSymbianFile(id);
file_id->handle.Close();
OpenCounter(-1);
return SQLITE_OK;
}
static int SymbianRead(sqlite3_file *id,
void *buffer,
int amount,
sqlite3_int64 offset) {
assert(buffer);
assert(amount >=0);
assert(offset >=0);
SymbianFile* file_id = ConvertToSymbianFile(id);
TPtr8 dest(static_cast<unsigned char*>(buffer), amount);
if (KErrNone == file_id->handle.Read(offset, dest, amount)) {
if (dest.Length() == amount) {
return SQLITE_OK;
} else {
return SQLITE_IOERR_SHORT_READ;
}
} else {
return SQLITE_IOERR;
}
}
static int SymbianWrite(sqlite3_file *id,
const void *buffer,
int amount,
sqlite3_int64 offset) {
assert(buffer);
assert(amount >=0);
assert(offset >=0);
SymbianFile *file_id = ConvertToSymbianFile(id);
TPtrC8 src(static_cast<const unsigned char*>(buffer), amount);
if (file_id->handle.Write(offset, src) != KErrNone) {
return SQLITE_IOERR_WRITE;
}
return SQLITE_OK;
}
static int SymbianTruncate(sqlite3_file *id, sqlite3_int64 bytes) {
assert(bytes >=0);
SymbianFile *file_id = ConvertToSymbianFile(id);
if (file_id->handle.SetSize(bytes) != KErrNone) {
return SQLITE_IOERR;
}
return SQLITE_OK;
}
static int SymbianSync(sqlite3_file *id, int /*flags*/) {
SymbianFile *file_id = ConvertToSymbianFile(id);
if (file_id->handle.Flush() != KErrNone) {
return SQLITE_IOERR;
} else {
return SQLITE_OK;
}
}
static int SymbianFileSize(sqlite3_file *id, sqlite3_int64 *size) {
assert(size);
SymbianFile *file_id = ConvertToSymbianFile(id);
TInt size_tmp;
if (file_id->handle.Size(size_tmp) != KErrNone) {
return SQLITE_IOERR;
}
*size = size_tmp;
return SQLITE_OK;
}
// File lock/unlock functions; see os_win.c for a description
// of the algorithm used.
static int GetReadLock(SymbianFile *file) {
file->shared_lock_byte = Math::Random() % (SHARED_SIZE - 1);
return file->handle.Lock(SHARED_FIRST + file->shared_lock_byte, 1);
}
static int UnlockReadLock(SymbianFile *file) {
return file->handle.UnLock(SHARED_FIRST + file->shared_lock_byte, 1);
}
static int SymbianLock(sqlite3_file *id, int lock_type) {
SymbianFile *file = ConvertToSymbianFile(id);
if (file->lock_type >= lock_type) {
return SQLITE_OK;
}
// Make sure the locking sequence is correct
assert(file->lock_type != NO_LOCK || lock_type == SHARED_LOCK);
assert(lock_type != PENDING_LOCK);
assert(lock_type != RESERVED_LOCK || file->lock_type == SHARED_LOCK);
// Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
// a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
// the PENDING_LOCK byte is temporary.
int new_lock_type = file->lock_type;
int got_pending_lock = 0;
int res = KErrNone;
if (file->lock_type == NO_LOCK ||
(lock_type == EXCLUSIVE_LOCK && file->lock_type == RESERVED_LOCK)) {
int count = kFileLockAttempts;
while (count-- > 0 &&
(res = file->handle.Lock(PENDING_BYTE, 1)) != KErrNone ) {
// Try 3 times to get the pending lock. The pending lock might be
// held by another reader process who will release it momentarily.
OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
User::After(1000);
}
got_pending_lock = (res == KErrNone? 1 : 0);
}
// Acquire a shared lock
if (lock_type == SHARED_LOCK && res == KErrNone) {
assert(file->lock_type == NO_LOCK);
res = GetReadLock(file);
if (res == KErrNone) {
new_lock_type = SHARED_LOCK;
}
}
// Acquire a RESERVED lock
if (lock_type == RESERVED_LOCK && res == KErrNone) {
assert(file->lock_type == SHARED_LOCK);
res = file->handle.Lock(RESERVED_BYTE, 1);
if (res == KErrNone) {
new_lock_type = RESERVED_LOCK;
}
}
// Acquire a PENDING lock
if (lock_type == EXCLUSIVE_LOCK && res == KErrNone) {
new_lock_type = PENDING_LOCK;
got_pending_lock = 0;
}
// Acquire an EXCLUSIVE lock
if (lock_type == EXCLUSIVE_LOCK && res == KErrNone) {
assert(file->lock_type >= SHARED_LOCK);
res = UnlockReadLock(file);
OSTRACE2("unreadlock = %d\n", res);
res = file->handle.Lock(SHARED_FIRST, SHARED_SIZE);
if (res == KErrNone) {
new_lock_type = EXCLUSIVE_LOCK;
} else {
OSTRACE2("error-code = %d\n", GetLastError());
GetReadLock(file);
}
}
// If we are holding a PENDING lock that ought to be released, then
// release it now.
if (got_pending_lock && lock_type == SHARED_LOCK) {
file->handle.UnLock(PENDING_BYTE, 1);
}
// Update the state of the lock held in the file descriptor, then
// return the appropriate result code.
file->lock_type = new_lock_type;
if (res == KErrNone) {
return SQLITE_OK;
} else {
OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", file->handle,
lock_type, new_lock_type);
return SQLITE_BUSY;
}
}
static int SymbianUnlock(sqlite3_file *id, int lock_type) {
int type;
int rc = SQLITE_OK;
SymbianFile *file = ConvertToSymbianFile(id);
assert(lock_type <= SHARED_LOCK);
OSTRACE5("UNLOCK %d to %d was %d(%d)\n", file->handle, lock_type,
file->lock_type, file->shared_lock_byte);
type = file->lock_type;
if (type >= EXCLUSIVE_LOCK) {
file->handle.UnLock(SHARED_FIRST, SHARED_SIZE);
if (lock_type == SHARED_LOCK && GetReadLock(file) != KErrNone) {
// This should never happen. We should always be able to
// reacquire the read lock
rc = SQLITE_IOERR_UNLOCK;
}
}
if (type >= RESERVED_LOCK) {
file->handle.UnLock(RESERVED_BYTE, 1);
}
if (lock_type == NO_LOCK && type >= SHARED_LOCK) {
UnlockReadLock(file);
}
if (type >= PENDING_LOCK) {
file->handle.UnLock(PENDING_BYTE, 1);
}
file->lock_type = lock_type;
return rc;
}
static int SymbianCheckReservedLock(sqlite3_file *id, int *result) {
int rc;
SymbianFile *file = ConvertToSymbianFile(id);
if (file->lock_type >= RESERVED_LOCK) {
rc = 1;
OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
} else {
rc = file->handle.Lock(RESERVED_BYTE, 1);
if (rc == KErrNone) {
file->handle.UnLock(RESERVED_BYTE, 1);
}
rc = !rc;
OSTRACE3("TEST WR-LOCK %d %d (remote)\n", file->handle, rc);
}
*result = rc;
return SQLITE_OK;
}
static int SymbianFileControl(sqlite3_file */*id*/,
int /*op*/,
void */*arg*/) {
return SQLITE_OK;
}
static int SymbianSectorSize(sqlite3_file */*id*/) {
return SQLITE_DEFAULT_SECTOR_SIZE;
}
static int SymbianDeviceCharacteristics(sqlite3_file */*id*/) {
return 0;
}
/*
** This vector defines all the methods that can operate on a
** sqlite3_file for Symbian.
*/
static const sqlite3_io_methods SymbianIoMethod = {
1, // iVersion
SymbianClose,
SymbianRead,
SymbianWrite,
SymbianTruncate,
SymbianSync,
SymbianFileSize,
SymbianLock,
SymbianUnlock,
SymbianCheckReservedLock,
SymbianFileControl,
SymbianSectorSize,
SymbianDeviceCharacteristics
};
// ============================================================================
// vfs methods begin here
// ============================================================================
static int SymbianOpen(sqlite3_vfs */*vfs*/,
const char *name,
sqlite3_file *id,
int flags,
int *out_flags) {
TUint desired_access;
TUint share_mode;
TInt err = KErrNone;
TFileName name_utf16;
SymbianFile *file = ConvertToSymbianFile(id);
if (out_flags) {
*out_flags = flags;
}
// if the name is NULL we have to open a temporary file.
if (!name) {
TPath private_path;
TFileName file_name;
if (g_fs_session.PrivatePath(private_path) != KErrNone) {
return SQLITE_CANTOPEN;
}
if (file->handle.Temp(g_fs_session,
private_path,
file_name,
EFileWrite) !=
KErrNone) {
return SQLITE_CANTOPEN;
}
file->methods = &SymbianIoMethod;
file->lock_type = NO_LOCK;
file->shared_lock_byte = 0;
OpenCounter(+1);
return SQLITE_OK;
}
if (UTF8ToUTF16(name, &name_utf16) != KErrNone)
return SQLITE_CANTOPEN;
if (flags & SQLITE_OPEN_READWRITE) {
desired_access = EFileWrite;
} else {
desired_access = EFileRead;
}
if (flags & SQLITE_OPEN_MAIN_DB) {
share_mode = EFileShareReadersOrWriters;
} else {
share_mode = 0;
}
if (flags & SQLITE_OPEN_CREATE) {
err = file->handle.Create(g_fs_session,
name_utf16,
desired_access | share_mode);
if (err != KErrNone && err != KErrAlreadyExists) {
return SQLITE_CANTOPEN;
}
}
if (err != KErrNone) {
err = file->handle.Open(g_fs_session,
name_utf16,
desired_access | share_mode);
if (err != KErrNone && flags & SQLITE_OPEN_READWRITE) {
if (out_flags) {
*out_flags = (flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE;
}
desired_access = EFileRead;
err = file->handle.Open(g_fs_session,
name_utf16,
desired_access | share_mode);
}
if (err != KErrNone) {
return SQLITE_CANTOPEN;
}
}
file->methods = &SymbianIoMethod;
file->lock_type = NO_LOCK;
file->shared_lock_byte = 0;
OpenCounter(+1);
return SQLITE_OK;
}
static int SymbianDelete(sqlite3_vfs */*vfs*/,
const char *file_name,
int /*sync_dir*/) {
assert(file_name);
TFileName file_name_utf16;
if (UTF8ToUTF16(file_name, &file_name_utf16) != KErrNone) {
return SQLITE_ERROR;
}
TInt result = g_fs_session.Delete(file_name_utf16);
return (result == KErrNone || result == KErrPathNotFound)?
SQLITE_OK : SQLITE_IOERR_DELETE;
}
static int SymbianAccess(sqlite3_vfs */*vfs*/,
const char *file_name,
int flags,
int *result) {
assert(file_name);
TEntry entry;
TFileName file_name_utf16;
if (UTF8ToUTF16(file_name, &file_name_utf16) != KErrNone) {
return SQLITE_ERROR;
}
if (g_fs_session.Entry(file_name_utf16, entry) != KErrNone) {
*result = 0;
return SQLITE_OK;
}
switch (flags) {
case SQLITE_ACCESS_READ:
case SQLITE_ACCESS_EXISTS:
*result = !entry.IsDir();
break;
case SQLITE_ACCESS_READWRITE:
*result = !entry.IsDir() && !entry.IsReadOnly();
break;
default:
return SQLITE_ERROR;
}
return SQLITE_OK;
}
static int SymbianFullPathname(sqlite3_vfs */*vfs*/,
const char *relative,
int full_len,
char *full) {
assert(relative);
assert(full);
TParse parse;
TPath relative_utf16;
TPath base_path;
TPtr8 full_utf8(reinterpret_cast<unsigned char*>(full), full_len);
g_fs_session.PrivatePath(base_path);
if (UTF8ToUTF16(relative, &relative_utf16) != KErrNone) {
return SQLITE_ERROR;
}
if (parse.Set(relative_utf16, &base_path, NULL) != KErrNone) {
return SQLITE_ERROR;
}
TDesC full_utf16(parse.FullName());
if (UTF16ToUTF8(relative_utf16, &full_utf8) != KErrNone) {
return SQLITE_ERROR;
}
full_utf8.PtrZ();
return SQLITE_OK;
}
static int SymbianRandomness(sqlite3_vfs */*vfs*/, int buf_len, char *buffer) {
assert(buffer);
TInt64 seed = User::TickCount();
for (TInt i = 0; i < buf_len; i++) {
buffer[i] = Math::Rand(seed) % 255;
}
return SQLITE_OK;
}
static int SymbianSleep(sqlite3_vfs */*vfs*/, int microsec) {
User::After(microsec);
return SQLITE_OK;
}
int SymbianCurrentTime(sqlite3_vfs */*vfs*/, double *now) {
_LIT(kEpoch, "19700101:000000.000000");
assert(now);
TTime time;
TTime epoch_time(kEpoch);
TTimeIntervalSeconds interval;
time.HomeTime();
// calculate seconds elapsed since 1-1-1970
time.SecondsFrom(epoch_time, interval);
// Julian date @ 1-1-1970 = 2440587.5
// seconds per day = 86400.0
*now = interval.Int()/86400.0 + 2440587.5;
return SQLITE_OK;
}
static int SymbianGetLastError(sqlite3_vfs */*vfs*/,
int /*buf_len*/,
char */*buf*/) {
assert(buf[0] == '\0');
return 0;
}
// Interfaces for opening a shared library, finding entry points
// within the shared library, and closing the shared library.
// TODO(marcogelmi): implement.
#define SymbianDlOpen 0
#define SymbianDlError 0
#define SymbianDlSym 0
#define SymbianDlClose 0
// Initialize and deinitialize the operating system interface.
int sqlite3_os_init(void) {
static sqlite3_vfs symbian_vfs = {
1, // iVersion
sizeof(SymbianFile), // szOsFile
KMaxPath, // mxPathname
0, // pNext
"symbian", // name
0, // pAppData
SymbianOpen, // xOpen
SymbianDelete, // xDelete
SymbianAccess, // xAccess
SymbianFullPathname, // xFullPathname
SymbianDlOpen, // xDlOpen
SymbianDlError, // xDlError
SymbianDlSym, // xDlSym
SymbianDlClose, // xDlClose
SymbianRandomness, // xRandomness
SymbianSleep, // xSleep
SymbianCurrentTime, // xCurrentTime
SymbianGetLastError // xGetLastError
};
if (g_fs_session.Connect() != KErrNone) {
return SQLITE_ERROR;
}
if (g_fs_session.ShareAuto() != KErrNone) {
g_fs_session.Close();
return SQLITE_ERROR;
}
sqlite3_vfs_register(&symbian_vfs, 1);
return SQLITE_OK;
}
int sqlite3_os_end(void) {
g_fs_session.Close();
return SQLITE_OK;
}
#endif /* OS_SYMBIAN*/
|