/ Check-in [12d0a885]
Login

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

Overview
Comment:Add the win32-longpath VFS allowing windows filenames up to 32K characters in length. Remove unused code when -DSQLITE_MAX_MMAP_SIZE=0. Fix some compiler warnings on windows.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 12d0a8859de0a9d823997cfeccc77bd572cb6d13
User & Date: drh 2013-08-28 18:18:12
Context
2013-08-28
19:01
Re-enable test file wild001.test. check-in: 4f182ddc user: dan tags: trunk
18:56
Merge updates from trunk. check-in: ffc6e682 user: mistachkin tags: toTypeFuncs
18:18
Add the win32-longpath VFS allowing windows filenames up to 32K characters in length. Remove unused code when -DSQLITE_MAX_MMAP_SIZE=0. Fix some compiler warnings on windows. check-in: 12d0a885 user: drh tags: trunk
18:06
Update list of supported Tcl shells to include 8.6. Remove stray OS2 references. Closed-Leaf check-in: ecd52ccf user: mistachkin tags: mmapDisabled
16:27
In the query optimizer, when converting BETWEEN and LIKE/GLOB expressions into simpler forms for processing, be sure to transfer the LEFT JOIN markings. Fix for ticket [bc878246eafe0f52c]. check-in: caab361e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to configure.

   870    870   SQLITE_THREADSAFE
   871    871   XTHREADCONNECT
   872    872   ALLOWRELEASE
   873    873   TEMP_STORE
   874    874   BUILD_EXEEXT
   875    875   SQLITE_OS_UNIX
   876    876   SQLITE_OS_WIN
   877         -SQLITE_OS_OS2
   878    877   TARGET_EXEEXT
   879    878   TCL_VERSION
   880    879   TCL_BIN_DIR
   881    880   TCL_SRC_DIR
   882    881   TCL_INCLUDE_SPEC
   883    882   TCL_LIB_FILE
   884    883   TCL_LIB_FLAG
................................................................................
  3729   3728   { $as_echo "$as_me:$LINENO: checking the name lister ($NM) interface" >&5
  3730   3729   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3731   3730   if test "${lt_cv_nm_interface+set}" = set; then
  3732   3731     $as_echo_n "(cached) " >&6
  3733   3732   else
  3734   3733     lt_cv_nm_interface="BSD nm"
  3735   3734     echo "int some_variable = 0;" > conftest.$ac_ext
  3736         -  (eval echo "\"\$as_me:3736: $ac_compile\"" >&5)
         3735  +  (eval echo "\"\$as_me:3735: $ac_compile\"" >&5)
  3737   3736     (eval "$ac_compile" 2>conftest.err)
  3738   3737     cat conftest.err >&5
  3739         -  (eval echo "\"\$as_me:3739: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3738  +  (eval echo "\"\$as_me:3738: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3740   3739     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3741   3740     cat conftest.err >&5
  3742         -  (eval echo "\"\$as_me:3742: output\"" >&5)
         3741  +  (eval echo "\"\$as_me:3741: output\"" >&5)
  3743   3742     cat conftest.out >&5
  3744   3743     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3745   3744       lt_cv_nm_interface="MS dumpbin"
  3746   3745     fi
  3747   3746     rm -f conftest*
  3748   3747   fi
  3749   3748   { $as_echo "$as_me:$LINENO: result: $lt_cv_nm_interface" >&5
................................................................................
  4957   4956   	;;
  4958   4957       esac
  4959   4958     fi
  4960   4959     rm -rf conftest*
  4961   4960     ;;
  4962   4961   *-*-irix6*)
  4963   4962     # Find out which ABI we are using.
  4964         -  echo '#line 4964 "configure"' > conftest.$ac_ext
         4963  +  echo '#line 4963 "configure"' > conftest.$ac_ext
  4965   4964     if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
  4966   4965     (eval $ac_compile) 2>&5
  4967   4966     ac_status=$?
  4968   4967     $as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
  4969   4968     (exit $ac_status); }; then
  4970   4969       if test "$lt_cv_prog_gnu_ld" = yes; then
  4971   4970         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6826   6825      # Note that $ac_compile itself does not contain backslashes and begins
  6827   6826      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6828   6827      # The option is referenced via a variable to avoid confusing sed.
  6829   6828      lt_compile=`echo "$ac_compile" | $SED \
  6830   6829      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6831   6830      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6832   6831      -e 's:$: $lt_compiler_flag:'`
  6833         -   (eval echo "\"\$as_me:6833: $lt_compile\"" >&5)
         6832  +   (eval echo "\"\$as_me:6832: $lt_compile\"" >&5)
  6834   6833      (eval "$lt_compile" 2>conftest.err)
  6835   6834      ac_status=$?
  6836   6835      cat conftest.err >&5
  6837         -   echo "$as_me:6837: \$? = $ac_status" >&5
         6836  +   echo "$as_me:6836: \$? = $ac_status" >&5
  6838   6837      if (exit $ac_status) && test -s "$ac_outfile"; then
  6839   6838        # The compiler can only warn and ignore the option if not recognized
  6840   6839        # So say no if there are warnings other than the usual output.
  6841   6840        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6842   6841        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6843   6842        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6844   6843          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  7165   7164      # Note that $ac_compile itself does not contain backslashes and begins
  7166   7165      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7167   7166      # The option is referenced via a variable to avoid confusing sed.
  7168   7167      lt_compile=`echo "$ac_compile" | $SED \
  7169   7168      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7170   7169      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7171   7170      -e 's:$: $lt_compiler_flag:'`
  7172         -   (eval echo "\"\$as_me:7172: $lt_compile\"" >&5)
         7171  +   (eval echo "\"\$as_me:7171: $lt_compile\"" >&5)
  7173   7172      (eval "$lt_compile" 2>conftest.err)
  7174   7173      ac_status=$?
  7175   7174      cat conftest.err >&5
  7176         -   echo "$as_me:7176: \$? = $ac_status" >&5
         7175  +   echo "$as_me:7175: \$? = $ac_status" >&5
  7177   7176      if (exit $ac_status) && test -s "$ac_outfile"; then
  7178   7177        # The compiler can only warn and ignore the option if not recognized
  7179   7178        # So say no if there are warnings other than the usual output.
  7180   7179        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7181   7180        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7182   7181        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7183   7182          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7270   7269      # (2) before a word containing "conftest.", or (3) at the end.
  7271   7270      # Note that $ac_compile itself does not contain backslashes and begins
  7272   7271      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7273   7272      lt_compile=`echo "$ac_compile" | $SED \
  7274   7273      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7275   7274      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7276   7275      -e 's:$: $lt_compiler_flag:'`
  7277         -   (eval echo "\"\$as_me:7277: $lt_compile\"" >&5)
         7276  +   (eval echo "\"\$as_me:7276: $lt_compile\"" >&5)
  7278   7277      (eval "$lt_compile" 2>out/conftest.err)
  7279   7278      ac_status=$?
  7280   7279      cat out/conftest.err >&5
  7281         -   echo "$as_me:7281: \$? = $ac_status" >&5
         7280  +   echo "$as_me:7280: \$? = $ac_status" >&5
  7282   7281      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7283   7282      then
  7284   7283        # The compiler can only warn and ignore the option if not recognized
  7285   7284        # So say no if there are warnings
  7286   7285        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7287   7286        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7288   7287        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7325   7324      # (2) before a word containing "conftest.", or (3) at the end.
  7326   7325      # Note that $ac_compile itself does not contain backslashes and begins
  7327   7326      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7328   7327      lt_compile=`echo "$ac_compile" | $SED \
  7329   7328      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7330   7329      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7331   7330      -e 's:$: $lt_compiler_flag:'`
  7332         -   (eval echo "\"\$as_me:7332: $lt_compile\"" >&5)
         7331  +   (eval echo "\"\$as_me:7331: $lt_compile\"" >&5)
  7333   7332      (eval "$lt_compile" 2>out/conftest.err)
  7334   7333      ac_status=$?
  7335   7334      cat out/conftest.err >&5
  7336         -   echo "$as_me:7336: \$? = $ac_status" >&5
         7335  +   echo "$as_me:7335: \$? = $ac_status" >&5
  7337   7336      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7338   7337      then
  7339   7338        # The compiler can only warn and ignore the option if not recognized
  7340   7339        # So say no if there are warnings
  7341   7340        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7342   7341        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7343   7342        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
 10138  10137   else
 10139  10138     	  if test "$cross_compiling" = yes; then :
 10140  10139     lt_cv_dlopen_self=cross
 10141  10140   else
 10142  10141     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10143  10142     lt_status=$lt_dlunknown
 10144  10143     cat > conftest.$ac_ext <<_LT_EOF
 10145         -#line 10145 "configure"
        10144  +#line 10144 "configure"
 10146  10145   #include "confdefs.h"
 10147  10146   
 10148  10147   #if HAVE_DLFCN_H
 10149  10148   #include <dlfcn.h>
 10150  10149   #endif
 10151  10150   
 10152  10151   #include <stdio.h>
................................................................................
 10234  10233   else
 10235  10234     	  if test "$cross_compiling" = yes; then :
 10236  10235     lt_cv_dlopen_self_static=cross
 10237  10236   else
 10238  10237     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
 10239  10238     lt_status=$lt_dlunknown
 10240  10239     cat > conftest.$ac_ext <<_LT_EOF
 10241         -#line 10241 "configure"
        10240  +#line 10240 "configure"
 10242  10241   #include "confdefs.h"
 10243  10242   
 10244  10243   #if HAVE_DLFCN_H
 10245  10244   #include <dlfcn.h>
 10246  10245   #endif
 10247  10246   
 10248  10247   #include <stdio.h>
................................................................................
 12253  12252   USE_AMALGAMATION=1
 12254  12253   
 12255  12254   #########
 12256  12255   # See whether we can run specific tclsh versions known to work well;
 12257  12256   # if not, then we fall back to plain tclsh.
 12258  12257   # TODO: try other versions before falling back?
 12259  12258   #
 12260         -for ac_prog in tclsh8.5 tclsh
        12259  +for ac_prog in tclsh8.6 tclsh8.5 tclsh
 12261  12260   do
 12262  12261     # Extract the first word of "$ac_prog", so it can be a program name with args.
 12263  12262   set dummy $ac_prog; ac_word=$2
 12264  12263   { $as_echo "$as_me:$LINENO: checking for $ac_word" >&5
 12265  12264   $as_echo_n "checking for $ac_word... " >&6; }
 12266  12265   if test "${ac_cv_prog_TCLSH_CMD+set}" = set; then
 12267  12266     $as_echo_n "(cached) " >&6
................................................................................
 12709  12708   fi
 12710  12709   if test x"$cross_compiling" = xno; then
 12711  12710     TARGET_EXEEXT=$BUILD_EXEEXT
 12712  12711   else
 12713  12712     TARGET_EXEEXT=$config_TARGET_EXEEXT
 12714  12713   fi
 12715  12714   if test "$TARGET_EXEEXT" = ".exe"; then
 12716         -  if test $OS2_SHELL ; then
 12717         -    SQLITE_OS_UNIX=0
 12718         -    SQLITE_OS_WIN=0
 12719         -    SQLITE_OS_OS2=1
 12720         -    CFLAGS="$CFLAGS -DSQLITE_OS_OS2=1"
 12721         -  else
 12722         -    SQLITE_OS_UNIX=0
 12723         -    SQLITE_OS_WIN=1
 12724         -    SQLITE_OS_OS2=0
 12725         -    CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1"
 12726         -  fi
        12715  +  SQLITE_OS_UNIX=0
        12716  +  SQLITE_OS_WIN=1
        12717  +  CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1"
 12727  12718   else
 12728  12719     SQLITE_OS_UNIX=1
 12729  12720     SQLITE_OS_WIN=0
 12730         -  SQLITE_OS_OS2=0
 12731  12721     CFLAGS="$CFLAGS -DSQLITE_OS_UNIX=1"
 12732  12722   fi
 12733         -
 12734  12723   
 12735  12724   
 12736  12725   
 12737  12726   
 12738  12727   
 12739  12728   
 12740  12729   ##########

Changes to configure.ac.

   135    135   USE_AMALGAMATION=1
   136    136   
   137    137   #########
   138    138   # See whether we can run specific tclsh versions known to work well;
   139    139   # if not, then we fall back to plain tclsh.
   140    140   # TODO: try other versions before falling back?
   141    141   # 
   142         -AC_CHECK_PROGS(TCLSH_CMD, [tclsh8.5 tclsh], none)
          142  +AC_CHECK_PROGS(TCLSH_CMD, [tclsh8.6 tclsh8.5 tclsh], none)
   143    143   if test "$TCLSH_CMD" = "none"; then
   144    144     # If we can't find a local tclsh, then building the amalgamation will fail.
   145    145     # We act as though --disable-amalgamation has been used.
   146    146     echo "Warning: can't find tclsh - defaulting to non-amalgamation build."
   147    147     USE_AMALGAMATION=0
   148    148     TCLSH_CMD="tclsh"
   149    149   fi
................................................................................
   336    336   fi
   337    337   if test x"$cross_compiling" = xno; then
   338    338     TARGET_EXEEXT=$BUILD_EXEEXT
   339    339   else
   340    340     TARGET_EXEEXT=$config_TARGET_EXEEXT
   341    341   fi
   342    342   if test "$TARGET_EXEEXT" = ".exe"; then
   343         -  if test $OS2_SHELL ; then
   344         -    SQLITE_OS_UNIX=0
   345         -    SQLITE_OS_WIN=0
   346         -    SQLITE_OS_OS2=1
   347         -    CFLAGS="$CFLAGS -DSQLITE_OS_OS2=1"
   348         -  else
   349         -    SQLITE_OS_UNIX=0
   350         -    SQLITE_OS_WIN=1
   351         -    SQLITE_OS_OS2=0
   352         -    CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1"
   353         -  fi
          343  +  SQLITE_OS_UNIX=0
          344  +  SQLITE_OS_WIN=1
          345  +  CFLAGS="$CFLAGS -DSQLITE_OS_WIN=1"
   354    346   else
   355    347     SQLITE_OS_UNIX=1
   356    348     SQLITE_OS_WIN=0
   357         -  SQLITE_OS_OS2=0
   358    349     CFLAGS="$CFLAGS -DSQLITE_OS_UNIX=1"
   359    350   fi
   360    351   
   361    352   AC_SUBST(BUILD_EXEEXT)
   362    353   AC_SUBST(SQLITE_OS_UNIX)
   363    354   AC_SUBST(SQLITE_OS_WIN)
   364         -AC_SUBST(SQLITE_OS_OS2)
   365    355   AC_SUBST(TARGET_EXEEXT)
   366    356   
   367    357   ##########
   368    358   # Figure out all the parameters needed to compile against Tcl.
   369    359   #
   370    360   # This code is derived from the SC_PATH_TCLCONFIG and SC_LOAD_TCLCONFIG
   371    361   # macros in the in the tcl.m4 file of the standard TCL distribution.

Changes to src/expr.c.

  1274   1274       case TK_UPLUS: {
  1275   1275         rc = sqlite3ExprIsInteger(p->pLeft, pValue);
  1276   1276         break;
  1277   1277       }
  1278   1278       case TK_UMINUS: {
  1279   1279         int v;
  1280   1280         if( sqlite3ExprIsInteger(p->pLeft, &v) ){
  1281         -        assert( v!=-2147483648 );
         1281  +        assert( v!=(-2147483647-1) );
  1282   1282           *pValue = -v;
  1283   1283           rc = 1;
  1284   1284         }
  1285   1285         break;
  1286   1286       }
  1287   1287       default: break;
  1288   1288     }

Changes to src/mem2.c.

   175    175   */
   176    176   static int sqlite3MemSize(void *p){
   177    177     struct MemBlockHdr *pHdr;
   178    178     if( !p ){
   179    179       return 0;
   180    180     }
   181    181     pHdr = sqlite3MemsysGetHeader(p);
   182         -  return pHdr->iSize;
          182  +  return (int)pHdr->iSize;
   183    183   }
   184    184   
   185    185   /*
   186    186   ** Initialize the memory allocation subsystem.
   187    187   */
   188    188   static int sqlite3MemInit(void *NotUsed){
   189    189     UNUSED_PARAMETER(NotUsed);
................................................................................
   217    217   ** to clear the content of a freed allocation to unpredictable values.
   218    218   */
   219    219   static void randomFill(char *pBuf, int nByte){
   220    220     unsigned int x, y, r;
   221    221     x = SQLITE_PTR_TO_INT(pBuf);
   222    222     y = nByte | 1;
   223    223     while( nByte >= 4 ){
   224         -    x = (x>>1) ^ (-(x&1) & 0xd0000001);
          224  +    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
   225    225       y = y*1103515245 + 12345;
   226    226       r = x ^ y;
   227    227       *(int*)pBuf = r;
   228    228       pBuf += 4;
   229    229       nByte -= 4;
   230    230     }
   231    231     while( nByte-- > 0 ){
   232         -    x = (x>>1) ^ (-(x&1) & 0xd0000001);
          232  +    x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
   233    233       y = y*1103515245 + 12345;
   234    234       r = x ^ y;
   235    235       *(pBuf++) = r & 0xff;
   236    236     }
   237    237   }
   238    238   
   239    239   /*
................................................................................
   320    320       pHdr->pNext->pPrev = pHdr->pPrev;
   321    321     }else{
   322    322       assert( mem.pLast==pHdr );
   323    323       mem.pLast = pHdr->pPrev;
   324    324     }
   325    325     z = (char*)pBt;
   326    326     z -= pHdr->nTitle;
   327         -  adjustStats(pHdr->iSize, -1);
          327  +  adjustStats((int)pHdr->iSize, -1);
   328    328     randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
   329         -                pHdr->iSize + sizeof(int) + pHdr->nTitle);
          329  +                (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
   330    330     free(z);
   331    331     sqlite3_mutex_leave(mem.mutex);  
   332    332   }
   333    333   
   334    334   /*
   335    335   ** Change the size of an existing memory allocation.
   336    336   **
................................................................................
   346    346     assert( mem.disallow==0 );
   347    347     assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
   348    348     pOldHdr = sqlite3MemsysGetHeader(pPrior);
   349    349     pNew = sqlite3MemMalloc(nByte);
   350    350     if( pNew ){
   351    351       memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
   352    352       if( nByte>pOldHdr->iSize ){
   353         -      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
          353  +      randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
   354    354       }
   355    355       sqlite3MemFree(pPrior);
   356    356     }
   357    357     return pNew;
   358    358   }
   359    359   
   360    360   /*
................................................................................
   461    461   }
   462    462   
   463    463   void sqlite3MemdebugSync(){
   464    464     struct MemBlockHdr *pHdr;
   465    465     for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   466    466       void **pBt = (void**)pHdr;
   467    467       pBt -= pHdr->nBacktraceSlots;
   468         -    mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
          468  +    mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
   469    469     }
   470    470   }
   471    471   
   472    472   /*
   473    473   ** Open the file indicated and write a log of all unfreed memory 
   474    474   ** allocations into that log.
   475    475   */

Changes to src/mutex_w32.c.

    65     65         OSVERSIONINFO sInfo;
    66     66         sInfo.dwOSVersionInfoSize = sizeof(sInfo);
    67     67         GetVersionEx(&sInfo);
    68     68         osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
    69     69       }
    70     70       return osType==2;
    71     71     }
    72         -#endif /* SQLITE_OS_WINCE */
           72  +#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
    73     73   #endif
    74     74   
    75     75   #ifdef SQLITE_DEBUG
    76     76   /*
    77     77   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
    78     78   ** intended for use only inside assert() statements.
    79     79   */
................................................................................
   103    103   };
   104    104   static int winMutex_isInit = 0;
   105    105   /* As winMutexInit() and winMutexEnd() are called as part
   106    106   ** of the sqlite3_initialize and sqlite3_shutdown()
   107    107   ** processing, the "interlocked" magic is probably not
   108    108   ** strictly necessary.
   109    109   */
   110         -static long winMutex_lock = 0;
          110  +static LONG winMutex_lock = 0;
   111    111   
   112    112   void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
   113    113   
   114    114   static int winMutexInit(void){ 
   115    115     /* The first to increment to 1 does actual initialization */
   116    116     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
   117    117       int i;

Changes to src/os_unix.c.

   214    214     unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
   215    215     int lastErrno;                      /* The unix errno from last I/O error */
   216    216     void *lockingContext;               /* Locking style specific state */
   217    217     UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
   218    218     const char *zPath;                  /* Name of the file */
   219    219     unixShm *pShm;                      /* Shared memory segment information */
   220    220     int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
          221  +#if SQLITE_MAX_MMAP_SIZE>0
   221    222     int nFetchOut;                      /* Number of outstanding xFetch refs */
   222    223     sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
   223    224     sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
   224    225     sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
   225    226     void *pMapRegion;                   /* Memory mapped region */
          227  +#endif
   226    228   #ifdef __QNXNTO__
   227    229     int sectorSize;                     /* Device sector size */
   228    230     int deviceCharacteristics;          /* Precomputed device characteristics */
   229    231   #endif
   230    232   #if SQLITE_ENABLE_LOCKING_STYLE
   231    233     int openFlags;                      /* The flags specified at open() */
   232    234   #endif
................................................................................
  1869   1871   ** the requested locking level, this routine is a no-op.
  1870   1872   */
  1871   1873   static int unixUnlock(sqlite3_file *id, int eFileLock){
  1872   1874     assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
  1873   1875     return posixUnlock(id, eFileLock, 0);
  1874   1876   }
  1875   1877   
         1878  +#if SQLITE_MAX_MMAP_SIZE>0
  1876   1879   static int unixMapfile(unixFile *pFd, i64 nByte);
  1877   1880   static void unixUnmapfile(unixFile *pFd);
         1881  +#endif
  1878   1882   
  1879   1883   /*
  1880   1884   ** This function performs the parts of the "close file" operation 
  1881   1885   ** common to all locking schemes. It closes the directory and file
  1882   1886   ** handles, if they are valid, and sets all fields of the unixFile
  1883   1887   ** structure to 0.
  1884   1888   **
  1885   1889   ** It is *not* necessary to hold the mutex when this routine is called,
  1886   1890   ** even on VxWorks.  A mutex will be acquired on VxWorks by the
  1887   1891   ** vxworksReleaseFileId() routine.
  1888   1892   */
  1889   1893   static int closeUnixFile(sqlite3_file *id){
  1890   1894     unixFile *pFile = (unixFile*)id;
         1895  +#if SQLITE_MAX_MMAP_SIZE>0
  1891   1896     unixUnmapfile(pFile);
         1897  +#endif
  1892   1898     if( pFile->h>=0 ){
  1893   1899       robust_close(pFile, pFile->h, __LINE__);
  1894   1900       pFile->h = -1;
  1895   1901     }
  1896   1902   #if OS_VXWORKS
  1897   1903     if( pFile->pId ){
  1898   1904       if( pFile->ctrlFlags & UNIXFILE_DELETE ){
................................................................................
  3588   3594       ** source.
  3589   3595       */
  3590   3596       if( pFile->inNormalWrite && nByte==0 ){
  3591   3597         pFile->transCntrChng = 1;
  3592   3598       }
  3593   3599   #endif
  3594   3600   
         3601  +#if SQLITE_MAX_MMAP_SIZE>0
  3595   3602       /* If the file was just truncated to a size smaller than the currently
  3596   3603       ** mapped region, reduce the effective mapping size as well. SQLite will
  3597   3604       ** use read() and write() to access data beyond this point from now on.  
  3598   3605       */
  3599   3606       if( nByte<pFile->mmapSize ){
  3600   3607         pFile->mmapSize = nByte;
  3601   3608       }
         3609  +#endif
  3602   3610   
  3603   3611       return SQLITE_OK;
  3604   3612     }
  3605   3613   }
  3606   3614   
  3607   3615   /*
  3608   3616   ** Determine the current size of a file in bytes
................................................................................
  3684   3692           if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
  3685   3693           iWrite += nBlk;
  3686   3694         }
  3687   3695   #endif
  3688   3696       }
  3689   3697     }
  3690   3698   
         3699  +#if SQLITE_MAX_MMAP_SIZE>0
  3691   3700     if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
  3692   3701       int rc;
  3693   3702       if( pFile->szChunk<=0 ){
  3694   3703         if( robust_ftruncate(pFile->h, nByte) ){
  3695   3704           pFile->lastErrno = errno;
  3696   3705           return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
  3697   3706         }
  3698   3707       }
  3699   3708   
  3700   3709       rc = unixMapfile(pFile, nByte);
  3701   3710       return rc;
  3702   3711     }
         3712  +#endif
  3703   3713   
  3704   3714     return SQLITE_OK;
  3705   3715   }
  3706   3716   
  3707   3717   /*
  3708   3718   ** If *pArg is inititially negative then this is a query.  Set *pArg to
  3709   3719   ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
................................................................................
  3764   3774         char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
  3765   3775         if( zTFile ){
  3766   3776           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
  3767   3777           *(char**)pArg = zTFile;
  3768   3778         }
  3769   3779         return SQLITE_OK;
  3770   3780       }
         3781  +#if SQLITE_MAX_MMAP_SIZE>0
  3771   3782       case SQLITE_FCNTL_MMAP_SIZE: {
  3772   3783         i64 newLimit = *(i64*)pArg;
  3773   3784         int rc = SQLITE_OK;
  3774   3785         if( newLimit>sqlite3GlobalConfig.mxMmap ){
  3775   3786           newLimit = sqlite3GlobalConfig.mxMmap;
  3776   3787         }
  3777   3788         *(i64*)pArg = pFile->mmapSizeMax;
................................................................................
  3780   3791           if( pFile->mmapSize>0 ){
  3781   3792             unixUnmapfile(pFile);
  3782   3793             rc = unixMapfile(pFile, -1);
  3783   3794           }
  3784   3795         }
  3785   3796         return rc;
  3786   3797       }
         3798  +#endif
  3787   3799   #ifdef SQLITE_DEBUG
  3788   3800       /* The pager calls this method to signal that it has done
  3789   3801       ** a rollback and that the database is therefore unchanged and
  3790   3802       ** it hence it is OK for the transaction change counter to be
  3791   3803       ** unchanged.
  3792   3804       */
  3793   3805       case SQLITE_FCNTL_DB_UNCHANGED: {
................................................................................
  4590   4602   #else
  4591   4603   # define unixShmMap     0
  4592   4604   # define unixShmLock    0
  4593   4605   # define unixShmBarrier 0
  4594   4606   # define unixShmUnmap   0
  4595   4607   #endif /* #ifndef SQLITE_OMIT_WAL */
  4596   4608   
         4609  +#if SQLITE_MAX_MMAP_SIZE>0
  4597   4610   /*
  4598   4611   ** If it is currently memory mapped, unmap file pFd.
  4599   4612   */
  4600   4613   static void unixUnmapfile(unixFile *pFd){
  4601   4614     assert( pFd->nFetchOut==0 );
  4602         -#if SQLITE_MAX_MMAP_SIZE>0
  4603   4615     if( pFd->pMapRegion ){
  4604   4616       osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
  4605   4617       pFd->pMapRegion = 0;
  4606   4618       pFd->mmapSize = 0;
  4607   4619       pFd->mmapSizeActual = 0;
  4608   4620     }
  4609         -#endif
  4610   4621   }
  4611   4622   
  4612         -#if SQLITE_MAX_MMAP_SIZE>0
  4613   4623   /*
  4614   4624   ** Return the system page size.
  4615   4625   */
  4616   4626   static int unixGetPagesize(void){
  4617   4627   #if HAVE_MREMAP
  4618   4628     return 512;
  4619   4629   #elif defined(_BSD_SOURCE)
  4620   4630     return getpagesize();
  4621   4631   #else
  4622   4632     return (int)sysconf(_SC_PAGESIZE);
  4623   4633   #endif
  4624   4634   }
  4625         -#endif /* SQLITE_MAX_MMAP_SIZE>0 */
  4626   4635   
  4627         -#if SQLITE_MAX_MMAP_SIZE>0
  4628   4636   /*
  4629   4637   ** Attempt to set the size of the memory mapping maintained by file 
  4630   4638   ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
  4631   4639   **
  4632   4640   ** If successful, this function sets the following variables:
  4633   4641   **
  4634   4642   **       unixFile.pMapRegion
................................................................................
  4705   4713       ** will probably fail too. Fall back to using xRead/xWrite exclusively
  4706   4714       ** in this case.  */
  4707   4715       pFd->mmapSizeMax = 0;
  4708   4716     }
  4709   4717     pFd->pMapRegion = (void *)pNew;
  4710   4718     pFd->mmapSize = pFd->mmapSizeActual = nNew;
  4711   4719   }
  4712         -#endif
  4713   4720   
  4714   4721   /*
  4715   4722   ** Memory map or remap the file opened by file-descriptor pFd (if the file
  4716   4723   ** is already mapped, the existing mapping is replaced by the new). Or, if 
  4717   4724   ** there already exists a mapping for this file, and there are still 
  4718   4725   ** outstanding xFetch() references to it, this function is a no-op.
  4719   4726   **
................................................................................
  4724   4731   ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
  4725   4732   **
  4726   4733   ** SQLITE_OK is returned if no error occurs (even if the mapping is not
  4727   4734   ** recreated as a result of outstanding references) or an SQLite error
  4728   4735   ** code otherwise.
  4729   4736   */
  4730   4737   static int unixMapfile(unixFile *pFd, i64 nByte){
  4731         -#if SQLITE_MAX_MMAP_SIZE>0
  4732   4738     i64 nMap = nByte;
  4733   4739     int rc;
  4734   4740   
  4735   4741     assert( nMap>=0 || pFd->nFetchOut==0 );
  4736   4742     if( pFd->nFetchOut>0 ) return SQLITE_OK;
  4737   4743   
  4738   4744     if( nMap<0 ){
................................................................................
  4750   4756     if( nMap!=pFd->mmapSize ){
  4751   4757       if( nMap>0 ){
  4752   4758         unixRemapfile(pFd, nMap);
  4753   4759       }else{
  4754   4760         unixUnmapfile(pFd);
  4755   4761       }
  4756   4762     }
  4757         -#endif
  4758   4763   
  4759   4764     return SQLITE_OK;
  4760   4765   }
         4766  +#endif /* SQLITE_MAX_MMAP_SIZE>0 */
  4761   4767   
  4762   4768   /*
  4763   4769   ** If possible, return a pointer to a mapping of file fd starting at offset
  4764   4770   ** iOff. The mapping must be valid for at least nAmt bytes.
  4765   4771   **
  4766   4772   ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
  4767   4773   ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
................................................................................
  4802   4808   ** to inform the VFS layer that, according to POSIX, any existing mapping 
  4803   4809   ** may now be invalid and should be unmapped.
  4804   4810   */
  4805   4811   static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  4806   4812     unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  4807   4813     UNUSED_PARAMETER(iOff);
  4808   4814   
         4815  +#if SQLITE_MAX_MMAP_SIZE>0
  4809   4816     /* If p==0 (unmap the entire file) then there must be no outstanding 
  4810   4817     ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
  4811   4818     ** then there must be at least one outstanding.  */
  4812   4819     assert( (p==0)==(pFd->nFetchOut==0) );
  4813   4820   
  4814   4821     /* If p!=0, it must match the iOff value. */
  4815   4822     assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
................................................................................
  4817   4824     if( p ){
  4818   4825       pFd->nFetchOut--;
  4819   4826     }else{
  4820   4827       unixUnmapfile(pFd);
  4821   4828     }
  4822   4829   
  4823   4830     assert( pFd->nFetchOut>=0 );
         4831  +#endif
  4824   4832     return SQLITE_OK;
  4825   4833   }
  4826   4834   
  4827   4835   /*
  4828   4836   ** Here ends the implementation of all sqlite3_file methods.
  4829   4837   **
  4830   4838   ********************** End sqlite3_file Methods *******************************
................................................................................
  5148   5156     assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
  5149   5157   
  5150   5158     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
  5151   5159     pNew->h = h;
  5152   5160     pNew->pVfs = pVfs;
  5153   5161     pNew->zPath = zFilename;
  5154   5162     pNew->ctrlFlags = (u8)ctrlFlags;
         5163  +#if SQLITE_MAX_MMAP_SIZE>0
  5155   5164     pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
         5165  +#endif
  5156   5166     if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
  5157   5167                              "psow", SQLITE_POWERSAFE_OVERWRITE) ){
  5158   5168       pNew->ctrlFlags |= UNIXFILE_PSOW;
  5159   5169     }
  5160   5170     if( strcmp(pVfs->zName,"unix-excl")==0 ){
  5161   5171       pNew->ctrlFlags |= UNIXFILE_EXCL;
  5162   5172     }

Changes to src/os_win.c.

    26     26   #include "os_common.h"
    27     27   
    28     28   /*
    29     29   ** Compiling and using WAL mode requires several APIs that are only
    30     30   ** available in Windows platforms based on the NT kernel.
    31     31   */
    32     32   #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
    33         -# error "WAL mode requires support from the Windows NT kernel, compile\
           33  +#  error "WAL mode requires support from the Windows NT kernel, compile\
    34     34    with SQLITE_OMIT_WAL."
    35     35   #endif
    36     36   
    37     37   /*
    38     38   ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
    39     39   ** based on the sub-platform)?
    40     40   */
................................................................................
    46     46   ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
    47     47   ** based on the sub-platform)?
    48     48   */
    49     49   #if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
    50     50   #  define SQLITE_WIN32_HAS_WIDE
    51     51   #endif
    52     52   
           53  +/*
           54  +** Maximum pathname length (in chars) for Win32.  This should normally be
           55  +** MAX_PATH.
           56  +*/
           57  +#ifndef SQLITE_WIN32_MAX_PATH_CHARS
           58  +#  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
           59  +#endif
           60  +
           61  +/*
           62  +** Maximum pathname length (in chars) for WinNT.  This should normally be
           63  +** 32767.
           64  +*/
           65  +#ifndef SQLITE_WINNT_MAX_PATH_CHARS
           66  +#  define SQLITE_WINNT_MAX_PATH_CHARS   (32767)
           67  +#endif
           68  +
           69  +/*
           70  +** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
           71  +** characters, so we allocate 3 bytes per character assuming worst-case of
           72  +** 4-bytes-per-character for UTF8.
           73  +*/
           74  +#ifndef SQLITE_WIN32_MAX_PATH_BYTES
           75  +#  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
           76  +#endif
           77  +
           78  +/*
           79  +** Maximum pathname length (in bytes) for WinNT.  This should normally be
           80  +** 32767 * sizeof(WCHAR).
           81  +*/
           82  +#ifndef SQLITE_WINNT_MAX_PATH_BYTES
           83  +#  define SQLITE_WINNT_MAX_PATH_BYTES   \
           84  +                            (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
           85  +#endif
           86  +
           87  +/*
           88  +** Maximum error message length (in chars) for WinRT.
           89  +*/
           90  +#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
           91  +#  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
           92  +#endif
           93  +
           94  +/*
           95  +** Returns non-zero if the character should be treated as a directory
           96  +** separator.
           97  +*/
           98  +#ifndef winIsDirSep
           99  +#  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
          100  +#endif
          101  +
          102  +/*
          103  +** Returns the string that should be used as the directory separator.
          104  +*/
          105  +#ifndef winGetDirDep
          106  +#  ifdef __CYGWIN__
          107  +#    define winGetDirDep()              "/"
          108  +#  else
          109  +#    define winGetDirDep()              "\\"
          110  +#  endif
          111  +#endif
          112  +
    53    113   /*
    54    114   ** Do we need to manually define the Win32 file mapping APIs for use with WAL
    55    115   ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
    56    116   ** are not present in the header file)?
    57    117   */
    58    118   #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
    59    119   /*
................................................................................
  1053   1113   ** the LockFileEx() API.  But we can still statically link against that
  1054   1114   ** API as long as we don't call it when running Win95/98/ME.  A call to
  1055   1115   ** this routine is used to determine if the host is Win95/98/ME or
  1056   1116   ** WinNT/2K/XP so that we will know whether or not we can safely call
  1057   1117   ** the LockFileEx() API.
  1058   1118   */
  1059   1119   #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
  1060         -# define isNT()  (1)
         1120  +# define osIsNT()  (1)
  1061   1121   #elif !defined(SQLITE_WIN32_HAS_WIDE)
  1062         -# define isNT()  (0)
         1122  +# define osIsNT()  (0)
  1063   1123   #else
  1064         -  static int isNT(void){
         1124  +  static int osIsNT(void){
  1065   1125       if( sqlite3_os_type==0 ){
  1066   1126         OSVERSIONINFOA sInfo;
  1067   1127         sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1068   1128         osGetVersionExA(&sInfo);
  1069   1129         sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
  1070   1130       }
  1071   1131       return sqlite3_os_type==2;
................................................................................
  1268   1328   #endif /* SQLITE_WIN32_MALLOC */
  1269   1329   
  1270   1330   /*
  1271   1331   ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
  1272   1332   **
  1273   1333   ** Space to hold the returned string is obtained from malloc.
  1274   1334   */
  1275         -static LPWSTR utf8ToUnicode(const char *zFilename){
         1335  +static LPWSTR winUtf8ToUnicode(const char *zFilename){
  1276   1336     int nChar;
  1277   1337     LPWSTR zWideFilename;
  1278   1338   
  1279   1339     nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
  1280   1340     if( nChar==0 ){
  1281   1341       return 0;
  1282   1342     }
................................................................................
  1293   1353     return zWideFilename;
  1294   1354   }
  1295   1355   
  1296   1356   /*
  1297   1357   ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
  1298   1358   ** obtained from sqlite3_malloc().
  1299   1359   */
  1300         -static char *unicodeToUtf8(LPCWSTR zWideFilename){
         1360  +static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
  1301   1361     int nByte;
  1302   1362     char *zFilename;
  1303   1363   
  1304   1364     nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
  1305   1365     if( nByte == 0 ){
  1306   1366       return 0;
  1307   1367     }
................................................................................
  1321   1381   /*
  1322   1382   ** Convert an ANSI string to Microsoft Unicode, based on the
  1323   1383   ** current codepage settings for file apis.
  1324   1384   ** 
  1325   1385   ** Space to hold the returned string is obtained
  1326   1386   ** from sqlite3_malloc.
  1327   1387   */
  1328         -static LPWSTR mbcsToUnicode(const char *zFilename){
         1388  +static LPWSTR winMbcsToUnicode(const char *zFilename){
  1329   1389     int nByte;
  1330   1390     LPWSTR zMbcsFilename;
  1331   1391     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1332   1392   
  1333   1393     nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
  1334   1394                                   0)*sizeof(WCHAR);
  1335   1395     if( nByte==0 ){
................................................................................
  1351   1411   /*
  1352   1412   ** Convert Microsoft Unicode to multi-byte character string, based on the
  1353   1413   ** user's ANSI codepage.
  1354   1414   **
  1355   1415   ** Space to hold the returned string is obtained from
  1356   1416   ** sqlite3_malloc().
  1357   1417   */
  1358         -static char *unicodeToMbcs(LPCWSTR zWideFilename){
         1418  +static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
  1359   1419     int nByte;
  1360   1420     char *zFilename;
  1361   1421     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1362   1422   
  1363   1423     nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
  1364   1424     if( nByte == 0 ){
  1365   1425       return 0;
................................................................................
  1381   1441   ** Convert multibyte character string to UTF-8.  Space to hold the
  1382   1442   ** returned string is obtained from sqlite3_malloc().
  1383   1443   */
  1384   1444   char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
  1385   1445     char *zFilenameUtf8;
  1386   1446     LPWSTR zTmpWide;
  1387   1447   
  1388         -  zTmpWide = mbcsToUnicode(zFilename);
         1448  +  zTmpWide = winMbcsToUnicode(zFilename);
  1389   1449     if( zTmpWide==0 ){
  1390   1450       return 0;
  1391   1451     }
  1392         -  zFilenameUtf8 = unicodeToUtf8(zTmpWide);
         1452  +  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
  1393   1453     sqlite3_free(zTmpWide);
  1394   1454     return zFilenameUtf8;
  1395   1455   }
  1396   1456   
  1397   1457   /*
  1398   1458   ** Convert UTF-8 to multibyte character string.  Space to hold the 
  1399   1459   ** returned string is obtained from sqlite3_malloc().
  1400   1460   */
  1401   1461   char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1402   1462     char *zFilenameMbcs;
  1403   1463     LPWSTR zTmpWide;
  1404   1464   
  1405         -  zTmpWide = utf8ToUnicode(zFilename);
         1465  +  zTmpWide = winUtf8ToUnicode(zFilename);
  1406   1466     if( zTmpWide==0 ){
  1407   1467       return 0;
  1408   1468     }
  1409         -  zFilenameMbcs = unicodeToMbcs(zTmpWide);
         1469  +  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
  1410   1470     sqlite3_free(zTmpWide);
  1411   1471     return zFilenameMbcs;
  1412   1472   }
  1413   1473   
  1414   1474   /*
  1415   1475   ** This function sets the data directory or the temporary directory based on
  1416   1476   ** the provided arguments.  The type argument must be 1 in order to set the
................................................................................
  1432   1492     assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
  1433   1493             || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
  1434   1494     );
  1435   1495     assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
  1436   1496     if( ppDirectory ){
  1437   1497       char *zValueUtf8 = 0;
  1438   1498       if( zValue && zValue[0] ){
  1439         -      zValueUtf8 = unicodeToUtf8(zValue);
         1499  +      zValueUtf8 = winUnicodeToUtf8(zValue);
  1440   1500         if ( zValueUtf8==0 ){
  1441   1501           return SQLITE_NOMEM;
  1442   1502         }
  1443   1503       }
  1444   1504       sqlite3_free(*ppDirectory);
  1445   1505       *ppDirectory = zValueUtf8;
  1446   1506       return SQLITE_OK;
  1447   1507     }
  1448   1508     return SQLITE_ERROR;
  1449   1509   }
  1450   1510   
  1451   1511   /*
  1452         -** The return value of getLastErrorMsg
         1512  +** The return value of winGetLastErrorMsg
  1453   1513   ** is zero if the error message fits in the buffer, or non-zero
  1454   1514   ** otherwise (if the message was truncated).
  1455   1515   */
  1456         -static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
         1516  +static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
  1457   1517     /* FormatMessage returns 0 on failure.  Otherwise it
  1458   1518     ** returns the number of TCHARs written to the output
  1459   1519     ** buffer, excluding the terminating null char.
  1460   1520     */
  1461   1521     DWORD dwLen = 0;
  1462   1522     char *zOut = 0;
  1463   1523   
  1464         -  if( isNT() ){
         1524  +  if( osIsNT() ){
  1465   1525   #if SQLITE_OS_WINRT
  1466         -    WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
         1526  +    WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
  1467   1527       dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
  1468   1528                                FORMAT_MESSAGE_IGNORE_INSERTS,
  1469   1529                                NULL,
  1470   1530                                lastErrno,
  1471   1531                                0,
  1472   1532                                zTempWide,
  1473         -                             MAX_PATH,
         1533  +                             SQLITE_WIN32_MAX_ERRMSG_CHARS,
  1474   1534                                0);
  1475   1535   #else
  1476   1536       LPWSTR zTempWide = NULL;
  1477   1537       dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1478   1538                                FORMAT_MESSAGE_FROM_SYSTEM |
  1479   1539                                FORMAT_MESSAGE_IGNORE_INSERTS,
  1480   1540                                NULL,
................................................................................
  1483   1543                                (LPWSTR) &zTempWide,
  1484   1544                                0,
  1485   1545                                0);
  1486   1546   #endif
  1487   1547       if( dwLen > 0 ){
  1488   1548         /* allocate a buffer and convert to UTF8 */
  1489   1549         sqlite3BeginBenignMalloc();
  1490         -      zOut = unicodeToUtf8(zTempWide);
         1550  +      zOut = winUnicodeToUtf8(zTempWide);
  1491   1551         sqlite3EndBenignMalloc();
  1492   1552   #if !SQLITE_OS_WINRT
  1493   1553         /* free the system buffer allocated by FormatMessage */
  1494   1554         osLocalFree(zTempWide);
  1495   1555   #endif
  1496   1556       }
  1497   1557     }
................................................................................
  1551   1611     const char *zPath,              /* File path associated with error */
  1552   1612     int iLine                       /* Source line number where error occurred */
  1553   1613   ){
  1554   1614     char zMsg[500];                 /* Human readable error text */
  1555   1615     int i;                          /* Loop counter */
  1556   1616   
  1557   1617     zMsg[0] = 0;
  1558         -  getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
         1618  +  winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
  1559   1619     assert( errcode!=SQLITE_OK );
  1560   1620     if( zPath==0 ) zPath = "";
  1561   1621     for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
  1562   1622     zMsg[i] = 0;
  1563   1623     sqlite3_log(errcode,
  1564   1624         "os_win.c:%d: (%lu) %s(%s) - %s",
  1565   1625         iLine, lastErrno, zFunc, zPath, zMsg
................................................................................
  1576   1636   */
  1577   1637   #ifndef SQLITE_WIN32_IOERR_RETRY
  1578   1638   # define SQLITE_WIN32_IOERR_RETRY 10
  1579   1639   #endif
  1580   1640   #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
  1581   1641   # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
  1582   1642   #endif
  1583         -static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
  1584         -static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
         1643  +static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
         1644  +static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
  1585   1645   
  1586   1646   /*
  1587   1647   ** If a ReadFile() or WriteFile() error occurs, invoke this routine
  1588   1648   ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
  1589   1649   ** to give up with an error.
  1590   1650   */
  1591         -static int retryIoerr(int *pnRetry, DWORD *pError){
         1651  +static int winRetryIoerr(int *pnRetry, DWORD *pError){
  1592   1652     DWORD e = osGetLastError();
  1593         -  if( *pnRetry>=win32IoerrRetry ){
         1653  +  if( *pnRetry>=winIoerrRetry ){
  1594   1654       if( pError ){
  1595   1655         *pError = e;
  1596   1656       }
  1597   1657       return 0;
  1598   1658     }
  1599   1659     if( e==ERROR_ACCESS_DENIED ||
  1600   1660         e==ERROR_LOCK_VIOLATION ||
  1601   1661         e==ERROR_SHARING_VIOLATION ){
  1602         -    sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
         1662  +    sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
  1603   1663       ++*pnRetry;
  1604   1664       return 1;
  1605   1665     }
  1606   1666     if( pError ){
  1607   1667       *pError = e;
  1608   1668     }
  1609   1669     return 0;
  1610   1670   }
  1611   1671   
  1612   1672   /*
  1613   1673   ** Log a I/O error retry episode.
  1614   1674   */
  1615         -static void logIoerr(int nRetry){
         1675  +static void winLogIoerr(int nRetry){
  1616   1676     if( nRetry ){
  1617   1677       sqlite3_log(SQLITE_IOERR, 
  1618   1678         "delayed %dms for lock/sharing conflict",
  1619         -      win32IoerrRetryDelay*nRetry*(nRetry+1)/2
         1679  +      winIoerrRetryDelay*nRetry*(nRetry+1)/2
  1620   1680       );
  1621   1681     }
  1622   1682   }
  1623   1683   
  1624   1684   #if SQLITE_OS_WINCE
  1625   1685   /*************************************************************************
  1626   1686   ** This section contains code for WinCE only.
................................................................................
  1677   1737   static int winceCreateLock(const char *zFilename, winFile *pFile){
  1678   1738     LPWSTR zTok;
  1679   1739     LPWSTR zName;
  1680   1740     DWORD lastErrno;
  1681   1741     BOOL bLogged = FALSE;
  1682   1742     BOOL bInit = TRUE;
  1683   1743   
  1684         -  zName = utf8ToUnicode(zFilename);
         1744  +  zName = winUtf8ToUnicode(zFilename);
  1685   1745     if( zName==0 ){
  1686   1746       /* out of memory */
  1687   1747       return SQLITE_IOERR_NOMEM;
  1688   1748     }
  1689   1749   
  1690   1750     /* Initialize the local lockdata */
  1691   1751     memset(&pFile->local, 0, sizeof(pFile->local));
................................................................................
  1950   2010     /*
  1951   2011     ** NOTE: Windows CE is handled differently here due its lack of the Win32
  1952   2012     **       API LockFile.
  1953   2013     */
  1954   2014     return winceLockFile(phFile, offsetLow, offsetHigh,
  1955   2015                          numBytesLow, numBytesHigh);
  1956   2016   #else
  1957         -  if( isNT() ){
         2017  +  if( osIsNT() ){
  1958   2018       OVERLAPPED ovlp;
  1959   2019       memset(&ovlp, 0, sizeof(OVERLAPPED));
  1960   2020       ovlp.Offset = offsetLow;
  1961   2021       ovlp.OffsetHigh = offsetHigh;
  1962   2022       return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
  1963   2023     }else{
  1964   2024       return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
................................................................................
  1981   2041     /*
  1982   2042     ** NOTE: Windows CE is handled differently here due its lack of the Win32
  1983   2043     **       API UnlockFile.
  1984   2044     */
  1985   2045     return winceUnlockFile(phFile, offsetLow, offsetHigh,
  1986   2046                            numBytesLow, numBytesHigh);
  1987   2047   #else
  1988         -  if( isNT() ){
         2048  +  if( osIsNT() ){
  1989   2049       OVERLAPPED ovlp;
  1990   2050       memset(&ovlp, 0, sizeof(OVERLAPPED));
  1991   2051       ovlp.Offset = offsetLow;
  1992   2052       ovlp.OffsetHigh = offsetHigh;
  1993   2053       return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
  1994   2054     }else{
  1995   2055       return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
................................................................................
  2011   2071   #endif
  2012   2072   
  2013   2073   /*
  2014   2074   ** Move the current position of the file handle passed as the first 
  2015   2075   ** argument to offset iOffset within the file. If successful, return 0. 
  2016   2076   ** Otherwise, set pFile->lastErrno and return non-zero.
  2017   2077   */
  2018         -static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
         2078  +static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
  2019   2079   #if !SQLITE_OS_WINRT
  2020   2080     LONG upperBits;                 /* Most sig. 32 bits of new offset */
  2021   2081     LONG lowerBits;                 /* Least sig. 32 bits of new offset */
  2022   2082     DWORD dwRet;                    /* Value returned by SetFilePointer() */
  2023   2083     DWORD lastErrno;                /* Value returned by GetLastError() */
  2024   2084   
  2025   2085     OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
................................................................................
  2036   2096     */
  2037   2097     dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
  2038   2098   
  2039   2099     if( (dwRet==INVALID_SET_FILE_POINTER
  2040   2100         && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
  2041   2101       pFile->lastErrno = lastErrno;
  2042   2102       winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
  2043         -             "seekWinFile", pFile->zPath);
         2103  +             "winSeekFile", pFile->zPath);
  2044   2104       OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
  2045   2105       return 1;
  2046   2106     }
  2047   2107   
  2048   2108     OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
  2049   2109     return 0;
  2050   2110   #else
................................................................................
  2057   2117   
  2058   2118     x.QuadPart = iOffset;
  2059   2119     bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
  2060   2120   
  2061   2121     if(!bRet){
  2062   2122       pFile->lastErrno = osGetLastError();
  2063   2123       winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
  2064         -             "seekWinFile", pFile->zPath);
         2124  +             "winSeekFile", pFile->zPath);
  2065   2125       OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
  2066   2126       return 1;
  2067   2127     }
  2068   2128   
  2069   2129     OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
  2070   2130     return 0;
  2071   2131   #endif
................................................................................
  2172   2232         amt -= nCopy;
  2173   2233         offset += nCopy;
  2174   2234       }
  2175   2235     }
  2176   2236   #endif
  2177   2237   
  2178   2238   #if SQLITE_OS_WINCE
  2179         -  if( seekWinFile(pFile, offset) ){
         2239  +  if( winSeekFile(pFile, offset) ){
  2180   2240       OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
  2181   2241       return SQLITE_FULL;
  2182   2242     }
  2183   2243     while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
  2184   2244   #else
  2185   2245     memset(&overlapped, 0, sizeof(OVERLAPPED));
  2186   2246     overlapped.Offset = (LONG)(offset & 0xffffffff);
  2187   2247     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
  2188   2248     while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
  2189   2249            osGetLastError()!=ERROR_HANDLE_EOF ){
  2190   2250   #endif
  2191   2251       DWORD lastErrno;
  2192         -    if( retryIoerr(&nRetry, &lastErrno) ) continue;
         2252  +    if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
  2193   2253       pFile->lastErrno = lastErrno;
  2194   2254       OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
  2195   2255       return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
  2196   2256                "winRead", pFile->zPath);
  2197   2257     }
  2198         -  logIoerr(nRetry);
         2258  +  winLogIoerr(nRetry);
  2199   2259     if( nRead<(DWORD)amt ){
  2200   2260       /* Unread parts of the buffer must be zero-filled */
  2201   2261       memset(&((char*)pBuf)[nRead], 0, amt-nRead);
  2202   2262       OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
  2203   2263       return SQLITE_IOERR_SHORT_READ;
  2204   2264     }
  2205   2265   
................................................................................
  2244   2304         amt -= nCopy;
  2245   2305         offset += nCopy;
  2246   2306       }
  2247   2307     }
  2248   2308   #endif
  2249   2309   
  2250   2310   #if SQLITE_OS_WINCE
  2251         -  rc = seekWinFile(pFile, offset);
         2311  +  rc = winSeekFile(pFile, offset);
  2252   2312     if( rc==0 ){
  2253   2313   #else
  2254   2314     {
  2255   2315   #endif
  2256   2316   #if !SQLITE_OS_WINCE
  2257   2317       OVERLAPPED overlapped;        /* The offset for WriteFile. */
  2258   2318   #endif
................................................................................
  2269   2329   
  2270   2330       while( nRem>0 ){
  2271   2331   #if SQLITE_OS_WINCE
  2272   2332         if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
  2273   2333   #else
  2274   2334         if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
  2275   2335   #endif
  2276         -        if( retryIoerr(&nRetry, &lastErrno) ) continue;
         2336  +        if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
  2277   2337           break;
  2278   2338         }
  2279   2339         assert( nWrite==0 || nWrite<=(DWORD)nRem );
  2280   2340         if( nWrite==0 || nWrite>(DWORD)nRem ){
  2281   2341           lastErrno = osGetLastError();
  2282   2342           break;
  2283   2343         }
................................................................................
  2301   2361         OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
  2302   2362         return SQLITE_FULL;
  2303   2363       }
  2304   2364       OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
  2305   2365       return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
  2306   2366                "winWrite", pFile->zPath);
  2307   2367     }else{
  2308         -    logIoerr(nRetry);
         2368  +    winLogIoerr(nRetry);
  2309   2369     }
  2310   2370     OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
  2311   2371     return SQLITE_OK;
  2312   2372   }
  2313   2373   
  2314   2374   /*
  2315   2375   ** Truncate an open file to a specified size
................................................................................
  2330   2390     ** size).
  2331   2391     */
  2332   2392     if( pFile->szChunk>0 ){
  2333   2393       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  2334   2394     }
  2335   2395   
  2336   2396     /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
  2337         -  if( seekWinFile(pFile, nByte) ){
         2397  +  if( winSeekFile(pFile, nByte) ){
  2338   2398       rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
  2339   2399                        "winTruncate1", pFile->zPath);
  2340   2400     }else if( 0==osSetEndOfFile(pFile->h) &&
  2341   2401               ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
  2342   2402       pFile->lastErrno = lastErrno;
  2343   2403       rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
  2344   2404                        "winTruncate2", pFile->zPath);
................................................................................
  2411   2471     sqlite3_sync_count++;
  2412   2472   #endif
  2413   2473   
  2414   2474     /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
  2415   2475     ** no-op
  2416   2476     */
  2417   2477   #ifdef SQLITE_NO_SYNC
         2478  +  OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
  2418   2479     return SQLITE_OK;
  2419   2480   #else
  2420   2481     rc = osFlushFileBuffers(pFile->h);
  2421   2482     SimulateIOError( rc=FALSE );
  2422   2483     if( rc ){
  2423   2484       OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
  2424   2485       return SQLITE_OK;
................................................................................
  2508   2569   #endif
  2509   2570   
  2510   2571   /*
  2511   2572   ** Acquire a reader lock.
  2512   2573   ** Different API routines are called depending on whether or not this
  2513   2574   ** is Win9x or WinNT.
  2514   2575   */
  2515         -static int getReadLock(winFile *pFile){
         2576  +static int winGetReadLock(winFile *pFile){
  2516   2577     int res;
  2517   2578     OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
  2518         -  if( isNT() ){
         2579  +  if( osIsNT() ){
  2519   2580   #if SQLITE_OS_WINCE
  2520   2581       /*
  2521   2582       ** NOTE: Windows CE is handled differently here due its lack of the Win32
  2522   2583       **       API LockFileEx.
  2523   2584       */
  2524   2585       res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
  2525   2586   #else
................................................................................
  2543   2604     OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  2544   2605     return res;
  2545   2606   }
  2546   2607   
  2547   2608   /*
  2548   2609   ** Undo a readlock
  2549   2610   */
  2550         -static int unlockReadLock(winFile *pFile){
         2611  +static int winUnlockReadLock(winFile *pFile){
  2551   2612     int res;
  2552   2613     DWORD lastErrno;
  2553   2614     OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
  2554         -  if( isNT() ){
         2615  +  if( osIsNT() ){
  2555   2616       res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
  2556   2617     }
  2557   2618   #ifdef SQLITE_WIN32_HAS_ANSI
  2558   2619     else{
  2559   2620       res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
  2560   2621     }
  2561   2622   #endif
  2562   2623     if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
  2563   2624       pFile->lastErrno = lastErrno;
  2564   2625       winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
  2565         -             "unlockReadLock", pFile->zPath);
         2626  +             "winUnlockReadLock", pFile->zPath);
  2566   2627     }
  2567   2628     OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
  2568   2629     return res;
  2569   2630   }
  2570   2631   
  2571   2632   /*
  2572   2633   ** Lock the file with the lock specified by parameter locktype - one
................................................................................
  2649   2710       }
  2650   2711     }
  2651   2712   
  2652   2713     /* Acquire a shared lock
  2653   2714     */
  2654   2715     if( locktype==SHARED_LOCK && res ){
  2655   2716       assert( pFile->locktype==NO_LOCK );
  2656         -    res = getReadLock(pFile);
         2717  +    res = winGetReadLock(pFile);
  2657   2718       if( res ){
  2658   2719         newLocktype = SHARED_LOCK;
  2659   2720       }else{
  2660   2721         lastErrno = osGetLastError();
  2661   2722       }
  2662   2723     }
  2663   2724   
................................................................................
  2680   2741       gotPendingLock = 0;
  2681   2742     }
  2682   2743   
  2683   2744     /* Acquire an EXCLUSIVE lock
  2684   2745     */
  2685   2746     if( locktype==EXCLUSIVE_LOCK && res ){
  2686   2747       assert( pFile->locktype>=SHARED_LOCK );
  2687         -    res = unlockReadLock(pFile);
         2748  +    res = winUnlockReadLock(pFile);
  2688   2749       res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
  2689   2750                         SHARED_SIZE, 0);
  2690   2751       if( res ){
  2691   2752         newLocktype = EXCLUSIVE_LOCK;
  2692   2753       }else{
  2693   2754         lastErrno = osGetLastError();
  2694         -      getReadLock(pFile);
         2755  +      winGetReadLock(pFile);
  2695   2756       }
  2696   2757     }
  2697   2758   
  2698   2759     /* If we are holding a PENDING lock that ought to be released, then
  2699   2760     ** release it now.
  2700   2761     */
  2701   2762     if( gotPendingLock && locktype==SHARED_LOCK ){
................................................................................
  2704   2765   
  2705   2766     /* Update the state of the lock has held in the file descriptor then
  2706   2767     ** return the appropriate result code.
  2707   2768     */
  2708   2769     if( res ){
  2709   2770       rc = SQLITE_OK;
  2710   2771     }else{
         2772  +    pFile->lastErrno = lastErrno;
         2773  +    rc = SQLITE_BUSY;
  2711   2774       OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
  2712   2775                pFile->h, locktype, newLocktype));
  2713         -    pFile->lastErrno = lastErrno;
  2714         -    rc = SQLITE_BUSY;
  2715   2776     }
  2716   2777     pFile->locktype = (u8)newLocktype;
  2717   2778     OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
  2718   2779              pFile->h, pFile->locktype, sqlite3ErrName(rc)));
  2719   2780     return rc;
  2720   2781   }
  2721   2782   
................................................................................
  2767   2828     assert( pFile!=0 );
  2768   2829     assert( locktype<=SHARED_LOCK );
  2769   2830     OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
  2770   2831              pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
  2771   2832     type = pFile->locktype;
  2772   2833     if( type>=EXCLUSIVE_LOCK ){
  2773   2834       winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
  2774         -    if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
         2835  +    if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
  2775   2836         /* This should never happen.  We should always be able to
  2776   2837         ** reacquire the read lock */
  2777   2838         rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
  2778   2839                  "winUnlock", pFile->zPath);
  2779   2840       }
  2780   2841     }
  2781   2842     if( type>=RESERVED_LOCK ){
  2782   2843       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
  2783   2844     }
  2784   2845     if( locktype==NO_LOCK && type>=SHARED_LOCK ){
  2785         -    unlockReadLock(pFile);
         2846  +    winUnlockReadLock(pFile);
  2786   2847     }
  2787   2848     if( type>=PENDING_LOCK ){
  2788   2849       winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
  2789   2850     }
  2790   2851     pFile->locktype = (u8)locktype;
  2791   2852     OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
  2792   2853              pFile->h, pFile->locktype, sqlite3ErrName(rc)));
................................................................................
  2806   2867       pFile->ctrlFlags &= ~mask;
  2807   2868     }else{
  2808   2869       pFile->ctrlFlags |= mask;
  2809   2870     }
  2810   2871   }
  2811   2872   
  2812   2873   /* Forward declaration */
  2813         -static int getTempname(int nBuf, char *zBuf);
         2874  +static int winGetTempname(sqlite3_vfs *, char **);
  2814   2875   #if SQLITE_MAX_MMAP_SIZE>0
  2815   2876   static int winMapfile(winFile*, sqlite3_int64);
  2816   2877   #endif
  2817   2878   
  2818   2879   /*
  2819   2880   ** Control and query of the open file handle.
  2820   2881   */
................................................................................
  2869   2930         *(char**)pArg = sqlite3_mprintf("win32");
  2870   2931         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  2871   2932         return SQLITE_OK;
  2872   2933       }
  2873   2934       case SQLITE_FCNTL_WIN32_AV_RETRY: {
  2874   2935         int *a = (int*)pArg;
  2875   2936         if( a[0]>0 ){
  2876         -        win32IoerrRetry = a[0];
         2937  +        winIoerrRetry = a[0];
  2877   2938         }else{
  2878         -        a[0] = win32IoerrRetry;
         2939  +        a[0] = winIoerrRetry;
  2879   2940         }
  2880   2941         if( a[1]>0 ){
  2881         -        win32IoerrRetryDelay = a[1];
         2942  +        winIoerrRetryDelay = a[1];
  2882   2943         }else{
  2883         -        a[1] = win32IoerrRetryDelay;
         2944  +        a[1] = winIoerrRetryDelay;
  2884   2945         }
  2885   2946         OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  2886   2947         return SQLITE_OK;
  2887   2948       }
  2888   2949       case SQLITE_FCNTL_TEMPFILENAME: {
  2889         -      char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
  2890         -      if( zTFile ){
  2891         -        getTempname(pFile->pVfs->mxPathname, zTFile);
         2950  +      char *zTFile = 0;
         2951  +      int rc = winGetTempname(pFile->pVfs, &zTFile);
         2952  +      if( rc==SQLITE_OK ){
  2892   2953           *(char**)pArg = zTFile;
  2893   2954         }
  2894         -      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
  2895         -      return SQLITE_OK;
         2955  +      OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
         2956  +      return rc;
  2896   2957       }
  2897   2958   #if SQLITE_MAX_MMAP_SIZE>0
  2898   2959       case SQLITE_FCNTL_MMAP_SIZE: {
  2899   2960         i64 newLimit = *(i64*)pArg;
  2900   2961         int rc = SQLITE_OK;
  2901   2962         if( newLimit>sqlite3GlobalConfig.mxMmap ){
  2902   2963           newLimit = sqlite3GlobalConfig.mxMmap;
................................................................................
  3850   3911   
  3851   3912   /*
  3852   3913   ** Convert a UTF-8 filename into whatever form the underlying
  3853   3914   ** operating system wants filenames in.  Space to hold the result
  3854   3915   ** is obtained from malloc and must be freed by the calling
  3855   3916   ** function.
  3856   3917   */
  3857         -static void *convertUtf8Filename(const char *zFilename){
         3918  +static void *winConvertUtf8Filename(const char *zFilename){
  3858   3919     void *zConverted = 0;
  3859         -  if( isNT() ){
  3860         -    zConverted = utf8ToUnicode(zFilename);
         3920  +  if( osIsNT() ){
         3921  +    zConverted = winUtf8ToUnicode(zFilename);
  3861   3922     }
  3862   3923   #ifdef SQLITE_WIN32_HAS_ANSI
  3863   3924     else{
  3864   3925       zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
  3865   3926     }
  3866   3927   #endif
  3867   3928     /* caller will handle out of memory */
  3868   3929     return zConverted;
  3869   3930   }
  3870   3931   
  3871   3932   /*
  3872         -** Maximum pathname length (in bytes) for windows.  The MAX_PATH macro is
  3873         -** in characters, so we allocate 3 bytes per character assuming worst-case
  3874         -** 3-bytes-per-character UTF8.
         3933  +** This function returns non-zero if the specified UTF-8 string buffer
         3934  +** ends with a directory separator character.
  3875   3935   */
  3876         -#ifndef SQLITE_WIN32_MAX_PATH
  3877         -#  define SQLITE_WIN32_MAX_PATH   (MAX_PATH*3)
  3878         -#endif
         3936  +static int winEndsInDirSep(char *zBuf){
         3937  +  if( zBuf ){
         3938  +    int nLen = sqlite3Strlen30(zBuf);
         3939  +    return nLen>0 && winIsDirSep(zBuf[nLen-1]);
         3940  +  }
         3941  +  return 0;
         3942  +}
  3879   3943   
  3880   3944   /*
  3881         -** Create a temporary file name in zBuf.  zBuf must be big enough to
  3882         -** hold at pVfs->mxPathname characters.
         3945  +** Create a temporary file name and store the resulting pointer into pzBuf.
         3946  +** The pointer returned in pzBuf must be freed via sqlite3_free().
  3883   3947   */
  3884         -static int getTempname(int nBuf, char *zBuf){
         3948  +static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
  3885   3949     static char zChars[] =
  3886   3950       "abcdefghijklmnopqrstuvwxyz"
  3887   3951       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  3888   3952       "0123456789";
  3889   3953     size_t i, j;
  3890         -  int nTempPath;
  3891         -  char zTempPath[SQLITE_WIN32_MAX_PATH+2];
         3954  +  int nBuf, nLen;
         3955  +  char *zBuf;
  3892   3956   
  3893   3957     /* It's odd to simulate an io-error here, but really this is just
  3894   3958     ** using the io-error infrastructure to test that SQLite handles this
  3895   3959     ** function failing. 
  3896   3960     */
  3897   3961     SimulateIOError( return SQLITE_IOERR );
  3898   3962   
         3963  +  /* Allocate a temporary buffer to store the fully qualified file
         3964  +  ** name for the temporary file.  If this fails, we cannot continue.
         3965  +  */
         3966  +  nBuf = pVfs->mxPathname;
         3967  +  zBuf = sqlite3MallocZero( nBuf+2 );
         3968  +  if( !zBuf ){
         3969  +    OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
         3970  +    return SQLITE_IOERR_NOMEM;
         3971  +  }
         3972  +
         3973  +  /* Figure out the effective temporary directory.  First, check if one
         3974  +  ** has been explicitly set by the application; otherwise, use the one
         3975  +  ** configured by the operating system.
         3976  +  */
         3977  +  assert( nBuf>30 );
  3899   3978     if( sqlite3_temp_directory ){
  3900         -    sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
  3901         -                     sqlite3_temp_directory);
         3979  +    sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
         3980  +                     winEndsInDirSep(sqlite3_temp_directory) ? "" :
         3981  +                     winGetDirDep());
  3902   3982     }
  3903   3983   #if !SQLITE_OS_WINRT
  3904         -  else if( isNT() ){
         3984  +  else if( osIsNT() ){
  3905   3985       char *zMulti;
  3906         -    WCHAR zWidePath[MAX_PATH];
  3907         -    if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
         3986  +    LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
         3987  +    if( !zWidePath ){
         3988  +      sqlite3_free(zBuf);
         3989  +      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
         3990  +      return SQLITE_IOERR_NOMEM;
         3991  +    }
         3992  +    if( osGetTempPathW(nBuf, zWidePath)==0 ){
         3993  +      sqlite3_free(zWidePath);
         3994  +      sqlite3_free(zBuf);
  3908   3995         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
  3909   3996         return SQLITE_IOERR_GETTEMPPATH;
  3910   3997       }
  3911         -    zMulti = unicodeToUtf8(zWidePath);
         3998  +    zMulti = winUnicodeToUtf8(zWidePath);
  3912   3999       if( zMulti ){
  3913         -      sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
         4000  +      sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
  3914   4001         sqlite3_free(zMulti);
         4002  +      sqlite3_free(zWidePath);
  3915   4003       }else{
         4004  +      sqlite3_free(zWidePath);
         4005  +      sqlite3_free(zBuf);
  3916   4006         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  3917   4007         return SQLITE_IOERR_NOMEM;
  3918   4008       }
  3919   4009     }
  3920   4010   #ifdef SQLITE_WIN32_HAS_ANSI
  3921   4011     else{
  3922   4012       char *zUtf8;
  3923         -    char zMbcsPath[SQLITE_WIN32_MAX_PATH];
  3924         -    if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
         4013  +    char *zMbcsPath = sqlite3MallocZero( nBuf );
         4014  +    if( !zMbcsPath ){
         4015  +      sqlite3_free(zBuf);
         4016  +      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
         4017  +      return SQLITE_IOERR_NOMEM;
         4018  +    }
         4019  +    if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
         4020  +      sqlite3_free(zBuf);
  3925   4021         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
  3926   4022         return SQLITE_IOERR_GETTEMPPATH;
  3927   4023       }
  3928   4024       zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  3929   4025       if( zUtf8 ){
  3930         -      sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
         4026  +      sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
  3931   4027         sqlite3_free(zUtf8);
  3932   4028       }else{
         4029  +      sqlite3_free(zBuf);
  3933   4030         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  3934   4031         return SQLITE_IOERR_NOMEM;
  3935   4032       }
  3936   4033     }
  3937         -#else
  3938         -  else{
  3939         -    /*
  3940         -    ** Compiled without ANSI support and the current operating system
  3941         -    ** is not Windows NT; therefore, just zero the temporary buffer.
  3942         -    */
  3943         -    memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
  3944         -  }
  3945   4034   #endif /* SQLITE_WIN32_HAS_ANSI */
  3946         -#else
  3947         -  else{
  3948         -    /*
  3949         -    ** Compiled for WinRT and the sqlite3_temp_directory is not set;
  3950         -    ** therefore, just zero the temporary buffer.
  3951         -    */
  3952         -    memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
  3953         -  }
  3954   4035   #endif /* !SQLITE_OS_WINRT */
  3955   4036   
  3956   4037     /* Check that the output buffer is large enough for the temporary file 
  3957   4038     ** name. If it is not, return SQLITE_ERROR.
  3958   4039     */
  3959         -  nTempPath = sqlite3Strlen30(zTempPath);
         4040  +  nLen = sqlite3Strlen30(zBuf);
  3960   4041   
  3961         -  if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
         4042  +  if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
         4043  +    sqlite3_free(zBuf);
  3962   4044       OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
  3963   4045       return SQLITE_ERROR;
  3964   4046     }
  3965   4047   
  3966         -  for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
  3967         -  zTempPath[i] = 0;
         4048  +  sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
  3968   4049   
  3969         -  sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
  3970         -                       "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
  3971         -                   zTempPath);
  3972   4050     j = sqlite3Strlen30(zBuf);
  3973   4051     sqlite3_randomness(15, &zBuf[j]);
  3974   4052     for(i=0; i<15; i++, j++){
  3975   4053       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
  3976   4054     }
  3977   4055     zBuf[j] = 0;
  3978   4056     zBuf[j+1] = 0;
         4057  +  *pzBuf = zBuf;
  3979   4058   
  3980   4059     OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
  3981   4060     return SQLITE_OK;
  3982   4061   }
  3983   4062   
  3984   4063   /*
  3985   4064   ** Return TRUE if the named file is really a directory.  Return false if
................................................................................
  3987   4066   ** allocation failure.
  3988   4067   */
  3989   4068   static int winIsDir(const void *zConverted){
  3990   4069     DWORD attr;
  3991   4070     int rc = 0;
  3992   4071     DWORD lastErrno;
  3993   4072   
  3994         -  if( isNT() ){
         4073  +  if( osIsNT() ){
  3995   4074       int cnt = 0;
  3996   4075       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  3997   4076       memset(&sAttrData, 0, sizeof(sAttrData));
  3998   4077       while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  3999   4078                                GetFileExInfoStandard,
  4000         -                             &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
         4079  +                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
  4001   4080       if( !rc ){
  4002   4081         return 0; /* Invalid name? */
  4003   4082       }
  4004   4083       attr = sAttrData.dwFileAttributes;
  4005   4084   #if SQLITE_OS_WINCE==0
  4006   4085     }else{
  4007   4086       attr = osGetFileAttributesA((char*)zConverted);
................................................................................
  4010   4089     return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
  4011   4090   }
  4012   4091   
  4013   4092   /*
  4014   4093   ** Open a file.
  4015   4094   */
  4016   4095   static int winOpen(
  4017         -  sqlite3_vfs *pVfs,        /* Not used */
         4096  +  sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
  4018   4097     const char *zName,        /* Name of the file (UTF-8) */
  4019   4098     sqlite3_file *id,         /* Write the SQLite file handle here */
  4020   4099     int flags,                /* Open mode flags */
  4021   4100     int *pOutFlags            /* Status return flags */
  4022   4101   ){
  4023   4102     HANDLE h;
  4024   4103     DWORD lastErrno;
................................................................................
  4033   4112     void *zConverted;              /* Filename in OS encoding */
  4034   4113     const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
  4035   4114     int cnt = 0;
  4036   4115   
  4037   4116     /* If argument zPath is a NULL pointer, this function is required to open
  4038   4117     ** a temporary file. Use this buffer to store the file name in.
  4039   4118     */
  4040         -  char zTmpname[SQLITE_WIN32_MAX_PATH+2];     /* Buffer used to create temp filename */
         4119  +  char *zTmpname = 0; /* For temporary filename, if necessary. */
  4041   4120   
  4042   4121     int rc = SQLITE_OK;            /* Function Return Code */
  4043   4122   #if !defined(NDEBUG) || SQLITE_OS_WINCE
  4044   4123     int eType = flags&0xFFFFFF00;  /* Type of file to open */
  4045   4124   #endif
  4046   4125   
  4047   4126     int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
................................................................................
  4088   4167     );
  4089   4168   
  4090   4169     assert( pFile!=0 );
  4091   4170     memset(pFile, 0, sizeof(winFile));
  4092   4171     pFile->h = INVALID_HANDLE_VALUE;
  4093   4172   
  4094   4173   #if SQLITE_OS_WINRT
  4095         -  if( !sqlite3_temp_directory ){
         4174  +  if( !zUtf8Name && !sqlite3_temp_directory ){
  4096   4175       sqlite3_log(SQLITE_ERROR,
  4097   4176           "sqlite3_temp_directory variable should be set for WinRT");
  4098   4177     }
  4099   4178   #endif
  4100   4179   
  4101   4180     /* If the second argument to this function is NULL, generate a 
  4102   4181     ** temporary file name to use 
  4103   4182     */
  4104   4183     if( !zUtf8Name ){
  4105         -    assert(isDelete && !isOpenJournal);
  4106         -    rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
         4184  +    assert( isDelete && !isOpenJournal );
         4185  +    rc = winGetTempname(pVfs, &zTmpname);
  4107   4186       if( rc!=SQLITE_OK ){
  4108   4187         OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
  4109   4188         return rc;
  4110   4189       }
  4111   4190       zUtf8Name = zTmpname;
  4112   4191     }
  4113   4192   
  4114   4193     /* Database filenames are double-zero terminated if they are not
  4115   4194     ** URIs with parameters.  Hence, they can always be passed into
  4116   4195     ** sqlite3_uri_parameter().
  4117   4196     */
  4118   4197     assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
  4119         -        zUtf8Name[strlen(zUtf8Name)+1]==0 );
         4198  +       zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
  4120   4199   
  4121   4200     /* Convert the filename to the system encoding. */
  4122         -  zConverted = convertUtf8Filename(zUtf8Name);
         4201  +  zConverted = winConvertUtf8Filename(zUtf8Name);
  4123   4202     if( zConverted==0 ){
         4203  +    sqlite3_free(zTmpname);
  4124   4204       OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
  4125   4205       return SQLITE_IOERR_NOMEM;
  4126   4206     }
  4127   4207   
  4128   4208     if( winIsDir(zConverted) ){
  4129   4209       sqlite3_free(zConverted);
         4210  +    sqlite3_free(zTmpname);
  4130   4211       OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
  4131   4212       return SQLITE_CANTOPEN_ISDIR;
  4132   4213     }
  4133   4214   
  4134   4215     if( isReadWrite ){
  4135   4216       dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
  4136   4217     }else{
................................................................................
  4169   4250     }
  4170   4251     /* Reports from the internet are that performance is always
  4171   4252     ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
  4172   4253   #if SQLITE_OS_WINCE
  4173   4254     dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
  4174   4255   #endif
  4175   4256   
  4176         -  if( isNT() ){
         4257  +  if( osIsNT() ){
  4177   4258   #if SQLITE_OS_WINRT
  4178   4259       CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
  4179   4260       extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
  4180   4261       extendedParameters.dwFileAttributes =
  4181   4262               dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
  4182   4263       extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
  4183   4264       extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
................................................................................
  4184   4265       extendedParameters.lpSecurityAttributes = NULL;
  4185   4266       extendedParameters.hTemplateFile = NULL;
  4186   4267       while( (h = osCreateFile2((LPCWSTR)zConverted,
  4187   4268                                 dwDesiredAccess,
  4188   4269                                 dwShareMode,
  4189   4270                                 dwCreationDisposition,
  4190   4271                                 &extendedParameters))==INVALID_HANDLE_VALUE &&
  4191         -                              retryIoerr(&cnt, &lastErrno) ){
         4272  +                              winRetryIoerr(&cnt, &lastErrno) ){
  4192   4273                  /* Noop */
  4193   4274       }
  4194   4275   #else
  4195   4276       while( (h = osCreateFileW((LPCWSTR)zConverted,
  4196   4277                                 dwDesiredAccess,
  4197   4278                                 dwShareMode, NULL,
  4198   4279                                 dwCreationDisposition,
  4199   4280                                 dwFlagsAndAttributes,
  4200   4281                                 NULL))==INVALID_HANDLE_VALUE &&
  4201         -                              retryIoerr(&cnt, &lastErrno) ){
         4282  +                              winRetryIoerr(&cnt, &lastErrno) ){
  4202   4283                  /* Noop */
  4203   4284       }
  4204   4285   #endif
  4205   4286     }
  4206   4287   #ifdef SQLITE_WIN32_HAS_ANSI
  4207   4288     else{
  4208   4289       while( (h = osCreateFileA((LPCSTR)zConverted,
  4209   4290                                 dwDesiredAccess,
  4210   4291                                 dwShareMode, NULL,
  4211   4292                                 dwCreationDisposition,
  4212   4293                                 dwFlagsAndAttributes,
  4213   4294                                 NULL))==INVALID_HANDLE_VALUE &&
  4214         -                              retryIoerr(&cnt, &lastErrno) ){
         4295  +                              winRetryIoerr(&cnt, &lastErrno) ){
  4215   4296                  /* Noop */
  4216   4297       }
  4217   4298     }
  4218   4299   #endif
  4219         -  logIoerr(cnt);
         4300  +  winLogIoerr(cnt);
  4220   4301   
  4221   4302     OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
  4222   4303              dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
  4223   4304   
  4224   4305     if( h==INVALID_HANDLE_VALUE ){
  4225   4306       pFile->lastErrno = lastErrno;
  4226   4307       winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
  4227   4308       sqlite3_free(zConverted);
         4309  +    sqlite3_free(zTmpname);
  4228   4310       if( isReadWrite && !isExclusive ){
  4229   4311         return winOpen(pVfs, zName, id, 
  4230   4312            ((flags|SQLITE_OPEN_READONLY) &
  4231   4313                        ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
  4232   4314            pOutFlags);
  4233   4315       }else{
  4234   4316         return SQLITE_CANTOPEN_BKPT;
................................................................................
  4249   4331   
  4250   4332   #if SQLITE_OS_WINCE
  4251   4333     if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
  4252   4334          && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
  4253   4335     ){
  4254   4336       osCloseHandle(h);
  4255   4337       sqlite3_free(zConverted);
         4338  +    sqlite3_free(zTmpname);
  4256   4339       OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
  4257   4340       return rc;
  4258   4341     }
  4259   4342     if( isTemp ){
  4260   4343       pFile->zDeleteOnClose = zConverted;
  4261   4344     }else
  4262   4345   #endif
  4263   4346     {
  4264   4347       sqlite3_free(zConverted);
         4348  +    sqlite3_free(zTmpname);
  4265   4349     }
  4266   4350   
  4267   4351     pFile->pMethod = &winIoMethod;
  4268   4352     pFile->pVfs = pVfs;
  4269   4353     pFile->h = h;
  4270   4354     if( isReadonly ){
  4271   4355       pFile->ctrlFlags |= WINFILE_RDONLY;
................................................................................
  4311   4395     void *zConverted;
  4312   4396     UNUSED_PARAMETER(pVfs);
  4313   4397     UNUSED_PARAMETER(syncDir);
  4314   4398   
  4315   4399     SimulateIOError(return SQLITE_IOERR_DELETE);
  4316   4400     OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
  4317   4401   
  4318         -  zConverted = convertUtf8Filename(zFilename);
         4402  +  zConverted = winConvertUtf8Filename(zFilename);
  4319   4403     if( zConverted==0 ){
         4404  +    OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  4320   4405       return SQLITE_IOERR_NOMEM;
  4321   4406     }
  4322         -  if( isNT() ){
         4407  +  if( osIsNT() ){
  4323   4408       do {
  4324   4409   #if SQLITE_OS_WINRT
  4325   4410         WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  4326   4411         memset(&sAttrData, 0, sizeof(sAttrData));
  4327   4412         if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
  4328   4413                                     &sAttrData) ){
  4329   4414           attr = sAttrData.dwFileAttributes;
................................................................................
  4354   4439           rc = SQLITE_ERROR; /* Files only. */
  4355   4440           break;
  4356   4441         }
  4357   4442         if ( osDeleteFileW(zConverted) ){
  4358   4443           rc = SQLITE_OK; /* Deleted OK. */
  4359   4444           break;
  4360   4445         }
  4361         -      if ( !retryIoerr(&cnt, &lastErrno) ){
         4446  +      if ( !winRetryIoerr(&cnt, &lastErrno) ){
  4362   4447           rc = SQLITE_ERROR; /* No more retries. */
  4363   4448           break;
  4364   4449         }
  4365   4450       } while(1);
  4366   4451     }
  4367   4452   #ifdef SQLITE_WIN32_HAS_ANSI
  4368   4453     else{
................................................................................
  4382   4467           rc = SQLITE_ERROR; /* Files only. */
  4383   4468           break;
  4384   4469         }
  4385   4470         if ( osDeleteFileA(zConverted) ){
  4386   4471           rc = SQLITE_OK; /* Deleted OK. */
  4387   4472           break;
  4388   4473         }
  4389         -      if ( !retryIoerr(&cnt, &lastErrno) ){
         4474  +      if ( !winRetryIoerr(&cnt, &lastErrno) ){
  4390   4475           rc = SQLITE_ERROR; /* No more retries. */
  4391   4476           break;
  4392   4477         }
  4393   4478       } while(1);
  4394   4479     }
  4395   4480   #endif
  4396   4481     if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
  4397   4482       rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
  4398   4483                "winDelete", zFilename);
  4399   4484     }else{
  4400         -    logIoerr(cnt);
         4485  +    winLogIoerr(cnt);
  4401   4486     }
  4402   4487     sqlite3_free(zConverted);
  4403   4488     OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
  4404   4489     return rc;
  4405   4490   }
  4406   4491   
  4407   4492   /*
................................................................................
  4419   4504     void *zConverted;
  4420   4505     UNUSED_PARAMETER(pVfs);
  4421   4506   
  4422   4507     SimulateIOError( return SQLITE_IOERR_ACCESS; );
  4423   4508     OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
  4424   4509              zFilename, flags, pResOut));
  4425   4510   
  4426         -  zConverted = convertUtf8Filename(zFilename);
         4511  +  zConverted = winConvertUtf8Filename(zFilename);
  4427   4512     if( zConverted==0 ){
  4428   4513       OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
  4429   4514       return SQLITE_IOERR_NOMEM;
  4430   4515     }
  4431         -  if( isNT() ){
         4516  +  if( osIsNT() ){
  4432   4517       int cnt = 0;
  4433   4518       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
  4434   4519       memset(&sAttrData, 0, sizeof(sAttrData));
  4435   4520       while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
  4436   4521                                GetFileExInfoStandard, 
  4437         -                             &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
         4522  +                             &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
  4438   4523       if( rc ){
  4439   4524         /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
  4440   4525         ** as if it does not exist.
  4441   4526         */
  4442   4527         if(    flags==SQLITE_ACCESS_EXISTS
  4443   4528             && sAttrData.nFileSizeHigh==0 
  4444   4529             && sAttrData.nFileSizeLow==0 ){
  4445   4530           attr = INVALID_FILE_ATTRIBUTES;
  4446   4531         }else{
  4447   4532           attr = sAttrData.dwFileAttributes;
  4448   4533         }
  4449   4534       }else{
  4450         -      logIoerr(cnt);
         4535  +      winLogIoerr(cnt);
  4451   4536         if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
  4452   4537           winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
  4453   4538           sqlite3_free(zConverted);
  4454   4539           return SQLITE_IOERR_ACCESS;
  4455   4540         }else{
  4456   4541           attr = INVALID_FILE_ATTRIBUTES;
  4457   4542         }
................................................................................
  4494   4579     /*
  4495   4580     ** If the path name starts with a forward slash or a backslash, it is either
  4496   4581     ** a legal UNC name, a volume relative path, or an absolute path name in the
  4497   4582     ** "Unix" format on Windows.  There is no easy way to differentiate between
  4498   4583     ** the final two cases; therefore, we return the safer return value of TRUE
  4499   4584     ** so that callers of this function will simply use it verbatim.
  4500   4585     */
  4501         -  if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
         4586  +  if ( winIsDirSep(zPathname[0]) ){
  4502   4587       return TRUE;
  4503   4588     }
  4504   4589   
  4505   4590     /*
  4506   4591     ** If the path name starts with a letter and a colon it is either a volume
  4507   4592     ** relative path or an absolute path.  Callers of this function must not
  4508   4593     ** attempt to treat it as a relative path name (i.e. they should simply use
................................................................................
  4530   4615     int nFull,                    /* Size of output buffer in bytes */
  4531   4616     char *zFull                   /* Output buffer */
  4532   4617   ){
  4533   4618     
  4534   4619   #if defined(__CYGWIN__)
  4535   4620     SimulateIOError( return SQLITE_ERROR );
  4536   4621     UNUSED_PARAMETER(nFull);
  4537         -  assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
  4538   4622     assert( nFull>=pVfs->mxPathname );
  4539   4623     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4540   4624       /*
  4541   4625       ** NOTE: We are dealing with a relative path name and the data
  4542   4626       **       directory has been set.  Therefore, use it as the basis
  4543   4627       **       for converting the relative path name to an absolute
  4544   4628       **       one by prepending the data directory and a slash.
  4545   4629       */
  4546         -    char zOut[SQLITE_WIN32_MAX_PATH+1];
         4630  +    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
         4631  +    if( !zOut ){
         4632  +      winLogError(SQLITE_IOERR_NOMEM, 0, "winFullPathname", zRelative);
         4633  +      return SQLITE_IOERR_NOMEM;
         4634  +    }
  4547   4635       if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
  4548         -                         SQLITE_WIN32_MAX_PATH+1)<0 ){
         4636  +                         pVfs->mxPathname+1)<0 ){
  4549   4637         winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
  4550   4638                     zRelative);
         4639  +      sqlite3_free(zOut);
  4551   4640         return SQLITE_CANTOPEN_FULLPATH;
  4552   4641       }
  4553         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
  4554         -                     sqlite3_data_directory, zOut);
         4642  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
         4643  +                     sqlite3_data_directory, winGetDirDep(), zOut);
         4644  +    sqlite3_free(zOut);
  4555   4645     }else{
  4556   4646       if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
  4557   4647         winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
  4558   4648                     zRelative);
  4559   4649         return SQLITE_CANTOPEN_FULLPATH;
  4560   4650       }
  4561   4651     }
................................................................................
  4569   4659     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4570   4660       /*
  4571   4661       ** NOTE: We are dealing with a relative path name and the data
  4572   4662       **       directory has been set.  Therefore, use it as the basis
  4573   4663       **       for converting the relative path name to an absolute
  4574   4664       **       one by prepending the data directory and a backslash.
  4575   4665       */
  4576         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
  4577         -                     sqlite3_data_directory, zRelative);
         4666  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
         4667  +                     sqlite3_data_directory, winGetDirDep(), zRelative);
  4578   4668     }else{
  4579   4669       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
  4580   4670     }
  4581   4671     return SQLITE_OK;
  4582   4672   #endif
  4583   4673   
  4584   4674   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
................................................................................
  4602   4692     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4603   4693       /*
  4604   4694       ** NOTE: We are dealing with a relative path name and the data
  4605   4695       **       directory has been set.  Therefore, use it as the basis
  4606   4696       **       for converting the relative path name to an absolute
  4607   4697       **       one by prepending the data directory and a backslash.
  4608   4698       */
  4609         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
  4610         -                     sqlite3_data_directory, zRelative);
         4699  +    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
         4700  +                     sqlite3_data_directory, winGetDirDep(), zRelative);
  4611   4701       return SQLITE_OK;
  4612   4702     }
  4613         -  zConverted = convertUtf8Filename(zRelative);
         4703  +  zConverted = winConvertUtf8Filename(zRelative);
  4614   4704     if( zConverted==0 ){
  4615   4705       return SQLITE_IOERR_NOMEM;
  4616   4706     }
  4617         -  if( isNT() ){
         4707  +  if( osIsNT() ){
  4618   4708       LPWSTR zTemp;
  4619   4709       nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
  4620   4710       if( nByte==0 ){
  4621   4711         winLogError(SQLITE_ERROR, osGetLastError(),
  4622   4712                     "GetFullPathNameW1", zConverted);
  4623   4713         sqlite3_free(zConverted);
  4624   4714         return SQLITE_CANTOPEN_FULLPATH;
................................................................................
  4634   4724         winLogError(SQLITE_ERROR, osGetLastError(),
  4635   4725                     "GetFullPathNameW2", zConverted);
  4636   4726         sqlite3_free(zConverted);
  4637   4727         sqlite3_free(zTemp);
  4638   4728         return SQLITE_CANTOPEN_FULLPATH;
  4639   4729       }
  4640   4730       sqlite3_free(zConverted);
  4641         -    zOut = unicodeToUtf8(zTemp);
         4731  +    zOut = winUnicodeToUtf8(zTemp);
  4642   4732       sqlite3_free(zTemp);
  4643   4733     }
  4644   4734   #ifdef SQLITE_WIN32_HAS_ANSI
  4645   4735     else{
  4646   4736       char *zTemp;
  4647   4737       nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
  4648   4738       if( nByte==0 ){
................................................................................
  4687   4777   */
  4688   4778   /*
  4689   4779   ** Interfaces for opening a shared library, finding entry points
  4690   4780   ** within the shared library, and closing the shared library.
  4691   4781   */
  4692   4782   static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
  4693   4783     HANDLE h;
  4694         -  void *zConverted = convertUtf8Filename(zFilename);
         4784  +  void *zConverted = winConvertUtf8Filename(zFilename);
  4695   4785     UNUSED_PARAMETER(pVfs);
  4696   4786     if( zConverted==0 ){
  4697   4787       return 0;
  4698   4788     }
  4699         -  if( isNT() ){
         4789  +  if( osIsNT() ){
  4700   4790   #if SQLITE_OS_WINRT
  4701   4791       h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
  4702   4792   #else
  4703   4793       h = osLoadLibraryW((LPCWSTR)zConverted);
  4704   4794   #endif
  4705   4795     }
  4706   4796   #ifdef SQLITE_WIN32_HAS_ANSI
................................................................................
  4709   4799     }
  4710   4800   #endif
  4711   4801     sqlite3_free(zConverted);
  4712   4802     return (void*)h;
  4713   4803   }
  4714   4804   static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
  4715   4805     UNUSED_PARAMETER(pVfs);
  4716         -  getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
         4806  +  winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
  4717   4807   }
  4718   4808   static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
  4719   4809     UNUSED_PARAMETER(pVfs);
  4720   4810     return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
  4721   4811   }
  4722   4812   static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
  4723   4813     UNUSED_PARAMETER(pVfs);
................................................................................
  4885   4975   **
  4886   4976   ** However if an error message is supplied, it will be incorporated
  4887   4977   ** by sqlite into the error message available to the user using
  4888   4978   ** sqlite3_errmsg(), possibly making IO errors easier to debug.
  4889   4979   */
  4890   4980   static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  4891   4981     UNUSED_PARAMETER(pVfs);
  4892         -  return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
         4982  +  return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
  4893   4983   }
  4894   4984   
  4895   4985   /*
  4896   4986   ** Initialize and deinitialize the operating system interface.
  4897   4987   */
  4898   4988   int sqlite3_os_init(void){
  4899   4989     static sqlite3_vfs winVfs = {
  4900   4990       3,                   /* iVersion */
  4901   4991       sizeof(winFile),     /* szOsFile */
  4902         -    SQLITE_WIN32_MAX_PATH, /* mxPathname */
         4992  +    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
  4903   4993       0,                   /* pNext */
  4904   4994       "win32",             /* zName */
  4905   4995       0,                   /* pAppData */
  4906   4996       winOpen,             /* xOpen */
  4907   4997       winDelete,           /* xDelete */
  4908   4998       winAccess,           /* xAccess */
  4909   4999       winFullPathname,     /* xFullPathname */
................................................................................
  4916   5006       winCurrentTime,      /* xCurrentTime */
  4917   5007       winGetLastError,     /* xGetLastError */
  4918   5008       winCurrentTimeInt64, /* xCurrentTimeInt64 */
  4919   5009       winSetSystemCall,    /* xSetSystemCall */
  4920   5010       winGetSystemCall,    /* xGetSystemCall */
  4921   5011       winNextSystemCall,   /* xNextSystemCall */
  4922   5012     };
         5013  +#if defined(SQLITE_WIN32_HAS_WIDE)
         5014  +  static sqlite3_vfs winLongPathVfs = {
         5015  +    3,                   /* iVersion */
         5016  +    sizeof(winFile),     /* szOsFile */
         5017  +    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
         5018  +    0,                   /* pNext */
         5019  +    "win32-longpath",    /* zName */
         5020  +    0,                   /* pAppData */
         5021  +    winOpen,             /* xOpen */
         5022  +    winDelete,           /* xDelete */
         5023  +    winAccess,           /* xAccess */
         5024  +    winFullPathname,     /* xFullPathname */
         5025  +    winDlOpen,           /* xDlOpen */
         5026  +    winDlError,          /* xDlError */
         5027  +    winDlSym,            /* xDlSym */
         5028  +    winDlClose,          /* xDlClose */
         5029  +    winRandomness,       /* xRandomness */
         5030  +    winSleep,            /* xSleep */
         5031  +    winCurrentTime,      /* xCurrentTime */
         5032  +    winGetLastError,     /* xGetLastError */
         5033  +    winCurrentTimeInt64, /* xCurrentTimeInt64 */
         5034  +    winSetSystemCall,    /* xSetSystemCall */
         5035  +    winGetSystemCall,    /* xGetSystemCall */
         5036  +    winNextSystemCall,   /* xNextSystemCall */
         5037  +  };
         5038  +#endif
  4923   5039   
  4924   5040     /* Double-check that the aSyscall[] array has been constructed
  4925   5041     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  4926   5042     assert( ArraySize(aSyscall)==74 );
  4927   5043   
  4928   5044     /* get memory map allocation granularity */
  4929   5045     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
................................................................................
  4932   5048   #else
  4933   5049     osGetSystemInfo(&winSysInfo);
  4934   5050   #endif
  4935   5051     assert( winSysInfo.dwAllocationGranularity>0 );
  4936   5052     assert( winSysInfo.dwPageSize>0 );
  4937   5053   
  4938   5054     sqlite3_vfs_register(&winVfs, 1);
         5055  +
         5056  +#if defined(SQLITE_WIN32_HAS_WIDE)
         5057  +  sqlite3_vfs_register(&winLongPathVfs, 0);
         5058  +#endif
         5059  +
  4939   5060     return SQLITE_OK; 
  4940   5061   }
  4941   5062   
  4942   5063   int sqlite3_os_end(void){ 
  4943   5064   #if SQLITE_OS_WINRT
  4944   5065     if( sleepObj!=NULL ){
  4945   5066       osCloseHandle(sleepObj);

Changes to src/pragma.c.

   657    657     **
   658    658     ** Get or set the size limit on rollback journal files.
   659    659     */
   660    660     if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
   661    661       Pager *pPager = sqlite3BtreePager(pDb->pBt);
   662    662       i64 iLimit = -2;
   663    663       if( zRight ){
   664         -      sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
          664  +      sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
   665    665         if( iLimit<-1 ) iLimit = -1;
   666    666       }
   667    667       iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
   668    668       returnSingleInt(pParse, "journal_size_limit", iLimit);
   669    669     }else
   670    670   
   671    671   #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
................................................................................
   791    791     **
   792    792     ** This value is advisory.  The underlying VFS is free to memory map
   793    793     ** as little or as much as it wants.  Except, if N is set to 0 then the
   794    794     ** upper layers will never invoke the xFetch interfaces to the VFS.
   795    795     */
   796    796     if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
   797    797       sqlite3_int64 sz;
          798  +#if SQLITE_MAX_MMAP_SIZE>0
   798    799       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   799    800       if( zRight ){
   800    801         int ii;
   801         -      sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
          802  +      sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
   802    803         if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
   803    804         if( pId2->n==0 ) db->szMmap = sz;
   804    805         for(ii=db->nDb-1; ii>=0; ii--){
   805    806           if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
   806    807             sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
   807    808           }
   808    809         }
   809    810       }
   810    811       sz = -1;
   811    812       rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
   812         -#if SQLITE_MAX_MMAP_SIZE==0
          813  +#else
   813    814       sz = 0;
          815  +    rc = SQLITE_OK;
   814    816   #endif
   815    817       if( rc==SQLITE_OK ){
   816    818         returnSingleInt(pParse, "mmap_size", sz);
   817    819       }else if( rc!=SQLITE_NOTFOUND ){
   818    820         pParse->nErr++;
   819    821         pParse->rc = rc;
   820    822       }

Changes to src/shell.c.

    67     67   #define popen _popen
    68     68   #undef pclose
    69     69   #define pclose _pclose
    70     70   #else
    71     71   /* Make sure isatty() has a prototype.
    72     72   */
    73     73   extern int isatty(int);
    74         -#endif
    75     74   
    76     75   /* popen and pclose are not C89 functions and so are sometimes omitted from
    77     76   ** the <stdio.h> header */
    78         -FILE *popen(const char*,const char*);
    79         -int pclose(FILE*);
           77  +extern FILE *popen(const char*,const char*);
           78  +extern int pclose(FILE*);
           79  +#endif
    80     80   
    81     81   #if defined(_WIN32_WCE)
    82     82   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
    83     83    * thus we always assume that we have a console. That can be
    84     84    * overridden with the -batch command line option.
    85     85    */
    86     86   #define isatty(x) 1
................................................................................
   550    550         fputc('t', out);
   551    551       }else if( c=='\n' ){
   552    552         fputc('\\', out);
   553    553         fputc('n', out);
   554    554       }else if( c=='\r' ){
   555    555         fputc('\\', out);
   556    556         fputc('r', out);
   557         -    }else if( !isprint(c) ){
          557  +    }else if( !isprint(c&0xff) ){
   558    558         fprintf(out, "\\%03o", c&0xff);
   559    559       }else{
   560    560         fputc(c, out);
   561    561       }
   562    562     }
   563    563     fputc('"', out);
   564    564   }

Changes to src/test1.c.

  5930   5930       Tcl_AppendResult(interp, "wait failed: ", zBuf, (char*)0);
  5931   5931       CloseHandle(ev);
  5932   5932       return TCL_ERROR;
  5933   5933     }
  5934   5934     CloseHandle(ev);
  5935   5935     return TCL_OK;
  5936   5936   }
         5937  +
         5938  +/*
         5939  +**      exists_win32_path PATH
         5940  +**
         5941  +** Returns non-zero if the specified path exists, whose fully qualified name
         5942  +** may exceed 260 characters if it is prefixed with "\\?\".
         5943  +*/
         5944  +static int win32_exists_path(
         5945  +  void *clientData,
         5946  +  Tcl_Interp *interp,
         5947  +  int objc,
         5948  +  Tcl_Obj *CONST objv[]
         5949  +){
         5950  +  if( objc!=2 ){
         5951  +    Tcl_WrongNumArgs(interp, 1, objv, "PATH");
         5952  +    return TCL_ERROR;
         5953  +  }
         5954  +  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(
         5955  +      GetFileAttributesW( Tcl_GetUnicode(objv[1]))!=INVALID_FILE_ATTRIBUTES ));
         5956  +  return TCL_OK;
         5957  +}
         5958  +
         5959  +/*
         5960  +**      find_win32_file PATTERN
         5961  +**
         5962  +** Returns a list of entries in a directory that match the specified pattern,
         5963  +** whose fully qualified name may exceed 248 characters if it is prefixed with
         5964  +** "\\?\".
         5965  +*/
         5966  +static int win32_find_file(
         5967  +  void *clientData,
         5968  +  Tcl_Interp *interp,
         5969  +  int objc,
         5970  +  Tcl_Obj *CONST objv[]
         5971  +){
         5972  +  HANDLE hFindFile = INVALID_HANDLE_VALUE;
         5973  +  WIN32_FIND_DATAW findData;
         5974  +  Tcl_Obj *listObj;
         5975  +  DWORD lastErrno;
         5976  +  if( objc!=2 ){
         5977  +    Tcl_WrongNumArgs(interp, 1, objv, "PATTERN");
         5978  +    return TCL_ERROR;
         5979  +  }
         5980  +  hFindFile = FindFirstFileW(Tcl_GetUnicode(objv[1]), &findData);
         5981  +  if( hFindFile==INVALID_HANDLE_VALUE ){
         5982  +    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
         5983  +    return TCL_ERROR;
         5984  +  }
         5985  +  listObj = Tcl_NewObj();
         5986  +  Tcl_IncrRefCount(listObj);
         5987  +  do {
         5988  +    Tcl_ListObjAppendElement(interp, listObj, Tcl_NewUnicodeObj(
         5989  +        findData.cFileName, -1));
         5990  +    Tcl_ListObjAppendElement(interp, listObj, Tcl_NewWideIntObj(
         5991  +        findData.dwFileAttributes));
         5992  +  } while( FindNextFileW(hFindFile, &findData) );
         5993  +  lastErrno = GetLastError();
         5994  +  if( lastErrno!=NO_ERROR && lastErrno!=ERROR_NO_MORE_FILES ){
         5995  +    FindClose(hFindFile);
         5996  +    Tcl_DecrRefCount(listObj);
         5997  +    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
         5998  +    return TCL_ERROR;
         5999  +  }
         6000  +  FindClose(hFindFile);
         6001  +  Tcl_SetObjResult(interp, listObj);
         6002  +  return TCL_OK;
         6003  +}
         6004  +
         6005  +/*
         6006  +**      delete_win32_file FILENAME
         6007  +**
         6008  +** Deletes the specified file, whose fully qualified name may exceed 260
         6009  +** characters if it is prefixed with "\\?\".
         6010  +*/
         6011  +static int win32_delete_file(
         6012  +  void *clientData,
         6013  +  Tcl_Interp *interp,
         6014  +  int objc,
         6015  +  Tcl_Obj *CONST objv[]
         6016  +){
         6017  +  if( objc!=2 ){
         6018  +    Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
         6019  +    return TCL_ERROR;
         6020  +  }
         6021  +  if( !DeleteFileW(Tcl_GetUnicode(objv[1])) ){
         6022  +    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
         6023  +    return TCL_ERROR;
         6024  +  }
         6025  +  Tcl_ResetResult(interp);
         6026  +  return TCL_OK;
         6027  +}
         6028  +
         6029  +/*
         6030  +**      make_win32_dir DIRECTORY
         6031  +**
         6032  +** Creates the specified directory, whose fully qualified name may exceed 248
         6033  +** characters if it is prefixed with "\\?\".
         6034  +*/
         6035  +static int win32_mkdir(
         6036  +  void *clientData,
         6037  +  Tcl_Interp *interp,
         6038  +  int objc,
         6039  +  Tcl_Obj *CONST objv[]
         6040  +){
         6041  +  if( objc!=2 ){
         6042  +    Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
         6043  +    return TCL_ERROR;
         6044  +  }
         6045  +  if( !CreateDirectoryW(Tcl_GetUnicode(objv[1]), NULL) ){
         6046  +    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
         6047  +    return TCL_ERROR;
         6048  +  }
         6049  +  Tcl_ResetResult(interp);
         6050  +  return TCL_OK;
         6051  +}
         6052  +
         6053  +/*
         6054  +**      remove_win32_dir DIRECTORY
         6055  +**
         6056  +** Removes the specified directory, whose fully qualified name may exceed 248
         6057  +** characters if it is prefixed with "\\?\".
         6058  +*/
         6059  +static int win32_rmdir(
         6060  +  void *clientData,
         6061  +  Tcl_Interp *interp,
         6062  +  int objc,
         6063  +  Tcl_Obj *CONST objv[]
         6064  +){
         6065  +  if( objc!=2 ){
         6066  +    Tcl_WrongNumArgs(interp, 1, objv, "DIRECTORY");
         6067  +    return TCL_ERROR;
         6068  +  }
         6069  +  if( !RemoveDirectoryW(Tcl_GetUnicode(objv[1])) ){
         6070  +    Tcl_SetObjResult(interp, Tcl_NewWideIntObj(GetLastError()));
         6071  +    return TCL_ERROR;
         6072  +  }
         6073  +  Tcl_ResetResult(interp);
         6074  +  return TCL_OK;
         6075  +}
  5937   6076   #endif
  5938   6077   
  5939   6078   
  5940   6079   /*
  5941   6080   **      optimization_control DB OPT BOOLEAN
  5942   6081   **
  5943   6082   ** Enable or disable query optimizations using the sqlite3_test_control()
................................................................................
  6189   6328   
  6190   6329        { "save_prng_state",               save_prng_state,    0 },
  6191   6330        { "restore_prng_state",            restore_prng_state, 0 },
  6192   6331        { "reset_prng_state",              reset_prng_state,   0 },
  6193   6332        { "optimization_control",          optimization_control,0},
  6194   6333   #if SQLITE_OS_WIN
  6195   6334        { "lock_win32_file",               win32_file_lock,    0 },
         6335  +     { "exists_win32_path",             win32_exists_path,  0 },
         6336  +     { "find_win32_file",               win32_find_file,    0 },
         6337  +     { "delete_win32_file",             win32_delete_file,  0 },
         6338  +     { "make_win32_dir",                win32_mkdir,        0 },
         6339  +     { "remove_win32_dir",              win32_rmdir,        0 },
  6196   6340   #endif
  6197   6341        { "tcl_objproc",                   runAsObjProc,       0 },
  6198   6342   
  6199   6343        /* sqlite3_column_*() API */
  6200   6344        { "sqlite3_column_count",          test_column_count  ,0 },
  6201   6345        { "sqlite3_data_count",            test_data_count    ,0 },
  6202   6346        { "sqlite3_column_type",           test_column_type   ,0 },

Changes to src/test_malloc.c.

   745    745   static void test_memdebug_callback(int nByte, int nFrame, void **aFrame){
   746    746     if( mallocLogEnabled ){
   747    747       MallocLog *pLog;
   748    748       Tcl_HashEntry *pEntry;
   749    749       int isNew;
   750    750   
   751    751       int aKey[MALLOC_LOG_KEYINTS];
   752         -    int nKey = sizeof(int)*MALLOC_LOG_KEYINTS;
          752  +    unsigned int nKey = sizeof(int)*MALLOC_LOG_KEYINTS;
   753    753   
   754    754       memset(aKey, 0, nKey);
   755    755       if( (sizeof(void*)*nFrame)<nKey ){
   756    756         nKey = nFrame*sizeof(void*);
   757    757       }
   758    758       memcpy(aKey, aFrame, nKey);
   759    759   

Changes to src/where.c.

  2996   2996     zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  2997   2997     if( !zAff ){
  2998   2998       pParse->db->mallocFailed = 1;
  2999   2999     }
  3000   3000   
  3001   3001     /* Evaluate the equality constraints
  3002   3002     */
  3003         -  assert( zAff==0 || strlen(zAff)>=nEq );
         3003  +  assert( zAff==0 || (int)strlen(zAff)>=nEq );
  3004   3004     for(j=0; j<nEq; j++){
  3005   3005       int r1;
  3006   3006       pTerm = pLoop->aLTerm[j];
  3007   3007       assert( pTerm!=0 );
  3008   3008       /* The following true for indices with redundant columns. 
  3009   3009       ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3010   3010       testcase( (pTerm->wtFlags & TERM_CODED)!=0 );

Added test/win32longpath.test.

            1  +# 2013 August 27
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this script is testing the file name handling provided
           13  +# by the "win32-longpath" VFS.
           14  +#
           15  +
           16  +if {$tcl_platform(platform)!="windows"} return
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +set testprefix win32longpath
           21  +
           22  +proc do_remove_win32_dir {args} {
           23  +  set nRetry [getFileRetries]     ;# Maximum number of retries.
           24  +  set nDelay [getFileRetryDelay]  ;# Delay in ms before retrying.
           25  +
           26  +  foreach dirName $args {
           27  +    # On windows, sometimes even a [remove_win32_dir] can fail just after
           28  +    # a directory is emptied. The cause is usually "tag-alongs" - programs
           29  +    # like anti-virus software, automatic backup tools and various explorer
           30  +    # extensions that keep a file open a little longer than we expect,
           31  +    # causing the delete to fail.
           32  +    #
           33  +    # The solution is to wait a short amount of time before retrying the
           34  +    # removal.
           35  +    #
           36  +    if {$nRetry > 0} {
           37  +      for {set i 0} {$i < $nRetry} {incr i} {
           38  +        set rc [catch {
           39  +          remove_win32_dir $dirName
           40  +        } msg]
           41  +        if {$rc == 0} break
           42  +        if {$nDelay > 0} { after $nDelay }
           43  +      }
           44  +      if {$rc} { error $msg }
           45  +    } else {
           46  +      remove_win32_dir $dirName
           47  +    }
           48  +  }
           49  +}
           50  +
           51  +proc do_delete_win32_file {args} {
           52  +  set nRetry [getFileRetries]     ;# Maximum number of retries.
           53  +  set nDelay [getFileRetryDelay]  ;# Delay in ms before retrying.
           54  +
           55  +  foreach fileName $args {
           56  +    # On windows, sometimes even a [delete_win32_file] can fail just after
           57  +    # a file is closed. The cause is usually "tag-alongs" - programs like
           58  +    # anti-virus software, automatic backup tools and various explorer
           59  +    # extensions that keep a file open a little longer than we expect,
           60  +    # causing the delete to fail.
           61  +    #
           62  +    # The solution is to wait a short amount of time before retrying the
           63  +    # delete.
           64  +    #
           65  +    if {$nRetry > 0} {
           66  +      for {set i 0} {$i < $nRetry} {incr i} {
           67  +        set rc [catch {
           68  +          delete_win32_file $fileName
           69  +        } msg]
           70  +        if {$rc == 0} break
           71  +        if {$nDelay > 0} { after $nDelay }
           72  +      }
           73  +      if {$rc} { error $msg }
           74  +    } else {
           75  +      delete_win32_file $fileName
           76  +    }
           77  +  }
           78  +}
           79  +
           80  +db close
           81  +set path [file nativename [get_pwd]]
           82  +sqlite3 db [file join $path test.db] -vfs win32-longpath
           83  +
           84  +do_test 1.1 {
           85  +  db eval {
           86  +    BEGIN EXCLUSIVE;
           87  +    CREATE TABLE t1(x);
           88  +    INSERT INTO t1 VALUES(1);
           89  +    INSERT INTO t1 VALUES(2);
           90  +    INSERT INTO t1 VALUES(3);
           91  +    INSERT INTO t1 VALUES(4);
           92  +    SELECT x FROM t1 ORDER BY x;
           93  +    COMMIT;
           94  +  }
           95  +} {1 2 3 4}
           96  +
           97  +set longPath(1) \\\\?\\$path\\[pid]
           98  +make_win32_dir $longPath(1)
           99  +
          100  +set longPath(2) $longPath(1)\\[string repeat X 255]
          101  +make_win32_dir $longPath(2)
          102  +
          103  +set longPath(3) $longPath(2)\\[string repeat Y 255]
          104  +make_win32_dir $longPath(3)
          105  +
          106  +set fileName $longPath(3)\\test.db
          107  +
          108  +do_test 1.2 {
          109  +  list [catch {sqlite3 db2 [string range $fileName 4 end]} msg] $msg
          110  +} {1 {unable to open database file}}
          111  +
          112  +sqlite3 db3 $fileName -vfs win32-longpath
          113  +
          114  +do_test 1.3 {
          115  +  db3 eval {
          116  +    BEGIN EXCLUSIVE;
          117  +    CREATE TABLE t1(x);
          118  +    INSERT INTO t1 VALUES(5);
          119  +    INSERT INTO t1 VALUES(6);
          120  +    INSERT INTO t1 VALUES(7);
          121  +    INSERT INTO t1 VALUES(8);
          122  +    SELECT x FROM t1 ORDER BY x;
          123  +    COMMIT;
          124  +  }
          125  +} {5 6 7 8}
          126  +
          127  +db3 close
          128  +# puts "  Database exists \{[exists_win32_path $fileName]\}"
          129  +
          130  +sqlite3 db3 $fileName -vfs win32-longpath
          131  +
          132  +do_test 1.4 {
          133  +  db3 eval {
          134  +    PRAGMA journal_mode = WAL;
          135  +  }
          136  +} {wal}
          137  +
          138  +do_test 1.5 {
          139  +  db3 eval {
          140  +    BEGIN EXCLUSIVE;
          141  +    INSERT INTO t1 VALUES(9);
          142  +    INSERT INTO t1 VALUES(10);
          143  +    INSERT INTO t1 VALUES(11);
          144  +    INSERT INTO t1 VALUES(12);
          145  +    SELECT x FROM t1 ORDER BY x;
          146  +    COMMIT;
          147  +  }
          148  +} {5 6 7 8 9 10 11 12}
          149  +
          150  +db3 close
          151  +# puts "  Database exists \{[exists_win32_path $fileName]\}"
          152  +
          153  +do_delete_win32_file $fileName
          154  +# puts "  Files remaining \{[find_win32_file $longPath(3)\\*]\}"
          155  +
          156  +do_remove_win32_dir $longPath(3)
          157  +do_remove_win32_dir $longPath(2)
          158  +do_remove_win32_dir $longPath(1)
          159  +
          160  +finish_test