Fossil

Check-in [2d75e87b]
Login

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

Overview
Comment:Cherrypicked a number of commits from a "bad branch" (I somehow fouled up a merge from trunk).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | winsymlink
Files: files | file ages | folders
SHA1:2d75e87b760c0a991fc3e4e8151d001bfef998e6
User & Date: sdr 2014-09-23 00:15:54
Context
2014-09-23
00:56
Merge updates from trunk. check-in: 44ded27f user: mistachkin tags: winsymlink
00:15
Cherrypicked a number of commits from a "bad branch" (I somehow fouled up a merge from trunk). check-in: 2d75e87b user: sdr tags: winsymlink
2014-09-15
01:49
Work in progress for windows symlink support. check-in: de8035cc user: sdr tags: winsymlink
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/checkin.c.

1112
1113
1114
1115
1116
1117
1118

1119




1120
1121
1122
1123
1124
1125
1126
1127
    ** 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));

      isExe = ( mPerm==PERM_EXE );




      isLink = ( mPerm==PERM_LNK );
    }

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







>

>
>
>
>
|







1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
    ** 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())
#endif
        isLink = ( mPerm==PERM_LNK );
    }

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

Changes to src/file.c.

205
206
207
208
209
210
211








212
213
214
215



216
217
218
219
220
221
222
...
228
229
230
231
232
233
234





















235
236
237
238
239
240
241
...
507
508
509
510
511
512
513


















514
515
516
517
518
519
520
      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 !defined(_WIN32)
    if( symlink(zTargetFile, zName)!=0 )
#else
    if( win32_symlink(zTargetFile, zName)!=0 )
................................................................................
  }else{
    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);
................................................................................
** Returns zero upon success.
*/
int file_delete(const char *zFilename){
  int rc;
#ifdef _WIN32
  wchar_t *z = fossil_utf8_to_filename(zFilename);
  rc = _wunlink(z);


















#else
  char *z = fossil_utf8_to_filename(zFilename);
  rc = unlink(zFilename);
#endif
  fossil_filename_free(z);
  return rc;
}







>
>
>
>
>
>
>
>




>
>
>







 







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







 







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







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
...
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
...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
      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 )
................................................................................
  }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);
................................................................................
** Returns zero upon success.
*/
int file_delete(const char *zFilename){
  int rc;
#ifdef _WIN32
  wchar_t *z = fossil_utf8_to_filename(zFilename);
  rc = _wunlink(z);
#else
  char *z = fossil_utf8_to_filename(zFilename);
  rc = unlink(zFilename);
#endif
  fossil_filename_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_filename(zFilename);
  rc = win32_unlink_rmdir(z);
#else
  char *z = fossil_utf8_to_filename(zFilename);
  rc = unlink(zFilename);
#endif
  fossil_filename_free(z);
  return rc;
}

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.

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    }
    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);







|
<
<
<
<
<
<
<







68
69
70
71
72
73
74
75







76
77
78
79
80
81
82
    }
    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.

764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
...
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
    vid = db_lget_int("checkout", 0);
    vfile_check_signature(vid, 0);
    db_multi_exec(
      "DELETE FROM vmerge;"
      "INSERT OR IGNORE INTO torevert "
      " SELECT pathname"
      "   FROM vfile "
      "  WHERE chnged OR deleted OR rid=0 OR pathname!=origname;"
    );
  }
  db_multi_exec(
    "INSERT OR IGNORE INTO torevert"
    " SELECT origname"
    "   FROM vfile"
    "  WHERE origname!=pathname AND pathname IN (SELECT name FROM torevert);"
................................................................................
        " WHERE pathname=%Q AND origname!=pathname;"
        "DELETE FROM vfile WHERE pathname=%Q",
        zFile, zFile
      );
    }else{
      sqlite3_int64 mtime;
      undo_save(zFile);
      if( file_wd_size(zFull)>=0 && (isLink || file_wd_islink(zFull)) ){
        file_delete(zFull);
      }
      if( isLink ){
        symlink_create(blob_str(&record), zFull);
      }else{
        blob_write_to_file(&record, zFull);
      }
      file_wd_setexe(zFull, isExe);
      fossil_print("REVERTED: %s\n", zFile);
      mtime = file_wd_mtime(zFull);
      db_multi_exec(
         "UPDATE vfile"
         "   SET mtime=%lld, chnged=0, deleted=0, isexe=%d, islink=%d,mrid=rid"
         " WHERE pathname=%Q OR origname=%Q",







|







 







|
<
<
<
<
<
<
<







764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
...
806
807
808
809
810
811
812
813







814
815
816
817
818
819
820
    vid = db_lget_int("checkout", 0);
    vfile_check_signature(vid, 0);
    db_multi_exec(
      "DELETE FROM vmerge;"
      "INSERT OR IGNORE INTO torevert "
      " SELECT pathname"
      "   FROM vfile "
      "  WHERE chnged OR deleted OR rid=0 OR pathname!=origname OR islink;"
    );
  }
  db_multi_exec(
    "INSERT OR IGNORE INTO torevert"
    " SELECT origname"
    "   FROM vfile"
    "  WHERE origname!=pathname AND pathname IN (SELECT name FROM torevert);"
................................................................................
        " WHERE pathname=%Q AND origname!=pathname;"
        "DELETE FROM vfile WHERE pathname=%Q",
        zFile, zFile
      );
    }else{
      sqlite3_int64 mtime;
      undo_save(zFile);
      create_symlink_or_file(file_wd_size(zFull)>=0, isLink, file_wd_islink(zFull), &record, zFull);







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

Changes to src/vfile.c.

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
      }
    }
    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\n", zName);
    }
    if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(zName)) ){
      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);
}







|
<
<
<
<
<
<
<







318
319
320
321
322
323
324
325







326
327
328
329
330
331
332
      }
    }
    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\n", zName);
    }
    create_symlink_or_file(file_wd_size(zName)>=0, isLink, file_wd_islink(zName), &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);
}

Changes to src/winfile.c.

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
...
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
#include <versionhelpers.h>
#include "winfile.h"

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

/* copy & paste from ntifs.h */









typedef struct _REPARSE_DATA_BUFFER {
  ULONG  ReparseTag;
  USHORT ReparseDataLength;
  USHORT Reserved;
  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;
  };
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;

#define LINK_BUFFER_SIZE 1024







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





    char *tname = fossil_filename_to_utf8(zFilename);
    char tlink[LINK_BUFFER_SIZE];
    ssize_t tlen = win32_readlink(tname, tlink, sizeof(tlink));



    ULARGE_INTEGER ull;


    buf->st_mode = (tlen > 0) ? S_IFLNK :
                   ((attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
                     S_IFDIR : S_IFREG);
    
    buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow;
    
    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 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 rc;
  HANDLE file;
  wchar_t nextFilename[LINK_BUFFER_SIZE];
  DWORD len;

  
  while (1){

    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, 0, NULL);
    if ((file == NULL) || (file == INVALID_HANDLE_VALUE)){
      rc = -1;
      break;
    }

    /* get the final path name and close the handle */
    len = GetFinalPathNameByHandleW(file, nextFilename, LINK_BUFFER_SIZE - 1, 0);
    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;
}






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);
................................................................................
    /* since it is a reparse point, open it */
    HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 
      FILE_FLAG_OPEN_REPARSE_POINT, NULL);      
    if ((file != NULL) && (file != INVALID_HANDLE_VALUE)){

      /* use DeviceIoControl to get the reparse point data */
    
      union {
        REPARSE_DATA_BUFFER data;
        char buffer[sizeof(REPARSE_DATA_BUFFER) + LINK_BUFFER_SIZE * sizeof(wchar_t)];
      } u;

      DWORD bytes;
      
      u.data.ReparseTag = IO_REPARSE_TAG_SYMLINK;
      u.data.ReparseDataLength = 0;
      u.data.Reserved = 0;
    
      int rc = DeviceIoControl(file, FSCTL_GET_REPARSE_POINT, NULL, 0,
        &u, sizeof(u), &bytes, NULL);


      /* did the reparse point data fit into the desired buffer? */
      if (rc && (bytes < sizeof(u))){

        /* it fit, so setup the print name for further processing */
        USHORT
          offset = u.data.SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(wchar_t),
          length = u.data.SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t);
        char *temp;
        u.data.SymbolicLinkReparseBuffer.PathBuffer[offset + length] = 0;

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


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

  return rv;
}































int win32_symlink(const char *oldpath, const char *newpath){
  fossilStat stat;
  int created = 0;
  DWORD flags = 0;
  wchar_t *zMbcs;

  /* does oldpath exist? is it a dir or a file? */  
  zMbcs = fossil_utf8_to_filename(oldpath);
  if (win32_stat(zMbcs, &stat) == 0){
    if (stat.st_mode == S_IFDIR)
      flags = SYMBOLIC_LINK_FLAG_DIRECTORY;
    DeleteFile(newpath);

    if (CreateSymbolicLink(newpath, oldpath, flags))
      created = 1;
  }



  fossil_filename_free(zMbcs);
................................................................................



  /* 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 ? 0 : -1;
}









int win32_symlinks_supported(){
  TOKEN_PRIVILEGES tp;
  LUID luid;
  HANDLE process, token;
  DWORD status;

  /* symlinks only supported on vista or greater */







|
>
>
>
>
>
>
>
>
>

|


|





|
|






|




|




>
>
>
>
>
>




>
>
>
>
>
|
|
|
>
>
>


>







 







|
|
|
>
>
>
>
>






>

<
>

>







|









|











>
>
>
>
>







 







<
<
|
<
>
|
|
|
|
|


<
>


<
>


|
|

|


|






>
>









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











<
>
|
<
|
>
>
>

 
>
>










|


>
>
>
>
>
>
>
>







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
...
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
#include <versionhelpers.h>
#include "winfile.h"

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

/* a couple defines to make the borrowed struct below compile */
#define _ANONYMOUS_UNION
#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 _REPARSE_DATA_BUFFER {
  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;
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;

#define LINK_BUFFER_SIZE 1024

/*
** 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_filename_to_utf8(zFilename);
      char tlink[LINK_BUFFER_SIZE];
      tlen = win32_readlink(tname, tlink, sizeof(tlink));
      fossil_filename_free(tname);
    }
    
    ULARGE_INTEGER ull;

    /* if a link was retrieved, it is a symlink, otherwise a dir or file */
    buf->st_mode = (tlen > 0) ? S_IFLNK :
                   ((attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ?
                     S_IFDIR : S_IFREG);
    
    buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow;
    
    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, 0, NULL);
    if ((file == NULL) || (file == INVALID_HANDLE_VALUE)){
      rc = 1;
      break;
    }

    /* get the final path name and close the handle */
    len = GetFinalPathNameByHandleW(file, nextFilename, LINK_BUFFER_SIZE - 1, 0);
    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);
................................................................................
    /* since it is a reparse point, open it */
    HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 
      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(REPARSE_DATA_BUFFER) + LINK_BUFFER_SIZE * sizeof(wchar_t);

      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_filename_to_utf8(data->SymbolicLinkReparseBuffer.PathBuffer + offset);
        rv = strlen(temp);
        if (rv >= bufsiz)
          rv = bufsiz;
        memcpy(buf, temp, rv);
        fossil_filename_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;
  fossilStat stat;
  if (win32_stat(zFilename, &stat) == 0){
    if (stat.st_mode == S_IFDIR)
      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 = 0;
  wchar_t *zMbcs;

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

  }
  fossil_filename_free(zMbcs);


  /* remove newpath before creating the symlink */
  zMbcs = fossil_utf8_to_filename(newpath);
  win32_unlink_rmdir(zMbcs);
  fossil_filename_free(zMbcs);
................................................................................

  created = CreateSymbolicLink(newpath, oldpath, flags);

  /* 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;
}

/*
** Check if symlinks are potentially supported on the current OS.
** 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(){
  TOKEN_PRIVILEGES tp;
  LUID luid;
  HANDLE process, token;
  DWORD status;

  /* symlinks only supported on vista or greater */