| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "mz.h" |
| 12 |
#include "mz_os.h" |
| 13 |
#include "mz_strm_os.h" |
| 14 |
|
| 15 |
#include <windows.h> |
| 16 |
#include <winioctl.h> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef SYMBOLIC_LINK_FLAG_DIRECTORY |
| 21 |
# define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1 |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifndef _WIN32_WINNT_WIN8 |
| 25 |
# define _WIN32_WINNT_WIN8 0x0602 |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
typedef struct DIR_int_s { |
| 31 |
void *find_handle; |
| 32 |
WIN32_FIND_DATAW find_data; |
| 33 |
struct dirent entry; |
| 34 |
uint8_t end; |
| 35 |
} DIR_int; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding) { |
| 40 |
wchar_t *string_wide = NULL; |
| 41 |
uint32_t string_wide_size = 0; |
| 42 |
|
| 43 |
string_wide_size = MultiByteToWideChar(encoding, 0, string, -1, NULL, 0); |
| 44 |
if (string_wide_size == 0) |
| 45 |
return NULL; |
| 46 |
string_wide = (wchar_t *)calloc(string_wide_size + 1, sizeof(wchar_t)); |
| 47 |
if (!string_wide) |
| 48 |
return NULL; |
| 49 |
|
| 50 |
MultiByteToWideChar(encoding, 0, string, -1, string_wide, string_wide_size); |
| 51 |
return string_wide; |
| 52 |
} |
| 53 |
|
| 54 |
void mz_os_unicode_string_delete(wchar_t **string) { |
| 55 |
if (string) { |
| 56 |
free(*string); |
| 57 |
*string = NULL; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
char *mz_os_utf8_string_create(const char *string, int32_t encoding) { |
| 62 |
wchar_t *string_wide = NULL; |
| 63 |
char *string_utf8 = NULL; |
| 64 |
uint32_t string_utf8_size = 0; |
| 65 |
|
| 66 |
string_wide = mz_os_unicode_string_create(string, encoding); |
| 67 |
if (string_wide) { |
| 68 |
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, NULL, 0, NULL, NULL); |
| 69 |
string_utf8 = (char *)calloc(string_utf8_size + 1, sizeof(char)); |
| 70 |
|
| 71 |
if (string_utf8) |
| 72 |
WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, string_utf8, string_utf8_size, NULL, NULL); |
| 73 |
|
| 74 |
mz_os_unicode_string_delete(&string_wide); |
| 75 |
} |
| 76 |
|
| 77 |
return string_utf8; |
| 78 |
} |
| 79 |
|
| 80 |
char *mz_os_utf8_string_create_from_unicode(const wchar_t *string, int32_t encoding) { |
| 81 |
char *string_utf8 = NULL; |
| 82 |
uint32_t string_utf8_size = 0; |
| 83 |
|
| 84 |
MZ_UNUSED(encoding); |
| 85 |
|
| 86 |
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL); |
| 87 |
string_utf8 = (char *)calloc(string_utf8_size + 1, sizeof(char)); |
| 88 |
|
| 89 |
if (string_utf8) |
| 90 |
WideCharToMultiByte(CP_UTF8, 0, string, -1, string_utf8, string_utf8_size, NULL, NULL); |
| 91 |
|
| 92 |
return string_utf8; |
| 93 |
} |
| 94 |
|
| 95 |
void mz_os_utf8_string_delete(char **string) { |
| 96 |
if (string) { |
| 97 |
free(*string); |
| 98 |
*string = NULL; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
int32_t mz_os_rand(uint8_t *buf, int32_t size) { |
| 105 |
unsigned __int64 pentium_tsc[1]; |
| 106 |
int32_t len = 0; |
| 107 |
|
| 108 |
for (len = 0; len < (int)size; len += 1) { |
| 109 |
if (len % 8 == 0) |
| 110 |
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc); |
| 111 |
buf[len] = ((unsigned char *)pentium_tsc)[len % 8]; |
| 112 |
} |
| 113 |
|
| 114 |
return len; |
| 115 |
} |
| 116 |
|
| 117 |
int32_t mz_os_rename(const char *source_path, const char *target_path) { |
| 118 |
wchar_t *source_path_wide = NULL; |
| 119 |
wchar_t *target_path_wide = NULL; |
| 120 |
int32_t result = 0; |
| 121 |
int32_t err = MZ_OK; |
| 122 |
|
| 123 |
if (!source_path || !target_path) |
| 124 |
return MZ_PARAM_ERROR; |
| 125 |
|
| 126 |
source_path_wide = mz_os_unicode_string_create(source_path, MZ_ENCODING_UTF8); |
| 127 |
if (!source_path_wide) { |
| 128 |
err = MZ_PARAM_ERROR; |
| 129 |
} else { |
| 130 |
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8); |
| 131 |
if (!target_path_wide) |
| 132 |
err = MZ_PARAM_ERROR; |
| 133 |
} |
| 134 |
|
| 135 |
if (err == MZ_OK) { |
| 136 |
#if _WIN32_WINNT >= _WIN32_WINNT_WINXP |
| 137 |
result = MoveFileExW(source_path_wide, target_path_wide, MOVEFILE_WRITE_THROUGH); |
| 138 |
#else |
| 139 |
result = MoveFileW(source_path_wide, target_path_wide); |
| 140 |
#endif |
| 141 |
if (result == 0) |
| 142 |
err = MZ_EXIST_ERROR; |
| 143 |
} |
| 144 |
|
| 145 |
if (target_path_wide) |
| 146 |
mz_os_unicode_string_delete(&target_path_wide); |
| 147 |
if (source_path_wide) |
| 148 |
mz_os_unicode_string_delete(&source_path_wide); |
| 149 |
|
| 150 |
return err; |
| 151 |
} |
| 152 |
|
| 153 |
int32_t mz_os_unlink(const char *path) { |
| 154 |
wchar_t *path_wide = NULL; |
| 155 |
int32_t result = 0; |
| 156 |
|
| 157 |
if (!path) |
| 158 |
return MZ_PARAM_ERROR; |
| 159 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 160 |
if (!path_wide) |
| 161 |
return MZ_PARAM_ERROR; |
| 162 |
|
| 163 |
if (mz_os_is_dir(path) == MZ_OK) |
| 164 |
result = RemoveDirectoryW(path_wide); |
| 165 |
else |
| 166 |
result = DeleteFileW(path_wide); |
| 167 |
|
| 168 |
mz_os_unicode_string_delete(&path_wide); |
| 169 |
|
| 170 |
if (result == 0) |
| 171 |
return MZ_EXIST_ERROR; |
| 172 |
|
| 173 |
return MZ_OK; |
| 174 |
} |
| 175 |
|
| 176 |
int32_t mz_os_file_exists(const char *path) { |
| 177 |
wchar_t *path_wide = NULL; |
| 178 |
DWORD attribs = 0; |
| 179 |
|
| 180 |
if (!path) |
| 181 |
return MZ_PARAM_ERROR; |
| 182 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 183 |
if (!path_wide) |
| 184 |
return MZ_PARAM_ERROR; |
| 185 |
|
| 186 |
attribs = GetFileAttributesW(path_wide); |
| 187 |
mz_os_unicode_string_delete(&path_wide); |
| 188 |
|
| 189 |
if (attribs == 0xFFFFFFFF) |
| 190 |
return MZ_EXIST_ERROR; |
| 191 |
|
| 192 |
return MZ_OK; |
| 193 |
} |
| 194 |
|
| 195 |
int64_t mz_os_get_file_size(const char *path) { |
| 196 |
HANDLE handle = NULL; |
| 197 |
LARGE_INTEGER large_size; |
| 198 |
wchar_t *path_wide = NULL; |
| 199 |
|
| 200 |
if (!path) |
| 201 |
return MZ_PARAM_ERROR; |
| 202 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 203 |
if (!path_wide) |
| 204 |
return MZ_PARAM_ERROR; |
| 205 |
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 206 |
handle = CreateFile2(path_wide, GENERIC_READ, 0, OPEN_EXISTING, NULL); |
| 207 |
#else |
| 208 |
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 209 |
#endif |
| 210 |
mz_os_unicode_string_delete(&path_wide); |
| 211 |
|
| 212 |
large_size.QuadPart = 0; |
| 213 |
|
| 214 |
if (handle != INVALID_HANDLE_VALUE) { |
| 215 |
GetFileSizeEx(handle, &large_size); |
| 216 |
CloseHandle(handle); |
| 217 |
} |
| 218 |
|
| 219 |
return large_size.QuadPart; |
| 220 |
} |
| 221 |
|
| 222 |
static void mz_os_file_to_unix_time(FILETIME file_time, time_t *unix_time) { |
| 223 |
uint64_t quad_file_time = 0; |
| 224 |
quad_file_time = file_time.dwLowDateTime; |
| 225 |
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32); |
| 226 |
*unix_time = (time_t)((quad_file_time - 116444736000000000LL) / 10000000); |
| 227 |
} |
| 228 |
|
| 229 |
static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time) { |
| 230 |
uint64_t quad_file_time = 0; |
| 231 |
quad_file_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL; |
| 232 |
file_time->dwHighDateTime = (quad_file_time >> 32); |
| 233 |
file_time->dwLowDateTime = (uint32_t)(quad_file_time); |
| 234 |
} |
| 235 |
|
| 236 |
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date) { |
| 237 |
WIN32_FILE_ATTRIBUTE_DATA wfad; |
| 238 |
wchar_t *path_wide = NULL; |
| 239 |
int32_t err = MZ_INTERNAL_ERROR; |
| 240 |
|
| 241 |
if (!path) |
| 242 |
return MZ_PARAM_ERROR; |
| 243 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 244 |
if (!path_wide) |
| 245 |
return MZ_PARAM_ERROR; |
| 246 |
|
| 247 |
if (GetFileAttributesExW(path_wide, GetFileExInfoStandard, &wfad)) { |
| 248 |
if (modified_date) |
| 249 |
mz_os_file_to_unix_time(wfad.ftLastWriteTime, modified_date); |
| 250 |
if (accessed_date) |
| 251 |
mz_os_file_to_unix_time(wfad.ftLastAccessTime, accessed_date); |
| 252 |
if (creation_date) |
| 253 |
mz_os_file_to_unix_time(wfad.ftCreationTime, creation_date); |
| 254 |
|
| 255 |
err = MZ_OK; |
| 256 |
} |
| 257 |
|
| 258 |
free(path_wide); |
| 259 |
|
| 260 |
return err; |
| 261 |
} |
| 262 |
|
| 263 |
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date) { |
| 264 |
HANDLE handle = NULL; |
| 265 |
FILETIME ftm_creation, ftm_accessed, ftm_modified; |
| 266 |
wchar_t *path_wide = NULL; |
| 267 |
int32_t err = MZ_OK; |
| 268 |
|
| 269 |
if (!path) |
| 270 |
return MZ_PARAM_ERROR; |
| 271 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 272 |
if (!path_wide) |
| 273 |
return MZ_PARAM_ERROR; |
| 274 |
|
| 275 |
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 276 |
handle = CreateFile2(path_wide, GENERIC_READ | GENERIC_WRITE, 0, OPEN_EXISTING, NULL); |
| 277 |
#else |
| 278 |
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 279 |
#endif |
| 280 |
mz_os_unicode_string_delete(&path_wide); |
| 281 |
|
| 282 |
if (handle != INVALID_HANDLE_VALUE) { |
| 283 |
GetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified); |
| 284 |
|
| 285 |
if (modified_date != 0) |
| 286 |
mz_os_unix_to_file_time(modified_date, &ftm_modified); |
| 287 |
if (accessed_date != 0) |
| 288 |
mz_os_unix_to_file_time(accessed_date, &ftm_accessed); |
| 289 |
if (creation_date != 0) |
| 290 |
mz_os_unix_to_file_time(creation_date, &ftm_creation); |
| 291 |
|
| 292 |
if (SetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified) == 0) |
| 293 |
err = MZ_INTERNAL_ERROR; |
| 294 |
|
| 295 |
CloseHandle(handle); |
| 296 |
} |
| 297 |
|
| 298 |
return err; |
| 299 |
} |
| 300 |
|
| 301 |
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes) { |
| 302 |
wchar_t *path_wide = NULL; |
| 303 |
int32_t err = MZ_OK; |
| 304 |
|
| 305 |
if (!path || !attributes) |
| 306 |
return MZ_PARAM_ERROR; |
| 307 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 308 |
if (!path_wide) |
| 309 |
return MZ_PARAM_ERROR; |
| 310 |
|
| 311 |
*attributes = GetFileAttributesW(path_wide); |
| 312 |
mz_os_unicode_string_delete(&path_wide); |
| 313 |
|
| 314 |
if (*attributes == INVALID_FILE_ATTRIBUTES) |
| 315 |
err = MZ_INTERNAL_ERROR; |
| 316 |
|
| 317 |
return err; |
| 318 |
} |
| 319 |
|
| 320 |
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes) { |
| 321 |
wchar_t *path_wide = NULL; |
| 322 |
int32_t err = MZ_OK; |
| 323 |
|
| 324 |
if (!path) |
| 325 |
return MZ_PARAM_ERROR; |
| 326 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 327 |
if (!path_wide) |
| 328 |
return MZ_PARAM_ERROR; |
| 329 |
|
| 330 |
if (SetFileAttributesW(path_wide, attributes) == 0) |
| 331 |
err = MZ_INTERNAL_ERROR; |
| 332 |
mz_os_unicode_string_delete(&path_wide); |
| 333 |
|
| 334 |
return err; |
| 335 |
} |
| 336 |
|
| 337 |
int32_t mz_os_make_dir(const char *path) { |
| 338 |
wchar_t *path_wide = NULL; |
| 339 |
int32_t err = MZ_OK; |
| 340 |
|
| 341 |
if (!path) |
| 342 |
return MZ_PARAM_ERROR; |
| 343 |
|
| 344 |
|
| 345 |
if ((path[0] != 0) && (strlen(path) <= 3) && (path[1] == ':')) |
| 346 |
return mz_os_is_dir(path); |
| 347 |
|
| 348 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 349 |
if (!path_wide) |
| 350 |
return MZ_PARAM_ERROR; |
| 351 |
|
| 352 |
if (CreateDirectoryW(path_wide, NULL) == 0) { |
| 353 |
if (GetLastError() != ERROR_ALREADY_EXISTS) |
| 354 |
err = MZ_INTERNAL_ERROR; |
| 355 |
} |
| 356 |
|
| 357 |
mz_os_unicode_string_delete(&path_wide); |
| 358 |
|
| 359 |
return err; |
| 360 |
} |
| 361 |
|
| 362 |
DIR *mz_os_open_dir(const char *path) { |
| 363 |
WIN32_FIND_DATAW find_data; |
| 364 |
DIR_int *dir_int = NULL; |
| 365 |
wchar_t *path_wide = NULL; |
| 366 |
char fixed_path[320]; |
| 367 |
void *handle = NULL; |
| 368 |
|
| 369 |
if (!path) |
| 370 |
return NULL; |
| 371 |
|
| 372 |
strncpy(fixed_path, path, sizeof(fixed_path) - 1); |
| 373 |
fixed_path[sizeof(fixed_path) - 1] = 0; |
| 374 |
|
| 375 |
mz_path_append_slash(fixed_path, sizeof(fixed_path), MZ_PATH_SLASH_PLATFORM); |
| 376 |
mz_path_combine(fixed_path, "*", sizeof(fixed_path)); |
| 377 |
|
| 378 |
path_wide = mz_os_unicode_string_create(fixed_path, MZ_ENCODING_UTF8); |
| 379 |
if (!path_wide) |
| 380 |
return NULL; |
| 381 |
|
| 382 |
handle = FindFirstFileW(path_wide, &find_data); |
| 383 |
mz_os_unicode_string_delete(&path_wide); |
| 384 |
|
| 385 |
if (handle == INVALID_HANDLE_VALUE) |
| 386 |
return NULL; |
| 387 |
|
| 388 |
dir_int = (DIR_int *)malloc(sizeof(DIR_int)); |
| 389 |
if (!dir_int) { |
| 390 |
FindClose(handle); |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
dir_int->find_handle = handle; |
| 394 |
dir_int->end = 0; |
| 395 |
|
| 396 |
memcpy(&dir_int->find_data, &find_data, sizeof(dir_int->find_data)); |
| 397 |
|
| 398 |
return (DIR *)dir_int; |
| 399 |
} |
| 400 |
|
| 401 |
struct dirent *mz_os_read_dir(DIR *dir) { |
| 402 |
DIR_int *dir_int; |
| 403 |
|
| 404 |
if (!dir) |
| 405 |
return NULL; |
| 406 |
|
| 407 |
dir_int = (DIR_int *)dir; |
| 408 |
if (dir_int->end) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1, dir_int->entry.d_name, |
| 412 |
sizeof(dir_int->entry.d_name), NULL, NULL); |
| 413 |
|
| 414 |
if (FindNextFileW(dir_int->find_handle, &dir_int->find_data) == 0) { |
| 415 |
if (GetLastError() != ERROR_NO_MORE_FILES) |
| 416 |
return NULL; |
| 417 |
|
| 418 |
dir_int->end = 1; |
| 419 |
} |
| 420 |
|
| 421 |
return &dir_int->entry; |
| 422 |
} |
| 423 |
|
| 424 |
int32_t mz_os_close_dir(DIR *dir) { |
| 425 |
DIR_int *dir_int; |
| 426 |
|
| 427 |
if (!dir) |
| 428 |
return MZ_PARAM_ERROR; |
| 429 |
|
| 430 |
dir_int = (DIR_int *)dir; |
| 431 |
if (dir_int->find_handle != INVALID_HANDLE_VALUE) |
| 432 |
FindClose(dir_int->find_handle); |
| 433 |
free(dir_int); |
| 434 |
return MZ_OK; |
| 435 |
} |
| 436 |
|
| 437 |
int32_t mz_os_is_dir_separator(const char c) { |
| 438 |
return c == '\\' || c == '/'; |
| 439 |
} |
| 440 |
|
| 441 |
int32_t mz_os_is_dir(const char *path) { |
| 442 |
wchar_t *path_wide = NULL; |
| 443 |
uint32_t attribs = 0; |
| 444 |
|
| 445 |
if (!path) |
| 446 |
return MZ_PARAM_ERROR; |
| 447 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 448 |
if (!path_wide) |
| 449 |
return MZ_PARAM_ERROR; |
| 450 |
|
| 451 |
attribs = GetFileAttributesW(path_wide); |
| 452 |
mz_os_unicode_string_delete(&path_wide); |
| 453 |
|
| 454 |
if (attribs != 0xFFFFFFFF) { |
| 455 |
if (attribs & FILE_ATTRIBUTE_DIRECTORY) |
| 456 |
return MZ_OK; |
| 457 |
} |
| 458 |
|
| 459 |
return MZ_EXIST_ERROR; |
| 460 |
} |
| 461 |
|
| 462 |
int32_t mz_os_is_symlink(const char *path) { |
| 463 |
wchar_t *path_wide = NULL; |
| 464 |
uint32_t attribs = 0; |
| 465 |
|
| 466 |
if (!path) |
| 467 |
return MZ_PARAM_ERROR; |
| 468 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 469 |
if (!path_wide) |
| 470 |
return MZ_PARAM_ERROR; |
| 471 |
|
| 472 |
attribs = GetFileAttributesW(path_wide); |
| 473 |
mz_os_unicode_string_delete(&path_wide); |
| 474 |
|
| 475 |
if (attribs != 0xFFFFFFFF) { |
| 476 |
if (attribs & FILE_ATTRIBUTE_REPARSE_POINT) |
| 477 |
return MZ_OK; |
| 478 |
} |
| 479 |
|
| 480 |
return MZ_EXIST_ERROR; |
| 481 |
} |
| 482 |
|
| 483 |
int32_t mz_os_make_symlink(const char *path, const char *target_path) { |
| 484 |
typedef BOOLEAN(WINAPI * LPCREATESYMBOLICLINKW)(LPCWSTR, LPCWSTR, DWORD); |
| 485 |
MEMORY_BASIC_INFORMATION mbi; |
| 486 |
LPCREATESYMBOLICLINKW create_symbolic_link_w = NULL; |
| 487 |
HMODULE kernel32_mod = NULL; |
| 488 |
wchar_t *path_wide = NULL; |
| 489 |
wchar_t *target_path_wide = NULL; |
| 490 |
int32_t err = MZ_OK; |
| 491 |
int32_t flags = 0; |
| 492 |
|
| 493 |
if (!path) |
| 494 |
return MZ_PARAM_ERROR; |
| 495 |
|
| 496 |
|
| 497 |
memset(&mbi, 0, sizeof(mbi)); |
| 498 |
VirtualQuery(VirtualQuery, &mbi, sizeof(mbi)); |
| 499 |
kernel32_mod = (HMODULE)mbi.AllocationBase; |
| 500 |
|
| 501 |
if (!kernel32_mod) |
| 502 |
return MZ_SUPPORT_ERROR; |
| 503 |
|
| 504 |
create_symbolic_link_w = (LPCREATESYMBOLICLINKW)GetProcAddress(kernel32_mod, "CreateSymbolicLinkW"); |
| 505 |
if (!create_symbolic_link_w) { |
| 506 |
return MZ_SUPPORT_ERROR; |
| 507 |
} |
| 508 |
|
| 509 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 510 |
if (!path_wide) { |
| 511 |
return MZ_PARAM_ERROR; |
| 512 |
} |
| 513 |
|
| 514 |
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8); |
| 515 |
if (target_path_wide) { |
| 516 |
if (mz_path_has_slash(target_path) == MZ_OK) |
| 517 |
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 518 |
|
| 519 |
if (create_symbolic_link_w(path_wide, target_path_wide, flags) == FALSE) |
| 520 |
err = MZ_SYMLINK_ERROR; |
| 521 |
|
| 522 |
mz_os_unicode_string_delete(&target_path_wide); |
| 523 |
} else { |
| 524 |
err = MZ_PARAM_ERROR; |
| 525 |
} |
| 526 |
|
| 527 |
mz_os_unicode_string_delete(&path_wide); |
| 528 |
|
| 529 |
return err; |
| 530 |
} |
| 531 |
|
| 532 |
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path) { |
| 533 |
typedef struct _REPARSE_DATA_BUFFER { |
| 534 |
ULONG ReparseTag; |
| 535 |
USHORT ReparseDataLength; |
| 536 |
USHORT Reserved; |
| 537 |
union { |
| 538 |
struct { |
| 539 |
USHORT SubstituteNameOffset; |
| 540 |
USHORT SubstituteNameLength; |
| 541 |
USHORT PrintNameOffset; |
| 542 |
USHORT PrintNameLength; |
| 543 |
ULONG Flags; |
| 544 |
WCHAR PathBuffer[1]; |
| 545 |
} SymbolicLinkReparseBuffer; |
| 546 |
struct { |
| 547 |
USHORT SubstituteNameOffset; |
| 548 |
USHORT SubstituteNameLength; |
| 549 |
USHORT PrintNameOffset; |
| 550 |
USHORT PrintNameLength; |
| 551 |
WCHAR PathBuffer[1]; |
| 552 |
} MountPointReparseBuffer; |
| 553 |
struct { |
| 554 |
UCHAR DataBuffer[1]; |
| 555 |
} GenericReparseBuffer; |
| 556 |
}; |
| 557 |
} REPARSE_DATA_BUFFER; |
| 558 |
REPARSE_DATA_BUFFER *reparse_data = NULL; |
| 559 |
DWORD length = 0; |
| 560 |
HANDLE handle = NULL; |
| 561 |
wchar_t *path_wide = NULL; |
| 562 |
wchar_t *target_path_wide = NULL; |
| 563 |
uint8_t buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 564 |
int32_t target_path_len = 0; |
| 565 |
int32_t target_path_idx = 0; |
| 566 |
int32_t err = MZ_OK; |
| 567 |
char *target_path_utf8 = NULL; |
| 568 |
|
| 569 |
if (!path) |
| 570 |
return MZ_PARAM_ERROR; |
| 571 |
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8); |
| 572 |
if (!path_wide) |
| 573 |
return MZ_PARAM_ERROR; |
| 574 |
|
| 575 |
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 |
| 576 |
CREATEFILE2_EXTENDED_PARAMETERS extended_params; |
| 577 |
memset(&extended_params, 0, sizeof(extended_params)); |
| 578 |
extended_params.dwSize = sizeof(extended_params); |
| 579 |
extended_params.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 580 |
extended_params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT; |
| 581 |
extended_params.dwSecurityQosFlags = SECURITY_ANONYMOUS; |
| 582 |
extended_params.lpSecurityAttributes = NULL; |
| 583 |
extended_params.hTemplateFile = NULL; |
| 584 |
handle = CreateFile2(path_wide, FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &extended_params); |
| 585 |
#else |
| 586 |
handle = CreateFileW(path_wide, FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
| 587 |
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 588 |
#endif |
| 589 |
|
| 590 |
if (handle == INVALID_HANDLE_VALUE) { |
| 591 |
mz_os_unicode_string_delete(&path_wide); |
| 592 |
return MZ_OPEN_ERROR; |
| 593 |
} |
| 594 |
|
| 595 |
if (DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, sizeof(buffer), &length, NULL) == TRUE) { |
| 596 |
reparse_data = (REPARSE_DATA_BUFFER *)buffer; |
| 597 |
if ((IsReparseTagMicrosoft(reparse_data->ReparseTag)) && (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK)) { |
| 598 |
target_path_len = max_target_path * sizeof(wchar_t); |
| 599 |
if (target_path_len > reparse_data->SymbolicLinkReparseBuffer.PrintNameLength) |
| 600 |
target_path_len = reparse_data->SymbolicLinkReparseBuffer.PrintNameLength; |
| 601 |
|
| 602 |
target_path_wide = (wchar_t *)malloc(target_path_len + sizeof(wchar_t)); |
| 603 |
if (target_path_wide) { |
| 604 |
target_path_idx = reparse_data->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(wchar_t); |
| 605 |
memcpy(target_path_wide, &reparse_data->SymbolicLinkReparseBuffer.PathBuffer[target_path_idx], |
| 606 |
target_path_len); |
| 607 |
|
| 608 |
target_path_wide[target_path_len / sizeof(wchar_t)] = 0; |
| 609 |
target_path_utf8 = mz_os_utf8_string_create_from_unicode(target_path_wide, MZ_ENCODING_UTF8); |
| 610 |
|
| 611 |
if (target_path_utf8) { |
| 612 |
strncpy(target_path, target_path_utf8, max_target_path - 1); |
| 613 |
target_path[max_target_path - 1] = 0; |
| 614 |
|
| 615 |
if (mz_os_is_dir(target_path_utf8) == MZ_OK) |
| 616 |
mz_path_append_slash(target_path, max_target_path, MZ_PATH_SLASH_PLATFORM); |
| 617 |
mz_os_utf8_string_delete(&target_path_utf8); |
| 618 |
} else { |
| 619 |
err = MZ_MEM_ERROR; |
| 620 |
} |
| 621 |
|
| 622 |
free(target_path_wide); |
| 623 |
} else { |
| 624 |
err = MZ_MEM_ERROR; |
| 625 |
} |
| 626 |
} |
| 627 |
} else { |
| 628 |
err = MZ_INTERNAL_ERROR; |
| 629 |
} |
| 630 |
|
| 631 |
CloseHandle(handle); |
| 632 |
mz_os_unicode_string_delete(&path_wide); |
| 633 |
return err; |
| 634 |
} |
| 635 |
|
| 636 |
uint64_t mz_os_ms_time(void) { |
| 637 |
SYSTEMTIME system_time; |
| 638 |
FILETIME file_time; |
| 639 |
uint64_t quad_file_time = 0; |
| 640 |
|
| 641 |
GetSystemTime(&system_time); |
| 642 |
SystemTimeToFileTime(&system_time, &file_time); |
| 643 |
|
| 644 |
quad_file_time = file_time.dwLowDateTime; |
| 645 |
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32); |
| 646 |
|
| 647 |
return quad_file_time / 10000 - 11644473600000LL; |
| 648 |
} |