/ Check-in [dbe9c8aa]
Login

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

Overview
Comment:Implement xFullpath for lsm1 on Win32.
Downloads: Tarball | ZIP archive | SQL 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
Original Comment: Implement xFullpath for Win32.
Context
2017-06-27
22:27
Initial work on porting lsmtest to Win32. check-in: 7e669d9b user: mistachkin tags: lsm-vtab
18:15
Implement xFullpath for lsm1 on Win32. check-in: dbe9c8aa user: mistachkin tags: lsm-vtab
06:28
Minor corrections to the previous check-in. check-in: e1cf8a78 user: mistachkin tags: lsm-vtab
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/lsm1/lsm_win32.c.

    41     41     HANDLE hMap;                    /* File handle for mapping */
    42     42     void *pMap;                     /* Pointer to mapping of file fd */
    43     43     size_t nMap;                    /* Size of mapping at pMap in bytes */
    44     44     int nShm;                       /* Number of entries in array apShm[] */
    45     45     void **apShm;                   /* Array of 32K shared memory segments */
    46     46   };
    47     47   
    48         -int lsmWin32OsSleep(lsm_env *pEnv, int us);
    49         -
    50     48   static char *win32ShmFile(Win32File *p){
    51     49     char *zShm;
    52     50     int nName = strlen(p->zName);
    53     51     zShm = (char *)lsmMallocZero(p->pEnv, nName+4+1);
    54     52     if( zShm ){
    55     53       memcpy(zShm, p->zName, nName);
    56     54       memcpy(&zShm[nName], "-shm", 5);
    57     55     }
    58     56     return zShm;
    59     57   }
           58  +
           59  +static int win32Sleep(int us){
           60  +  Sleep((us + 999) / 1000);
           61  +  return LSM_OK;
           62  +}
    60     63   
    61     64   /*
    62     65   ** The number of times that an I/O operation will be retried following a
    63     66   ** locking error - probably caused by antivirus software.  Also the initial
    64     67   ** delay before the first retry.  The delay increases linearly with each
    65     68   ** retry.
    66     69   */
................................................................................
   111    114   ){
   112    115     DWORD lastErrno;
   113    116     if( *pnRetry>=win32IoerrRetry ){
   114    117       return 0;
   115    118     }
   116    119     lastErrno = GetLastError();
   117    120     if( win32IoerrCanRetry1(lastErrno) ){
   118         -    lsmWin32OsSleep(pEnv, win32IoerrRetryDelay*(1+*pnRetry));
          121  +    win32Sleep(win32IoerrRetryDelay*(1+*pnRetry));
   119    122       ++*pnRetry;
   120    123       return 1;
   121    124     }
   122    125   #if defined(win32IoerrCanRetry2)
   123    126     else if( win32IoerrCanRetry2(lastErrno) ){
   124         -    lsmWin32OsSleep(pEnv, win32IoerrRetryDelay*(1+*pnRetry));
          127  +    win32Sleep(win32IoerrRetryDelay*(1+*pnRetry));
   125    128       ++*pnRetry;
   126    129       return 1;
   127    130     }
   128    131   #endif
   129    132     return 0;
   130    133   }
   131    134   
................................................................................
   149    152     nChar = MultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, nChar);
   150    153     if( nChar==0 ){
   151    154       lsmFree(pEnv, zWideText);
   152    155       zWideText = 0;
   153    156     }
   154    157     return zWideText;
   155    158   }
          159  +
          160  +/*
          161  +** Convert a Microsoft Unicode string to UTF-8.
          162  +**
          163  +** Space to hold the returned string is obtained from lsmMalloc().
          164  +*/
          165  +static char *win32UnicodeToUtf8(lsm_env *pEnv, LPCWSTR zWideText){
          166  +  int nByte;
          167  +  char *zText;
          168  +
          169  +  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
          170  +  if( nByte == 0 ){
          171  +    return 0;
          172  +  }
          173  +  zText = lsmMallocZero(pEnv, nByte);
          174  +  if( zText==0 ){
          175  +    return 0;
          176  +  }
          177  +  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, 0, 0);
          178  +  if( nByte == 0 ){
          179  +    lsmFree(pEnv, zText);
          180  +    zText = 0;
          181  +  }
          182  +  return zText;
          183  +}
   156    184   
   157    185   #if !defined(win32IsNotFound)
   158    186   #define win32IsNotFound(a) (((a)==ERROR_FILE_NOT_FOUND)  || \
   159    187                               ((a)==ERROR_PATH_NOT_FOUND))
   160    188   #endif
   161    189   
   162    190   static int lsmWin32OsOpen(
................................................................................
   168    196     int rc = LSM_OK;
   169    197     Win32File *pWin32File;
   170    198   
   171    199     pWin32File = lsmMallocZero(pEnv, sizeof(Win32File));
   172    200     if( pWin32File==0 ){
   173    201       rc = LSM_NOMEM_BKPT;
   174    202     }else{
   175         -    LPCWSTR zConverted;
          203  +    LPWSTR zConverted;
   176    204       int bReadonly = (flags & LSM_OPEN_READONLY);
   177    205       DWORD dwDesiredAccess;
   178    206       DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
   179    207       DWORD dwCreationDisposition;
   180    208       DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
   181    209       HANDLE hFile;
   182    210   
................................................................................
   329    357     lsm_file *pFile,
   330    358     lsm_i64 iMin,
   331    359     void **ppOut,
   332    360     lsm_i64 *pnOut
   333    361   ){
   334    362     return LSM_ERROR;
   335    363   }
          364  +
          365  +static BOOL win32IsDriveLetterAndColon(
          366  +  const char *zPathname
          367  +){
          368  +  return ( isalpha(zPathname[0]) && zPathname[1]==':' );
          369  +}
   336    370   
   337    371   static int lsmWin32OsFullpath(
   338    372     lsm_env *pEnv,
   339    373     const char *zName,
   340    374     char *zOut,
   341    375     int *pnOut
   342    376   ){
   343         -  return LSM_ERROR;
          377  +  DWORD nByte;
          378  +  void *zConverted;
          379  +  LPWSTR zTempWide;
          380  +  char *zTempUtf8;
          381  +
          382  +  if( zName[0]=='/' && win32IsDriveLetterAndColon(zName+1) ){
          383  +    zName++;
          384  +  }
          385  +  zConverted = win32Utf8ToUnicode(pEnv, zName);
          386  +  if( zConverted==0 ){
          387  +    return LSM_NOMEM_BKPT;
          388  +  }
          389  +  nByte = GetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
          390  +  if( nByte==0 ){
          391  +    lsmFree(pEnv, zConverted);
          392  +    return LSM_IOERR_BKPT;
          393  +  }
          394  +  nByte += 3;
          395  +  zTempWide = lsmMallocZero(pEnv, nByte * sizeof(zTempWide[0]));
          396  +  if( zTempWide==0 ){
          397  +    lsmFree(pEnv, zConverted);
          398  +    return LSM_NOMEM_BKPT;
          399  +  }
          400  +  nByte = GetFullPathNameW((LPCWSTR)zConverted, nByte, zTempWide, 0);
          401  +  if( nByte==0 ){
          402  +    lsmFree(pEnv, zConverted);
          403  +    lsmFree(pEnv, zTempWide);
          404  +    return LSM_IOERR_BKPT;
          405  +  }
          406  +  lsmFree(pEnv, zConverted);
          407  +  zTempUtf8 = win32UnicodeToUtf8(pEnv, zTempWide);
          408  +  lsmFree(pEnv, zTempWide);
          409  +  if( zTempUtf8 ){
          410  +    int nOut = *pnOut;
          411  +    int nLen = strlen(zTempUtf8) + 1;
          412  +    if( nLen>=nOut ){
          413  +      lsmFree(pEnv, zTempUtf8);
          414  +      return LSM_IOERR_BKPT;
          415  +    }
          416  +    snprintf(zOut, nOut, "%s", zTempUtf8);
          417  +    lsmFree(pEnv, zTempUtf8);
          418  +    *pnOut = nLen;
          419  +    return LSM_OK;
          420  +  }else{
          421  +    return LSM_NOMEM_BKPT;
          422  +  }
   344    423   }
   345    424   
   346    425   static int lsmWin32OsFileid(
   347    426     lsm_file *pFile,
   348    427     void *pBuf,
   349    428     int *pnBuf
   350    429   ){
................................................................................
   427    506     lsmFree(pWin32File->pEnv, pWin32File->apShm);
   428    507     lsmFree(pWin32File->pEnv, pWin32File);
   429    508     return rc;
   430    509   }
   431    510   
   432    511   static int lsmWin32OsSleep(lsm_env *pEnv, int us){
   433    512     unused_parameter(pEnv);
   434         -  Sleep((us + 999) / 1000);
   435         -  return LSM_OK;
          513  +  return win32Sleep(us);
   436    514   }
   437    515   
   438    516   /****************************************************************************
   439    517   ** Memory allocation routines.
   440    518   */
   441    519   
   442    520   static void *lsmWin32OsMalloc(lsm_env *pEnv, size_t N){