| | @@ -21,34 +21,233 @@ |
| 21 | 21 | #include "config.h" |
| 22 | 22 | #ifdef _WIN32 |
| 23 | 23 | /* This code is for win32 only */ |
| 24 | 24 | #include <sys/stat.h> |
| 25 | 25 | #include <windows.h> |
| 26 | +#include <versionhelpers.h> |
| 26 | 27 | #include "winfile.h" |
| 27 | 28 | |
| 28 | 29 | #ifndef LABEL_SECURITY_INFORMATION |
| 29 | 30 | # define LABEL_SECURITY_INFORMATION (0x00000010L) |
| 30 | 31 | #endif |
| 32 | + |
| 33 | +/* copy & paste from ntifs.h */ |
| 34 | +typedef struct _REPARSE_DATA_BUFFER { |
| 35 | + ULONG ReparseTag; |
| 36 | + USHORT ReparseDataLength; |
| 37 | + USHORT Reserved; |
| 38 | + union { |
| 39 | + struct { |
| 40 | + USHORT SubstituteNameOffset; |
| 41 | + USHORT SubstituteNameLength; |
| 42 | + USHORT PrintNameOffset; |
| 43 | + USHORT PrintNameLength; |
| 44 | + ULONG Flags; |
| 45 | + WCHAR PathBuffer[1]; |
| 46 | + } SymbolicLinkReparseBuffer; |
| 47 | + struct { |
| 48 | + USHORT SubstituteNameOffset; |
| 49 | + USHORT SubstituteNameLength; |
| 50 | + USHORT PrintNameOffset; |
| 51 | + USHORT PrintNameLength; |
| 52 | + WCHAR PathBuffer[1]; |
| 53 | + } MountPointReparseBuffer; |
| 54 | + struct { |
| 55 | + UCHAR DataBuffer[1]; |
| 56 | + } GenericReparseBuffer; |
| 57 | + }; |
| 58 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 59 | + |
| 60 | +#define LINK_BUFFER_SIZE 1024 |
| 61 | + |
| 62 | +int win32_lstat(const wchar_t *zFilename, struct fossilStat *buf){ |
| 63 | + WIN32_FILE_ATTRIBUTE_DATA attr; |
| 64 | + int rc = GetFileAttributesExW(zFilename, GetFileExInfoStandard, &attr); |
| 65 | + if( rc ){ |
| 66 | + char *tname = fossil_filename_to_utf8(zFilename); |
| 67 | + char tlink[LINK_BUFFER_SIZE]; |
| 68 | + ssize_t tlen = win32_readlink(tname, tlink, sizeof(tlink)); |
| 69 | + ULARGE_INTEGER ull; |
| 70 | + |
| 71 | + buf->st_mode = (tlen > 0) ? S_IFLNK : |
| 72 | + ((attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? |
| 73 | + S_IFDIR : S_IFREG); |
| 74 | + |
| 75 | + buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow; |
| 76 | + |
| 77 | + ull.LowPart = attr.ftLastWriteTime.dwLowDateTime; |
| 78 | + ull.HighPart = attr.ftLastWriteTime.dwHighDateTime; |
| 79 | + buf->st_mtime = ull.QuadPart / 10000000ULL - 11644473600ULL; |
| 80 | + } |
| 81 | + return !rc; |
| 82 | +} |
| 31 | 83 | |
| 32 | 84 | /* |
| 33 | 85 | ** Fill stat buf with information received from stat() or lstat(). |
| 34 | 86 | ** lstat() is called on Unix if isWd is TRUE and allow-symlinks setting is on. |
| 35 | 87 | ** |
| 36 | 88 | */ |
| 37 | | -int win32_stat(const wchar_t *zFilename, struct fossilStat *buf, int isWd){ |
| 89 | +int win32_stat(const wchar_t *zFilename, struct fossilStat *buf){ |
| 90 | + int rc; |
| 91 | + HANDLE file; |
| 92 | + wchar_t nextFilename[LINK_BUFFER_SIZE]; |
| 93 | + DWORD len; |
| 94 | + |
| 95 | + while (1){ |
| 96 | + rc = win32_lstat(zFilename, buf); |
| 97 | + /* exit on error or not link */ |
| 98 | + if ((rc != 0) || (buf->st_mode != S_IFLNK)) |
| 99 | + break; |
| 100 | + |
| 101 | + /* it is a link, so open the linked file */ |
| 102 | + file = CreateFileW(zFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 103 | + if ((file == NULL) || (file == INVALID_HANDLE_VALUE)){ |
| 104 | + rc = -1; |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + /* get the final path name and close the handle */ |
| 109 | + len = GetFinalPathNameByHandleW(file, nextFilename, LINK_BUFFER_SIZE - 1, 0); |
| 110 | + CloseHandle(file); |
| 111 | + |
| 112 | + /* if any problems getting the final path name error so exit */ |
| 113 | + if ((len <= 0) || (len > LINK_BUFFER_SIZE - 1)){ |
| 114 | + rc = -1; |
| 115 | + break; |
| 116 | + } |
| 117 | + |
| 118 | + /* prepare to try again just in case we have a chain to follow */ |
| 119 | + /* this shouldn't happen, but just trying to be safe */ |
| 120 | + zFilename = nextFilename; |
| 121 | + } |
| 122 | + |
| 123 | + return rc; |
| 124 | +} |
| 125 | + |
| 126 | +ssize_t win32_readlink(const char *path, char *buf, size_t bufsiz){ |
| 127 | + /* assume we're going to fail */ |
| 128 | + ssize_t rv = -1; |
| 129 | + |
| 130 | + /* does path reference a reparse point? */ |
| 38 | 131 | WIN32_FILE_ATTRIBUTE_DATA attr; |
| 39 | | - int rc = GetFileAttributesExW(zFilename, GetFileExInfoStandard, &attr); |
| 40 | | - if( rc ){ |
| 41 | | - ULARGE_INTEGER ull; |
| 42 | | - ull.LowPart = attr.ftLastWriteTime.dwLowDateTime; |
| 43 | | - ull.HighPart = attr.ftLastWriteTime.dwHighDateTime; |
| 44 | | - buf->st_mode = (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? |
| 45 | | - S_IFDIR : S_IFREG; |
| 46 | | - buf->st_size = (((i64)attr.nFileSizeHigh)<<32) | attr.nFileSizeLow; |
| 47 | | - buf->st_mtime = ull.QuadPart / 10000000ULL - 11644473600ULL; |
| 48 | | - } |
| 49 | | - return !rc; |
| 132 | + int rc = GetFileAttributesEx(path, GetFileExInfoStandard, &attr); |
| 133 | + if (rc && (attr.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)){ |
| 134 | + |
| 135 | + /* since it is a reparse point, open it */ |
| 136 | + HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
| 137 | + FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 138 | + if ((file != NULL) && (file != INVALID_HANDLE_VALUE)){ |
| 139 | + |
| 140 | + /* use DeviceIoControl to get the reparse point data */ |
| 141 | + |
| 142 | + union { |
| 143 | + REPARSE_DATA_BUFFER data; |
| 144 | + char buffer[sizeof(REPARSE_DATA_BUFFER) + LINK_BUFFER_SIZE * sizeof(wchar_t)]; |
| 145 | + } u; |
| 146 | + DWORD bytes; |
| 147 | + |
| 148 | + u.data.ReparseTag = IO_REPARSE_TAG_SYMLINK; |
| 149 | + u.data.ReparseDataLength = 0; |
| 150 | + u.data.Reserved = 0; |
| 151 | + |
| 152 | + int rc = DeviceIoControl(file, FSCTL_GET_REPARSE_POINT, NULL, 0, |
| 153 | + &u, sizeof(u), &bytes, NULL); |
| 154 | + |
| 155 | + /* did the reparse point data fit into the desired buffer? */ |
| 156 | + if (rc && (bytes < sizeof(u))){ |
| 157 | + /* it fit, so setup the print name for further processing */ |
| 158 | + USHORT |
| 159 | + offset = u.data.SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(wchar_t), |
| 160 | + length = u.data.SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t); |
| 161 | + char *temp; |
| 162 | + u.data.SymbolicLinkReparseBuffer.PathBuffer[offset + length] = 0; |
| 163 | + |
| 164 | + /* convert the filename to utf8, copy it, and discard the converted copy */ |
| 165 | + temp = fossil_filename_to_utf8(u.data.SymbolicLinkReparseBuffer.PathBuffer + offset); |
| 166 | + rv = strlen(temp); |
| 167 | + if (rv >= bufsiz) |
| 168 | + rv = bufsiz; |
| 169 | + memcpy(buf, temp, rv); |
| 170 | + fossil_filename_free(temp); |
| 171 | + } |
| 172 | + |
| 173 | + /* all done, close the reparse point */ |
| 174 | + CloseHandle(file); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return rv; |
| 179 | +} |
| 180 | + |
| 181 | +int win32_symlink(const char *oldpath, const char *newpath){ |
| 182 | + fossilStat stat; |
| 183 | + int created = 0; |
| 184 | + DWORD flags = 0; |
| 185 | + wchar_t *zMbcs; |
| 186 | + |
| 187 | + /* does oldpath exist? is it a dir or a file? */ |
| 188 | + zMbcs = fossil_utf8_to_filename(oldpath); |
| 189 | + if (win32_stat(zMbcs, &stat) == 0){ |
| 190 | + if (stat.st_mode == S_IFDIR) |
| 191 | + flags = SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 192 | + DeleteFile(newpath); |
| 193 | + if (CreateSymbolicLink(newpath, oldpath, flags)) |
| 194 | + created = 1; |
| 195 | + } |
| 196 | + fossil_filename_free(zMbcs); |
| 197 | + |
| 198 | + /* if the symlink was not created, create a plain text file */ |
| 199 | + if (!created){ |
| 200 | + Blob content; |
| 201 | + blob_set(&content, oldpath); |
| 202 | + blob_write_to_file(&content, newpath); |
| 203 | + blob_reset(&content); |
| 204 | + created = 1; |
| 205 | + } |
| 206 | + |
| 207 | + return created ? 0 : -1; |
| 208 | +} |
| 209 | + |
| 210 | +int win32_symlinks_supported(){ |
| 211 | + TOKEN_PRIVILEGES tp; |
| 212 | + LUID luid; |
| 213 | + HANDLE process, token; |
| 214 | + DWORD status; |
| 215 | + |
| 216 | + /* symlinks only supported on vista or greater */ |
| 217 | + if (!IsWindowsVistaOrGreater()) |
| 218 | + return 0; |
| 219 | + |
| 220 | + /* next we need to check to see if the privilege is available */ |
| 221 | + |
| 222 | + /* can't check privilege if we can't lookup its value */ |
| 223 | + if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
| 224 | + return 0; |
| 225 | + |
| 226 | + /* can't check privilege if we can't open the process token */ |
| 227 | + process = GetCurrentProcess(); |
| 228 | + if (!OpenProcessToken(process, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token)) |
| 229 | + return 0; |
| 230 | + |
| 231 | + /* by this point, we have a process token and the privilege value */ |
| 232 | + /* try to enable the privilege then close the token */ |
| 233 | + |
| 234 | + tp.PrivilegeCount = 1; |
| 235 | + tp.Privileges[0].Luid = luid; |
| 236 | + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 237 | + |
| 238 | + AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL); |
| 239 | + status = GetLastError(); |
| 240 | + |
| 241 | + CloseHandle(token); |
| 242 | + |
| 243 | + /* any error means we failed to enable the privilege, symlinks not supported */ |
| 244 | + if (status != ERROR_SUCCESS) |
| 245 | + return 0; |
| 246 | + |
| 247 | + /* we made it this far, symlinks must be supported */ |
| 248 | + return 1; |
| 50 | 249 | } |
| 51 | 250 | |
| 52 | 251 | /* |
| 53 | 252 | ** Wrapper around the access() system call. This code was copied from Tcl |
| 54 | 253 | ** 8.6 and then modified. |
| 55 | 254 | |