Fossil

Changes On Branch winsymlink
Login

Changes On Branch winsymlink

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

Changes In Branch winsymlink Excluding Merge-Ins

This is equivalent to a diff from 613fe1b1fb to 2999c7dc8a

2017-04-23
13:50
gzip.c:70:13: warning: implicit conversion from 'int' to 'char' changes value from 255 to -1 [-Wconstant-conversion] aHdr[9] = 255; ~ ^~~ ... (check-in: fe1332ca2c user: jan.nijtmans tags: trunk)
13:08
merge trunk. Use new Win10 flag: SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE. See: [https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/#Y9sMOye6Y8vVYmDx.97] for why this might make this branch work better. ... (Closed-Leaf check-in: 2999c7dc8a user: jan.nijtmans tags: winsymlink)
2017-04-19
07:08
Add image/bmp and image/vnd.microsoft.icon (ico) to the mime-type table ... (check-in: 613fe1b1fb user: jan.nijtmans tags: trunk)
06:58
some end-of-line spacing ... (check-in: 7f4b50b23d user: jan.nijtmans tags: trunk)
2016-05-31
08:17
merge from trunk with a few tweaks for merge conflicts ... (check-in: 598b58a5ee user: sdr tags: winsymlink)

Changes to src/blob.c.

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

833
834
835
836







837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
  if( got<size ){
    blob_resize(pBlob, got);
  }
  return got;
}

/*
** Reads symlink destination path and puts int into blob.
** Any prior content of the blob is discarded, not freed.
**
** Returns length of destination path.
**
** On windows, zeros blob and returns 0.
*/
int blob_read_link(Blob *pBlob, const char *zFilename){
#if !defined(_WIN32)
  char zBuf[1024];

  ssize_t len = readlink(zFilename, zBuf, 1023);
  if( len < 0 ){
    fossil_fatal("cannot read symbolic link %s", zFilename);
  }







  zBuf[len] = 0;   /* null-terminate */
  blob_zero(pBlob);
  blob_appendf(pBlob, "%s", zBuf);
  return len;
#else
  blob_zero(pBlob);
  return 0;
#endif
}


/*
** Write the content of a blob into a file.
**
** If the filename is blank or "-" then write to standard output.







|




|


<

>




>
>
>
>
>
>
>




<
<
<
<







816
817
818
819
820
821
822
823
824
825
826
827
828
829
830

831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847




848
849
850
851
852
853
854
  if( got<size ){
    blob_resize(pBlob, got);
  }
  return got;
}

/*
** Reads symlink destination path and puts it into blob.
** Any prior content of the blob is discarded, not freed.
**
** Returns length of destination path.
**
** On windows, zeros blob and returns 0 if symlinks are not supported.
*/
int blob_read_link(Blob *pBlob, const char *zFilename){

  char zBuf[1024];
#if !defined(_WIN32)
  ssize_t len = readlink(zFilename, zBuf, 1023);
  if( len < 0 ){
    fossil_fatal("cannot read symbolic link %s", zFilename);
  }
#else
  ssize_t len = win32_readlink(zFilename, zBuf, 1023);
  if( len < 0 ){
    blob_zero(pBlob);
    return 0;
  }
#endif
  zBuf[len] = 0;   /* null-terminate */
  blob_zero(pBlob);
  blob_appendf(pBlob, "%s", zBuf);
  return len;




}


/*
** Write the content of a blob into a file.
**
** If the filename is blank or "-" then write to standard output.

Changes to src/checkin.c.

1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565

1566




1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
    int isSelected = db_column_int(&q, 6);
    const char *zPerm;
    int cmp;

    blob_resize(&filename, nBasename);
    blob_append(&filename, zName, -1);

#if !defined(_WIN32)
    /* For unix, extract the "executable" and "symlink" permissions
    ** directly from the filesystem.  On windows, permissions are
    ** unchanged from the original.  However, only do this if the file
    ** itself is actually selected to be part of this check-in.
    */
    if( isSelected ){
      int mPerm;

      mPerm = file_wd_perm(blob_str(&filename));

      isExe = ( mPerm==PERM_EXE );




      isLink = ( mPerm==PERM_LNK );
    }
#endif
    if( isExe ){
      zPerm = " x";
    }else if( isLink ){
      zPerm = " l"; /* note: symlinks don't have executable bit on unix */
    }else{
      zPerm = "";
    }







<
|
|
<






>

>
>
>
>
|

|







1549
1550
1551
1552
1553
1554
1555

1556
1557

1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
    int isSelected = db_column_int(&q, 6);
    const char *zPerm;
    int cmp;

    blob_resize(&filename, nBasename);
    blob_append(&filename, zName, -1);


    /* Extract the "executable" and "symlink" permissions
    ** directly from the filesystem.  However, only do this if the file

    ** itself is actually selected to be part of this check-in.
    */
    if( isSelected ){
      int mPerm;

      mPerm = file_wd_perm(blob_str(&filename));
#if !defined(_WIN32)
      isExe = ( mPerm==PERM_EXE );
#endif
#if defined(_WIN32)
      if (win32_symlinks_supported(blob_str(&filename)))
#endif
        isLink = ( mPerm==PERM_LNK );
    }

    if( isExe ){
      zPerm = " x";
    }else if( isLink ){
      zPerm = " l"; /* note: symlinks don't have executable bit on unix */
    }else{
      zPerm = "";
    }

Changes to src/file.c.

69
70
71
72
73
74
75

76
77
78







79
80
81


82
83
84
85
86
87
88

#endif /* INTERFACE */

#if !defined(_WIN32) || !(defined(__MSVCRT__) || defined(_MSC_VER))
# define fossilStat stat
#endif


/*
** On Windows S_ISLNK always returns FALSE.
*/







#if !defined(S_ISLNK)
# define S_ISLNK(x) (0)
#endif


static int fileStatValid = 0;
static struct fossilStat fileStat;

/*
** Fill stat buf with information received from stat() or lstat().
** lstat() is called on Unix if isWd is TRUE and allow-symlinks setting is on.
**







>

|

>
>
>
>
>
>
>

|

>
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#endif /* INTERFACE */

#if !defined(_WIN32) || !(defined(__MSVCRT__) || defined(_MSC_VER))
# define fossilStat stat
#endif

#if defined(_WIN32)
/*
** On Windows S_ISLNK can be true or false.
*/
/* the S_ISLNK provided by dirent.h for windows is inadequate, so fix it */
#if defined(S_ISLNK)
# undef S_ISLNK
#endif
#if !defined(S_IFLNK)
# define S_IFLNK 0120000
#endif
#if !defined(S_ISLNK)
# define S_ISLNK(x) ((x)==S_IFLNK)
#endif
#endif

static int fileStatValid = 0;
static struct fossilStat fileStat;

/*
** Fill stat buf with information received from stat() or lstat().
** lstat() is called on Unix if isWd is TRUE and allow-symlinks setting is on.
**
98
99
100
101
102
103
104



105

106
107
108
109
110
111
112
#if !defined(_WIN32)
  if( isWd && (forceWd || db_allow_symlinks(0)) ){
    rc = lstat(zMbcs, buf);
  }else{
    rc = stat(zMbcs, buf);
  }
#else



  rc = win32_stat(zMbcs, buf, isWd);

#endif
  fossil_path_free(zMbcs);
  return rc;
}

/*
** Clears the fileStat variable and its associated validity flag.







>
>
>
|
>







108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#if !defined(_WIN32)
  if( isWd && (forceWd || db_allow_symlinks(0)) ){
    rc = lstat(zMbcs, buf);
  }else{
    rc = stat(zMbcs, buf);
  }
#else
  if( isWd && g.allowSymlinks ){
    rc = win32_lstat(zMbcs, buf);
  }else{
    rc = win32_stat(zMbcs, buf);
  }
#endif
  fossil_path_free(zMbcs);
  return rc;
}

/*
** Clears the fileStat variable and its associated validity flag.
218
219
220
221
222
223
224




225
226
227
228
229
230
231
232
233
234
235
236
237
238
239








240
241
242
243



244
245
246

247




248
249
250
251
252
253
254
255
256
257
258
259





















260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278




279
280
281
282
283
284
285
286
287
288
289
290
291
292
** Create symlink to file on Unix, or plain-text file with
** symlink target if "allow-symlinks" is off or we're on Windows.
**
** Arguments: target file (symlink will point to it), link file
**/
void symlink_create(const char *zTargetFile, const char *zLinkFile){
#if !defined(_WIN32)




  if( db_allow_symlinks(0) ){
    int i, nName;
    char *zName, zBuf[1000];

    nName = strlen(zLinkFile);
    if( nName>=sizeof(zBuf) ){
      zName = mprintf("%s", zLinkFile);
    }else{
      zName = zBuf;
      memcpy(zName, zLinkFile, nName+1);
    }
    nName = file_simplify_name(zName, nName, 0);
    for(i=1; i<nName; i++){
      if( zName[i]=='/' ){
        zName[i] = 0;








          if( file_mkdir(zName, 1) ){
            fossil_fatal_recursive("unable to create directory %s", zName);
            return;
          }



        zName[i] = '/';
      }
    }

    if( symlink(zTargetFile, zName)!=0 ){




      fossil_fatal_recursive("unable to create symlink \"%s\"", zName);
    }
    if( zName!=zBuf ) free(zName);
  }else
#endif
  {
    Blob content;
    blob_set(&content, zTargetFile);
    blob_write_to_file(&content, zLinkFile);
    blob_reset(&content);
  }
}






















/*
** Copy symbolic link from zFrom to zTo.
*/
void symlink_copy(const char *zFrom, const char *zTo){
  Blob content;
  blob_read_link(&content, zFrom);
  symlink_create(blob_str(&content), zTo);
  blob_reset(&content);
}

/*
** Return file permissions (normal, executable, or symlink):
**   - PERM_EXE on Unix if file is executable;
**   - PERM_LNK on Unix if file is symlink and allow-symlinks option is on;
**   - PERM_REG for all other cases (regular file, directory, fifo, etc).
*/
int file_wd_perm(const char *zFilename){
#if !defined(_WIN32)




  if( !getStat(zFilename, 1) ){
     if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
      return PERM_EXE;
    else if( db_allow_symlinks(0) && S_ISLNK(fileStat.st_mode) )
      return PERM_LNK;
  }
#endif
  return PERM_REG;
}

/*
** Return TRUE if the named file is an executable.  Return false
** for directories, devices, fifos, symlinks, etc.
*/







>
>
>
>
|














>
>
>
>
>
>
>
>




>
>
>



>
|
>
>
>
>



|
<
<






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













|
|



|
>
>
>
>

|




<







232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

342
343
344
345
346
347
348
** Create symlink to file on Unix, or plain-text file with
** symlink target if "allow-symlinks" is off or we're on Windows.
**
** Arguments: target file (symlink will point to it), link file
**/
void symlink_create(const char *zTargetFile, const char *zLinkFile){
#if !defined(_WIN32)
  int symlinks_supported = 1;
#else
  int symlinks_supported = win32_symlinks_supported(zLinkFile);
#endif
  if( symlinks_supported && db_allow_symlinks(0) ){
    int i, nName;
    char *zName, zBuf[1000];

    nName = strlen(zLinkFile);
    if( nName>=sizeof(zBuf) ){
      zName = mprintf("%s", zLinkFile);
    }else{
      zName = zBuf;
      memcpy(zName, zLinkFile, nName+1);
    }
    nName = file_simplify_name(zName, nName, 0);
    for(i=1; i<nName; i++){
      if( zName[i]=='/' ){
        zName[i] = 0;
#if defined(_WIN32) || defined(__CYGWIN__)
        /*
        ** On Windows, local path looks like: C:/develop/project/file.txt
        ** The if stops us from trying to create a directory of a drive letter
        ** C: in this example.
        */
        if (!(i == 2 && zName[1] == ':')){
#endif
          if( file_mkdir(zName, 1) ){
            fossil_fatal_recursive("unable to create directory %s", zName);
            return;
          }
#if defined(_WIN32) || defined(__CYGWIN__)
        }
#endif
        zName[i] = '/';
      }
    }
#if !defined(_WIN32)
    if( symlink(zTargetFile, zName)!=0 )
#else
    if( win32_symlink(zTargetFile, zName)!=0 )
#endif
    {
      fossil_fatal_recursive("unable to create symlink \"%s\"", zName);
    }
    if( zName!=zBuf ) free(zName);
  }else{


    Blob content;
    blob_set(&content, zTargetFile);
    blob_write_to_file(&content, zLinkFile);
    blob_reset(&content);
  }
}

/*
** This code is used in multiple places around fossil. Rather than having
** four or five slightly different cut & paste chunks of code, this function
** does the task of deleting a (possible) link (if necessary) and then
** either creating a link or a file based on input parameters.
** mayNeedDelete is true if we might need to call link_delete, false otherwise.
** needLink is true if we need to create a symlink, false otherwise.
** mayBeLink is true if zName might be a symlink based on the state of the
** checkout and/or file system, false otherwise.
** blob is the binary data to either write as a symlink or as a file.
** zName is the name of the file or symlink to write.
*/
void create_symlink_or_file(int mayNeedDelete, int needLink, int mayBeLink, Blob* blob, const char* zName){
  if (mayNeedDelete && (needLink || mayBeLink))
    link_delete(zName);
  if (needLink)
    symlink_create(blob_str(blob), zName);
  else
    blob_write_to_file(blob, zName);
}

/*
** Copy symbolic link from zFrom to zTo.
*/
void symlink_copy(const char *zFrom, const char *zTo){
  Blob content;
  blob_read_link(&content, zFrom);
  symlink_create(blob_str(&content), zTo);
  blob_reset(&content);
}

/*
** Return file permissions (normal, executable, or symlink):
**   - PERM_EXE if file is executable;
**   - PERM_LNK if file is symlink and allow-symlinks option is on;
**   - PERM_REG for all other cases (regular file, directory, fifo, etc).
*/
int file_wd_perm(const char *zFilename){
#if defined(_WIN32)
#  ifndef S_IXUSR
#    define S_IXUSR  _S_IEXEC
#  endif
#endif
  if( !getStat(zFilename, 1) ){
    if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
      return PERM_EXE;
    else if( db_allow_symlinks(0) && S_ISLNK(fileStat.st_mode) )
      return PERM_LNK;
  }

  return PERM_REG;
}

/*
** Return TRUE if the named file is an executable.  Return false
** for directories, devices, fifos, symlinks, etc.
*/
581
582
583
584
585
586
587


















588
589
590
591
592
593
594
** Returns zero upon success.
*/
int file_delete(const char *zFilename){
  int rc;
#ifdef _WIN32
  wchar_t *z = fossil_utf8_to_path(zFilename, 0);
  rc = _wunlink(z);


















#else
  char *z = fossil_utf8_to_path(zFilename, 0);
  rc = unlink(zFilename);
#endif
  fossil_path_free(z);
  return rc;
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
** Returns zero upon success.
*/
int file_delete(const char *zFilename){
  int rc;
#ifdef _WIN32
  wchar_t *z = fossil_utf8_to_path(zFilename, 0);
  rc = _wunlink(z);
#else
  char *z = fossil_utf8_to_path(zFilename, 0);
  rc = unlink(zFilename);
#endif
  fossil_path_free(z);
  return rc;
}

/*
** Delete a link.
**
** Returns zero upon success.
*/
int link_delete(const char *zFilename){
  int rc;
#ifdef _WIN32
  wchar_t *z = fossil_utf8_to_path(zFilename, 0);
  rc = win32_unlink_rmdir(z);
#else
  char *z = fossil_utf8_to_path(zFilename, 0);
  rc = unlink(zFilename);
#endif
  fossil_path_free(z);
  return rc;
}

Changes to src/rebuild.c.

913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

      if( pEntry->d_name[0]=='.' ){
        continue;
      }
      zUtf8Name = fossil_path_to_utf8(pEntry->d_name);
      zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
      fossil_path_free(zUtf8Name);
#ifdef _DIRENT_HAVE_D_TYPE
      if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_isdir(zSubpath)==1) : (pEntry->d_type==DT_DIR) )
#else
      if( file_isdir(zSubpath)==1 )
#endif
      {
        recon_read_dir(zSubpath);







|







913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

      if( pEntry->d_name[0]=='.' ){
        continue;
      }
      zUtf8Name = fossil_path_to_utf8(pEntry->d_name);
      zSubpath = mprintf("%s/%s", zPath, zUtf8Name);
      fossil_path_free(zUtf8Name);
#if defined(_DIRENT_HAVE_D_TYPE) && !defined(_WIN32)
      if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_isdir(zSubpath)==1) : (pEntry->d_type==DT_DIR) )
#else
      if( file_isdir(zSubpath)==1 )
#endif
      {
        recon_read_dir(zSubpath);

Changes to src/stash.c.

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        blob_read_link(&disk, zOPath);
      }else{
        blob_read_from_file(&disk, zOPath);
      }
      content_get(rid, &a);
      blob_delta_apply(&a, &delta, &b);
      if( isLink == isNewLink && blob_compare(&disk, &a)==0 ){
        if( isLink || isNewLink ){
          file_delete(zNPath);
        }
        if( isLink ){
          symlink_create(blob_str(&b), zNPath);
        }else{
          blob_write_to_file(&b, zNPath);
        }
        file_wd_setexe(zNPath, isExec);
        fossil_print("UPDATE %s\n", zNew);
      }else{
        int rc;
        if( isLink || isNewLink ){
          rc = -1;
          blob_zero(&b); /* because we reset it later */







<
<
<
<
<
<
|
<







239
240
241
242
243
244
245






246

247
248
249
250
251
252
253
        blob_read_link(&disk, zOPath);
      }else{
        blob_read_from_file(&disk, zOPath);
      }
      content_get(rid, &a);
      blob_delta_apply(&a, &delta, &b);
      if( isLink == isNewLink && blob_compare(&disk, &a)==0 ){






        create_symlink_or_file(1, isLink, isNewLink, &b, zNPath);

        file_wd_setexe(zNPath, isExec);
        fossil_print("UPDATE %s\n", zNew);
      }else{
        int rc;
        if( isLink || isNewLink ){
          rc = -1;
          blob_zero(&b); /* because we reset it later */

Changes to src/undo.c.

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    }
    if( old_exists ){
      if( new_exists ){
        fossil_print("%s   %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
      }else{
        fossil_print("NEW    %s\n", zPathname);
      }
      if( new_exists && (new_link || old_link) ){
        file_delete(zFullname);
      }
      if( old_link ){
        symlink_create(blob_str(&new), zFullname);
      }else{
        blob_write_to_file(&new, zFullname);
      }
      file_wd_setexe(zFullname, old_exe);
    }else{
      fossil_print("DELETE %s\n", zPathname);
      file_delete(zFullname);
    }
    blob_reset(&new);
    free(zFullname);







<
<
<
<
<
<
|
<







77
78
79
80
81
82
83






84

85
86
87
88
89
90
91
    }
    if( old_exists ){
      if( new_exists ){
        fossil_print("%s   %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
      }else{
        fossil_print("NEW    %s\n", zPathname);
      }






      create_symlink_or_file(new_exists, old_link, new_link, &new, zFullname);

      file_wd_setexe(zFullname, old_exe);
    }else{
      fossil_print("DELETE %s\n", zPathname);
      file_delete(zFullname);
    }
    blob_reset(&new);
    free(zFullname);

Changes to src/update.c.

750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  Manifest *pCoManifest;          /* Manifest of current checkout */
  Manifest *pRvManifest;          /* Manifest of selected revert version */
  ManifestFile *pCoFile;          /* File within current checkout manifest */
  ManifestFile *pRvFile;          /* File within revert version manifest */
  const char *zFile;              /* Filename relative to checkout root */
  const char *zRevision;          /* Selected revert version, NULL if current */
  Blob record = BLOB_INITIALIZER; /* Contents of each reverted file */
  int i;
  Stmt q;

  undo_capture_command_line();
  zRevision = find_option("revision", "r", 1);
  verify_all_options();

  if( g.argc<2 ){







|







750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  Manifest *pCoManifest;          /* Manifest of current checkout */
  Manifest *pRvManifest;          /* Manifest of selected revert version */
  ManifestFile *pCoFile;          /* File within current checkout manifest */
  ManifestFile *pRvFile;          /* File within revert version manifest */
  const char *zFile;              /* Filename relative to checkout root */
  const char *zRevision;          /* Selected revert version, NULL if current */
  Blob record = BLOB_INITIALIZER; /* Contents of each reverted file */
  int i, mayNeedDelete, mayBeLink;
  Stmt q;

  undo_capture_command_line();
  zRevision = find_option("revision", "r", 1);
  verify_all_options();

  if( g.argc<2 ){
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
                || fossil_strcmp(pRvFile->zUuid, pCoFile->zUuid)!=0;
      }

      /* Get contents of reverted-to file. */
      content_get(fast_uuid_to_rid(pRvFile->zUuid), &record);

      undo_save(zFile);
      if( file_wd_size(zFull)>=0 && (rvPerm==PERM_LNK || file_wd_islink(0)) ){
        file_delete(zFull);
      }
      if( rvPerm==PERM_LNK ){
        symlink_create(blob_str(&record), zFull);
      }else{
        blob_write_to_file(&record, zFull);
      }
      file_wd_setexe(zFull, rvPerm==PERM_EXE);
      fossil_print("REVERT   %s\n", zFile);
      mtime = file_wd_mtime(zFull);
      db_multi_exec(
         "UPDATE vfile"
         "   SET mtime=%lld, chnged=%d, deleted=0, isexe=%d, islink=%d,mrid=rid"
         " WHERE pathname=%Q OR origname=%Q",







|
|
<
|
<
<
<
<







849
850
851
852
853
854
855
856
857

858




859
860
861
862
863
864
865
                || fossil_strcmp(pRvFile->zUuid, pCoFile->zUuid)!=0;
      }

      /* Get contents of reverted-to file. */
      content_get(fast_uuid_to_rid(pRvFile->zUuid), &record);

      undo_save(zFile);
      mayNeedDelete = file_wd_size(zFull)>=0;
      mayBeLink = file_wd_islink(0);

      create_symlink_or_file(mayNeedDelete, rvPerm==PERM_LNK, mayBeLink, &record, zFull);




      file_wd_setexe(zFull, rvPerm==PERM_EXE);
      fossil_print("REVERT   %s\n", zFile);
      mtime = file_wd_mtime(zFull);
      db_multi_exec(
         "UPDATE vfile"
         "   SET mtime=%lld, chnged=%d, deleted=0, isexe=%d, islink=%d,mrid=rid"
         " WHERE pathname=%Q OR origname=%Q",

Changes to src/vfile.c.

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211









212
213
214
215
216
217
218
219
220
221
222







223
224
225
226
227
228
229
                 " WHERE vid=%d ", g.zLocalRoot, PERM_EXE, PERM_LNK, PERM_REG,
                 vid);
  while( db_step(&q)==SQLITE_ROW ){
    int id, rid, isDeleted;
    const char *zName;
    int chnged = 0;
    int oldChnged;
#ifndef _WIN32
    int origPerm;
    int currentPerm;
#endif
    i64 oldMtime;
    i64 currentMtime;
    i64 origSize;
    i64 currentSize;

    id = db_column_int(&q, 0);
    zName = db_column_text(&q, 1);
    rid = db_column_int(&q, 2);
    isDeleted = db_column_int(&q, 3);
    oldChnged = chnged = db_column_int(&q, 4);
    oldMtime = db_column_int64(&q, 7);
    origSize = db_column_int64(&q, 6);
    currentSize = file_wd_size(zName);
    currentMtime = file_wd_mtime(0);
#ifndef _WIN32
    origPerm = db_column_int(&q, 8);
    currentPerm = file_wd_perm(zName);









#endif
    if( chnged==0 && (isDeleted || rid==0) ){
      /* "fossil rm" or "fossil add" always change the file */
      chnged = 1;
    }else if( !file_wd_isfile_or_link(0) && currentSize>=0 ){
      if( cksigFlags & CKSIG_ENOTFILE ){
        fossil_warning("not an ordinary file: %s", zName);
        nErr++;
      }
      chnged = 1;
    }







    if( origSize!=currentSize ){
      if( chnged!=1 ){
        /* A file size change is definitive - the file has changed.  No
        ** need to check the mtime or hash */
        chnged = 1;
      }
    }else if( chnged==1 && rid!=0 && !isDeleted ){







<


<














<


>
>
>
>
>
>
>
>
>











>
>
>
>
>
>
>







184
185
186
187
188
189
190

191
192

193
194
195
196
197
198
199
200
201
202
203
204
205
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
                 " WHERE vid=%d ", g.zLocalRoot, PERM_EXE, PERM_LNK, PERM_REG,
                 vid);
  while( db_step(&q)==SQLITE_ROW ){
    int id, rid, isDeleted;
    const char *zName;
    int chnged = 0;
    int oldChnged;

    int origPerm;
    int currentPerm;

    i64 oldMtime;
    i64 currentMtime;
    i64 origSize;
    i64 currentSize;

    id = db_column_int(&q, 0);
    zName = db_column_text(&q, 1);
    rid = db_column_int(&q, 2);
    isDeleted = db_column_int(&q, 3);
    oldChnged = chnged = db_column_int(&q, 4);
    oldMtime = db_column_int64(&q, 7);
    origSize = db_column_int64(&q, 6);
    currentSize = file_wd_size(zName);
    currentMtime = file_wd_mtime(0);

    origPerm = db_column_int(&q, 8);
    currentPerm = file_wd_perm(zName);
#if defined(_WIN32)
    /*
    ** Windows doesn't have an execute bit, but it does support symlinks;
    ** if the current permission is not a symlink, make it the original
    ** permission (EXE or REG); if it is a symlink, leave it alone
    */
    if( currentPerm != PERM_LNK ){
      currentPerm = origPerm;
    }
#endif
    if( chnged==0 && (isDeleted || rid==0) ){
      /* "fossil rm" or "fossil add" always change the file */
      chnged = 1;
    }else if( !file_wd_isfile_or_link(0) && currentSize>=0 ){
      if( cksigFlags & CKSIG_ENOTFILE ){
        fossil_warning("not an ordinary file: %s", zName);
        nErr++;
      }
      chnged = 1;
    }
#if defined(_WIN32)
    if (win32_check_symlink_type_changed(zName)){
      if( chnged!=1 ){
        chnged = 1;
      }
    }else /* make the following if an else if */
#endif
    if( origSize!=currentSize ){
      if( chnged!=1 ){
        /* A file size change is definitive - the file has changed.  No
        ** need to check the mtime or hash */
        chnged = 1;
      }
    }else if( chnged==1 && rid!=0 && !isDeleted ){
249
250
251
252
253
254
255
256









257
258
259
260
261
262
263
      if( mtime_of_manifest_file(vid,rid,&desiredMtime)==0 ){
        if( currentMtime!=desiredMtime ){
          file_set_mtime(zName, desiredMtime);
          currentMtime = file_wd_mtime(zName);
        }
      }
    }
#ifndef _WIN32









    if( origPerm!=PERM_LNK && currentPerm==PERM_LNK ){
       /* Changing to a symlink takes priority over all other change types. */
       chnged = 7;
    }else if( chnged==0 || chnged==6 || chnged==7 || chnged==8 || chnged==9 ){
       /* Confirm metadata change types. */
      if( origPerm==currentPerm ){
        chnged = 0;







|
>
>
>
>
>
>
>
>
>







262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      if( mtime_of_manifest_file(vid,rid,&desiredMtime)==0 ){
        if( currentMtime!=desiredMtime ){
          file_set_mtime(zName, desiredMtime);
          currentMtime = file_wd_mtime(zName);
        }
      }
    }
#if !defined(_WIN32) || 1
    /*
    ** as written this block was only designed to work on posix systems;
    ** I can see no reason why it shouldn't work on Windows as well
    ** in the winsymlink branch (after making a couple of other minor
    ** tweak), so I've modified the condition to be "not windows or true";
    ** the conditional can be removed when / if this is ever merged
    ** to trunk, but I think it serves as a handy reminder for now
    ** that this code might be "special"
    */
    if( origPerm!=PERM_LNK && currentPerm==PERM_LNK ){
       /* Changing to a symlink takes priority over all other change types. */
       chnged = 7;
    }else if( chnged==0 || chnged==6 || chnged==7 || chnged==8 || chnged==9 ){
       /* Confirm metadata change types. */
      if( origPerm==currentPerm ){
        chnged = 0;
289
290
291
292
293
294
295

296
297
298
299
300
301
302
  int id,                /* Write this one file, if not zero */
  int verbose,           /* Output progress information */
  int promptFlag         /* Prompt user to confirm overwrites */
){
  Stmt q;
  Blob content;
  int nRepos = strlen(g.zLocalRoot);


  if( vid>0 && id==0 ){
    db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
                   "  FROM vfile"
                   " WHERE vid=%d AND mrid>0",
                   g.zLocalRoot, vid);
  }else{







>







311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  int id,                /* Write this one file, if not zero */
  int verbose,           /* Output progress information */
  int promptFlag         /* Prompt user to confirm overwrites */
){
  Stmt q;
  Blob content;
  int nRepos = strlen(g.zLocalRoot);
  int mayNeedDelete, mayBeLink;

  if( vid>0 && id==0 ){
    db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
                   "  FROM vfile"
                   " WHERE vid=%d AND mrid>0",
                   g.zLocalRoot, vid);
  }else{
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      }
    }
    if( verbose ) fossil_print("%s\n", &zName[nRepos]);
    if( file_wd_isdir(zName)==1 ){
      /*TODO(dchest): remove directories? */
      fossil_fatal("%s is directory, cannot overwrite", zName);
    }
    if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(0)) ){
      file_delete(zName);
    }
    if( isLink ){
      symlink_create(blob_str(&content), zName);
    }else{
      blob_write_to_file(&content, zName);
    }
    file_wd_setexe(zName, isExe);
    blob_reset(&content);
    db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
                  file_wd_mtime(zName), id);
  }
  db_finalize(&q);
}







|
<
<
|
<
<
|
<







364
365
366
367
368
369
370
371


372


373

374
375
376
377
378
379
380
      }
    }
    if( verbose ) fossil_print("%s\n", &zName[nRepos]);
    if( file_wd_isdir(zName)==1 ){
      /*TODO(dchest): remove directories? */
      fossil_fatal("%s is directory, cannot overwrite", zName);
    }
    mayNeedDelete = file_wd_size(zName)>=0;


    mayBeLink = file_wd_islink(0);


    create_symlink_or_file(mayNeedDelete, isLink, mayBeLink, &content, zName);

    file_wd_setexe(zName, isExe);
    blob_reset(&content);
    db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
                  file_wd_mtime(zName), id);
  }
  db_finalize(&q);
}
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
      }
      zUtf8 = fossil_path_to_utf8(pEntry->d_name);
      blob_appendf(pPath, "/%s", zUtf8);
      zPath = blob_str(pPath);
      if( glob_match(pIgnore1, &zPath[nPrefix+1]) ||
          glob_match(pIgnore2, &zPath[nPrefix+1]) ){
        /* do nothing */
#ifdef _DIRENT_HAVE_D_TYPE
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isdir(zPath)==1) : (pEntry->d_type==DT_DIR) ){
#else
      }else if( file_wd_isdir(zPath)==1 ){
#endif
        if( !vfile_top_of_checkout(zPath) ){
          vfile_scan(pPath, nPrefix, scanFlags, pIgnore1, pIgnore2);
        }
#ifdef _DIRENT_HAVE_D_TYPE
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isfile_or_link(zPath)) : (pEntry->d_type==DT_REG) ){
#else
      }else if( file_wd_isfile_or_link(zPath) ){
#endif
        if( (scanFlags & SCAN_TEMP)==0 || is_temporary_file(zUtf8) ){
          db_bind_text(&ins, ":file", &zPath[nPrefix+1]);







|








|







536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
      }
      zUtf8 = fossil_path_to_utf8(pEntry->d_name);
      blob_appendf(pPath, "/%s", zUtf8);
      zPath = blob_str(pPath);
      if( glob_match(pIgnore1, &zPath[nPrefix+1]) ||
          glob_match(pIgnore2, &zPath[nPrefix+1]) ){
        /* do nothing */
#if defined(_DIRENT_HAVE_D_TYPE) && !defined(_WIN32)
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isdir(zPath)==1) : (pEntry->d_type==DT_DIR) ){
#else
      }else if( file_wd_isdir(zPath)==1 ){
#endif
        if( !vfile_top_of_checkout(zPath) ){
          vfile_scan(pPath, nPrefix, scanFlags, pIgnore1, pIgnore2);
        }
#if defined(_DIRENT_HAVE_D_TYPE) && !defined(_WIN32)
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isfile_or_link(zPath)) : (pEntry->d_type==DT_REG) ){
#else
      }else if( file_wd_isfile_or_link(zPath) ){
#endif
        if( (scanFlags & SCAN_TEMP)==0 || is_temporary_file(zUtf8) ){
          db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
      zOrigPath = mprintf("%s", blob_str(pPath));
      zUtf8 = fossil_path_to_utf8(pEntry->d_name);
      blob_appendf(pPath, "/%s", zUtf8);
      zPath = blob_str(pPath);
      if( glob_match(pIgnore1, &zPath[nPrefix+1]) ||
          glob_match(pIgnore2, &zPath[nPrefix+1]) ){
        /* do nothing */
#ifdef _DIRENT_HAVE_D_TYPE
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isdir(zPath)==1) : (pEntry->d_type==DT_DIR) ){
#else
      }else if( file_wd_isdir(zPath)==1 ){
#endif
        if( (scanFlags & SCAN_NESTED) || !vfile_top_of_checkout(zPath) ){
          char *zSavePath = mprintf("%s", zPath);
          int count = vfile_dir_scan(pPath, nPrefix, scanFlags, pIgnore1,
                                     pIgnore2);
          db_bind_text(&ins, ":file", &zSavePath[nPrefix+1]);
          db_bind_int(&ins, ":count", count);
          db_step(&ins);
          db_reset(&ins);
          fossil_free(zSavePath);
          result += count; /* found X normal files? */
        }
#ifdef _DIRENT_HAVE_D_TYPE
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isfile_or_link(zPath)) : (pEntry->d_type==DT_REG) ){
#else
      }else if( file_wd_isfile_or_link(zPath) ){
#endif
        db_bind_text(&upd, ":file", zOrigPath);
        db_step(&upd);







|
















|







656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
      zOrigPath = mprintf("%s", blob_str(pPath));
      zUtf8 = fossil_path_to_utf8(pEntry->d_name);
      blob_appendf(pPath, "/%s", zUtf8);
      zPath = blob_str(pPath);
      if( glob_match(pIgnore1, &zPath[nPrefix+1]) ||
          glob_match(pIgnore2, &zPath[nPrefix+1]) ){
        /* do nothing */
#if defined(_DIRENT_HAVE_D_TYPE) && !defined(_WIN32)
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isdir(zPath)==1) : (pEntry->d_type==DT_DIR) ){
#else
      }else if( file_wd_isdir(zPath)==1 ){
#endif
        if( (scanFlags & SCAN_NESTED) || !vfile_top_of_checkout(zPath) ){
          char *zSavePath = mprintf("%s", zPath);
          int count = vfile_dir_scan(pPath, nPrefix, scanFlags, pIgnore1,
                                     pIgnore2);
          db_bind_text(&ins, ":file", &zSavePath[nPrefix+1]);
          db_bind_int(&ins, ":count", count);
          db_step(&ins);
          db_reset(&ins);
          fossil_free(zSavePath);
          result += count; /* found X normal files? */
        }
#if defined(_DIRENT_HAVE_D_TYPE) && !defined(_WIN32)
      }else if( (pEntry->d_type==DT_UNKNOWN || pEntry->d_type==DT_LNK)
          ? (file_wd_isfile_or_link(zPath)) : (pEntry->d_type==DT_REG) ){
#else
      }else if( file_wd_isfile_or_link(zPath) ){
#endif
        db_bind_text(&upd, ":file", zOrigPath);
        db_step(&upd);

Changes to src/winfile.c.

20
21
22
23
24
25
26
27







28
29
30
31














32












































33
34

35

36
37
38
39
40











41
42
43


44
45

46







47
48
49
50





























































































































































































































































































51
52
53
54
55
56
57
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */
#include <sys/stat.h>
#include <windows.h>
#include "winfile.h"








#ifndef LABEL_SECURITY_INFORMATION
#   define LABEL_SECURITY_INFORMATION (0x00000010L)
#endif















/*












































** Fill stat buf with information received from stat() or lstat().
** lstat() is called on Unix if isWd is TRUE and allow-symlinks setting is on.

**

*/
int win32_stat(const wchar_t *zFilename, struct fossilStat *buf, int isWd){
  WIN32_FILE_ATTRIBUTE_DATA attr;
  int rc = GetFileAttributesExW(zFilename, GetFileExInfoStandard, &attr);
  if( rc ){











    ULARGE_INTEGER ull;
    ull.LowPart = attr.ftLastWriteTime.dwLowDateTime;
    ull.HighPart = attr.ftLastWriteTime.dwHighDateTime;


    buf->st_mode = (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
                   S_IFDIR : S_IFREG;

    buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow;







    buf->st_mtime = ull.QuadPart / 10000000ULL - 11644473600ULL;
  }
  return !rc;
}






























































































































































































































































































/*
** Wrapper around the access() system call.  This code was copied from Tcl
** 8.6 and then modified.
*/
int win32_access(const wchar_t *zFilename, int flags){
  int rc = 0;








>
>
>
>
>
>
>

|


>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
|
>

|



>
>
>
>
>
>
>
>
>
>
>

|
<
>
>
|
|
>
|
>
>
>
>
>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */
#include <sys/stat.h>
#include <windows.h>
#include "winfile.h"

#if !defined(S_IFLNK)
# define S_IFLNK 0120000
#endif
#if !defined(SYMBOLIC_LINK_FLAG_DIRECTORY)
# define SYMBOLIC_LINK_FLAG_DIRECTORY (0x1)
#endif

#ifndef LABEL_SECURITY_INFORMATION
# define LABEL_SECURITY_INFORMATION (0x00000010L)
#endif

#ifndef FSCTL_GET_REPARSE_POINT
# define FSCTL_GET_REPARSE_POINT (((0x00000009) << 16) | ((0x00000000) << 14) | ((42) << 2) | (0))
#endif

static HANDLE dllhandle = NULL;
static DWORD (WINAPI *getFinalPathNameByHandleW) (HANDLE, LPWSTR, DWORD, DWORD) = NULL;
static BOOLEAN (APIENTRY *createSymbolicLinkW) (LPCWSTR, LPCWSTR, DWORD) = NULL;

/* a couple defines to make the borrowed struct below compile */
#ifndef _ANONYMOUS_UNION
# define _ANONYMOUS_UNION
#endif
#define DUMMYUNIONNAME

/*
** this structure copied on 20 Sept 2014 from
** https://reactos-mirror.googlecode.com/svn-history/r54752/branches/usb-bringup/include/ddk/ntifs.h
** which is a public domain file from the ReactOS DDK package.
*/

typedef struct {
  ULONG ReparseTag;
  USHORT ReparseDataLength;
  USHORT Reserved;
  _ANONYMOUS_UNION union {
    struct {
      USHORT SubstituteNameOffset;
      USHORT SubstituteNameLength;
      USHORT PrintNameOffset;
      USHORT PrintNameLength;
      ULONG Flags;
      WCHAR PathBuffer[1];
    } SymbolicLinkReparseBuffer;
    struct {
      USHORT SubstituteNameOffset;
      USHORT SubstituteNameLength;
      USHORT PrintNameOffset;
      USHORT PrintNameLength;
      WCHAR PathBuffer[1];
    } MountPointReparseBuffer;
    struct {
      UCHAR DataBuffer[1];
    } GenericReparseBuffer;
  } DUMMYUNIONNAME;
} FOSSIL_REPARSE_DATA_BUFFER;

#define LINK_BUFFER_SIZE 1024

static int isVistaOrLater(){
    if( !dllhandle ){
        HANDLE h = LoadLibraryW(L"KERNEL32");
        createSymbolicLinkW = (BOOLEAN (APIENTRY *) (LPCWSTR, LPCWSTR, DWORD)) GetProcAddress(h, "CreateSymbolicLinkW");
        getFinalPathNameByHandleW = (DWORD (WINAPI *) (HANDLE, LPWSTR, DWORD, DWORD)) GetProcAddress(h, "GetFinalPathNameByHandleW");
        dllhandle = h;
    }
    return createSymbolicLinkW != NULL;
}

/*
** Fill stat buf with information received from GetFileAttributesExW().

** Does not follow symbolic links, returning instead information about
** the link itself.
** Returns 0 on success, 1 on failure.
*/
int win32_lstat(const wchar_t *zFilename, struct fossilStat *buf){
  WIN32_FILE_ATTRIBUTE_DATA attr;
  int rc = GetFileAttributesExW(zFilename, GetFileExInfoStandard, &attr);
  if( rc ){
    ssize_t tlen = 0; /* assume it is not a symbolic link */

    /* if it is a reparse point it *might* be a symbolic link */
    /* so defer to win32_readlink to actually check */
    if( attr.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ){
      char *tname = fossil_path_to_utf8(zFilename);
      char tlink[LINK_BUFFER_SIZE];
      tlen = win32_readlink(tname, tlink, sizeof(tlink));
      fossil_path_free(tname);
    }

    ULARGE_INTEGER ull;


    /* if a link was retrieved, it is a symlink, otherwise a dir or file */
    if( tlen == 0 ){
      buf->st_mode = ((attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
                       S_IFDIR : S_IFREG);

      buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow;
    }else{
      buf->st_mode = S_IFLNK;
      buf->st_size = tlen;
    }

    ull.LowPart = attr.ftLastWriteTime.dwLowDateTime;
    ull.HighPart = attr.ftLastWriteTime.dwHighDateTime;
    buf->st_mtime = ull.QuadPart / 10000000ULL - 11644473600ULL;
  }
  return !rc;
}

/*
** Fill stat buf with information received from win32_lstat().
** If a symbolic link is found, follow it and return information about
** the target, repeating until an actual target is found.
** Limit the number of loop iterations so as to avoid an infinite loop
** due to circular links. This should never happen because
** GetFinalPathNameByHandleW() should always preclude that need, but being
** prepared to loop seems prudent, or at least not harmful.
** Returns 0 on success, 1 on failure.
*/
int win32_stat(const wchar_t *zFilename, struct fossilStat *buf){
  int rc;
  HANDLE file;
  wchar_t nextFilename[LINK_BUFFER_SIZE];
  DWORD len;
  int iterationsRemaining = 8; /* 8 is arbitrary, can be modified as needed */

  while (iterationsRemaining-- > 0){
    rc = win32_lstat(zFilename, buf);

    /* exit on error or not link */
    if( (rc != 0) || (buf->st_mode != S_IFLNK) )
      break;

    /* it is a link, so open the linked file */
    file = CreateFileW(zFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    if( (file == NULL) || (file == INVALID_HANDLE_VALUE) ){
      rc = 1;
      break;
    }

    /* get the final path name and close the handle */
    if( isVistaOrLater() ){
      len = getFinalPathNameByHandleW(file, nextFilename, LINK_BUFFER_SIZE - 1, 0);
    }else{
      len = -1;
    }
    CloseHandle(file);

    /* if any problems getting the final path name error so exit */
    if( (len <= 0) || (len > LINK_BUFFER_SIZE - 1) ){
      rc = 1;
      break;
    }

    /* prepare to try again just in case we have a chain to follow */
    /* this shouldn't happen, but just trying to be safe */
    zFilename = nextFilename;
  }

  return rc;
}

/*
** An implementation of a posix-like readlink function for win32.
** Copies the target of a symbolic link to buf if possible.
** Returns the length of the link copied to buf on success, -1 on failure.
*/
ssize_t win32_readlink(const char *path, char *buf, size_t bufsiz){
  /* assume we're going to fail */
  ssize_t rv = -1;

  /* does path reference a reparse point? */
  WIN32_FILE_ATTRIBUTE_DATA attr;
  int rc = GetFileAttributesEx(path, GetFileExInfoStandard, &attr);
  if( rc && (attr.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ){

    /* since it is a reparse point, open it */
    HANDLE file = CreateFile(path, FILE_READ_EA, 0, NULL, OPEN_EXISTING,
      FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
    if( (file != NULL) && (file != INVALID_HANDLE_VALUE) ){

      /* use DeviceIoControl to get the reparse point data */

      int data_size = sizeof(FOSSIL_REPARSE_DATA_BUFFER) + LINK_BUFFER_SIZE * sizeof(wchar_t);
      FOSSIL_REPARSE_DATA_BUFFER* data = fossil_malloc(data_size);
      DWORD data_used;

      data->ReparseTag = IO_REPARSE_TAG_SYMLINK;
      data->ReparseDataLength = 0;
      data->Reserved = 0;

      int rc = DeviceIoControl(file, FSCTL_GET_REPARSE_POINT, NULL, 0,
        data, data_size, &data_used, NULL);

      /* did the reparse point data fit into the desired buffer? */
      if( rc && (data_used < data_size) ){
        /* it fit, so setup the print name for further processing */
        USHORT
          offset = data->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(wchar_t),
          length = data->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t);
        char *temp;
        data->SymbolicLinkReparseBuffer.PathBuffer[offset + length] = 0;

        /* convert the filename to utf8, copy it, and discard the converted copy */
        temp = fossil_path_to_utf8(data->SymbolicLinkReparseBuffer.PathBuffer + offset);
        rv = strlen(temp);
        if( rv >= bufsiz )
          rv = bufsiz;
        memcpy(buf, temp, rv);
        fossil_path_free(temp);
      }

      fossil_free(data);

      /* all done, close the reparse point */
      CloseHandle(file);
    }
  }

  return rv;
}

/*
** Either unlink a file or remove a directory on win32 systems.
** To delete a symlink on a posix system, you simply unlink the entry.
** Unfortunately for our purposes, win32 differentiates between symlinks for
** files and for directories. Thus you must unlink a file symlink or rmdir a
** directory symlink. This is a convenience function used when we know we're
** deleting a symlink of some type.
** Returns 0 on success, 1 on failure.
*/
int win32_unlink_rmdir(const wchar_t *zFilename){
  int rc = 0;
  WIN32_FILE_ATTRIBUTE_DATA attr;
  if( GetFileAttributesExW(zFilename, GetFileExInfoStandard, &attr) ){
    if( (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
      rc = RemoveDirectoryW(zFilename);
    else
      rc = DeleteFileW(zFilename);
  }
  return !rc;
}

/*
** An implementation of a posix-like symlink function for win32.
** Attempts to create a file or directory symlink based on the target.
** Defaults to a file symlink if the target does not exist / can't be checked.
** Finally, if the symlink cannot be created for whatever reason (perhaps
** newpath is on a network share or a FAT derived file system), default to
** creation of a text file with the context of the link.
** Returns 0 on success, 1 on failure.
*/
int win32_symlink(const char *oldpath, const char *newpath){
  fossilStat stat;
  int created = 0;
  DWORD flags = 0x2; /*SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE*/
  wchar_t *zMbcs, *zMbcsOld;

  /* does oldpath exist? is it a dir or a file? */
  zMbcsOld = fossil_utf8_to_path(oldpath, 0);
  if( win32_stat(zMbcsOld, &stat) == 0 ){
    if( stat.st_mode == S_IFDIR ){
      flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
    }
  }

  /* remove newpath before creating the symlink */
  zMbcs = fossil_utf8_to_path(newpath, 0);
  win32_unlink_rmdir(zMbcs);
  if( isVistaOrLater() ){
    created = createSymbolicLinkW(zMbcs, zMbcsOld, flags);
  }
  fossil_path_free(zMbcs);
  fossil_path_free(zMbcsOld);

  /* if the symlink was not created, create a plain text file */
  if( !created ){
    Blob content;
    blob_set(&content, oldpath);
    blob_write_to_file(&content, newpath);
    blob_reset(&content);
    created = 1;
  }

  return !created;
}

/*
** Given a pathname to a file, return true if:
**   1. the file exists
**   2. the file is a symbolic link
**   3. the symbolic link's attributes can be acquired
**   4. the symbolic link type is different than the target type
*/
int win32_check_symlink_type_changed(const char* zName){
  int changed = 0;
  wchar_t* zMbcs;
  fossilStat lstat_buf, stat_buf;
  WIN32_FILE_ATTRIBUTE_DATA lstat_attr;
  zMbcs = fossil_utf8_to_path(zName, 0);
  if( win32_stat(zMbcs, &stat_buf) != 0 ){
    stat_buf.st_mode = S_IFREG;
  }
  changed =
    (win32_lstat(zMbcs, &lstat_buf) == 0) &&
    (lstat_buf.st_mode == S_IFLNK) &&
    GetFileAttributesExW(zMbcs, GetFileExInfoStandard, &lstat_attr) &&
    ((stat_buf.st_mode == S_IFDIR) != ((lstat_attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY));
  fossil_path_free(zMbcs);
  return changed;
}

/*
** Check if symlinks are potentially supported on the current OS for the given file.
** Theoretically this code should work on any NT based version of windows
** but I have no way of testing that. The initial check for
** IsWindowsVistaOrGreater() should in theory eliminate any system prior to
** Windows Vista, but I have no way to test that at this time.
** Return 1 if supported, 0 if not.
*/
int win32_symlinks_supported(const char* zFilename){
  TOKEN_PRIVILEGES tp;
  LUID luid;
  HANDLE process, token;
  DWORD status;
  int success;
  wchar_t *pFilename;
  wchar_t fullName[MAX_PATH+1];
  DWORD fullLength;
  wchar_t volName[MAX_PATH+1];
  DWORD fsFlags;

  /* symlinks only supported on vista or greater */
  if( !isVistaOrLater() ){
    return 0;
  }

  /* next we need to check to see if the privilege is available */

  /* can't check privilege if we can't lookup its value */
  if( !LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid) ){
    return 0;
  }

  /* can't check privilege if we can't open the process token */
  process = GetCurrentProcess();
  if( !OpenProcessToken(process, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token) ){
    return 0;
  }

  /* by this point, we have a process token and the privilege value */
  /* try to enable the privilege then close the token */

  tp.PrivilegeCount = 1;
  tp.Privileges[0].Luid = luid;
  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

  AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
  status = GetLastError();

  CloseHandle(token);

  /* any error means we failed to enable the privilege, symlinks not supported */
  if( status != ERROR_SUCCESS ){
    return 0;
  }

  /* assume no support for symlinks */
  success = 0;

  pFilename = fossil_utf8_to_path(zFilename, 0);

  /* given the filename we're interested in, symlinks are supported if */
  /* 1. we can get the full name of the path from the given path */
  fullLength = GetFullPathNameW(pFilename, sizeof(fullName), fullName, NULL);
  if( (fullLength > 0) && (fullLength < sizeof(fullName)) ){
    /* 2. we can get the volume path name from the full name */
    if( GetVolumePathNameW(fullName, volName, sizeof(volName)) ){
      /* 3. we can get volume information from the volume path name */
      if( GetVolumeInformationW(volName, NULL, 0, NULL, NULL, &fsFlags, NULL, 0) ){
        /* 4. the given volume support reparse points */
        if( fsFlags & FILE_SUPPORTS_REPARSE_POINTS ){
          /* all four conditions were true, so we support symlinks; success! */
          success = 1;
        }
      }
    }
  }

  fossil_path_free(pFilename);

  return success;
}

/*
** Wrapper around the access() system call.  This code was copied from Tcl
** 8.6 and then modified.
*/
int win32_access(const wchar_t *zFilename, int flags){
  int rc = 0;