SQLite

Check-in [dbe9c8aa8d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implement xFullpath for lsm1 on Win32.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | lsm-vtab
Files: files | file ages | folders
SHA3-256: dbe9c8aa8d70051fafec569054eeda6c02efe9d036ab6beada00da3ed42e52d9
User & Date: mistachkin 2017-06-27 18:15:38.105
Original Comment: Implement xFullpath for Win32.
Context
2017-06-27
22:27
Initial work on porting lsmtest to Win32. (check-in: 7e669d9bfa user: mistachkin tags: lsm-vtab)
18:15
Implement xFullpath for lsm1 on Win32. (check-in: dbe9c8aa8d user: mistachkin tags: lsm-vtab)
06:28
Minor corrections to the previous check-in. (check-in: e1cf8a78a0 user: mistachkin tags: lsm-vtab)
Changes
Side-by-Side Diff Ignore Whitespace Patch
Changes to ext/lsm1/lsm_win32.c.
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
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







-
-










+
+
+
+
+







  HANDLE hMap;                    /* File handle for mapping */
  void *pMap;                     /* Pointer to mapping of file fd */
  size_t nMap;                    /* Size of mapping at pMap in bytes */
  int nShm;                       /* Number of entries in array apShm[] */
  void **apShm;                   /* Array of 32K shared memory segments */
};

int lsmWin32OsSleep(lsm_env *pEnv, int us);

static char *win32ShmFile(Win32File *p){
  char *zShm;
  int nName = strlen(p->zName);
  zShm = (char *)lsmMallocZero(p->pEnv, nName+4+1);
  if( zShm ){
    memcpy(zShm, p->zName, nName);
    memcpy(&zShm[nName], "-shm", 5);
  }
  return zShm;
}

static int win32Sleep(int us){
  Sleep((us + 999) / 1000);
  return LSM_OK;
}

/*
** The number of times that an I/O operation will be retried following a
** locking error - probably caused by antivirus software.  Also the initial
** delay before the first retry.  The delay increases linearly with each
** retry.
*/
111
112
113
114
115
116
117
118

119
120
121
122
123
124

125
126
127
128
129
130
131
114
115
116
117
118
119
120

121
122
123
124
125
126

127
128
129
130
131
132
133
134







-
+





-
+







){
  DWORD lastErrno;
  if( *pnRetry>=win32IoerrRetry ){
    return 0;
  }
  lastErrno = GetLastError();
  if( win32IoerrCanRetry1(lastErrno) ){
    lsmWin32OsSleep(pEnv, win32IoerrRetryDelay*(1+*pnRetry));
    win32Sleep(win32IoerrRetryDelay*(1+*pnRetry));
    ++*pnRetry;
    return 1;
  }
#if defined(win32IoerrCanRetry2)
  else if( win32IoerrCanRetry2(lastErrno) ){
    lsmWin32OsSleep(pEnv, win32IoerrRetryDelay*(1+*pnRetry));
    win32Sleep(win32IoerrRetryDelay*(1+*pnRetry));
    ++*pnRetry;
    return 1;
  }
#endif
  return 0;
}

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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



















-
+







  nChar = MultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, nChar);
  if( nChar==0 ){
    lsmFree(pEnv, zWideText);
    zWideText = 0;
  }
  return zWideText;
}

/*
** Convert a Microsoft Unicode string to UTF-8.
**
** Space to hold the returned string is obtained from lsmMalloc().
*/
static char *win32UnicodeToUtf8(lsm_env *pEnv, LPCWSTR zWideText){
  int nByte;
  char *zText;

  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
  if( nByte == 0 ){
    return 0;
  }
  zText = lsmMallocZero(pEnv, nByte);
  if( zText==0 ){
    return 0;
  }
  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, 0, 0);
  if( nByte == 0 ){
    lsmFree(pEnv, zText);
    zText = 0;
  }
  return zText;
}

#if !defined(win32IsNotFound)
#define win32IsNotFound(a) (((a)==ERROR_FILE_NOT_FOUND)  || \
                            ((a)==ERROR_PATH_NOT_FOUND))
#endif

static int lsmWin32OsOpen(
  lsm_env *pEnv,
  const char *zFile,
  int flags,
  lsm_file **ppFile
){
  int rc = LSM_OK;
  Win32File *pWin32File;

  pWin32File = lsmMallocZero(pEnv, sizeof(Win32File));
  if( pWin32File==0 ){
    rc = LSM_NOMEM_BKPT;
  }else{
    LPCWSTR zConverted;
    LPWSTR zConverted;
    int bReadonly = (flags & LSM_OPEN_READONLY);
    DWORD dwDesiredAccess;
    DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
    DWORD dwCreationDisposition;
    DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
    HANDLE hFile;

329
330
331
332
333
334
335






336
337
338
339
340
341
342










































343




344
345
346
347
348
349
350
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







+
+
+
+
+
+







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+







  lsm_file *pFile,
  lsm_i64 iMin,
  void **ppOut,
  lsm_i64 *pnOut
){
  return LSM_ERROR;
}

static BOOL win32IsDriveLetterAndColon(
  const char *zPathname
){
  return ( isalpha(zPathname[0]) && zPathname[1]==':' );
}

static int lsmWin32OsFullpath(
  lsm_env *pEnv,
  const char *zName,
  char *zOut,
  int *pnOut
){
  DWORD nByte;
  void *zConverted;
  LPWSTR zTempWide;
  char *zTempUtf8;

  if( zName[0]=='/' && win32IsDriveLetterAndColon(zName+1) ){
    zName++;
  }
  zConverted = win32Utf8ToUnicode(pEnv, zName);
  if( zConverted==0 ){
    return LSM_NOMEM_BKPT;
  }
  nByte = GetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
  if( nByte==0 ){
    lsmFree(pEnv, zConverted);
    return LSM_IOERR_BKPT;
  }
  nByte += 3;
  zTempWide = lsmMallocZero(pEnv, nByte * sizeof(zTempWide[0]));
  if( zTempWide==0 ){
    lsmFree(pEnv, zConverted);
    return LSM_NOMEM_BKPT;
  }
  nByte = GetFullPathNameW((LPCWSTR)zConverted, nByte, zTempWide, 0);
  if( nByte==0 ){
    lsmFree(pEnv, zConverted);
    lsmFree(pEnv, zTempWide);
    return LSM_IOERR_BKPT;
  }
  lsmFree(pEnv, zConverted);
  zTempUtf8 = win32UnicodeToUtf8(pEnv, zTempWide);
  lsmFree(pEnv, zTempWide);
  if( zTempUtf8 ){
    int nOut = *pnOut;
    int nLen = strlen(zTempUtf8) + 1;
    if( nLen>=nOut ){
      lsmFree(pEnv, zTempUtf8);
      return LSM_IOERR_BKPT;
    }
    snprintf(zOut, nOut, "%s", zTempUtf8);
    lsmFree(pEnv, zTempUtf8);
    *pnOut = nLen;
  return LSM_ERROR;
    return LSM_OK;
  }else{
    return LSM_NOMEM_BKPT;
  }
}

static int lsmWin32OsFileid(
  lsm_file *pFile,
  void *pBuf,
  int *pnBuf
){
427
428
429
430
431
432
433
434
435

436
437
438
439
440
441
442
506
507
508
509
510
511
512


513
514
515
516
517
518
519
520







-
-
+







  lsmFree(pWin32File->pEnv, pWin32File->apShm);
  lsmFree(pWin32File->pEnv, pWin32File);
  return rc;
}

static int lsmWin32OsSleep(lsm_env *pEnv, int us){
  unused_parameter(pEnv);
  Sleep((us + 999) / 1000);
  return LSM_OK;
  return win32Sleep(us);
}

/****************************************************************************
** Memory allocation routines.
*/

static void *lsmWin32OsMalloc(lsm_env *pEnv, size_t N){