| 1 |
#ifndef fs_c |
| 2 |
#define fs_c |
| 3 |
|
| 4 |
#include "fs.h" |
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
|
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <stdarg.h> |
| 13 |
|
| 14 |
#if defined(__clang__) && defined(__has_attribute) |
| 15 |
#if __has_attribute(suppress) |
| 16 |
#define FS_SUPPRESS_CLANG_ANALYZER __attribute__((suppress)) |
| 17 |
#else |
| 18 |
#define FS_SUPPRESS_CLANG_ANALYZER |
| 19 |
#endif |
| 20 |
#else |
| 21 |
#define FS_SUPPRESS_CLANG_ANALYZER |
| 22 |
#endif |
| 23 |
|
| 24 |
|
| 25 |
#ifndef fs_va_copy |
| 26 |
#if !defined(_MSC_VER) || _MSC_VER >= 1800 |
| 27 |
#if !defined(__STDC_VERSION__) || (defined(__GNUC__) && __GNUC__ < 3) |
| 28 |
#if defined(__va_copy) |
| 29 |
#define fs_va_copy(dst, src) __va_copy(dst, src) |
| 30 |
#else |
| 31 |
#define fs_va_copy(dst, src) ((dst) = (src)) |
| 32 |
#endif |
| 33 |
#else |
| 34 |
#define fs_va_copy(dst, src) va_copy((dst), (src)) |
| 35 |
#endif |
| 36 |
#else |
| 37 |
#define fs_va_copy(dst, src) ((dst) = (src)) |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
#define FS_UNUSED(x) (void)x |
| 43 |
|
| 44 |
#ifndef FS_MALLOC |
| 45 |
#define FS_MALLOC(sz) malloc((sz)) |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifndef FS_REALLOC |
| 49 |
#define FS_REALLOC(p, sz) realloc((p), (sz)) |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifndef FS_FREE |
| 53 |
#define FS_FREE(p) free((p)) |
| 54 |
#endif |
| 55 |
|
| 56 |
static void fs_zero_memory_default(void* p, size_t sz) |
| 57 |
{ |
| 58 |
if (sz > 0) { |
| 59 |
memset(p, 0, sz); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
#ifndef FS_ZERO_MEMORY |
| 64 |
#define FS_ZERO_MEMORY(p, sz) fs_zero_memory_default((p), (sz)) |
| 65 |
#endif |
| 66 |
|
| 67 |
#ifndef FS_COPY_MEMORY |
| 68 |
#define FS_COPY_MEMORY(dst, src, sz) memcpy((dst), (src), (sz)) |
| 69 |
#endif |
| 70 |
|
| 71 |
#ifndef FS_MOVE_MEMORY |
| 72 |
#define FS_MOVE_MEMORY(dst, src, sz) memmove((dst), (src), (sz)) |
| 73 |
#endif |
| 74 |
|
| 75 |
#ifndef FS_ASSERT |
| 76 |
#define FS_ASSERT(condition) assert(condition) |
| 77 |
#endif |
| 78 |
|
| 79 |
#define FS_ZERO_OBJECT(p) FS_ZERO_MEMORY((p), sizeof(*(p))) |
| 80 |
#define FS_COUNTOF(x) (sizeof(x) / sizeof(x[0])) |
| 81 |
#define FS_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 82 |
#define FS_MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 83 |
#define FS_ABS(x) (((x) > 0) ? (x) : -(x)) |
| 84 |
#define FS_CLAMP(x, lo, hi) (FS_MAX((lo), FS_MIN((x), (hi)))) |
| 85 |
#define FS_OFFSET_PTR(p, offset) (((unsigned char*)(p)) + (offset)) |
| 86 |
#define FS_ALIGN(x, a) ((x + (a-1)) & ~(a-1)) |
| 87 |
|
| 88 |
|
| 89 |
FS_API char* fs_strcpy(char* dst, const char* src) |
| 90 |
{ |
| 91 |
char* dstorig; |
| 92 |
|
| 93 |
FS_ASSERT(dst != NULL); |
| 94 |
FS_ASSERT(src != NULL); |
| 95 |
|
| 96 |
dstorig = dst; |
| 97 |
|
| 98 |
|
| 99 |
for (;;) { |
| 100 |
*dst = *src; |
| 101 |
if (*src == '\0') { |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
dst += 1; |
| 106 |
src += 1; |
| 107 |
} |
| 108 |
|
| 109 |
return dstorig; |
| 110 |
} |
| 111 |
|
| 112 |
FS_API int fs_strncpy(char* dst, const char* src, size_t count) |
| 113 |
{ |
| 114 |
size_t maxcount; |
| 115 |
size_t i; |
| 116 |
|
| 117 |
if (dst == 0) { |
| 118 |
return EINVAL; |
| 119 |
} |
| 120 |
if (src == 0) { |
| 121 |
dst[0] = '\0'; |
| 122 |
return EINVAL; |
| 123 |
} |
| 124 |
|
| 125 |
maxcount = count; |
| 126 |
|
| 127 |
for (i = 0; i < maxcount && src[i] != '\0'; ++i) { |
| 128 |
dst[i] = src[i]; |
| 129 |
} |
| 130 |
|
| 131 |
if (src[i] == '\0' || i == count || count == ((size_t)-1)) { |
| 132 |
dst[i] = '\0'; |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
dst[0] = '\0'; |
| 137 |
return ERANGE; |
| 138 |
} |
| 139 |
|
| 140 |
FS_API int fs_strcpy_s(char* dst, size_t dstCap, const char* src) |
| 141 |
{ |
| 142 |
size_t i; |
| 143 |
|
| 144 |
if (dst == 0) { |
| 145 |
return EINVAL; |
| 146 |
} |
| 147 |
if (dstCap == 0) { |
| 148 |
return ERANGE; |
| 149 |
} |
| 150 |
if (src == 0) { |
| 151 |
dst[0] = '\0'; |
| 152 |
return EINVAL; |
| 153 |
} |
| 154 |
|
| 155 |
for (i = 0; i < dstCap && src[i] != '\0'; ++i) { |
| 156 |
dst[i] = src[i]; |
| 157 |
} |
| 158 |
|
| 159 |
if (i < dstCap) { |
| 160 |
dst[i] = '\0'; |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
dst[0] = '\0'; |
| 165 |
return ERANGE; |
| 166 |
} |
| 167 |
|
| 168 |
FS_API int fs_strncpy_s(char* dst, size_t dstCap, const char* src, size_t count) |
| 169 |
{ |
| 170 |
size_t maxcount; |
| 171 |
size_t i; |
| 172 |
|
| 173 |
if (dst == 0) { |
| 174 |
return EINVAL; |
| 175 |
} |
| 176 |
if (dstCap == 0) { |
| 177 |
return EINVAL; |
| 178 |
} |
| 179 |
if (src == 0) { |
| 180 |
dst[0] = '\0'; |
| 181 |
return EINVAL; |
| 182 |
} |
| 183 |
|
| 184 |
maxcount = count; |
| 185 |
if (count == ((size_t)-1) || count >= dstCap) { |
| 186 |
maxcount = dstCap - 1; |
| 187 |
} |
| 188 |
|
| 189 |
for (i = 0; i < maxcount && src[i] != '\0'; ++i) { |
| 190 |
dst[i] = src[i]; |
| 191 |
} |
| 192 |
|
| 193 |
if (src[i] == '\0' || i == count || count == ((size_t)-1)) { |
| 194 |
dst[i] = '\0'; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
dst[0] = '\0'; |
| 199 |
return ERANGE; |
| 200 |
} |
| 201 |
|
| 202 |
FS_API int fs_strcat_s(char* dst, size_t dstCap, const char* src) |
| 203 |
{ |
| 204 |
char* dstorig; |
| 205 |
|
| 206 |
if (dst == 0) { |
| 207 |
return EINVAL; |
| 208 |
} |
| 209 |
if (dstCap == 0) { |
| 210 |
return ERANGE; |
| 211 |
} |
| 212 |
if (src == 0) { |
| 213 |
dst[0] = '\0'; |
| 214 |
return EINVAL; |
| 215 |
} |
| 216 |
|
| 217 |
dstorig = dst; |
| 218 |
|
| 219 |
while (dstCap > 0 && dst[0] != '\0') { |
| 220 |
dst += 1; |
| 221 |
dstCap -= 1; |
| 222 |
} |
| 223 |
|
| 224 |
if (dstCap == 0) { |
| 225 |
return EINVAL; |
| 226 |
} |
| 227 |
|
| 228 |
while (dstCap > 0 && src[0] != '\0') { |
| 229 |
*dst++ = *src++; |
| 230 |
dstCap -= 1; |
| 231 |
} |
| 232 |
|
| 233 |
if (dstCap > 0) { |
| 234 |
dst[0] = '\0'; |
| 235 |
} else { |
| 236 |
dstorig[0] = '\0'; |
| 237 |
return ERANGE; |
| 238 |
} |
| 239 |
|
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
FS_API int fs_strncat_s(char* dst, size_t dstCap, const char* src, size_t count) |
| 244 |
{ |
| 245 |
char* dstorig; |
| 246 |
|
| 247 |
if (dst == 0) { |
| 248 |
return EINVAL; |
| 249 |
} |
| 250 |
if (dstCap == 0) { |
| 251 |
return ERANGE; |
| 252 |
} |
| 253 |
if (src == 0) { |
| 254 |
return EINVAL; |
| 255 |
} |
| 256 |
|
| 257 |
dstorig = dst; |
| 258 |
|
| 259 |
while (dstCap > 0 && dst[0] != '\0') { |
| 260 |
dst += 1; |
| 261 |
dstCap -= 1; |
| 262 |
} |
| 263 |
|
| 264 |
if (dstCap == 0) { |
| 265 |
return EINVAL; |
| 266 |
} |
| 267 |
|
| 268 |
if (count == ((size_t)-1)) { |
| 269 |
count = dstCap - 1; |
| 270 |
} |
| 271 |
|
| 272 |
while (dstCap > 0 && src[0] != '\0' && count > 0) { |
| 273 |
*dst++ = *src++; |
| 274 |
dstCap -= 1; |
| 275 |
count -= 1; |
| 276 |
} |
| 277 |
|
| 278 |
if (dstCap > 0) { |
| 279 |
dst[0] = '\0'; |
| 280 |
} else { |
| 281 |
dstorig[0] = '\0'; |
| 282 |
return ERANGE; |
| 283 |
} |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
FS_API int fs_strncmp(const char* str1, const char* str2, size_t maxLen) |
| 289 |
{ |
| 290 |
if (str1 == str2) return 0; |
| 291 |
|
| 292 |
|
| 293 |
if (str1 == NULL) return -1; |
| 294 |
if (str2 == NULL) return 1; |
| 295 |
|
| 296 |
|
| 297 |
for (;;) { |
| 298 |
if (maxLen == 0) { |
| 299 |
break; |
| 300 |
} |
| 301 |
|
| 302 |
if (str1[0] == '\0') { |
| 303 |
break; |
| 304 |
} |
| 305 |
|
| 306 |
if (str1[0] != str2[0]) { |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
str1 += 1; |
| 311 |
str2 += 1; |
| 312 |
maxLen -= 1; |
| 313 |
} |
| 314 |
|
| 315 |
if (maxLen == 0) { |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
return ((unsigned char*)str1)[0] - ((unsigned char*)str2)[0]; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
FS_API int fs_strnicmp_ascii(const char* str1, const char* str2, size_t count) |
| 324 |
{ |
| 325 |
if (str1 == NULL || str2 == NULL) { |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
while (*str1 != '\0' && *str2 != '\0' && count > 0) { |
| 330 |
int c1 = (int)*str1; |
| 331 |
int c2 = (int)*str2; |
| 332 |
|
| 333 |
if (c1 >= 'A' && c1 <= 'Z') { |
| 334 |
c1 += 'a' - 'A'; |
| 335 |
} |
| 336 |
if (c2 >= 'A' && c2 <= 'Z') { |
| 337 |
c2 += 'a' - 'A'; |
| 338 |
} |
| 339 |
|
| 340 |
if (c1 != c2) { |
| 341 |
return c1 - c2; |
| 342 |
} |
| 343 |
|
| 344 |
str1 += 1; |
| 345 |
str2 += 1; |
| 346 |
count -= 1; |
| 347 |
} |
| 348 |
|
| 349 |
if (count == 0) { |
| 350 |
return 0; |
| 351 |
} else if (*str1 == '\0' && *str2 == '\0') { |
| 352 |
return 0; |
| 353 |
} else if (*str1 == '\0') { |
| 354 |
return -1; |
| 355 |
} else { |
| 356 |
return 1; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
FS_API int fs_strnicmp(const char* str1, const char* str2, size_t count) |
| 361 |
{ |
| 362 |
|
| 363 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 364 |
return _strnicmp(str1, str2, count); |
| 365 |
#elif defined(__GNUC__) && defined(__USE_GNU) |
| 366 |
return strncasecmp(str1, str2, count); |
| 367 |
#else |
| 368 |
|
| 369 |
return fs_strnicmp_ascii(str1, str2, count); |
| 370 |
#endif |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
FS_API const char* fs_result_description(fs_result result) |
| 376 |
{ |
| 377 |
switch (result) |
| 378 |
{ |
| 379 |
case FS_SUCCESS: return "No error"; |
| 380 |
case FS_ERROR: return "Unknown error"; |
| 381 |
case FS_INVALID_ARGS: return "Invalid argument"; |
| 382 |
case FS_INVALID_OPERATION: return "Invalid operation"; |
| 383 |
case FS_OUT_OF_MEMORY: return "Out of memory"; |
| 384 |
case FS_OUT_OF_RANGE: return "Out of range"; |
| 385 |
case FS_ACCESS_DENIED: return "Permission denied"; |
| 386 |
case FS_DOES_NOT_EXIST: return "Resource does not exist"; |
| 387 |
case FS_ALREADY_EXISTS: return "Resource already exists"; |
| 388 |
case FS_TOO_MANY_OPEN_FILES: return "Too many open files"; |
| 389 |
case FS_INVALID_FILE: return "Invalid file"; |
| 390 |
case FS_TOO_BIG: return "Too large"; |
| 391 |
case FS_PATH_TOO_LONG: return "Path too long"; |
| 392 |
case FS_NAME_TOO_LONG: return "Name too long"; |
| 393 |
case FS_NOT_DIRECTORY: return "Not a directory"; |
| 394 |
case FS_IS_DIRECTORY: return "Is a directory"; |
| 395 |
case FS_DIRECTORY_NOT_EMPTY: return "Directory not empty"; |
| 396 |
case FS_AT_END: return "At end"; |
| 397 |
case FS_NO_SPACE: return "No space available"; |
| 398 |
case FS_BUSY: return "Device or resource busy"; |
| 399 |
case FS_IO_ERROR: return "Input/output error"; |
| 400 |
case FS_INTERRUPT: return "Interrupted"; |
| 401 |
case FS_UNAVAILABLE: return "Resource unavailable"; |
| 402 |
case FS_ALREADY_IN_USE: return "Resource already in use"; |
| 403 |
case FS_BAD_ADDRESS: return "Bad address"; |
| 404 |
case FS_BAD_SEEK: return "Illegal seek"; |
| 405 |
case FS_BAD_PIPE: return "Broken pipe"; |
| 406 |
case FS_DEADLOCK: return "Deadlock"; |
| 407 |
case FS_TOO_MANY_LINKS: return "Too many links"; |
| 408 |
case FS_NOT_IMPLEMENTED: return "Not implemented"; |
| 409 |
case FS_NO_MESSAGE: return "No message of desired type"; |
| 410 |
case FS_BAD_MESSAGE: return "Invalid message"; |
| 411 |
case FS_NO_DATA_AVAILABLE: return "No data available"; |
| 412 |
case FS_INVALID_DATA: return "Invalid data"; |
| 413 |
case FS_TIMEOUT: return "Timeout"; |
| 414 |
case FS_NO_NETWORK: return "Network unavailable"; |
| 415 |
case FS_NOT_UNIQUE: return "Not unique"; |
| 416 |
case FS_NOT_SOCKET: return "Socket operation on non-socket"; |
| 417 |
case FS_NO_ADDRESS: return "Destination address required"; |
| 418 |
case FS_BAD_PROTOCOL: return "Protocol wrong type for socket"; |
| 419 |
case FS_PROTOCOL_UNAVAILABLE: return "Protocol not available"; |
| 420 |
case FS_PROTOCOL_NOT_SUPPORTED: return "Protocol not supported"; |
| 421 |
case FS_PROTOCOL_FAMILY_NOT_SUPPORTED: return "Protocol family not supported"; |
| 422 |
case FS_ADDRESS_FAMILY_NOT_SUPPORTED: return "Address family not supported"; |
| 423 |
case FS_SOCKET_NOT_SUPPORTED: return "Socket type not supported"; |
| 424 |
case FS_CONNECTION_RESET: return "Connection reset"; |
| 425 |
case FS_ALREADY_CONNECTED: return "Already connected"; |
| 426 |
case FS_NOT_CONNECTED: return "Not connected"; |
| 427 |
case FS_CONNECTION_REFUSED: return "Connection refused"; |
| 428 |
case FS_NO_HOST: return "No host"; |
| 429 |
case FS_IN_PROGRESS: return "Operation in progress"; |
| 430 |
case FS_CANCELLED: return "Operation cancelled"; |
| 431 |
case FS_MEMORY_ALREADY_MAPPED: return "Memory already mapped"; |
| 432 |
case FS_DIFFERENT_DEVICE: return "Different device"; |
| 433 |
default: return "Unknown error"; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
#include <errno.h> |
| 438 |
|
| 439 |
FS_API fs_result fs_result_from_errno(int error) |
| 440 |
{ |
| 441 |
if (error == 0) { |
| 442 |
return FS_SUCCESS; |
| 443 |
} |
| 444 |
#ifdef EPERM |
| 445 |
else if (error == EPERM) { return FS_INVALID_OPERATION; } |
| 446 |
#endif |
| 447 |
#ifdef ENOENT |
| 448 |
else if (error == ENOENT) { return FS_DOES_NOT_EXIST; } |
| 449 |
#endif |
| 450 |
#ifdef ESRCH |
| 451 |
else if (error == ESRCH) { return FS_DOES_NOT_EXIST; } |
| 452 |
#endif |
| 453 |
#ifdef EINTR |
| 454 |
else if (error == EINTR) { return FS_INTERRUPT; } |
| 455 |
#endif |
| 456 |
#ifdef EIO |
| 457 |
else if (error == EIO) { return FS_IO_ERROR; } |
| 458 |
#endif |
| 459 |
#ifdef ENXIO |
| 460 |
else if (error == ENXIO) { return FS_DOES_NOT_EXIST; } |
| 461 |
#endif |
| 462 |
#ifdef E2BIG |
| 463 |
else if (error == E2BIG) { return FS_INVALID_ARGS; } |
| 464 |
#endif |
| 465 |
#ifdef ENOEXEC |
| 466 |
else if (error == ENOEXEC) { return FS_INVALID_FILE; } |
| 467 |
#endif |
| 468 |
#ifdef EBADF |
| 469 |
else if (error == EBADF) { return FS_INVALID_FILE; } |
| 470 |
#endif |
| 471 |
#ifdef EAGAIN |
| 472 |
else if (error == EAGAIN) { return FS_UNAVAILABLE; } |
| 473 |
#endif |
| 474 |
#ifdef ENOMEM |
| 475 |
else if (error == ENOMEM) { return FS_OUT_OF_MEMORY; } |
| 476 |
#endif |
| 477 |
#ifdef EACCES |
| 478 |
else if (error == EACCES) { return FS_ACCESS_DENIED; } |
| 479 |
#endif |
| 480 |
#ifdef EFAULT |
| 481 |
else if (error == EFAULT) { return FS_BAD_ADDRESS; } |
| 482 |
#endif |
| 483 |
#ifdef EBUSY |
| 484 |
else if (error == EBUSY) { return FS_BUSY; } |
| 485 |
#endif |
| 486 |
#ifdef EEXIST |
| 487 |
else if (error == EEXIST) { return FS_ALREADY_EXISTS; } |
| 488 |
#endif |
| 489 |
#ifdef EXDEV |
| 490 |
else if (error == EXDEV) { return FS_DIFFERENT_DEVICE; } |
| 491 |
#endif |
| 492 |
#ifdef ENODEV |
| 493 |
else if (error == ENODEV) { return FS_DOES_NOT_EXIST; } |
| 494 |
#endif |
| 495 |
#ifdef ENOTDIR |
| 496 |
else if (error == ENOTDIR) { return FS_NOT_DIRECTORY; } |
| 497 |
#endif |
| 498 |
#ifdef EISDIR |
| 499 |
else if (error == EISDIR) { return FS_IS_DIRECTORY; } |
| 500 |
#endif |
| 501 |
#ifdef EINVAL |
| 502 |
else if (error == EINVAL) { return FS_INVALID_ARGS; } |
| 503 |
#endif |
| 504 |
#ifdef ENFILE |
| 505 |
else if (error == ENFILE) { return FS_TOO_MANY_OPEN_FILES; } |
| 506 |
#endif |
| 507 |
#ifdef EMFILE |
| 508 |
else if (error == EMFILE) { return FS_TOO_MANY_OPEN_FILES; } |
| 509 |
#endif |
| 510 |
#ifdef ENOTTY |
| 511 |
else if (error == ENOTTY) { return FS_INVALID_OPERATION; } |
| 512 |
#endif |
| 513 |
#ifdef ETXTBSY |
| 514 |
else if (error == ETXTBSY) { return FS_BUSY; } |
| 515 |
#endif |
| 516 |
#ifdef EFBIG |
| 517 |
else if (error == EFBIG) { return FS_TOO_BIG; } |
| 518 |
#endif |
| 519 |
#ifdef ENOSPC |
| 520 |
else if (error == ENOSPC) { return FS_NO_SPACE; } |
| 521 |
#endif |
| 522 |
#ifdef ESPIPE |
| 523 |
else if (error == ESPIPE) { return FS_BAD_SEEK; } |
| 524 |
#endif |
| 525 |
#ifdef EROFS |
| 526 |
else if (error == EROFS) { return FS_ACCESS_DENIED; } |
| 527 |
#endif |
| 528 |
#ifdef EPIPE |
| 529 |
else if (error == EPIPE) { return FS_BAD_PIPE; } |
| 530 |
#endif |
| 531 |
#ifdef EDOM |
| 532 |
else if (error == EDOM) { return FS_OUT_OF_RANGE; } |
| 533 |
#endif |
| 534 |
#ifdef ERANGE |
| 535 |
else if (error == ERANGE) { return FS_OUT_OF_RANGE; } |
| 536 |
#endif |
| 537 |
#ifdef EDEADLK |
| 538 |
else if (error == EDEADLK) { return FS_DEADLOCK; } |
| 539 |
#endif |
| 540 |
#ifdef ENAMETOOLONG |
| 541 |
else if (error == ENAMETOOLONG) { return FS_PATH_TOO_LONG; } |
| 542 |
#endif |
| 543 |
#ifdef ENOSYS |
| 544 |
else if (error == ENOSYS) { return FS_NOT_IMPLEMENTED; } |
| 545 |
#endif |
| 546 |
#ifdef ENOTEMPTY |
| 547 |
else if (error == ENOTEMPTY) { return FS_DIRECTORY_NOT_EMPTY; } |
| 548 |
#endif |
| 549 |
#ifdef ELNRNG |
| 550 |
else if (error == ELNRNG) { return FS_OUT_OF_RANGE; } |
| 551 |
#endif |
| 552 |
#ifdef EBFONT |
| 553 |
else if (error == EBFONT) { return FS_INVALID_FILE; } |
| 554 |
#endif |
| 555 |
#ifdef ENODATA |
| 556 |
else if (error == ENODATA) { return FS_NO_DATA_AVAILABLE; } |
| 557 |
#endif |
| 558 |
#ifdef ETIME |
| 559 |
else if (error == ETIME) { return FS_TIMEOUT; } |
| 560 |
#endif |
| 561 |
#ifdef ENOSR |
| 562 |
else if (error == ENOSR) { return FS_NO_DATA_AVAILABLE; } |
| 563 |
#endif |
| 564 |
#ifdef ENONET |
| 565 |
else if (error == ENONET) { return FS_NO_NETWORK; } |
| 566 |
#endif |
| 567 |
#ifdef EOVERFLOW |
| 568 |
else if (error == EOVERFLOW) { return FS_TOO_BIG; } |
| 569 |
#endif |
| 570 |
#ifdef ELIBACC |
| 571 |
else if (error == ELIBACC) { return FS_ACCESS_DENIED; } |
| 572 |
#endif |
| 573 |
#ifdef ELIBBAD |
| 574 |
else if (error == ELIBBAD) { return FS_INVALID_FILE; } |
| 575 |
#endif |
| 576 |
#ifdef ELIBSCN |
| 577 |
else if (error == ELIBSCN) { return FS_INVALID_FILE; } |
| 578 |
#endif |
| 579 |
#ifdef EILSEQ |
| 580 |
else if (error == EILSEQ) { return FS_INVALID_DATA; } |
| 581 |
#endif |
| 582 |
#ifdef ENOTSOCK |
| 583 |
else if (error == ENOTSOCK) { return FS_NOT_SOCKET; } |
| 584 |
#endif |
| 585 |
#ifdef EDESTADDRREQ |
| 586 |
else if (error == EDESTADDRREQ) { return FS_NO_ADDRESS; } |
| 587 |
#endif |
| 588 |
#ifdef EMSGSIZE |
| 589 |
else if (error == EMSGSIZE) { return FS_TOO_BIG; } |
| 590 |
#endif |
| 591 |
#ifdef EPROTOTYPE |
| 592 |
else if (error == EPROTOTYPE) { return FS_BAD_PROTOCOL; } |
| 593 |
#endif |
| 594 |
#ifdef ENOPROTOOPT |
| 595 |
else if (error == ENOPROTOOPT) { return FS_PROTOCOL_UNAVAILABLE; } |
| 596 |
#endif |
| 597 |
#ifdef EPROTONOSUPPORT |
| 598 |
else if (error == EPROTONOSUPPORT) { return FS_PROTOCOL_NOT_SUPPORTED; } |
| 599 |
#endif |
| 600 |
#ifdef ESOCKTNOSUPPORT |
| 601 |
else if (error == ESOCKTNOSUPPORT) { return FS_SOCKET_NOT_SUPPORTED; } |
| 602 |
#endif |
| 603 |
#ifdef EOPNOTSUPP |
| 604 |
else if (error == EOPNOTSUPP) { return FS_INVALID_OPERATION; } |
| 605 |
#endif |
| 606 |
#ifdef EPFNOSUPPORT |
| 607 |
else if (error == EPFNOSUPPORT) { return FS_PROTOCOL_FAMILY_NOT_SUPPORTED; } |
| 608 |
#endif |
| 609 |
#ifdef EAFNOSUPPORT |
| 610 |
else if (error == EAFNOSUPPORT) { return FS_ADDRESS_FAMILY_NOT_SUPPORTED; } |
| 611 |
#endif |
| 612 |
#ifdef EADDRINUSE |
| 613 |
else if (error == EADDRINUSE) { return FS_ALREADY_IN_USE; } |
| 614 |
#endif |
| 615 |
#ifdef ENETDOWN |
| 616 |
else if (error == ENETDOWN) { return FS_NO_NETWORK; } |
| 617 |
#endif |
| 618 |
#ifdef ENETUNREACH |
| 619 |
else if (error == ENETUNREACH) { return FS_NO_NETWORK; } |
| 620 |
#endif |
| 621 |
#ifdef ENETRESET |
| 622 |
else if (error == ENETRESET) { return FS_NO_NETWORK; } |
| 623 |
#endif |
| 624 |
#ifdef ECONNABORTED |
| 625 |
else if (error == ECONNABORTED) { return FS_NO_NETWORK; } |
| 626 |
#endif |
| 627 |
#ifdef ECONNRESET |
| 628 |
else if (error == ECONNRESET) { return FS_CONNECTION_RESET; } |
| 629 |
#endif |
| 630 |
#ifdef ENOBUFS |
| 631 |
else if (error == ENOBUFS) { return FS_NO_SPACE; } |
| 632 |
#endif |
| 633 |
#ifdef EISCONN |
| 634 |
else if (error == EISCONN) { return FS_ALREADY_CONNECTED; } |
| 635 |
#endif |
| 636 |
#ifdef ENOTCONN |
| 637 |
else if (error == ENOTCONN) { return FS_NOT_CONNECTED; } |
| 638 |
#endif |
| 639 |
#ifdef ETIMEDOUT |
| 640 |
else if (error == ETIMEDOUT) { return FS_TIMEOUT; } |
| 641 |
#endif |
| 642 |
#ifdef ECONNREFUSED |
| 643 |
else if (error == ECONNREFUSED) { return FS_CONNECTION_REFUSED; } |
| 644 |
#endif |
| 645 |
#ifdef EHOSTDOWN |
| 646 |
else if (error == EHOSTDOWN) { return FS_NO_HOST; } |
| 647 |
#endif |
| 648 |
#ifdef EHOSTUNREACH |
| 649 |
else if (error == EHOSTUNREACH) { return FS_NO_HOST; } |
| 650 |
#endif |
| 651 |
#ifdef EALREADY |
| 652 |
else if (error == EALREADY) { return FS_IN_PROGRESS; } |
| 653 |
#endif |
| 654 |
#ifdef EINPROGRESS |
| 655 |
else if (error == EINPROGRESS) { return FS_IN_PROGRESS; } |
| 656 |
#endif |
| 657 |
#ifdef ESTALE |
| 658 |
else if (error == ESTALE) { return FS_INVALID_FILE; } |
| 659 |
#endif |
| 660 |
#ifdef EREMOTEIO |
| 661 |
else if (error == EREMOTEIO) { return FS_IO_ERROR; } |
| 662 |
#endif |
| 663 |
#ifdef EDQUOT |
| 664 |
else if (error == EDQUOT) { return FS_NO_SPACE; } |
| 665 |
#endif |
| 666 |
#ifdef ENOMEDIUM |
| 667 |
else if (error == ENOMEDIUM) { return FS_DOES_NOT_EXIST; } |
| 668 |
#endif |
| 669 |
#ifdef ECANCELED |
| 670 |
else if (error == ECANCELED) { return FS_CANCELLED; } |
| 671 |
#endif |
| 672 |
|
| 673 |
return FS_ERROR; |
| 674 |
} |
| 675 |
|
| 676 |
#if defined(_WIN32) |
| 677 |
#include <windows.h> |
| 678 |
|
| 679 |
FS_API fs_result fs_result_from_GetLastError(void) |
| 680 |
{ |
| 681 |
switch (GetLastError()) |
| 682 |
{ |
| 683 |
case ERROR_SUCCESS: return FS_SUCCESS; |
| 684 |
case ERROR_NOT_ENOUGH_MEMORY: return FS_OUT_OF_MEMORY; |
| 685 |
case ERROR_OUTOFMEMORY: return FS_OUT_OF_MEMORY; |
| 686 |
case ERROR_BUSY: return FS_BUSY; |
| 687 |
case ERROR_SEM_TIMEOUT: return FS_TIMEOUT; |
| 688 |
case ERROR_ALREADY_EXISTS: return FS_ALREADY_EXISTS; |
| 689 |
case ERROR_FILE_EXISTS: return FS_ALREADY_EXISTS; |
| 690 |
case ERROR_ACCESS_DENIED: return FS_ACCESS_DENIED; |
| 691 |
case ERROR_WRITE_PROTECT: return FS_ACCESS_DENIED; |
| 692 |
case ERROR_PRIVILEGE_NOT_HELD: return FS_ACCESS_DENIED; |
| 693 |
case ERROR_SHARING_VIOLATION: return FS_ACCESS_DENIED; |
| 694 |
case ERROR_LOCK_VIOLATION: return FS_ACCESS_DENIED; |
| 695 |
case ERROR_FILE_NOT_FOUND: return FS_DOES_NOT_EXIST; |
| 696 |
case ERROR_PATH_NOT_FOUND: return FS_DOES_NOT_EXIST; |
| 697 |
case ERROR_INVALID_NAME: return FS_INVALID_ARGS; |
| 698 |
case ERROR_BAD_PATHNAME: return FS_INVALID_ARGS; |
| 699 |
case ERROR_INVALID_PARAMETER: return FS_INVALID_ARGS; |
| 700 |
case ERROR_INVALID_HANDLE: return FS_INVALID_ARGS; |
| 701 |
case ERROR_INVALID_FUNCTION: return FS_INVALID_OPERATION; |
| 702 |
case ERROR_FILENAME_EXCED_RANGE: return FS_PATH_TOO_LONG; |
| 703 |
case ERROR_DIRECTORY: return FS_NOT_DIRECTORY; |
| 704 |
case ERROR_DIR_NOT_EMPTY: return FS_DIRECTORY_NOT_EMPTY; |
| 705 |
case ERROR_FILE_TOO_LARGE: return FS_TOO_BIG; |
| 706 |
case ERROR_DISK_FULL: return FS_OUT_OF_RANGE; |
| 707 |
case ERROR_HANDLE_EOF: return FS_AT_END; |
| 708 |
case ERROR_SEEK: return FS_BAD_SEEK; |
| 709 |
case ERROR_OPERATION_ABORTED: return FS_CANCELLED; |
| 710 |
case ERROR_CANCELLED: return FS_INTERRUPT; |
| 711 |
case ERROR_TOO_MANY_OPEN_FILES: return FS_TOO_MANY_OPEN_FILES; |
| 712 |
case ERROR_INVALID_DATA: return FS_INVALID_DATA; |
| 713 |
case ERROR_NO_DATA: return FS_NO_DATA_AVAILABLE; |
| 714 |
case ERROR_NOT_SAME_DEVICE: return FS_DIFFERENT_DEVICE; |
| 715 |
default: return FS_ERROR; |
| 716 |
} |
| 717 |
} |
| 718 |
#endif |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
static void* fs_malloc_default(size_t sz, void* pUserData) |
| 724 |
{ |
| 725 |
FS_UNUSED(pUserData); |
| 726 |
return FS_MALLOC(sz); |
| 727 |
} |
| 728 |
|
| 729 |
static void* fs_realloc_default(void* p, size_t sz, void* pUserData) |
| 730 |
{ |
| 731 |
FS_UNUSED(pUserData); |
| 732 |
return FS_REALLOC(p, sz); |
| 733 |
} |
| 734 |
|
| 735 |
static void fs_free_default(void* p, void* pUserData) |
| 736 |
{ |
| 737 |
FS_UNUSED(pUserData); |
| 738 |
FS_FREE(p); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
static fs_allocation_callbacks fs_allocation_callbacks_init_default(void) |
| 743 |
{ |
| 744 |
fs_allocation_callbacks allocationCallbacks; |
| 745 |
|
| 746 |
allocationCallbacks.pUserData = NULL; |
| 747 |
allocationCallbacks.onMalloc = fs_malloc_default; |
| 748 |
allocationCallbacks.onRealloc = fs_realloc_default; |
| 749 |
allocationCallbacks.onFree = fs_free_default; |
| 750 |
|
| 751 |
return allocationCallbacks; |
| 752 |
} |
| 753 |
|
| 754 |
static fs_allocation_callbacks fs_allocation_callbacks_init_copy(const fs_allocation_callbacks* pAllocationCallbacks) |
| 755 |
{ |
| 756 |
if (pAllocationCallbacks != NULL) { |
| 757 |
return *pAllocationCallbacks; |
| 758 |
} else { |
| 759 |
return fs_allocation_callbacks_init_default(); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
FS_API void* fs_malloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks) |
| 765 |
{ |
| 766 |
if (pAllocationCallbacks != NULL) { |
| 767 |
if (pAllocationCallbacks->onMalloc != NULL) { |
| 768 |
return pAllocationCallbacks->onMalloc(sz, pAllocationCallbacks->pUserData); |
| 769 |
} else { |
| 770 |
return NULL; |
| 771 |
} |
| 772 |
} else { |
| 773 |
return fs_malloc_default(sz, NULL); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
FS_API void* fs_calloc(size_t sz, const fs_allocation_callbacks* pAllocationCallbacks) |
| 778 |
{ |
| 779 |
void* p = fs_malloc(sz, pAllocationCallbacks); |
| 780 |
if (p != NULL) { |
| 781 |
FS_ZERO_MEMORY(p, sz); |
| 782 |
} |
| 783 |
|
| 784 |
return p; |
| 785 |
} |
| 786 |
|
| 787 |
FS_API void* fs_realloc(void* p, size_t sz, const fs_allocation_callbacks* pAllocationCallbacks) |
| 788 |
{ |
| 789 |
if (pAllocationCallbacks != NULL) { |
| 790 |
if (pAllocationCallbacks->onRealloc != NULL) { |
| 791 |
return pAllocationCallbacks->onRealloc(p, sz, pAllocationCallbacks->pUserData); |
| 792 |
} else { |
| 793 |
return NULL; |
| 794 |
} |
| 795 |
} else { |
| 796 |
return fs_realloc_default(p, sz, NULL); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
FS_API void fs_free(void* p, const fs_allocation_callbacks* pAllocationCallbacks) |
| 801 |
{ |
| 802 |
if (p == NULL) { |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
if (pAllocationCallbacks != NULL) { |
| 807 |
if (pAllocationCallbacks->onFree != NULL) { |
| 808 |
pAllocationCallbacks->onFree(p, pAllocationCallbacks->pUserData); |
| 809 |
} else { |
| 810 |
return; |
| 811 |
} |
| 812 |
} else { |
| 813 |
fs_free_default(p, NULL); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
#if defined(FS_WIN32) && !defined(FS_USE_PTHREAD) |
| 838 |
FS_API int fs_mtx_init(fs_mtx* mutex, int type) |
| 839 |
{ |
| 840 |
HANDLE hMutex; |
| 841 |
|
| 842 |
if (mutex == NULL) { |
| 843 |
return FS_ERROR; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
mutex->handle = NULL; |
| 848 |
mutex->type = 0; |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
if ((type & fs_mtx_recursive) != 0) { |
| 856 |
hMutex = CreateMutexA(NULL, FALSE, NULL); |
| 857 |
} else { |
| 858 |
hMutex = CreateEventA(NULL, FALSE, TRUE, NULL); |
| 859 |
} |
| 860 |
|
| 861 |
if (hMutex == NULL) { |
| 862 |
return fs_result_from_GetLastError(); |
| 863 |
} |
| 864 |
|
| 865 |
mutex->handle = (void*)hMutex; |
| 866 |
mutex->type = type; |
| 867 |
|
| 868 |
return FS_SUCCESS; |
| 869 |
} |
| 870 |
|
| 871 |
FS_API void fs_mtx_destroy(fs_mtx* mutex) |
| 872 |
{ |
| 873 |
if (mutex == NULL) { |
| 874 |
return; |
| 875 |
} |
| 876 |
|
| 877 |
CloseHandle((HANDLE)mutex->handle); |
| 878 |
} |
| 879 |
|
| 880 |
FS_API int fs_mtx_lock(fs_mtx* mutex) |
| 881 |
{ |
| 882 |
DWORD result; |
| 883 |
|
| 884 |
if (mutex == NULL) { |
| 885 |
return FS_ERROR; |
| 886 |
} |
| 887 |
|
| 888 |
result = WaitForSingleObject((HANDLE)mutex->handle, INFINITE); |
| 889 |
if (result != WAIT_OBJECT_0) { |
| 890 |
return FS_ERROR; |
| 891 |
} |
| 892 |
|
| 893 |
return FS_SUCCESS; |
| 894 |
} |
| 895 |
|
| 896 |
FS_API int fs_mtx_unlock(fs_mtx* mutex) |
| 897 |
{ |
| 898 |
BOOL result; |
| 899 |
|
| 900 |
if (mutex == NULL) { |
| 901 |
return FS_ERROR; |
| 902 |
} |
| 903 |
|
| 904 |
if ((mutex->type & fs_mtx_recursive) != 0) { |
| 905 |
result = ReleaseMutex((HANDLE)mutex->handle); |
| 906 |
} else { |
| 907 |
result = SetEvent((HANDLE)mutex->handle); |
| 908 |
} |
| 909 |
|
| 910 |
if (!result) { |
| 911 |
return FS_ERROR; |
| 912 |
} |
| 913 |
|
| 914 |
return FS_SUCCESS; |
| 915 |
} |
| 916 |
#else |
| 917 |
FS_API int fs_mtx_init(fs_mtx* mutex, int type) |
| 918 |
{ |
| 919 |
int result; |
| 920 |
|
| 921 |
if (mutex == NULL) { |
| 922 |
return FS_ERROR; |
| 923 |
} |
| 924 |
|
| 925 |
#ifdef FS_USE_MANUAL_RECURSIVE_MUTEX |
| 926 |
{ |
| 927 |
|
| 928 |
result = pthread_mutex_init(&mutex->mutex, NULL); |
| 929 |
if (result != 0) { |
| 930 |
return FS_ERROR; |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
if ((type & fs_mtx_recursive) != 0) { |
| 935 |
if (pthread_mutex_init(&mutex->guard, NULL) != 0) { |
| 936 |
pthread_mutex_destroy(&mutex->mutex); |
| 937 |
return FS_ERROR; |
| 938 |
} |
| 939 |
|
| 940 |
mutex->owner = 0; |
| 941 |
mutex->recursionCount = 0; |
| 942 |
} |
| 943 |
|
| 944 |
mutex->type = type; |
| 945 |
|
| 946 |
return FS_SUCCESS; |
| 947 |
} |
| 948 |
#else |
| 949 |
{ |
| 950 |
pthread_mutexattr_t attr; |
| 951 |
|
| 952 |
pthread_mutexattr_init(&attr); |
| 953 |
if ((type & fs_mtx_recursive) != 0) { |
| 954 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 955 |
} else { |
| 956 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
| 957 |
} |
| 958 |
|
| 959 |
result = pthread_mutex_init((pthread_mutex_t*)mutex, &attr); |
| 960 |
pthread_mutexattr_destroy(&attr); |
| 961 |
|
| 962 |
if (result != 0) { |
| 963 |
return FS_ERROR; |
| 964 |
} |
| 965 |
|
| 966 |
return FS_SUCCESS; |
| 967 |
} |
| 968 |
#endif |
| 969 |
} |
| 970 |
|
| 971 |
FS_API void fs_mtx_destroy(fs_mtx* mutex) |
| 972 |
{ |
| 973 |
if (mutex == NULL) { |
| 974 |
return; |
| 975 |
} |
| 976 |
|
| 977 |
#ifdef FS_USE_MANUAL_RECURSIVE_MUTEX |
| 978 |
{ |
| 979 |
|
| 980 |
if ((mutex->type & fs_mtx_recursive) != 0) { |
| 981 |
pthread_mutex_destroy(&mutex->guard); |
| 982 |
} |
| 983 |
|
| 984 |
pthread_mutex_destroy(&mutex->mutex); |
| 985 |
} |
| 986 |
#else |
| 987 |
{ |
| 988 |
pthread_mutex_destroy((pthread_mutex_t*)mutex); |
| 989 |
} |
| 990 |
#endif |
| 991 |
} |
| 992 |
|
| 993 |
FS_API int fs_mtx_lock(fs_mtx* mutex) |
| 994 |
{ |
| 995 |
int result; |
| 996 |
|
| 997 |
if (mutex == NULL) { |
| 998 |
return FS_ERROR; |
| 999 |
} |
| 1000 |
|
| 1001 |
#ifdef FS_USE_MANUAL_RECURSIVE_MUTEX |
| 1002 |
{ |
| 1003 |
pthread_t currentThread; |
| 1004 |
|
| 1005 |
|
| 1006 |
if ((mutex->type & fs_mtx_recursive) == 0) { |
| 1007 |
result = pthread_mutex_lock(&mutex->mutex); |
| 1008 |
if (result != 0) { |
| 1009 |
return FS_ERROR; |
| 1010 |
} |
| 1011 |
|
| 1012 |
return FS_SUCCESS; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
currentThread = pthread_self(); |
| 1017 |
|
| 1018 |
|
| 1019 |
result = pthread_mutex_lock(&mutex->guard); |
| 1020 |
if (result != 0) { |
| 1021 |
return FS_ERROR; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
if (mutex->recursionCount > 0 && pthread_equal(mutex->owner, currentThread)) { |
| 1026 |
mutex->recursionCount += 1; |
| 1027 |
pthread_mutex_unlock(&mutex->guard); |
| 1028 |
return FS_SUCCESS; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
pthread_mutex_unlock(&mutex->guard); |
| 1033 |
|
| 1034 |
result = pthread_mutex_lock(&mutex->mutex); |
| 1035 |
if (result != 0) { |
| 1036 |
return FS_ERROR; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
result = pthread_mutex_lock(&mutex->guard); |
| 1041 |
if (result != 0) { |
| 1042 |
pthread_mutex_unlock(&mutex->mutex); |
| 1043 |
return FS_ERROR; |
| 1044 |
} |
| 1045 |
|
| 1046 |
mutex->owner = currentThread; |
| 1047 |
mutex->recursionCount = 1; |
| 1048 |
|
| 1049 |
pthread_mutex_unlock(&mutex->guard); |
| 1050 |
|
| 1051 |
return FS_SUCCESS; |
| 1052 |
} |
| 1053 |
#else |
| 1054 |
{ |
| 1055 |
result = pthread_mutex_lock((pthread_mutex_t*)mutex); |
| 1056 |
if (result != 0) { |
| 1057 |
return FS_ERROR; |
| 1058 |
} |
| 1059 |
|
| 1060 |
return FS_SUCCESS; |
| 1061 |
} |
| 1062 |
#endif |
| 1063 |
} |
| 1064 |
|
| 1065 |
FS_API int fs_mtx_unlock(fs_mtx* mutex) |
| 1066 |
{ |
| 1067 |
int result; |
| 1068 |
|
| 1069 |
if (mutex == NULL) { |
| 1070 |
return FS_ERROR; |
| 1071 |
} |
| 1072 |
|
| 1073 |
#ifdef FS_USE_MANUAL_RECURSIVE_MUTEX |
| 1074 |
{ |
| 1075 |
pthread_t currentThread; |
| 1076 |
|
| 1077 |
|
| 1078 |
if ((mutex->type & fs_mtx_recursive) == 0) { |
| 1079 |
result = pthread_mutex_unlock(&mutex->mutex); |
| 1080 |
if (result != 0) { |
| 1081 |
return FS_ERROR; |
| 1082 |
} |
| 1083 |
|
| 1084 |
return FS_SUCCESS; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
currentThread = pthread_self(); |
| 1089 |
|
| 1090 |
|
| 1091 |
result = pthread_mutex_lock(&mutex->guard); |
| 1092 |
if (result != 0) { |
| 1093 |
return FS_ERROR; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
if (mutex->recursionCount == 0 || !pthread_equal(mutex->owner, currentThread)) { |
| 1098 |
|
| 1099 |
pthread_mutex_unlock(&mutex->guard); |
| 1100 |
return FS_ERROR; |
| 1101 |
} |
| 1102 |
|
| 1103 |
mutex->recursionCount -= 1; |
| 1104 |
|
| 1105 |
if (mutex->recursionCount == 0) { |
| 1106 |
|
| 1107 |
mutex->owner = 0; |
| 1108 |
pthread_mutex_unlock(&mutex->guard); |
| 1109 |
|
| 1110 |
result = pthread_mutex_unlock(&mutex->mutex); |
| 1111 |
if (result != 0) { |
| 1112 |
return FS_ERROR; |
| 1113 |
} |
| 1114 |
} else { |
| 1115 |
|
| 1116 |
pthread_mutex_unlock(&mutex->guard); |
| 1117 |
} |
| 1118 |
|
| 1119 |
return FS_SUCCESS; |
| 1120 |
} |
| 1121 |
#else |
| 1122 |
{ |
| 1123 |
result = pthread_mutex_unlock((pthread_mutex_t*)mutex); |
| 1124 |
if (result != 0) { |
| 1125 |
return FS_ERROR; |
| 1126 |
} |
| 1127 |
|
| 1128 |
return FS_SUCCESS; |
| 1129 |
} |
| 1130 |
#endif |
| 1131 |
} |
| 1132 |
#endif |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
FS_API fs_result fs_stream_init(const fs_stream_vtable* pVTable, fs_stream* pStream) |
| 1140 |
{ |
| 1141 |
if (pStream == NULL) { |
| 1142 |
return FS_INVALID_ARGS; |
| 1143 |
} |
| 1144 |
|
| 1145 |
pStream->pVTable = pVTable; |
| 1146 |
|
| 1147 |
if (pVTable == NULL) { |
| 1148 |
return FS_INVALID_ARGS; |
| 1149 |
} |
| 1150 |
|
| 1151 |
return FS_SUCCESS; |
| 1152 |
} |
| 1153 |
|
| 1154 |
FS_API fs_result fs_stream_read(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 1155 |
{ |
| 1156 |
size_t bytesRead; |
| 1157 |
fs_result result; |
| 1158 |
|
| 1159 |
if (pBytesRead != NULL) { |
| 1160 |
*pBytesRead = 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
if (pStream == NULL) { |
| 1164 |
return FS_INVALID_ARGS; |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (pStream->pVTable->read == NULL) { |
| 1168 |
return FS_NOT_IMPLEMENTED; |
| 1169 |
} |
| 1170 |
|
| 1171 |
bytesRead = 0; |
| 1172 |
result = pStream->pVTable->read(pStream, pDst, bytesToRead, &bytesRead); |
| 1173 |
|
| 1174 |
if (pBytesRead != NULL) { |
| 1175 |
*pBytesRead = bytesRead; |
| 1176 |
} else { |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
if (result == FS_SUCCESS && bytesRead != bytesToRead) { |
| 1182 |
result = FS_ERROR; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
return result; |
| 1187 |
} |
| 1188 |
|
| 1189 |
FS_API fs_result fs_stream_write(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 1190 |
{ |
| 1191 |
size_t bytesWritten; |
| 1192 |
fs_result result; |
| 1193 |
|
| 1194 |
if (pBytesWritten != NULL) { |
| 1195 |
*pBytesWritten = 0; |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (pStream == NULL) { |
| 1199 |
return FS_INVALID_ARGS; |
| 1200 |
} |
| 1201 |
|
| 1202 |
if (pStream->pVTable->write == NULL) { |
| 1203 |
return FS_NOT_IMPLEMENTED; |
| 1204 |
} |
| 1205 |
|
| 1206 |
bytesWritten = 0; |
| 1207 |
result = pStream->pVTable->write(pStream, pSrc, bytesToWrite, &bytesWritten); |
| 1208 |
|
| 1209 |
if (pBytesWritten != NULL) { |
| 1210 |
*pBytesWritten = bytesWritten; |
| 1211 |
} |
| 1212 |
|
| 1213 |
return result; |
| 1214 |
} |
| 1215 |
|
| 1216 |
FS_API fs_result fs_stream_writef(fs_stream* pStream, const char* fmt, ...) |
| 1217 |
{ |
| 1218 |
va_list args; |
| 1219 |
fs_result result; |
| 1220 |
|
| 1221 |
va_start(args, fmt); |
| 1222 |
result = fs_stream_writefv(pStream, fmt, args); |
| 1223 |
va_end(args); |
| 1224 |
|
| 1225 |
return result; |
| 1226 |
} |
| 1227 |
|
| 1228 |
FS_API fs_result fs_stream_writef_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, ...) |
| 1229 |
{ |
| 1230 |
va_list args; |
| 1231 |
fs_result result; |
| 1232 |
|
| 1233 |
va_start(args, fmt); |
| 1234 |
result = fs_stream_writefv_ex(pStream, pAllocationCallbacks, fmt, args); |
| 1235 |
va_end(args); |
| 1236 |
|
| 1237 |
return result; |
| 1238 |
} |
| 1239 |
|
| 1240 |
FS_API fs_result fs_stream_writefv(fs_stream* pStream, const char* fmt, va_list args) |
| 1241 |
{ |
| 1242 |
return fs_stream_writefv_ex(pStream, NULL, fmt, args); |
| 1243 |
} |
| 1244 |
|
| 1245 |
FS_API fs_result fs_stream_writefv_ex(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, const char* fmt, va_list args) |
| 1246 |
{ |
| 1247 |
fs_result result; |
| 1248 |
int strLen; |
| 1249 |
char pStrStack[1024]; |
| 1250 |
va_list args2; |
| 1251 |
|
| 1252 |
if (pStream == NULL || fmt == NULL) { |
| 1253 |
return FS_INVALID_ARGS; |
| 1254 |
} |
| 1255 |
|
| 1256 |
fs_va_copy(args2, args); |
| 1257 |
{ |
| 1258 |
strLen = fs_vsnprintf(pStrStack, sizeof(pStrStack), fmt, args2); |
| 1259 |
} |
| 1260 |
va_end(args2); |
| 1261 |
|
| 1262 |
if (strLen < 0) { |
| 1263 |
return FS_ERROR; |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (strLen < (int)sizeof(pStrStack)) { |
| 1267 |
|
| 1268 |
result = fs_stream_write(pStream, pStrStack, strLen, NULL); |
| 1269 |
} else { |
| 1270 |
|
| 1271 |
char* pStrHeap = NULL; |
| 1272 |
|
| 1273 |
pStrHeap = (char*)fs_malloc(strLen + 1, pAllocationCallbacks); |
| 1274 |
if (pStrHeap == NULL) { |
| 1275 |
return FS_OUT_OF_MEMORY; |
| 1276 |
} |
| 1277 |
|
| 1278 |
fs_vsnprintf(pStrHeap, strLen + 1, fmt, args); |
| 1279 |
result = fs_stream_write(pStream, pStrHeap, strLen, NULL); |
| 1280 |
|
| 1281 |
fs_free(pStrHeap, pAllocationCallbacks); |
| 1282 |
} |
| 1283 |
|
| 1284 |
return result; |
| 1285 |
} |
| 1286 |
|
| 1287 |
FS_API fs_result fs_stream_seek(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin) |
| 1288 |
{ |
| 1289 |
if (pStream == NULL) { |
| 1290 |
return FS_INVALID_ARGS; |
| 1291 |
} |
| 1292 |
|
| 1293 |
if (pStream->pVTable->seek == NULL) { |
| 1294 |
return FS_NOT_IMPLEMENTED; |
| 1295 |
} |
| 1296 |
|
| 1297 |
return pStream->pVTable->seek(pStream, offset, origin); |
| 1298 |
} |
| 1299 |
|
| 1300 |
FS_API fs_result fs_stream_tell(fs_stream* pStream, fs_int64* pCursor) |
| 1301 |
{ |
| 1302 |
if (pCursor == NULL) { |
| 1303 |
return FS_INVALID_ARGS; |
| 1304 |
} |
| 1305 |
|
| 1306 |
*pCursor = 0; |
| 1307 |
|
| 1308 |
if (pStream == NULL) { |
| 1309 |
return FS_INVALID_ARGS; |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (pStream->pVTable->tell == NULL) { |
| 1313 |
return FS_NOT_IMPLEMENTED; |
| 1314 |
} |
| 1315 |
|
| 1316 |
return pStream->pVTable->tell(pStream, pCursor); |
| 1317 |
} |
| 1318 |
|
| 1319 |
FS_API fs_result fs_stream_duplicate(fs_stream* pStream, const fs_allocation_callbacks* pAllocationCallbacks, fs_stream** ppDuplicatedStream) |
| 1320 |
{ |
| 1321 |
fs_result result; |
| 1322 |
fs_stream* pDuplicatedStream; |
| 1323 |
|
| 1324 |
if (ppDuplicatedStream == NULL) { |
| 1325 |
return FS_INVALID_ARGS; |
| 1326 |
} |
| 1327 |
|
| 1328 |
*ppDuplicatedStream = NULL; |
| 1329 |
|
| 1330 |
if (pStream == NULL) { |
| 1331 |
return FS_INVALID_ARGS; |
| 1332 |
} |
| 1333 |
|
| 1334 |
if (pStream->pVTable->duplicate_alloc_size == NULL || pStream->pVTable->duplicate == NULL) { |
| 1335 |
return FS_NOT_IMPLEMENTED; |
| 1336 |
} |
| 1337 |
|
| 1338 |
pDuplicatedStream = (fs_stream*)fs_calloc(pStream->pVTable->duplicate_alloc_size(pStream), pAllocationCallbacks); |
| 1339 |
if (pDuplicatedStream == NULL) { |
| 1340 |
return FS_OUT_OF_MEMORY; |
| 1341 |
} |
| 1342 |
|
| 1343 |
result = fs_stream_init(pStream->pVTable, pDuplicatedStream); |
| 1344 |
if (result != FS_SUCCESS) { |
| 1345 |
fs_free(pDuplicatedStream, pAllocationCallbacks); |
| 1346 |
return result; |
| 1347 |
} |
| 1348 |
|
| 1349 |
result = pStream->pVTable->duplicate(pStream, pDuplicatedStream); |
| 1350 |
if (result != FS_SUCCESS) { |
| 1351 |
fs_free(pDuplicatedStream, pAllocationCallbacks); |
| 1352 |
return result; |
| 1353 |
} |
| 1354 |
|
| 1355 |
*ppDuplicatedStream = pDuplicatedStream; |
| 1356 |
|
| 1357 |
return FS_SUCCESS; |
| 1358 |
} |
| 1359 |
|
| 1360 |
FS_API void fs_stream_delete_duplicate(fs_stream* pDuplicatedStream, const fs_allocation_callbacks* pAllocationCallbacks) |
| 1361 |
{ |
| 1362 |
if (pDuplicatedStream == NULL) { |
| 1363 |
return; |
| 1364 |
} |
| 1365 |
|
| 1366 |
if (pDuplicatedStream->pVTable->uninit != NULL) { |
| 1367 |
pDuplicatedStream->pVTable->uninit(pDuplicatedStream); |
| 1368 |
} |
| 1369 |
|
| 1370 |
fs_free(pDuplicatedStream, pAllocationCallbacks); |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
FS_API fs_result fs_stream_read_to_end(fs_stream* pStream, fs_format format, const fs_allocation_callbacks* pAllocationCallbacks, void** ppData, size_t* pDataSize) |
| 1375 |
{ |
| 1376 |
fs_result result = FS_SUCCESS; |
| 1377 |
size_t dataSize = 0; |
| 1378 |
size_t dataCap = 0; |
| 1379 |
void* pData = NULL; |
| 1380 |
|
| 1381 |
if (ppData != NULL) { |
| 1382 |
*ppData = NULL; |
| 1383 |
} |
| 1384 |
if (pDataSize != NULL) { |
| 1385 |
*pDataSize = 0; |
| 1386 |
} |
| 1387 |
|
| 1388 |
if (pStream == NULL || ppData == NULL) { |
| 1389 |
return FS_INVALID_ARGS; |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
for (;;) { |
| 1394 |
size_t chunkSize = 4096; |
| 1395 |
size_t bytesRead; |
| 1396 |
|
| 1397 |
if (dataSize + chunkSize > dataCap) { |
| 1398 |
void* pNewData; |
| 1399 |
size_t newCap = dataCap * 2; |
| 1400 |
if (newCap == 0) { |
| 1401 |
newCap = chunkSize; |
| 1402 |
} |
| 1403 |
|
| 1404 |
pNewData = fs_realloc(pData, newCap, pAllocationCallbacks); |
| 1405 |
if (pNewData == NULL) { |
| 1406 |
fs_free(pData, pAllocationCallbacks); |
| 1407 |
return FS_OUT_OF_MEMORY; |
| 1408 |
} |
| 1409 |
|
| 1410 |
pData = pNewData; |
| 1411 |
dataCap = newCap; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
result = fs_stream_read(pStream, FS_OFFSET_PTR(pData, dataSize), chunkSize, &bytesRead); |
| 1416 |
dataSize += bytesRead; |
| 1417 |
|
| 1418 |
if (result != FS_SUCCESS || bytesRead < chunkSize) { |
| 1419 |
break; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
if (format == FS_FORMAT_TEXT) { |
| 1425 |
if (dataSize >= dataCap) { |
| 1426 |
void* pNewData; |
| 1427 |
pNewData = fs_realloc(pData, dataSize + 1, pAllocationCallbacks); |
| 1428 |
if (pNewData == NULL) { |
| 1429 |
fs_free(pData, pAllocationCallbacks); |
| 1430 |
return FS_OUT_OF_MEMORY; |
| 1431 |
} |
| 1432 |
|
| 1433 |
pData = pNewData; |
| 1434 |
} |
| 1435 |
|
| 1436 |
((char*)pData)[dataSize] = '\0'; |
| 1437 |
} |
| 1438 |
|
| 1439 |
*ppData = pData; |
| 1440 |
|
| 1441 |
if (pDataSize != NULL) { |
| 1442 |
*pDataSize = dataSize; |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
if (result != FS_SUCCESS && result != FS_AT_END) { |
| 1447 |
return result; |
| 1448 |
} else { |
| 1449 |
return FS_SUCCESS; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
const char* FS_STDIN = "<si>"; |
| 1457 |
const char* FS_STDOUT = "<so>"; |
| 1458 |
const char* FS_STDERR = "<se>"; |
| 1459 |
|
| 1460 |
static size_t fs_backend_alloc_size(const fs_backend* pBackend, const void* pBackendConfig) |
| 1461 |
{ |
| 1462 |
FS_ASSERT(pBackend != NULL); |
| 1463 |
|
| 1464 |
if (pBackend->alloc_size == NULL) { |
| 1465 |
return 0; |
| 1466 |
} else { |
| 1467 |
return pBackend->alloc_size(pBackendConfig); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
static fs_result fs_backend_init(const fs_backend* pBackend, fs* pFS, const void* pBackendConfig, fs_stream* pStream) |
| 1472 |
{ |
| 1473 |
FS_ASSERT(pBackend != NULL); |
| 1474 |
|
| 1475 |
if (pBackend->init == NULL) { |
| 1476 |
return FS_NOT_IMPLEMENTED; |
| 1477 |
} else { |
| 1478 |
return pBackend->init(pFS, pBackendConfig, pStream); |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
static void fs_backend_uninit(const fs_backend* pBackend, fs* pFS) |
| 1483 |
{ |
| 1484 |
FS_ASSERT(pBackend != NULL); |
| 1485 |
|
| 1486 |
if (pBackend->uninit == NULL) { |
| 1487 |
return; |
| 1488 |
} else { |
| 1489 |
pBackend->uninit(pFS); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
static fs_result fs_backend_remove(const fs_backend* pBackend, fs* pFS, const char* pFilePath) |
| 1494 |
{ |
| 1495 |
FS_ASSERT(pBackend != NULL); |
| 1496 |
|
| 1497 |
if (pBackend->remove == NULL) { |
| 1498 |
return FS_NOT_IMPLEMENTED; |
| 1499 |
} else { |
| 1500 |
return pBackend->remove(pFS, pFilePath); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
static fs_result fs_backend_rename(const fs_backend* pBackend, fs* pFS, const char* pOldName, const char* pNewName) |
| 1505 |
{ |
| 1506 |
FS_ASSERT(pBackend != NULL); |
| 1507 |
|
| 1508 |
if (pBackend->remove == NULL) { |
| 1509 |
return FS_NOT_IMPLEMENTED; |
| 1510 |
} else { |
| 1511 |
return pBackend->rename(pFS, pOldName, pNewName); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
static fs_result fs_backend_mkdir(const fs_backend* pBackend, fs* pFS, const char* pPath) |
| 1516 |
{ |
| 1517 |
FS_ASSERT(pBackend != NULL); |
| 1518 |
|
| 1519 |
if (pBackend->mkdir == NULL) { |
| 1520 |
return FS_NOT_IMPLEMENTED; |
| 1521 |
} else { |
| 1522 |
return pBackend->mkdir(pFS, pPath); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
static fs_result fs_backend_info(const fs_backend* pBackend, fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo) |
| 1527 |
{ |
| 1528 |
FS_ASSERT(pBackend != NULL); |
| 1529 |
|
| 1530 |
if (pBackend->info == NULL) { |
| 1531 |
return FS_NOT_IMPLEMENTED; |
| 1532 |
} else { |
| 1533 |
return pBackend->info(pFS, pPath, openMode, pInfo); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
static size_t fs_backend_file_alloc_size(const fs_backend* pBackend, fs* pFS) |
| 1538 |
{ |
| 1539 |
FS_ASSERT(pBackend != NULL); |
| 1540 |
|
| 1541 |
if (pBackend->file_alloc_size == NULL) { |
| 1542 |
return 0; |
| 1543 |
} else { |
| 1544 |
return pBackend->file_alloc_size(pFS); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
static fs_result fs_backend_file_open(const fs_backend* pBackend, fs* pFS, fs_stream* pStream, const char* pFilePath, int openMode, fs_file* pFile) |
| 1549 |
{ |
| 1550 |
FS_ASSERT(pBackend != NULL); |
| 1551 |
|
| 1552 |
if (pBackend->file_open == NULL) { |
| 1553 |
return FS_NOT_IMPLEMENTED; |
| 1554 |
} else { |
| 1555 |
return pBackend->file_open(pFS, pStream, pFilePath, openMode, pFile); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
static void fs_backend_file_close(const fs_backend* pBackend, fs_file* pFile) |
| 1560 |
{ |
| 1561 |
FS_ASSERT(pBackend != NULL); |
| 1562 |
|
| 1563 |
if (pBackend->file_close == NULL) { |
| 1564 |
return; |
| 1565 |
} else { |
| 1566 |
pBackend->file_close(pFile); |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
static fs_result fs_backend_file_read(const fs_backend* pBackend, fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 1571 |
{ |
| 1572 |
FS_ASSERT(pBackend != NULL); |
| 1573 |
|
| 1574 |
if (pBackend->file_read == NULL) { |
| 1575 |
return FS_NOT_IMPLEMENTED; |
| 1576 |
} else { |
| 1577 |
return pBackend->file_read(pFile, pDst, bytesToRead, pBytesRead); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
static fs_result fs_backend_file_write(const fs_backend* pBackend, fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 1582 |
{ |
| 1583 |
FS_ASSERT(pBackend != NULL); |
| 1584 |
|
| 1585 |
if (pBackend->file_write == NULL) { |
| 1586 |
return FS_NOT_IMPLEMENTED; |
| 1587 |
} else { |
| 1588 |
return pBackend->file_write(pFile, pSrc, bytesToWrite, pBytesWritten); |
| 1589 |
} |
| 1590 |
} |
| 1591 |
|
| 1592 |
static fs_result fs_backend_file_seek(const fs_backend* pBackend, fs_file* pFile, fs_int64 offset, fs_seek_origin origin) |
| 1593 |
{ |
| 1594 |
FS_ASSERT(pBackend != NULL); |
| 1595 |
|
| 1596 |
if (pBackend->file_seek == NULL) { |
| 1597 |
return FS_NOT_IMPLEMENTED; |
| 1598 |
} else { |
| 1599 |
return pBackend->file_seek(pFile, offset, origin); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
static fs_result fs_backend_file_tell(const fs_backend* pBackend, fs_file* pFile, fs_int64* pCursor) |
| 1604 |
{ |
| 1605 |
FS_ASSERT(pBackend != NULL); |
| 1606 |
|
| 1607 |
if (pBackend->file_tell == NULL) { |
| 1608 |
return FS_NOT_IMPLEMENTED; |
| 1609 |
} else { |
| 1610 |
return pBackend->file_tell(pFile, pCursor); |
| 1611 |
} |
| 1612 |
} |
| 1613 |
|
| 1614 |
static fs_result fs_backend_file_flush(const fs_backend* pBackend, fs_file* pFile) |
| 1615 |
{ |
| 1616 |
FS_ASSERT(pBackend != NULL); |
| 1617 |
|
| 1618 |
if (pBackend->file_flush == NULL) { |
| 1619 |
return FS_NOT_IMPLEMENTED; |
| 1620 |
} else { |
| 1621 |
return pBackend->file_flush(pFile); |
| 1622 |
} |
| 1623 |
} |
| 1624 |
|
| 1625 |
static fs_result fs_backend_file_truncate(const fs_backend* pBackend, fs_file* pFile) |
| 1626 |
{ |
| 1627 |
FS_ASSERT(pBackend != NULL); |
| 1628 |
|
| 1629 |
if (pBackend->file_truncate == NULL) { |
| 1630 |
return FS_NOT_IMPLEMENTED; |
| 1631 |
} else { |
| 1632 |
return pBackend->file_truncate(pFile); |
| 1633 |
} |
| 1634 |
} |
| 1635 |
|
| 1636 |
static fs_result fs_backend_file_info(const fs_backend* pBackend, fs_file* pFile, fs_file_info* pInfo) |
| 1637 |
{ |
| 1638 |
FS_ASSERT(pBackend != NULL); |
| 1639 |
|
| 1640 |
if (pBackend->file_info == NULL) { |
| 1641 |
return FS_NOT_IMPLEMENTED; |
| 1642 |
} else { |
| 1643 |
return pBackend->file_info(pFile, pInfo); |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
static fs_result fs_backend_file_duplicate(const fs_backend* pBackend, fs_file* pFile, fs_file* pDuplicatedFile) |
| 1648 |
{ |
| 1649 |
FS_ASSERT(pBackend != NULL); |
| 1650 |
|
| 1651 |
if (pBackend->file_duplicate == NULL) { |
| 1652 |
return FS_NOT_IMPLEMENTED; |
| 1653 |
} else { |
| 1654 |
return pBackend->file_duplicate(pFile, pDuplicatedFile); |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
static fs_iterator* fs_backend_first(const fs_backend* pBackend, fs* pFS, const char* pDirectoryPath, size_t directoryPathLen) |
| 1659 |
{ |
| 1660 |
FS_ASSERT(pBackend != NULL); |
| 1661 |
|
| 1662 |
if (pBackend->first == NULL) { |
| 1663 |
return NULL; |
| 1664 |
} else { |
| 1665 |
fs_iterator* pIterator; |
| 1666 |
|
| 1667 |
pIterator = pBackend->first(pFS, pDirectoryPath, directoryPathLen); |
| 1668 |
|
| 1669 |
|
| 1670 |
if (pIterator != NULL) { |
| 1671 |
pIterator->pFS = pFS; |
| 1672 |
} |
| 1673 |
|
| 1674 |
return pIterator; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
static fs_iterator* fs_backend_next(const fs_backend* pBackend, fs_iterator* pIterator) |
| 1679 |
{ |
| 1680 |
FS_ASSERT(pBackend != NULL); |
| 1681 |
|
| 1682 |
if (pBackend->next == NULL) { |
| 1683 |
return NULL; |
| 1684 |
} else { |
| 1685 |
return pBackend->next(pIterator); |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
static void fs_backend_free_iterator(const fs_backend* pBackend, fs_iterator* pIterator) |
| 1690 |
{ |
| 1691 |
FS_ASSERT(pBackend != NULL); |
| 1692 |
|
| 1693 |
if (pBackend->free_iterator == NULL) { |
| 1694 |
return; |
| 1695 |
} else { |
| 1696 |
pBackend->free_iterator(pIterator); |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
|
| 1701 |
FS_API fs_archive_type fs_archive_type_init(const fs_backend* pBackend, const char* pExtension) |
| 1702 |
{ |
| 1703 |
fs_archive_type archiveType; |
| 1704 |
|
| 1705 |
archiveType.pBackend = pBackend; |
| 1706 |
archiveType.pExtension = pExtension; |
| 1707 |
|
| 1708 |
return archiveType; |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
#ifndef FS_DEFAULT_ARCHIVE_GC_THRESHOLD |
| 1717 |
#define FS_DEFAULT_ARCHIVE_GC_THRESHOLD 10 |
| 1718 |
#endif |
| 1719 |
|
| 1720 |
#define FS_IS_OPAQUE(mode) ((mode & FS_OPAQUE ) == FS_OPAQUE ) |
| 1721 |
#define FS_IS_VERBOSE(mode) ((mode & FS_VERBOSE) == FS_VERBOSE) |
| 1722 |
#define FS_IS_TRANSPARENT(mode) (!FS_IS_OPAQUE(mode) && !FS_IS_VERBOSE(mode)) |
| 1723 |
|
| 1724 |
FS_API fs_config fs_config_init_default(void) |
| 1725 |
{ |
| 1726 |
fs_config config; |
| 1727 |
|
| 1728 |
FS_ZERO_OBJECT(&config); |
| 1729 |
|
| 1730 |
return config; |
| 1731 |
} |
| 1732 |
|
| 1733 |
FS_API fs_config fs_config_init(const fs_backend* pBackend, const void* pBackendConfig, fs_stream* pStream) |
| 1734 |
{ |
| 1735 |
fs_config config = fs_config_init_default(); |
| 1736 |
config.pBackend = pBackend; |
| 1737 |
config.pBackendConfig = pBackendConfig; |
| 1738 |
config.pStream = pStream; |
| 1739 |
|
| 1740 |
return config; |
| 1741 |
} |
| 1742 |
|
| 1743 |
typedef struct fs_opened_archive |
| 1744 |
{ |
| 1745 |
fs* pArchive; |
| 1746 |
char pPath[1]; |
| 1747 |
} fs_opened_archive; |
| 1748 |
|
| 1749 |
typedef struct fs_mount_point |
| 1750 |
{ |
| 1751 |
size_t pathOff; |
| 1752 |
size_t pathLen; |
| 1753 |
size_t mountPointOff; |
| 1754 |
size_t mountPointLen; |
| 1755 |
fs* pArchive; |
| 1756 |
fs_bool32 closeArchiveOnUnmount; |
| 1757 |
fs_bool32 padding; |
| 1758 |
} fs_mount_point; |
| 1759 |
|
| 1760 |
typedef struct fs_mount_list fs_mount_list; |
| 1761 |
|
| 1762 |
struct fs |
| 1763 |
{ |
| 1764 |
const fs_backend* pBackend; |
| 1765 |
fs_stream* pStream; |
| 1766 |
fs_allocation_callbacks allocationCallbacks; |
| 1767 |
void* pArchiveTypes; |
| 1768 |
size_t archiveTypesAllocSize; |
| 1769 |
fs_bool32 isOwnerOfArchiveTypes; |
| 1770 |
size_t backendDataSize; |
| 1771 |
fs_on_refcount_changed_proc onRefCountChanged; |
| 1772 |
void* pRefCountChangedUserData; |
| 1773 |
fs_mtx archiveLock; |
| 1774 |
void* pOpenedArchives; |
| 1775 |
size_t openedArchivesSize; |
| 1776 |
size_t openedArchivesCap; |
| 1777 |
size_t archiveGCThreshold; |
| 1778 |
fs_mount_list* pReadMountPoints; |
| 1779 |
fs_mount_list* pWriteMountPoints; |
| 1780 |
fs_mtx refLock; |
| 1781 |
fs_uint32 refCount; |
| 1782 |
}; |
| 1783 |
|
| 1784 |
struct fs_file |
| 1785 |
{ |
| 1786 |
fs_stream stream; |
| 1787 |
fs* pFS; |
| 1788 |
fs_stream* pStreamForBackend; |
| 1789 |
size_t backendDataSize; |
| 1790 |
}; |
| 1791 |
|
| 1792 |
typedef enum fs_mount_priority |
| 1793 |
{ |
| 1794 |
FS_MOUNT_PRIORITY_HIGHEST = 0, |
| 1795 |
FS_MOUNT_PRIORITY_LOWEST = 1 |
| 1796 |
} fs_mount_priority; |
| 1797 |
|
| 1798 |
|
| 1799 |
static void fs_gc(fs* pFS, int policy, fs* pSpecificArchive); |
| 1800 |
|
| 1801 |
|
| 1802 |
static const char* fs_mount_point_real_path(const fs_mount_point* pMountPoint) |
| 1803 |
{ |
| 1804 |
FS_ASSERT(pMountPoint != NULL); |
| 1805 |
|
| 1806 |
return (const char*)FS_OFFSET_PTR(pMountPoint, sizeof(fs_mount_point) + pMountPoint->pathOff); |
| 1807 |
} |
| 1808 |
|
| 1809 |
static size_t fs_mount_point_real_path_len(const fs_mount_point* pMountPoint) |
| 1810 |
{ |
| 1811 |
FS_ASSERT(pMountPoint != NULL); |
| 1812 |
|
| 1813 |
return pMountPoint->pathLen; |
| 1814 |
} |
| 1815 |
|
| 1816 |
static const char* fs_mount_point_virtual_path(const fs_mount_point* pMountPoint) |
| 1817 |
{ |
| 1818 |
FS_ASSERT(pMountPoint != NULL); |
| 1819 |
|
| 1820 |
return (const char*)FS_OFFSET_PTR(pMountPoint, sizeof(fs_mount_point) + pMountPoint->mountPointOff); |
| 1821 |
} |
| 1822 |
|
| 1823 |
static size_t fs_mount_point_virtual_path_len(const fs_mount_point* pMountPoint) |
| 1824 |
{ |
| 1825 |
FS_ASSERT(pMountPoint != NULL); |
| 1826 |
|
| 1827 |
return pMountPoint->mountPointLen; |
| 1828 |
} |
| 1829 |
|
| 1830 |
static size_t fs_mount_point_size(size_t pathLen, size_t mountPointLen) |
| 1831 |
{ |
| 1832 |
return FS_ALIGN(sizeof(fs_mount_point) + pathLen + 1 + mountPointLen + 1, FS_SIZEOF_PTR); |
| 1833 |
} |
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
static size_t fs_mount_list_get_header_size(void) |
| 1838 |
{ |
| 1839 |
return sizeof(size_t)*2; |
| 1840 |
} |
| 1841 |
|
| 1842 |
static size_t fs_mount_list_get_alloc_size(const fs_mount_list* pList) |
| 1843 |
{ |
| 1844 |
if (pList == NULL) { |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
|
| 1848 |
return *(size_t*)FS_OFFSET_PTR(pList, 0); |
| 1849 |
} |
| 1850 |
|
| 1851 |
static size_t fs_mount_list_get_alloc_cap(const fs_mount_list* pList) |
| 1852 |
{ |
| 1853 |
if (pList == NULL) { |
| 1854 |
return 0; |
| 1855 |
} |
| 1856 |
|
| 1857 |
return *(size_t*)FS_OFFSET_PTR(pList, 1 * sizeof(size_t)); |
| 1858 |
} |
| 1859 |
|
| 1860 |
static void fs_mount_list_set_alloc_size(fs_mount_list* pList, size_t newSize) |
| 1861 |
{ |
| 1862 |
FS_ASSERT(pList != NULL); |
| 1863 |
*(size_t*)FS_OFFSET_PTR(pList, 0) = newSize; |
| 1864 |
} |
| 1865 |
|
| 1866 |
static void fs_mount_list_set_alloc_cap(fs_mount_list* pList, size_t newCap) |
| 1867 |
{ |
| 1868 |
FS_ASSERT(pList != NULL); |
| 1869 |
*(size_t*)FS_OFFSET_PTR(pList, 1 * sizeof(size_t)) = newCap; |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
typedef struct fs_mount_list_iterator |
| 1874 |
{ |
| 1875 |
const char* pPath; |
| 1876 |
const char* pMountPointPath; |
| 1877 |
fs* pArchive; |
| 1878 |
struct |
| 1879 |
{ |
| 1880 |
fs_mount_list* pList; |
| 1881 |
fs_mount_point* pMountPoint; |
| 1882 |
size_t cursor; |
| 1883 |
} internal; |
| 1884 |
} fs_mount_list_iterator; |
| 1885 |
|
| 1886 |
static fs_result fs_mount_list_iterator_resolve_members(fs_mount_list_iterator* pIterator, size_t cursor) |
| 1887 |
{ |
| 1888 |
FS_ASSERT(pIterator != NULL); |
| 1889 |
|
| 1890 |
if (cursor >= fs_mount_list_get_alloc_size(pIterator->internal.pList)) { |
| 1891 |
return FS_AT_END; |
| 1892 |
} |
| 1893 |
|
| 1894 |
pIterator->internal.cursor = cursor; |
| 1895 |
pIterator->internal.pMountPoint = (fs_mount_point*)FS_OFFSET_PTR(pIterator->internal.pList, fs_mount_list_get_header_size() + pIterator->internal.cursor); |
| 1896 |
FS_ASSERT(pIterator->internal.pMountPoint != NULL); |
| 1897 |
|
| 1898 |
|
| 1899 |
pIterator->pPath = (const char*)FS_OFFSET_PTR(pIterator->internal.pMountPoint, sizeof(fs_mount_point) + pIterator->internal.pMountPoint->pathOff); |
| 1900 |
pIterator->pMountPointPath = (const char*)FS_OFFSET_PTR(pIterator->internal.pMountPoint, sizeof(fs_mount_point) + pIterator->internal.pMountPoint->mountPointOff); |
| 1901 |
pIterator->pArchive = pIterator->internal.pMountPoint->pArchive; |
| 1902 |
|
| 1903 |
return FS_SUCCESS; |
| 1904 |
} |
| 1905 |
|
| 1906 |
static fs_bool32 fs_mount_list_at_end(const fs_mount_list_iterator* pIterator) |
| 1907 |
{ |
| 1908 |
FS_ASSERT(pIterator != NULL); |
| 1909 |
|
| 1910 |
return (pIterator->internal.cursor >= fs_mount_list_get_alloc_size(pIterator->internal.pList)); |
| 1911 |
} |
| 1912 |
|
| 1913 |
static fs_result fs_mount_list_first(fs_mount_list* pList, fs_mount_list_iterator* pIterator) |
| 1914 |
{ |
| 1915 |
FS_ASSERT(pIterator != NULL); |
| 1916 |
|
| 1917 |
FS_ZERO_OBJECT(pIterator); |
| 1918 |
pIterator->internal.pList = pList; |
| 1919 |
|
| 1920 |
if (fs_mount_list_get_alloc_size(pList) == 0) { |
| 1921 |
return FS_AT_END; |
| 1922 |
} |
| 1923 |
|
| 1924 |
return fs_mount_list_iterator_resolve_members(pIterator, 0); |
| 1925 |
} |
| 1926 |
|
| 1927 |
static fs_result fs_mount_list_next(fs_mount_list_iterator* pIterator) |
| 1928 |
{ |
| 1929 |
size_t newCursor; |
| 1930 |
|
| 1931 |
FS_ASSERT(pIterator != NULL); |
| 1932 |
|
| 1933 |
|
| 1934 |
if (fs_mount_list_at_end(pIterator)) { |
| 1935 |
return FS_AT_END; |
| 1936 |
} |
| 1937 |
|
| 1938 |
|
| 1939 |
newCursor = pIterator->internal.cursor + fs_mount_point_size(pIterator->internal.pMountPoint->pathLen, pIterator->internal.pMountPoint->mountPointLen); |
| 1940 |
FS_ASSERT(newCursor <= fs_mount_list_get_alloc_size(pIterator->internal.pList)); |
| 1941 |
|
| 1942 |
return fs_mount_list_iterator_resolve_members(pIterator, newCursor); |
| 1943 |
} |
| 1944 |
|
| 1945 |
static fs_mount_list* fs_mount_list_alloc(fs_mount_list* pList, const char* pPathToMount, const char* pMountPoint, fs_mount_priority priority, const fs_allocation_callbacks* pAllocationCallbacks, fs_mount_point** ppMountPoint) |
| 1946 |
{ |
| 1947 |
fs_mount_point* pNewMountPoint = NULL; |
| 1948 |
size_t pathToMountLen; |
| 1949 |
size_t mountPointLen; |
| 1950 |
size_t mountPointAllocSize; |
| 1951 |
|
| 1952 |
FS_ASSERT(ppMountPoint != NULL); |
| 1953 |
*ppMountPoint = NULL; |
| 1954 |
|
| 1955 |
pathToMountLen = strlen(pPathToMount); |
| 1956 |
mountPointLen = strlen(pMountPoint); |
| 1957 |
mountPointAllocSize = fs_mount_point_size(pathToMountLen, mountPointLen); |
| 1958 |
|
| 1959 |
if (fs_mount_list_get_alloc_cap(pList) < fs_mount_list_get_alloc_size(pList) + mountPointAllocSize) { |
| 1960 |
size_t newCap; |
| 1961 |
fs_mount_list* pNewList; |
| 1962 |
|
| 1963 |
newCap = fs_mount_list_get_alloc_cap(pList) * 2; |
| 1964 |
if (newCap < fs_mount_list_get_alloc_size(pList) + mountPointAllocSize) { |
| 1965 |
newCap = fs_mount_list_get_alloc_size(pList) + mountPointAllocSize; |
| 1966 |
} |
| 1967 |
|
| 1968 |
pNewList = (fs_mount_list*)fs_realloc(pList, fs_mount_list_get_header_size() + newCap, pAllocationCallbacks); |
| 1969 |
if (pNewList == NULL) { |
| 1970 |
return NULL; |
| 1971 |
} |
| 1972 |
|
| 1973 |
|
| 1974 |
if (pList == NULL) { |
| 1975 |
FS_ZERO_MEMORY(pNewList, fs_mount_list_get_header_size()); |
| 1976 |
} |
| 1977 |
|
| 1978 |
pList = (fs_mount_list*)pNewList; |
| 1979 |
fs_mount_list_set_alloc_cap(pList, newCap); |
| 1980 |
} |
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
if (priority == FS_MOUNT_PRIORITY_LOWEST) { |
| 1987 |
|
| 1988 |
pNewMountPoint = (fs_mount_point*)FS_OFFSET_PTR(pList, fs_mount_list_get_header_size() + fs_mount_list_get_alloc_size(pList)); |
| 1989 |
} else if (priority == FS_MOUNT_PRIORITY_HIGHEST) { |
| 1990 |
|
| 1991 |
FS_MOVE_MEMORY(FS_OFFSET_PTR(pList, fs_mount_list_get_header_size() + mountPointAllocSize), FS_OFFSET_PTR(pList, fs_mount_list_get_header_size()), fs_mount_list_get_alloc_size(pList)); |
| 1992 |
pNewMountPoint = (fs_mount_point*)FS_OFFSET_PTR(pList, fs_mount_list_get_header_size()); |
| 1993 |
} else { |
| 1994 |
FS_ASSERT(!"Unknown mount priority."); |
| 1995 |
return NULL; |
| 1996 |
} |
| 1997 |
|
| 1998 |
fs_mount_list_set_alloc_size(pList, fs_mount_list_get_alloc_size(pList) + mountPointAllocSize); |
| 1999 |
|
| 2000 |
|
| 2001 |
pNewMountPoint->pathOff = 0; |
| 2002 |
pNewMountPoint->pathLen = pathToMountLen; |
| 2003 |
pNewMountPoint->mountPointOff = pathToMountLen + 1; |
| 2004 |
pNewMountPoint->mountPointLen = mountPointLen; |
| 2005 |
|
| 2006 |
memcpy(FS_OFFSET_PTR(pNewMountPoint, sizeof(fs_mount_point) + pNewMountPoint->pathOff), pPathToMount, pathToMountLen + 1); |
| 2007 |
memcpy(FS_OFFSET_PTR(pNewMountPoint, sizeof(fs_mount_point) + pNewMountPoint->mountPointOff), pMountPoint, mountPointLen + 1); |
| 2008 |
|
| 2009 |
*ppMountPoint = pNewMountPoint; |
| 2010 |
return pList; |
| 2011 |
} |
| 2012 |
|
| 2013 |
static fs_result fs_mount_list_remove(fs_mount_list* pList, fs_mount_point* pMountPoint) |
| 2014 |
{ |
| 2015 |
size_t mountPointAllocSize = fs_mount_point_size(pMountPoint->pathLen, pMountPoint->mountPointLen); |
| 2016 |
size_t newMountPointsAllocSize = fs_mount_list_get_alloc_size(pList) - mountPointAllocSize; |
| 2017 |
|
| 2018 |
FS_MOVE_MEMORY |
| 2019 |
( |
| 2020 |
pMountPoint, |
| 2021 |
FS_OFFSET_PTR(pList, fs_mount_list_get_header_size() + mountPointAllocSize), |
| 2022 |
fs_mount_list_get_alloc_size(pList) - ((fs_uintptr)pMountPoint - (fs_uintptr)FS_OFFSET_PTR(pList, fs_mount_list_get_header_size())) - mountPointAllocSize |
| 2023 |
); |
| 2024 |
|
| 2025 |
fs_mount_list_set_alloc_size(pList, newMountPointsAllocSize); |
| 2026 |
|
| 2027 |
return FS_SUCCESS; |
| 2028 |
} |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
static const fs_backend* fs_get_default_backend(void) |
| 2033 |
{ |
| 2034 |
if (FS_BACKEND_POSIX != NULL) { |
| 2035 |
return FS_BACKEND_POSIX; |
| 2036 |
} else if (FS_BACKEND_WIN32 != NULL) { |
| 2037 |
return FS_BACKEND_WIN32; |
| 2038 |
} else { |
| 2039 |
return NULL; |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
static const fs_backend* fs_get_backend_or_default(const fs* pFS) |
| 2044 |
{ |
| 2045 |
if (pFS == NULL) { |
| 2046 |
return fs_get_default_backend(); |
| 2047 |
} else { |
| 2048 |
return pFS->pBackend; |
| 2049 |
} |
| 2050 |
} |
| 2051 |
|
| 2052 |
typedef struct fs_registered_backend_iterator |
| 2053 |
{ |
| 2054 |
const fs* pFS; |
| 2055 |
size_t cursor; |
| 2056 |
const fs_backend* pBackend; |
| 2057 |
void* pBackendConfig; |
| 2058 |
const char* pExtension; |
| 2059 |
size_t extensionLen; |
| 2060 |
} fs_registered_backend_iterator; |
| 2061 |
|
| 2062 |
static fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile, fs_file_info* pInfo); |
| 2063 |
static fs_result fs_next_registered_backend(fs_registered_backend_iterator* pIterator); |
| 2064 |
|
| 2065 |
static fs_result fs_first_registered_backend(fs* pFS, fs_registered_backend_iterator* pIterator) |
| 2066 |
{ |
| 2067 |
FS_ASSERT(pFS != NULL); |
| 2068 |
FS_ASSERT(pIterator != NULL); |
| 2069 |
|
| 2070 |
FS_ZERO_OBJECT(pIterator); |
| 2071 |
pIterator->pFS = pFS; |
| 2072 |
|
| 2073 |
return fs_next_registered_backend(pIterator); |
| 2074 |
} |
| 2075 |
|
| 2076 |
static fs_result fs_next_registered_backend(fs_registered_backend_iterator* pIterator) |
| 2077 |
{ |
| 2078 |
FS_ASSERT(pIterator != NULL); |
| 2079 |
|
| 2080 |
if (pIterator->cursor >= pIterator->pFS->archiveTypesAllocSize) { |
| 2081 |
return FS_AT_END; |
| 2082 |
} |
| 2083 |
|
| 2084 |
pIterator->pBackend = *(const fs_backend**)FS_OFFSET_PTR(pIterator->pFS->pArchiveTypes, pIterator->cursor); |
| 2085 |
pIterator->pBackendConfig = NULL; |
| 2086 |
pIterator->pExtension = (const char* )FS_OFFSET_PTR(pIterator->pFS->pArchiveTypes, pIterator->cursor + sizeof(fs_backend*)); |
| 2087 |
pIterator->extensionLen = strlen(pIterator->pExtension); |
| 2088 |
|
| 2089 |
pIterator->cursor += FS_ALIGN(sizeof(fs_backend*) + pIterator->extensionLen + 1, FS_SIZEOF_PTR); |
| 2090 |
|
| 2091 |
return FS_SUCCESS; |
| 2092 |
} |
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
static fs_opened_archive* fs_find_opened_archive(fs* pFS, const char* pArchivePath, size_t archivePathLen) |
| 2097 |
{ |
| 2098 |
size_t cursor; |
| 2099 |
|
| 2100 |
if (pFS == NULL) { |
| 2101 |
return NULL; |
| 2102 |
} |
| 2103 |
|
| 2104 |
FS_ASSERT(pArchivePath != NULL); |
| 2105 |
FS_ASSERT(archivePathLen > 0); |
| 2106 |
|
| 2107 |
cursor = 0; |
| 2108 |
while (cursor < pFS->openedArchivesSize) { |
| 2109 |
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); |
| 2110 |
|
| 2111 |
if (fs_strncmp(pOpenedArchive->pPath, pArchivePath, archivePathLen) == 0) { |
| 2112 |
return pOpenedArchive; |
| 2113 |
} |
| 2114 |
|
| 2115 |
|
| 2116 |
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 2117 |
} |
| 2118 |
|
| 2119 |
|
| 2120 |
return NULL; |
| 2121 |
} |
| 2122 |
|
| 2123 |
#if 0 |
| 2124 |
static fs_opened_archive* fs_find_opened_archive_by_fs(fs* pFS, fs* pArchive) |
| 2125 |
{ |
| 2126 |
size_t cursor; |
| 2127 |
|
| 2128 |
if (pFS == NULL) { |
| 2129 |
return NULL; |
| 2130 |
} |
| 2131 |
|
| 2132 |
FS_ASSERT(pArchive != NULL); |
| 2133 |
|
| 2134 |
cursor = 0; |
| 2135 |
while (cursor < pFS->openedArchivesSize) { |
| 2136 |
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); |
| 2137 |
|
| 2138 |
if (pOpenedArchive->pArchive == pArchive) { |
| 2139 |
return pOpenedArchive; |
| 2140 |
} |
| 2141 |
|
| 2142 |
|
| 2143 |
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 2144 |
} |
| 2145 |
|
| 2146 |
|
| 2147 |
return NULL; |
| 2148 |
} |
| 2149 |
#endif |
| 2150 |
|
| 2151 |
static fs_result fs_add_opened_archive(fs* pFS, fs* pArchive, const char* pArchivePath, size_t archivePathLen) |
| 2152 |
{ |
| 2153 |
size_t openedArchiveSize; |
| 2154 |
fs_opened_archive* pOpenedArchive; |
| 2155 |
|
| 2156 |
FS_ASSERT(pFS != NULL); |
| 2157 |
FS_ASSERT(pArchive != NULL); |
| 2158 |
FS_ASSERT(pArchivePath != NULL); |
| 2159 |
|
| 2160 |
if (archivePathLen == FS_NULL_TERMINATED) { |
| 2161 |
archivePathLen = strlen(pArchivePath); |
| 2162 |
} |
| 2163 |
|
| 2164 |
openedArchiveSize = FS_ALIGN(sizeof(fs*) + sizeof(size_t) + archivePathLen + 1, FS_SIZEOF_PTR); |
| 2165 |
|
| 2166 |
if (pFS->openedArchivesSize + openedArchiveSize > pFS->openedArchivesCap) { |
| 2167 |
size_t newOpenedArchivesCap; |
| 2168 |
void* pNewOpenedArchives; |
| 2169 |
|
| 2170 |
newOpenedArchivesCap = pFS->openedArchivesCap * 2; |
| 2171 |
if (newOpenedArchivesCap < pFS->openedArchivesSize + openedArchiveSize) { |
| 2172 |
newOpenedArchivesCap = pFS->openedArchivesSize + openedArchiveSize; |
| 2173 |
} |
| 2174 |
|
| 2175 |
pNewOpenedArchives = fs_realloc(pFS->pOpenedArchives, newOpenedArchivesCap, fs_get_allocation_callbacks(pFS)); |
| 2176 |
if (pNewOpenedArchives == NULL) { |
| 2177 |
return FS_OUT_OF_MEMORY; |
| 2178 |
} |
| 2179 |
|
| 2180 |
pFS->pOpenedArchives = pNewOpenedArchives; |
| 2181 |
pFS->openedArchivesCap = newOpenedArchivesCap; |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
FS_ASSERT(pFS->openedArchivesSize + openedArchiveSize <= pFS->openedArchivesCap); |
| 2186 |
|
| 2187 |
pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, pFS->openedArchivesSize); |
| 2188 |
pOpenedArchive->pArchive = pArchive; |
| 2189 |
fs_strncpy(pOpenedArchive->pPath, pArchivePath, archivePathLen); |
| 2190 |
|
| 2191 |
pFS->openedArchivesSize += openedArchiveSize; |
| 2192 |
|
| 2193 |
return FS_SUCCESS; |
| 2194 |
} |
| 2195 |
|
| 2196 |
static fs_result fs_remove_opened_archive(fs* pFS, fs_opened_archive* pOpenedArchive) |
| 2197 |
{ |
| 2198 |
|
| 2199 |
size_t openedArchiveSize; |
| 2200 |
|
| 2201 |
openedArchiveSize = FS_ALIGN(sizeof(fs_opened_archive*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 2202 |
|
| 2203 |
FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) > ((fs_uintptr)pFS->pOpenedArchives)); |
| 2204 |
FS_ASSERT(((fs_uintptr)pOpenedArchive + openedArchiveSize) <= ((fs_uintptr)pFS->pOpenedArchives + pFS->openedArchivesSize)); |
| 2205 |
|
| 2206 |
FS_MOVE_MEMORY(pOpenedArchive, FS_OFFSET_PTR(pOpenedArchive, openedArchiveSize), (size_t)((((fs_uintptr)pFS->pOpenedArchives + pFS->openedArchivesSize)) - ((fs_uintptr)pOpenedArchive + openedArchiveSize))); |
| 2207 |
pFS->openedArchivesSize -= openedArchiveSize; |
| 2208 |
|
| 2209 |
return FS_SUCCESS; |
| 2210 |
} |
| 2211 |
|
| 2212 |
|
| 2213 |
static size_t fs_archive_type_sizeof(const fs_archive_type* pArchiveType) |
| 2214 |
{ |
| 2215 |
return FS_ALIGN(sizeof(pArchiveType->pBackend) + strlen(pArchiveType->pExtension) + 1, FS_SIZEOF_PTR); |
| 2216 |
} |
| 2217 |
|
| 2218 |
|
| 2219 |
typedef struct |
| 2220 |
{ |
| 2221 |
size_t len; |
| 2222 |
char stack[1024]; |
| 2223 |
char* heap; |
| 2224 |
const char* ref; |
| 2225 |
} fs_string; |
| 2226 |
|
| 2227 |
static fs_string fs_string_new(void) |
| 2228 |
{ |
| 2229 |
fs_string str; |
| 2230 |
|
| 2231 |
str.len = 0; |
| 2232 |
str.stack[0] = '\0'; |
| 2233 |
str.heap = NULL; |
| 2234 |
str.ref = NULL; |
| 2235 |
|
| 2236 |
return str; |
| 2237 |
} |
| 2238 |
|
| 2239 |
static fs_string fs_string_new_ref(const char* ref, size_t len) |
| 2240 |
{ |
| 2241 |
fs_string str = fs_string_new(); |
| 2242 |
str.ref = ref; |
| 2243 |
str.len = len; |
| 2244 |
|
| 2245 |
if (str.len == FS_NULL_TERMINATED) { |
| 2246 |
str.len = strlen(ref); |
| 2247 |
} |
| 2248 |
|
| 2249 |
return str; |
| 2250 |
} |
| 2251 |
|
| 2252 |
static fs_result fs_string_alloc(size_t len, const fs_allocation_callbacks* pAllocationCallbacks, fs_string* pString) |
| 2253 |
{ |
| 2254 |
*pString = fs_string_new(); |
| 2255 |
|
| 2256 |
if (len < sizeof(pString->stack)) { |
| 2257 |
pString->stack[0] = '\0'; |
| 2258 |
} else { |
| 2259 |
pString->heap = (char*)fs_malloc(len + 1, pAllocationCallbacks); |
| 2260 |
if (pString->heap == NULL) { |
| 2261 |
return FS_OUT_OF_MEMORY; |
| 2262 |
} |
| 2263 |
|
| 2264 |
pString->heap[0] = '\0'; |
| 2265 |
} |
| 2266 |
|
| 2267 |
return FS_SUCCESS; |
| 2268 |
} |
| 2269 |
|
| 2270 |
static void fs_string_free(fs_string* pString, const fs_allocation_callbacks* pAllocationCallbacks) |
| 2271 |
{ |
| 2272 |
if (pString == NULL) { |
| 2273 |
return; |
| 2274 |
} |
| 2275 |
|
| 2276 |
if (pString->heap != NULL) { |
| 2277 |
fs_free(pString->heap, pAllocationCallbacks); |
| 2278 |
pString->heap = NULL; |
| 2279 |
} |
| 2280 |
} |
| 2281 |
|
| 2282 |
static const char* fs_string_cstr(const fs_string* pString) |
| 2283 |
{ |
| 2284 |
FS_ASSERT(pString != NULL); |
| 2285 |
|
| 2286 |
if (pString->ref != NULL) { |
| 2287 |
return pString->ref; |
| 2288 |
} |
| 2289 |
|
| 2290 |
if (pString->heap != NULL) { |
| 2291 |
return pString->heap; |
| 2292 |
} |
| 2293 |
|
| 2294 |
return pString->stack; |
| 2295 |
} |
| 2296 |
|
| 2297 |
static char* fs_string_cstr_mutable(fs_string* pString) |
| 2298 |
{ |
| 2299 |
FS_ASSERT(pString != NULL); |
| 2300 |
FS_ASSERT(pString->ref == NULL); |
| 2301 |
|
| 2302 |
if (pString->ref != NULL) { |
| 2303 |
return NULL; |
| 2304 |
} |
| 2305 |
|
| 2306 |
if (pString->heap != NULL) { |
| 2307 |
return pString->heap; |
| 2308 |
} |
| 2309 |
|
| 2310 |
return pString->stack; |
| 2311 |
} |
| 2312 |
|
| 2313 |
static size_t fs_string_len(const fs_string* pString) |
| 2314 |
{ |
| 2315 |
return pString->len; |
| 2316 |
} |
| 2317 |
|
| 2318 |
static void fs_string_append_preallocated(fs_string* pString, const char* pSrc, size_t srcLen) |
| 2319 |
{ |
| 2320 |
char* pDst; |
| 2321 |
|
| 2322 |
FS_ASSERT(pSrc != NULL); |
| 2323 |
FS_ASSERT(pString != NULL); |
| 2324 |
FS_ASSERT(pString->ref == NULL); |
| 2325 |
|
| 2326 |
if (srcLen == FS_NULL_TERMINATED) { |
| 2327 |
srcLen = strlen(pSrc); |
| 2328 |
} |
| 2329 |
|
| 2330 |
if (pString->heap != NULL) { |
| 2331 |
pDst = pString->heap; |
| 2332 |
} else { |
| 2333 |
pDst = pString->stack; |
| 2334 |
} |
| 2335 |
|
| 2336 |
FS_COPY_MEMORY(pDst + pString->len, pSrc, srcLen); |
| 2337 |
pString->len += srcLen; |
| 2338 |
pDst[pString->len] = '\0'; |
| 2339 |
} |
| 2340 |
|
| 2341 |
|
| 2342 |
static const char* fs_path_trim_mount_point_base(const char* pPath, size_t pathLen, const char* pMountPoint, size_t mountPointLen) |
| 2343 |
{ |
| 2344 |
FS_ASSERT(pPath != NULL && pMountPoint != NULL); |
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
if (pathLen > 0 && pPath[0] == '/' && pMountPoint[0] == '\0') { |
| 2351 |
return NULL; |
| 2352 |
} |
| 2353 |
|
| 2354 |
return fs_path_trim_base(pPath, pathLen, pMountPoint, mountPointLen); |
| 2355 |
} |
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
static fs_result fs_resolve_sub_path_from_mount_point(fs* pFS, fs_mount_point* pMountPoint, const char* pPath, int openMode, fs_string* pResolvedSubPath) |
| 2362 |
{ |
| 2363 |
fs_result result; |
| 2364 |
int stringLen; |
| 2365 |
int normalizeOptions = (openMode & FS_NO_ABOVE_ROOT_NAVIGATION); |
| 2366 |
const char* pSubPath; |
| 2367 |
|
| 2368 |
FS_ASSERT(pPath != NULL); |
| 2369 |
FS_ASSERT(pResolvedSubPath != NULL); |
| 2370 |
|
| 2371 |
*pResolvedSubPath = fs_string_new(); |
| 2372 |
|
| 2373 |
if (pFS == NULL || pMountPoint == NULL) { |
| 2374 |
return FS_INVALID_ARGS; |
| 2375 |
} |
| 2376 |
|
| 2377 |
pSubPath = fs_path_trim_mount_point_base(pPath, FS_NULL_TERMINATED, fs_mount_point_virtual_path(pMountPoint), fs_mount_point_virtual_path_len(pMountPoint)); |
| 2378 |
if (pSubPath == NULL) { |
| 2379 |
return FS_DOES_NOT_EXIST; |
| 2380 |
} |
| 2381 |
|
| 2382 |
|
| 2383 |
if (pPath[0] == '/' || pPath[0] == '\\') { |
| 2384 |
normalizeOptions |= FS_NO_ABOVE_ROOT_NAVIGATION; |
| 2385 |
} |
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
if ((openMode & FS_NO_SPECIAL_DIRS) == 0) { |
| 2393 |
stringLen = fs_path_normalize(pResolvedSubPath->stack, sizeof(pResolvedSubPath->stack), pSubPath, FS_NULL_TERMINATED, normalizeOptions); |
| 2394 |
if (stringLen < 0) { |
| 2395 |
return FS_DOES_NOT_EXIST; |
| 2396 |
} |
| 2397 |
|
| 2398 |
pResolvedSubPath->len = (size_t)stringLen; |
| 2399 |
|
| 2400 |
if ((size_t)stringLen >= sizeof(pResolvedSubPath->stack)) { |
| 2401 |
result = fs_string_alloc((size_t)stringLen, fs_get_allocation_callbacks(pFS), pResolvedSubPath); |
| 2402 |
if (result != FS_SUCCESS) { |
| 2403 |
return result; |
| 2404 |
} |
| 2405 |
|
| 2406 |
fs_path_normalize(pResolvedSubPath->heap, pResolvedSubPath->len + 1, pSubPath, FS_NULL_TERMINATED, normalizeOptions); |
| 2407 |
} |
| 2408 |
} else { |
| 2409 |
*pResolvedSubPath = fs_string_new_ref(pSubPath, FS_NULL_TERMINATED); |
| 2410 |
} |
| 2411 |
|
| 2412 |
return FS_SUCCESS; |
| 2413 |
} |
| 2414 |
|
| 2415 |
|
| 2416 |
static fs_result fs_resolve_real_path_from_mount_point(fs* pFS, fs_mount_point* pMountPoint, const char* pPath, int openMode, fs_string* pResolvedRealPath) |
| 2417 |
{ |
| 2418 |
fs_result result; |
| 2419 |
fs_string subPath; |
| 2420 |
int stringLen; |
| 2421 |
|
| 2422 |
*pResolvedRealPath = fs_string_new(); |
| 2423 |
|
| 2424 |
result = fs_resolve_sub_path_from_mount_point(pFS, pMountPoint, pPath, openMode, &subPath); |
| 2425 |
if (result != FS_SUCCESS) { |
| 2426 |
return result; |
| 2427 |
} |
| 2428 |
|
| 2429 |
stringLen = fs_path_append(pResolvedRealPath->stack, sizeof(pResolvedRealPath->stack), fs_mount_point_real_path(pMountPoint), fs_mount_point_real_path_len(pMountPoint), fs_string_cstr(&subPath), fs_string_len(&subPath)); |
| 2430 |
if (stringLen < 0) { |
| 2431 |
fs_string_free(&subPath, fs_get_allocation_callbacks(pFS)); |
| 2432 |
return FS_PATH_TOO_LONG; |
| 2433 |
} |
| 2434 |
|
| 2435 |
pResolvedRealPath->len = (size_t)stringLen; |
| 2436 |
|
| 2437 |
if ((size_t)stringLen >= sizeof(pResolvedRealPath->stack)) { |
| 2438 |
result = fs_string_alloc((size_t)stringLen, fs_get_allocation_callbacks(pFS), pResolvedRealPath); |
| 2439 |
if (result != FS_SUCCESS) { |
| 2440 |
return result; |
| 2441 |
} |
| 2442 |
|
| 2443 |
fs_path_append(pResolvedRealPath->heap, pResolvedRealPath->len + 1, fs_mount_point_real_path(pMountPoint), fs_mount_point_real_path_len(pMountPoint), fs_string_cstr(&subPath), fs_string_len(&subPath)); |
| 2444 |
} |
| 2445 |
|
| 2446 |
|
| 2447 |
fs_string_free(&subPath, fs_get_allocation_callbacks(pFS)); |
| 2448 |
|
| 2449 |
return FS_SUCCESS; |
| 2450 |
} |
| 2451 |
|
| 2452 |
static fs_mount_point* fs_find_best_write_mount_point(fs* pFS, const char* pPath, int openMode, fs_string* pResolvedRealPath) |
| 2453 |
{ |
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
fs_result result; |
| 2469 |
fs_mount_list_iterator iMountPoint; |
| 2470 |
fs_mount_point* pBestMountPoint = NULL; |
| 2471 |
const char* pBestMountPointFileSubPath = NULL; |
| 2472 |
|
| 2473 |
for (result = fs_mount_list_first(pFS->pWriteMountPoints, &iMountPoint); result == FS_SUCCESS; result = fs_mount_list_next(&iMountPoint)) { |
| 2474 |
const char* pFileSubPath = fs_path_trim_mount_point_base(pPath, FS_NULL_TERMINATED, iMountPoint.pMountPointPath, FS_NULL_TERMINATED); |
| 2475 |
if (pFileSubPath == NULL) { |
| 2476 |
continue; |
| 2477 |
} |
| 2478 |
|
| 2479 |
if (pBestMountPointFileSubPath == NULL || strlen(pFileSubPath) < strlen(pBestMountPointFileSubPath)) { |
| 2480 |
pBestMountPoint = iMountPoint.internal.pMountPoint; |
| 2481 |
pBestMountPointFileSubPath = pFileSubPath; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
if (pBestMountPoint == NULL) { |
| 2486 |
return NULL; |
| 2487 |
} |
| 2488 |
|
| 2489 |
|
| 2490 |
result = fs_resolve_real_path_from_mount_point(pFS, pBestMountPoint, pPath, openMode, pResolvedRealPath); |
| 2491 |
if (result != FS_SUCCESS) { |
| 2492 |
return NULL; |
| 2493 |
} |
| 2494 |
|
| 2495 |
return pBestMountPoint; |
| 2496 |
} |
| 2497 |
|
| 2498 |
|
| 2499 |
FS_API fs_result fs_init(const fs_config* pConfig, fs** ppFS) |
| 2500 |
{ |
| 2501 |
fs* pFS; |
| 2502 |
fs_config defaultConfig; |
| 2503 |
const fs_backend* pBackend = NULL; |
| 2504 |
size_t backendDataSizeInBytes = 0; |
| 2505 |
fs_int64 initialStreamCursor = -1; |
| 2506 |
size_t archiveTypesAllocSize = 0; |
| 2507 |
size_t iArchiveType; |
| 2508 |
fs_result result; |
| 2509 |
|
| 2510 |
if (ppFS == NULL) { |
| 2511 |
return FS_INVALID_ARGS; |
| 2512 |
} |
| 2513 |
|
| 2514 |
*ppFS = NULL; |
| 2515 |
|
| 2516 |
if (pConfig == NULL) { |
| 2517 |
defaultConfig = fs_config_init_default(); |
| 2518 |
pConfig = &defaultConfig; |
| 2519 |
} |
| 2520 |
|
| 2521 |
pBackend = pConfig->pBackend; |
| 2522 |
if (pBackend == NULL) { |
| 2523 |
pBackend = fs_get_default_backend(); |
| 2524 |
} |
| 2525 |
|
| 2526 |
|
| 2527 |
if (pBackend == NULL) { |
| 2528 |
return FS_INVALID_ARGS; |
| 2529 |
} |
| 2530 |
|
| 2531 |
backendDataSizeInBytes = fs_backend_alloc_size(pBackend, pConfig->pBackendConfig); |
| 2532 |
|
| 2533 |
|
| 2534 |
for (iArchiveType = 0; iArchiveType < pConfig->archiveTypeCount; iArchiveType += 1) { |
| 2535 |
archiveTypesAllocSize += fs_archive_type_sizeof(&pConfig->pArchiveTypes[iArchiveType]); |
| 2536 |
} |
| 2537 |
|
| 2538 |
pFS = (fs*)fs_calloc(sizeof(fs) + archiveTypesAllocSize + backendDataSizeInBytes, pConfig->pAllocationCallbacks); |
| 2539 |
if (pFS == NULL) { |
| 2540 |
return FS_OUT_OF_MEMORY; |
| 2541 |
} |
| 2542 |
|
| 2543 |
pFS->pBackend = pBackend; |
| 2544 |
pFS->pStream = pConfig->pStream; |
| 2545 |
pFS->refCount = 1; |
| 2546 |
pFS->allocationCallbacks = fs_allocation_callbacks_init_copy(pConfig->pAllocationCallbacks); |
| 2547 |
pFS->backendDataSize = backendDataSizeInBytes; |
| 2548 |
pFS->onRefCountChanged = pConfig->onRefCountChanged; |
| 2549 |
pFS->pRefCountChangedUserData = pConfig->pRefCountChangedUserData; |
| 2550 |
pFS->isOwnerOfArchiveTypes = FS_TRUE; |
| 2551 |
pFS->archiveGCThreshold = FS_DEFAULT_ARCHIVE_GC_THRESHOLD; |
| 2552 |
pFS->archiveTypesAllocSize = archiveTypesAllocSize; |
| 2553 |
pFS->pArchiveTypes = (void*)FS_OFFSET_PTR(pFS, sizeof(fs)); |
| 2554 |
|
| 2555 |
|
| 2556 |
if (pConfig->archiveTypeCount > 0) { |
| 2557 |
size_t cursor = 0; |
| 2558 |
|
| 2559 |
for (iArchiveType = 0; iArchiveType < pConfig->archiveTypeCount; iArchiveType += 1) { |
| 2560 |
size_t extensionLength = strlen(pConfig->pArchiveTypes[iArchiveType].pExtension); |
| 2561 |
|
| 2562 |
FS_COPY_MEMORY(FS_OFFSET_PTR(pFS->pArchiveTypes, cursor ), &pConfig->pArchiveTypes[iArchiveType].pBackend, sizeof(fs_backend*)); |
| 2563 |
FS_COPY_MEMORY(FS_OFFSET_PTR(pFS->pArchiveTypes, cursor + sizeof(fs_backend*)), pConfig->pArchiveTypes[iArchiveType].pExtension, extensionLength + 1); |
| 2564 |
|
| 2565 |
cursor += fs_archive_type_sizeof(&pConfig->pArchiveTypes[iArchiveType]); |
| 2566 |
} |
| 2567 |
} else { |
| 2568 |
pFS->pArchiveTypes = NULL; |
| 2569 |
pFS->archiveTypesAllocSize = 0; |
| 2570 |
} |
| 2571 |
|
| 2572 |
|
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
if (pConfig->pStream != NULL) { |
| 2578 |
|
| 2579 |
if (fs_stream_tell(pConfig->pStream, &initialStreamCursor) != FS_SUCCESS) { |
| 2580 |
initialStreamCursor = -1; |
| 2581 |
} |
| 2582 |
} |
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
fs_mtx_init(&pFS->archiveLock, fs_mtx_recursive); |
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
fs_mtx_init(&pFS->refLock, fs_mtx_recursive); |
| 2595 |
|
| 2596 |
|
| 2597 |
result = fs_backend_init(pBackend, pFS, pConfig->pBackendConfig, pConfig->pStream); |
| 2598 |
if (result != FS_NOT_IMPLEMENTED) { |
| 2599 |
if (result != FS_SUCCESS) { |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
if (pConfig->pStream != NULL && initialStreamCursor != -1) { |
| 2606 |
fs_stream_seek(pConfig->pStream, initialStreamCursor, FS_SEEK_SET); |
| 2607 |
} |
| 2608 |
|
| 2609 |
fs_free(pFS, fs_get_allocation_callbacks(pFS)); |
| 2610 |
return result; |
| 2611 |
} |
| 2612 |
} else { |
| 2613 |
|
| 2614 |
result = FS_SUCCESS; |
| 2615 |
} |
| 2616 |
|
| 2617 |
if (result != FS_SUCCESS) { |
| 2618 |
return result; |
| 2619 |
} |
| 2620 |
|
| 2621 |
*ppFS = pFS; |
| 2622 |
return FS_SUCCESS; |
| 2623 |
} |
| 2624 |
|
| 2625 |
FS_API void fs_uninit(fs* pFS) |
| 2626 |
{ |
| 2627 |
if (pFS == NULL) { |
| 2628 |
return; |
| 2629 |
} |
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
fs_gc_archives(pFS, FS_GC_POLICY_FULL); |
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
#if !defined(FS_ENABLE_OPENED_FILES_ASSERT) |
| 2645 |
{ |
| 2646 |
if (fs_refcount(pFS) > 1) { |
| 2647 |
FS_ASSERT(!"You have outstanding opened files. You must close all files before uninitializing the fs object."); |
| 2648 |
} |
| 2649 |
} |
| 2650 |
#endif |
| 2651 |
|
| 2652 |
|
| 2653 |
fs_backend_uninit(pFS->pBackend, pFS); |
| 2654 |
|
| 2655 |
fs_free(pFS->pReadMountPoints, &pFS->allocationCallbacks); |
| 2656 |
pFS->pReadMountPoints = NULL; |
| 2657 |
|
| 2658 |
fs_free(pFS->pWriteMountPoints, &pFS->allocationCallbacks); |
| 2659 |
pFS->pWriteMountPoints = NULL; |
| 2660 |
|
| 2661 |
fs_free(pFS->pOpenedArchives, &pFS->allocationCallbacks); |
| 2662 |
pFS->pOpenedArchives = NULL; |
| 2663 |
|
| 2664 |
fs_mtx_destroy(&pFS->refLock); |
| 2665 |
fs_mtx_destroy(&pFS->archiveLock); |
| 2666 |
|
| 2667 |
fs_free(pFS, &pFS->allocationCallbacks); |
| 2668 |
} |
| 2669 |
|
| 2670 |
FS_API fs_result fs_remove(fs* pFS, const char* pFilePath, int options) |
| 2671 |
{ |
| 2672 |
fs_result result; |
| 2673 |
const fs_backend* pBackend; |
| 2674 |
|
| 2675 |
if (pFilePath == NULL) { |
| 2676 |
return FS_INVALID_ARGS; |
| 2677 |
} |
| 2678 |
|
| 2679 |
pBackend = fs_get_backend_or_default(pFS); |
| 2680 |
if (pBackend == NULL) { |
| 2681 |
return FS_INVALID_ARGS; |
| 2682 |
} |
| 2683 |
|
| 2684 |
|
| 2685 |
if (pFS == NULL) { |
| 2686 |
options |= FS_IGNORE_MOUNTS; |
| 2687 |
} |
| 2688 |
|
| 2689 |
if ((options & FS_IGNORE_MOUNTS) != 0) { |
| 2690 |
result = fs_backend_remove(pBackend, pFS, pFilePath); |
| 2691 |
} else { |
| 2692 |
fs_string realFilePath; |
| 2693 |
fs_mount_point* pMountPoint; |
| 2694 |
|
| 2695 |
pMountPoint = fs_find_best_write_mount_point(pFS, pFilePath, options, &realFilePath); |
| 2696 |
if (pMountPoint == NULL) { |
| 2697 |
return FS_DOES_NOT_EXIST; |
| 2698 |
} |
| 2699 |
|
| 2700 |
result = fs_backend_remove(pBackend, pFS, fs_string_cstr(&realFilePath)); |
| 2701 |
fs_string_free(&realFilePath, fs_get_allocation_callbacks(pFS)); |
| 2702 |
} |
| 2703 |
|
| 2704 |
return result; |
| 2705 |
} |
| 2706 |
|
| 2707 |
FS_API fs_result fs_rename(fs* pFS, const char* pOldPath, const char* pNewPath, int options) |
| 2708 |
{ |
| 2709 |
fs_result result; |
| 2710 |
const fs_backend* pBackend; |
| 2711 |
|
| 2712 |
if (pOldPath == NULL || pNewPath == NULL) { |
| 2713 |
return FS_INVALID_ARGS; |
| 2714 |
} |
| 2715 |
|
| 2716 |
pBackend = fs_get_backend_or_default(pFS); |
| 2717 |
if (pBackend == NULL) { |
| 2718 |
return FS_INVALID_ARGS; |
| 2719 |
} |
| 2720 |
|
| 2721 |
|
| 2722 |
if (pFS == NULL) { |
| 2723 |
options |= FS_IGNORE_MOUNTS; |
| 2724 |
} |
| 2725 |
|
| 2726 |
|
| 2727 |
if ((options & FS_IGNORE_MOUNTS) != 0) { |
| 2728 |
result = fs_backend_rename(pBackend, pFS, pOldPath, pNewPath); |
| 2729 |
} else { |
| 2730 |
fs_string realOldPath; |
| 2731 |
fs_string realNewPath; |
| 2732 |
fs_mount_point* pMountPointOld; |
| 2733 |
fs_mount_point* pMountPointNew; |
| 2734 |
|
| 2735 |
pMountPointOld = fs_find_best_write_mount_point(pFS, pOldPath, options, &realOldPath); |
| 2736 |
if (pMountPointOld == NULL) { |
| 2737 |
return FS_DOES_NOT_EXIST; |
| 2738 |
} |
| 2739 |
|
| 2740 |
pMountPointNew = fs_find_best_write_mount_point(pFS, pNewPath, options, &realNewPath); |
| 2741 |
if (pMountPointNew == NULL) { |
| 2742 |
fs_string_free(&realOldPath, fs_get_allocation_callbacks(pFS)); |
| 2743 |
return FS_DOES_NOT_EXIST; |
| 2744 |
} |
| 2745 |
|
| 2746 |
result = fs_backend_rename(pBackend, pFS, fs_string_cstr(&realOldPath), fs_string_cstr(&realNewPath)); |
| 2747 |
fs_string_free(&realOldPath, fs_get_allocation_callbacks(pFS)); |
| 2748 |
fs_string_free(&realNewPath, fs_get_allocation_callbacks(pFS)); |
| 2749 |
} |
| 2750 |
|
| 2751 |
return result; |
| 2752 |
} |
| 2753 |
|
| 2754 |
FS_API fs_result fs_mkdir(fs* pFS, const char* pPath, int options) |
| 2755 |
{ |
| 2756 |
fs_result result; |
| 2757 |
char pRunningPathStack[1024]; |
| 2758 |
char* pRunningPathHeap = NULL; |
| 2759 |
char* pRunningPath = pRunningPathStack; |
| 2760 |
size_t runningPathLen = 0; |
| 2761 |
fs_path_iterator iSegment; |
| 2762 |
const fs_backend* pBackend; |
| 2763 |
fs_mount_point* pMountPoint = NULL; |
| 2764 |
fs_string realPath; |
| 2765 |
|
| 2766 |
pBackend = fs_get_backend_or_default(pFS); |
| 2767 |
|
| 2768 |
if (pBackend == NULL) { |
| 2769 |
return FS_INVALID_ARGS; |
| 2770 |
} |
| 2771 |
|
| 2772 |
if (pPath == NULL) { |
| 2773 |
return FS_INVALID_ARGS; |
| 2774 |
} |
| 2775 |
|
| 2776 |
|
| 2777 |
if (pFS == NULL) { |
| 2778 |
options |= FS_IGNORE_MOUNTS; |
| 2779 |
} |
| 2780 |
|
| 2781 |
|
| 2782 |
if ((options & FS_IGNORE_MOUNTS) != 0) { |
| 2783 |
pMountPoint = NULL; |
| 2784 |
realPath = fs_string_new_ref(pPath, FS_NULL_TERMINATED); |
| 2785 |
} else { |
| 2786 |
pMountPoint = fs_find_best_write_mount_point(pFS, pPath, options, &realPath); |
| 2787 |
if (pMountPoint == NULL) { |
| 2788 |
return FS_DOES_NOT_EXIST; |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
result = fs_backend_mkdir(pBackend, pFS, fs_string_cstr(&realPath)); |
| 2798 |
if (result != FS_DOES_NOT_EXIST) { |
| 2799 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2800 |
return result; |
| 2801 |
} |
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
if ((options & FS_NO_CREATE_DIRS) != 0) { |
| 2809 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2810 |
|
| 2811 |
FS_ASSERT(result == FS_DOES_NOT_EXIST); |
| 2812 |
return result; |
| 2813 |
} |
| 2814 |
|
| 2815 |
|
| 2816 |
if (fs_path_first(fs_string_cstr(&realPath), FS_NULL_TERMINATED, &iSegment) != FS_SUCCESS) { |
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
|
| 2822 |
FS_ASSERT(FS_FALSE); |
| 2823 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2824 |
return FS_ALREADY_EXISTS; |
| 2825 |
} |
| 2826 |
|
| 2827 |
pRunningPath[0] = '\0'; |
| 2828 |
|
| 2829 |
for (;;) { |
| 2830 |
if (runningPathLen + iSegment.segmentLength + 1 + 1 >= sizeof(pRunningPathStack)) { |
| 2831 |
if (pRunningPath == pRunningPathStack) { |
| 2832 |
pRunningPathHeap = (char*)fs_malloc(runningPathLen + iSegment.segmentLength + 1 + 1, fs_get_allocation_callbacks(pFS)); |
| 2833 |
if (pRunningPathHeap == NULL) { |
| 2834 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2835 |
return FS_OUT_OF_MEMORY; |
| 2836 |
} |
| 2837 |
|
| 2838 |
FS_COPY_MEMORY(pRunningPathHeap, pRunningPathStack, runningPathLen); |
| 2839 |
} else { |
| 2840 |
char* pNewRunningPathHeap; |
| 2841 |
|
| 2842 |
pNewRunningPathHeap = (char*)fs_realloc(pRunningPathHeap, runningPathLen + iSegment.segmentLength + 1 + 1, fs_get_allocation_callbacks(pFS)); |
| 2843 |
if (pNewRunningPathHeap == NULL) { |
| 2844 |
fs_free(pRunningPathHeap, fs_get_allocation_callbacks(pFS)); |
| 2845 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2846 |
return FS_OUT_OF_MEMORY; |
| 2847 |
} |
| 2848 |
|
| 2849 |
pRunningPathHeap = pNewRunningPathHeap; |
| 2850 |
} |
| 2851 |
|
| 2852 |
pRunningPath = pRunningPathHeap; |
| 2853 |
} |
| 2854 |
|
| 2855 |
FS_COPY_MEMORY(pRunningPath + runningPathLen, iSegment.pFullPath + iSegment.segmentOffset, iSegment.segmentLength); |
| 2856 |
runningPathLen += iSegment.segmentLength; |
| 2857 |
pRunningPath[runningPathLen] = '\0'; |
| 2858 |
|
| 2859 |
|
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
if (runningPathLen > 0) { |
| 2865 |
result = fs_backend_mkdir(pBackend, pFS, pRunningPath); |
| 2866 |
} else { |
| 2867 |
result = FS_ALREADY_EXISTS; |
| 2868 |
} |
| 2869 |
|
| 2870 |
|
| 2871 |
if (result == FS_ALREADY_EXISTS) { |
| 2872 |
result = FS_SUCCESS; |
| 2873 |
} |
| 2874 |
|
| 2875 |
if (result != FS_SUCCESS) { |
| 2876 |
fs_free(pRunningPathHeap, fs_get_allocation_callbacks(pFS)); |
| 2877 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2878 |
return result; |
| 2879 |
} |
| 2880 |
|
| 2881 |
pRunningPath[runningPathLen] = '/'; |
| 2882 |
runningPathLen += 1; |
| 2883 |
|
| 2884 |
result = fs_path_next(&iSegment); |
| 2885 |
if (result != FS_SUCCESS) { |
| 2886 |
break; |
| 2887 |
} |
| 2888 |
} |
| 2889 |
|
| 2890 |
fs_free(pRunningPathHeap, fs_get_allocation_callbacks(pFS)); |
| 2891 |
fs_string_free(&realPath, fs_get_allocation_callbacks(pFS)); |
| 2892 |
|
| 2893 |
return FS_SUCCESS; |
| 2894 |
} |
| 2895 |
|
| 2896 |
FS_API fs_result fs_info(fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo) |
| 2897 |
{ |
| 2898 |
if (pInfo == NULL) { |
| 2899 |
return FS_INVALID_ARGS; |
| 2900 |
} |
| 2901 |
|
| 2902 |
FS_ZERO_OBJECT(pInfo); |
| 2903 |
|
| 2904 |
return fs_file_open_or_info(pFS, pPath, openMode, NULL, pInfo); |
| 2905 |
} |
| 2906 |
|
| 2907 |
FS_API fs_stream* fs_get_stream(fs* pFS) |
| 2908 |
{ |
| 2909 |
if (pFS == NULL) { |
| 2910 |
return NULL; |
| 2911 |
} |
| 2912 |
|
| 2913 |
return pFS->pStream; |
| 2914 |
} |
| 2915 |
|
| 2916 |
FS_API const fs_allocation_callbacks* fs_get_allocation_callbacks(fs* pFS) |
| 2917 |
{ |
| 2918 |
if (pFS == NULL) { |
| 2919 |
return NULL; |
| 2920 |
} |
| 2921 |
|
| 2922 |
return &pFS->allocationCallbacks; |
| 2923 |
} |
| 2924 |
|
| 2925 |
FS_API void* fs_get_backend_data(fs* pFS) |
| 2926 |
{ |
| 2927 |
size_t offset = sizeof(fs); |
| 2928 |
|
| 2929 |
if (pFS == NULL) { |
| 2930 |
return NULL; |
| 2931 |
} |
| 2932 |
|
| 2933 |
if (pFS->isOwnerOfArchiveTypes) { |
| 2934 |
offset += pFS->archiveTypesAllocSize; |
| 2935 |
} |
| 2936 |
|
| 2937 |
return FS_OFFSET_PTR(pFS, offset); |
| 2938 |
} |
| 2939 |
|
| 2940 |
FS_API size_t fs_get_backend_data_size(fs* pFS) |
| 2941 |
{ |
| 2942 |
if (pFS == NULL) { |
| 2943 |
return 0; |
| 2944 |
} |
| 2945 |
|
| 2946 |
return pFS->backendDataSize; |
| 2947 |
} |
| 2948 |
|
| 2949 |
|
| 2950 |
static void fs_on_refcount_changed(fs* pFS, fs_uint32 newRefCount, fs_uint32 oldRefCount) |
| 2951 |
{ |
| 2952 |
if (pFS->onRefCountChanged != NULL) { |
| 2953 |
pFS->onRefCountChanged(pFS->pRefCountChangedUserData, pFS, newRefCount, oldRefCount); |
| 2954 |
} |
| 2955 |
} |
| 2956 |
|
| 2957 |
FS_API fs* fs_ref(fs* pFS) |
| 2958 |
{ |
| 2959 |
fs_uint32 newRefCount; |
| 2960 |
fs_uint32 oldRefCount; |
| 2961 |
|
| 2962 |
if (pFS == NULL) { |
| 2963 |
return NULL; |
| 2964 |
} |
| 2965 |
|
| 2966 |
fs_mtx_lock(&pFS->refLock); |
| 2967 |
{ |
| 2968 |
oldRefCount = pFS->refCount; |
| 2969 |
newRefCount = pFS->refCount + 1; |
| 2970 |
|
| 2971 |
pFS->refCount = newRefCount; |
| 2972 |
|
| 2973 |
fs_on_refcount_changed(pFS, newRefCount, oldRefCount); |
| 2974 |
} |
| 2975 |
fs_mtx_unlock(&pFS->refLock); |
| 2976 |
|
| 2977 |
return pFS; |
| 2978 |
} |
| 2979 |
|
| 2980 |
FS_API fs_uint32 fs_unref(fs* pFS) |
| 2981 |
{ |
| 2982 |
fs_uint32 newRefCount; |
| 2983 |
fs_uint32 oldRefCount; |
| 2984 |
|
| 2985 |
if (pFS == NULL) { |
| 2986 |
return 0; |
| 2987 |
} |
| 2988 |
|
| 2989 |
if (pFS->refCount == 1) { |
| 2990 |
#if !defined(FS_ENABLE_OPENED_FILES_ASSERT) |
| 2991 |
{ |
| 2992 |
FS_ASSERT(!"ref/funref mismatch. Ensure all fs_ref() calls are matched with fs_unref() calls."); |
| 2993 |
} |
| 2994 |
#endif |
| 2995 |
return pFS->refCount; |
| 2996 |
} |
| 2997 |
|
| 2998 |
fs_mtx_lock(&pFS->refLock); |
| 2999 |
{ |
| 3000 |
oldRefCount = pFS->refCount; |
| 3001 |
newRefCount = pFS->refCount - 1; |
| 3002 |
|
| 3003 |
pFS->refCount = newRefCount; |
| 3004 |
|
| 3005 |
fs_on_refcount_changed(pFS, newRefCount, oldRefCount); |
| 3006 |
} |
| 3007 |
fs_mtx_unlock(&pFS->refLock); |
| 3008 |
|
| 3009 |
return newRefCount; |
| 3010 |
} |
| 3011 |
|
| 3012 |
FS_API fs_uint32 fs_refcount(fs* pFS) |
| 3013 |
{ |
| 3014 |
fs_uint32 refCount; |
| 3015 |
|
| 3016 |
if (pFS == NULL) { |
| 3017 |
return 0; |
| 3018 |
} |
| 3019 |
|
| 3020 |
fs_mtx_lock(&pFS->refLock); |
| 3021 |
{ |
| 3022 |
refCount = pFS->refCount; |
| 3023 |
} |
| 3024 |
fs_mtx_unlock(&pFS->refLock); |
| 3025 |
|
| 3026 |
return refCount; |
| 3027 |
} |
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
static fs_result fs_find_registered_archive_type_by_path(fs* pFS, const char* pPath, size_t pathLen, const fs_backend** ppBackend, const void** ppBackendConfig) |
| 3032 |
{ |
| 3033 |
fs_result result; |
| 3034 |
fs_registered_backend_iterator iBackend; |
| 3035 |
|
| 3036 |
if (ppBackend != NULL) { |
| 3037 |
*ppBackend = NULL; |
| 3038 |
} |
| 3039 |
if (ppBackendConfig != NULL) { |
| 3040 |
*ppBackendConfig = NULL; |
| 3041 |
} |
| 3042 |
|
| 3043 |
for (result = fs_first_registered_backend(pFS, &iBackend); result == FS_SUCCESS; result = fs_next_registered_backend(&iBackend)) { |
| 3044 |
if (fs_path_extension_equal(pPath, pathLen, iBackend.pExtension, iBackend.extensionLen)) { |
| 3045 |
if (ppBackend != NULL) { |
| 3046 |
*ppBackend = iBackend.pBackend; |
| 3047 |
} |
| 3048 |
if (ppBackendConfig != NULL) { |
| 3049 |
*ppBackendConfig = iBackend.pBackendConfig; |
| 3050 |
} |
| 3051 |
|
| 3052 |
return FS_SUCCESS; |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
return FS_DOES_NOT_EXIST; |
| 3057 |
} |
| 3058 |
|
| 3059 |
FS_API fs_bool32 fs_path_looks_like_archive(fs* pFS, const char* pPath, size_t pathLen) |
| 3060 |
{ |
| 3061 |
fs_result result; |
| 3062 |
|
| 3063 |
if (pFS == NULL || pPath == NULL || pathLen == 0 || pPath[0] == '\0') { |
| 3064 |
return FS_FALSE; |
| 3065 |
} |
| 3066 |
|
| 3067 |
result = fs_find_registered_archive_type_by_path(pFS, pPath, pathLen, NULL, NULL); |
| 3068 |
if (result == FS_SUCCESS) { |
| 3069 |
return FS_TRUE; |
| 3070 |
} |
| 3071 |
|
| 3072 |
return FS_FALSE; |
| 3073 |
} |
| 3074 |
|
| 3075 |
|
| 3076 |
static size_t fs_file_duplicate_alloc_size(fs* pFS) |
| 3077 |
{ |
| 3078 |
return sizeof(fs_file) + fs_backend_file_alloc_size(fs_get_backend_or_default(pFS), pFS); |
| 3079 |
} |
| 3080 |
|
| 3081 |
static void fs_file_preinit_no_stream(fs_file* pFile, fs* pFS, size_t backendDataSize) |
| 3082 |
{ |
| 3083 |
FS_ASSERT(pFile != NULL); |
| 3084 |
|
| 3085 |
pFile->pFS = pFS; |
| 3086 |
pFile->backendDataSize = backendDataSize; |
| 3087 |
} |
| 3088 |
|
| 3089 |
static void fs_file_uninit(fs_file* pFile); |
| 3090 |
|
| 3091 |
|
| 3092 |
static fs_result fs_file_stream_read(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 3093 |
{ |
| 3094 |
return fs_file_read((fs_file*)pStream, pDst, bytesToRead, pBytesRead); |
| 3095 |
} |
| 3096 |
|
| 3097 |
static fs_result fs_file_stream_write(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 3098 |
{ |
| 3099 |
return fs_file_write((fs_file*)pStream, pSrc, bytesToWrite, pBytesWritten); |
| 3100 |
} |
| 3101 |
|
| 3102 |
static fs_result fs_file_stream_seek(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin) |
| 3103 |
{ |
| 3104 |
return fs_file_seek((fs_file*)pStream, offset, origin); |
| 3105 |
} |
| 3106 |
|
| 3107 |
static fs_result fs_file_stream_tell(fs_stream* pStream, fs_int64* pCursor) |
| 3108 |
{ |
| 3109 |
return fs_file_tell((fs_file*)pStream, pCursor); |
| 3110 |
} |
| 3111 |
|
| 3112 |
static size_t fs_file_stream_alloc_size(fs_stream* pStream) |
| 3113 |
{ |
| 3114 |
return fs_file_duplicate_alloc_size(fs_file_get_fs((fs_file*)pStream)); |
| 3115 |
} |
| 3116 |
|
| 3117 |
static fs_result fs_file_stream_duplicate(fs_stream* pStream, fs_stream* pDuplicatedStream) |
| 3118 |
{ |
| 3119 |
fs_result result; |
| 3120 |
fs_file* pStreamFile = (fs_file*)pStream; |
| 3121 |
fs_file* pDuplicatedStreamFile = (fs_file*)pDuplicatedStream; |
| 3122 |
|
| 3123 |
FS_ASSERT(pStreamFile != NULL); |
| 3124 |
FS_ASSERT(pDuplicatedStreamFile != NULL); |
| 3125 |
|
| 3126 |
|
| 3127 |
fs_file_preinit_no_stream(pDuplicatedStreamFile, fs_file_get_fs(pStreamFile), pStreamFile->backendDataSize); |
| 3128 |
|
| 3129 |
result = fs_backend_file_duplicate(fs_get_backend_or_default(pStreamFile->pFS), pStreamFile, pDuplicatedStreamFile); |
| 3130 |
if (result != FS_SUCCESS) { |
| 3131 |
return result; |
| 3132 |
} |
| 3133 |
|
| 3134 |
return FS_SUCCESS; |
| 3135 |
} |
| 3136 |
|
| 3137 |
static void fs_file_stream_uninit(fs_stream* pStream) |
| 3138 |
{ |
| 3139 |
|
| 3140 |
fs_file_uninit((fs_file*)pStream); |
| 3141 |
} |
| 3142 |
|
| 3143 |
static fs_stream_vtable fs_file_stream_vtable = |
| 3144 |
{ |
| 3145 |
fs_file_stream_read, |
| 3146 |
fs_file_stream_write, |
| 3147 |
fs_file_stream_seek, |
| 3148 |
fs_file_stream_tell, |
| 3149 |
fs_file_stream_alloc_size, |
| 3150 |
fs_file_stream_duplicate, |
| 3151 |
fs_file_stream_uninit |
| 3152 |
}; |
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
|
| 3157 |
static const fs_backend* fs_file_get_backend(fs_file* pFile) |
| 3158 |
{ |
| 3159 |
return fs_get_backend_or_default(fs_file_get_fs(pFile)); |
| 3160 |
} |
| 3161 |
|
| 3162 |
static fs_result fs_open_or_info_from_archive(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile, fs_file_info* pInfo) |
| 3163 |
{ |
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
fs_result result; |
| 3169 |
fs_path_iterator iFilePathSeg; |
| 3170 |
fs_path_iterator iFilePathSegLast; |
| 3171 |
|
| 3172 |
FS_ASSERT(pFS != NULL); |
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
if (FS_IS_OPAQUE(openMode)) { |
| 3179 |
return FS_DOES_NOT_EXIST; |
| 3180 |
} |
| 3181 |
|
| 3182 |
|
| 3183 |
if (pFS->archiveTypesAllocSize == 0) { |
| 3184 |
return FS_DOES_NOT_EXIST; |
| 3185 |
} |
| 3186 |
|
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
if (fs_path_first(pFilePath, FS_NULL_TERMINATED, &iFilePathSeg) != FS_SUCCESS) { |
| 3194 |
return FS_DOES_NOT_EXIST; |
| 3195 |
} |
| 3196 |
|
| 3197 |
|
| 3198 |
fs_path_last(pFilePath, FS_NULL_TERMINATED, &iFilePathSegLast); |
| 3199 |
|
| 3200 |
do |
| 3201 |
{ |
| 3202 |
fs_registered_backend_iterator iBackend; |
| 3203 |
fs_bool32 isArchive = FS_FALSE; |
| 3204 |
|
| 3205 |
|
| 3206 |
if (fs_strncmp(iFilePathSeg.pFullPath, ".", iFilePathSeg.segmentLength) == 0) { |
| 3207 |
continue; |
| 3208 |
} |
| 3209 |
if (fs_strncmp(iFilePathSeg.pFullPath, "..", iFilePathSeg.segmentLength) == 0) { |
| 3210 |
continue; |
| 3211 |
} |
| 3212 |
|
| 3213 |
|
| 3214 |
for (result = fs_first_registered_backend(pFS, &iBackend); result == FS_SUCCESS; result = fs_next_registered_backend(&iBackend)) { |
| 3215 |
if (fs_path_extension_equal(iFilePathSeg.pFullPath + iFilePathSeg.segmentOffset, iFilePathSeg.segmentLength, iBackend.pExtension, iBackend.extensionLen)) { |
| 3216 |
const fs_backend* pBackend = iBackend.pBackend; |
| 3217 |
const void* pBackendConfig = iBackend.pBackendConfig; |
| 3218 |
|
| 3219 |
isArchive = FS_TRUE; |
| 3220 |
|
| 3221 |
|
| 3222 |
if (fs_path_iterators_compare(&iFilePathSeg, &iFilePathSegLast) == 0) { |
| 3223 |
|
| 3224 |
|
| 3225 |
|
| 3226 |
|
| 3227 |
|
| 3228 |
return FS_DOES_NOT_EXIST; |
| 3229 |
} else { |
| 3230 |
fs* pArchive; |
| 3231 |
|
| 3232 |
result = fs_open_archive_ex(pFS, pBackend, pBackendConfig, iFilePathSeg.pFullPath, iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength, FS_NO_INCREMENT_REFCOUNT | FS_OPAQUE | openMode, &pArchive); |
| 3233 |
if (result != FS_SUCCESS) { |
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
if (result != FS_DOES_NOT_EXIST) { |
| 3239 |
return result; |
| 3240 |
} else { |
| 3241 |
continue; |
| 3242 |
} |
| 3243 |
} |
| 3244 |
|
| 3245 |
result = fs_file_open_or_info(pArchive, iFilePathSeg.pFullPath + iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength + 1, openMode, ppFile, pInfo); |
| 3246 |
if (result != FS_SUCCESS) { |
| 3247 |
if (fs_refcount(pArchive) == 1) { fs_gc_archives(pFS, FS_GC_POLICY_THRESHOLD); } |
| 3248 |
return result; |
| 3249 |
} |
| 3250 |
|
| 3251 |
if (ppFile == NULL) { |
| 3252 |
|
| 3253 |
if (fs_refcount(pArchive) == 1) { fs_gc_archives(pFS, FS_GC_POLICY_THRESHOLD); } |
| 3254 |
} |
| 3255 |
|
| 3256 |
return FS_SUCCESS; |
| 3257 |
} |
| 3258 |
} |
| 3259 |
} |
| 3260 |
|
| 3261 |
|
| 3262 |
if (isArchive) { |
| 3263 |
return FS_DOES_NOT_EXIST; |
| 3264 |
} |
| 3265 |
|
| 3266 |
|
| 3267 |
|
| 3268 |
|
| 3269 |
|
| 3270 |
if (FS_IS_VERBOSE(openMode)) { |
| 3271 |
|
| 3272 |
|
| 3273 |
|
| 3274 |
|
| 3275 |
|
| 3276 |
continue; |
| 3277 |
} else { |
| 3278 |
|
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
fs_iterator* pIterator; |
| 3287 |
|
| 3288 |
for (pIterator = fs_backend_first(fs_get_backend_or_default(pFS), pFS, iFilePathSeg.pFullPath, iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength); pIterator != NULL; pIterator = fs_backend_next(fs_get_backend_or_default(pFS), pIterator)) { |
| 3289 |
for (result = fs_first_registered_backend(pFS, &iBackend); result == FS_SUCCESS; result = fs_next_registered_backend(&iBackend)) { |
| 3290 |
if (fs_path_extension_equal(pIterator->pName, pIterator->nameLen, iBackend.pExtension, iBackend.extensionLen)) { |
| 3291 |
|
| 3292 |
const fs_backend* pBackend = iBackend.pBackend; |
| 3293 |
const void* pBackendConfig = iBackend.pBackendConfig; |
| 3294 |
fs* pArchive; |
| 3295 |
fs_string archivePath; |
| 3296 |
|
| 3297 |
result = fs_string_alloc(iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength + 1 + pIterator->nameLen, fs_get_allocation_callbacks(pFS), &archivePath); |
| 3298 |
if (result != FS_SUCCESS) { |
| 3299 |
fs_backend_free_iterator(fs_get_backend_or_default(pFS), pIterator); |
| 3300 |
return result; |
| 3301 |
} |
| 3302 |
|
| 3303 |
fs_string_append_preallocated(&archivePath, iFilePathSeg.pFullPath, iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength); |
| 3304 |
fs_string_append_preallocated(&archivePath, "/", FS_NULL_TERMINATED); |
| 3305 |
fs_string_append_preallocated(&archivePath, pIterator->pName, pIterator->nameLen); |
| 3306 |
|
| 3307 |
|
| 3308 |
result = fs_open_archive_ex(pFS, pBackend, pBackendConfig, fs_string_cstr(&archivePath), FS_NULL_TERMINATED, FS_NO_INCREMENT_REFCOUNT | FS_OPAQUE | openMode, &pArchive); |
| 3309 |
fs_string_free(&archivePath, fs_get_allocation_callbacks(pFS)); |
| 3310 |
|
| 3311 |
if (result != FS_SUCCESS) { |
| 3312 |
continue; |
| 3313 |
} |
| 3314 |
|
| 3315 |
|
| 3316 |
|
| 3317 |
|
| 3318 |
|
| 3319 |
result = fs_file_open_or_info(pArchive, iFilePathSeg.pFullPath + iFilePathSeg.segmentOffset + iFilePathSeg.segmentLength + 1, openMode, ppFile, pInfo); |
| 3320 |
if (result != FS_SUCCESS) { |
| 3321 |
if (fs_refcount(pArchive) == 1) { fs_gc_archives(pFS, FS_GC_POLICY_THRESHOLD); } |
| 3322 |
continue; |
| 3323 |
} |
| 3324 |
|
| 3325 |
|
| 3326 |
fs_backend_free_iterator(fs_get_backend_or_default(pFS), pIterator); |
| 3327 |
pIterator = NULL; |
| 3328 |
|
| 3329 |
if (ppFile == NULL) { |
| 3330 |
|
| 3331 |
if (fs_refcount(pArchive) == 1) { fs_gc_archives(pFS, FS_GC_POLICY_THRESHOLD); } |
| 3332 |
} |
| 3333 |
|
| 3334 |
|
| 3335 |
return FS_SUCCESS; |
| 3336 |
} |
| 3337 |
} |
| 3338 |
|
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
|
| 3343 |
} |
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
|
| 3349 |
} |
| 3350 |
} while (fs_path_next(&iFilePathSeg) == FS_SUCCESS); |
| 3351 |
|
| 3352 |
|
| 3353 |
return FS_DOES_NOT_EXIST; |
| 3354 |
} |
| 3355 |
|
| 3356 |
static void fs_file_free(fs_file** ppFile) |
| 3357 |
{ |
| 3358 |
fs_file* pFile; |
| 3359 |
|
| 3360 |
if (ppFile == NULL) { |
| 3361 |
return; |
| 3362 |
} |
| 3363 |
|
| 3364 |
pFile = *ppFile; |
| 3365 |
if (pFile == NULL) { |
| 3366 |
return; |
| 3367 |
} |
| 3368 |
|
| 3369 |
fs_unref(pFile->pFS); |
| 3370 |
fs_free(pFile, fs_get_allocation_callbacks(pFile->pFS)); |
| 3371 |
|
| 3372 |
*ppFile = NULL; |
| 3373 |
} |
| 3374 |
|
| 3375 |
static fs_result fs_file_alloc(fs* pFS, fs_file** ppFile) |
| 3376 |
{ |
| 3377 |
fs_file* pFile; |
| 3378 |
fs_result result; |
| 3379 |
const fs_backend* pBackend; |
| 3380 |
size_t backendDataSizeInBytes = 0; |
| 3381 |
|
| 3382 |
FS_ASSERT(ppFile != NULL); |
| 3383 |
FS_ASSERT(*ppFile == NULL); |
| 3384 |
|
| 3385 |
pBackend = fs_get_backend_or_default(pFS); |
| 3386 |
FS_ASSERT(pBackend != NULL); |
| 3387 |
|
| 3388 |
backendDataSizeInBytes = fs_backend_file_alloc_size(pBackend, pFS); |
| 3389 |
|
| 3390 |
pFile = (fs_file*)fs_calloc(sizeof(fs_file) + backendDataSizeInBytes, fs_get_allocation_callbacks(pFS)); |
| 3391 |
if (pFile == NULL) { |
| 3392 |
return FS_OUT_OF_MEMORY; |
| 3393 |
} |
| 3394 |
|
| 3395 |
|
| 3396 |
result = fs_stream_init(&fs_file_stream_vtable, &pFile->stream); |
| 3397 |
if (result != 0) { |
| 3398 |
fs_free(pFile, fs_get_allocation_callbacks(pFS)); |
| 3399 |
return result; |
| 3400 |
} |
| 3401 |
|
| 3402 |
pFile->pFS = pFS; |
| 3403 |
pFile->backendDataSize = backendDataSizeInBytes; |
| 3404 |
|
| 3405 |
|
| 3406 |
fs_ref(pFS); |
| 3407 |
|
| 3408 |
*ppFile = pFile; |
| 3409 |
return FS_SUCCESS; |
| 3410 |
} |
| 3411 |
|
| 3412 |
static fs_result fs_file_alloc_and_open_or_info(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile, fs_file_info* pInfo) |
| 3413 |
{ |
| 3414 |
fs_result result; |
| 3415 |
const fs_backend* pBackend; |
| 3416 |
fs_bool32 isStandardIOFile; |
| 3417 |
|
| 3418 |
pBackend = fs_get_backend_or_default(pFS); |
| 3419 |
if (pBackend == NULL) { |
| 3420 |
return FS_INVALID_ARGS; |
| 3421 |
} |
| 3422 |
|
| 3423 |
if (ppFile != NULL) { |
| 3424 |
FS_ASSERT(*ppFile == NULL); |
| 3425 |
|
| 3426 |
result = fs_file_alloc(pFS, ppFile); |
| 3427 |
if (result != FS_SUCCESS) { |
| 3428 |
return result; |
| 3429 |
} |
| 3430 |
} |
| 3431 |
|
| 3432 |
|
| 3433 |
|
| 3434 |
|
| 3435 |
|
| 3436 |
if (pFS != NULL && ppFile != NULL && pFS->pStream != NULL) { |
| 3437 |
result = fs_stream_duplicate(pFS->pStream, fs_get_allocation_callbacks(pFS), &(*ppFile)->pStreamForBackend); |
| 3438 |
if (result != FS_SUCCESS) { |
| 3439 |
fs_file_free(ppFile); |
| 3440 |
return result; |
| 3441 |
} |
| 3442 |
} |
| 3443 |
|
| 3444 |
isStandardIOFile = (pFilePath == FS_STDIN || pFilePath == FS_STDOUT || pFilePath == FS_STDERR); |
| 3445 |
|
| 3446 |
|
| 3447 |
|
| 3448 |
|
| 3449 |
|
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
|
| 3456 |
if (ppFile != NULL) { |
| 3457 |
|
| 3458 |
if ((openMode & FS_WRITE) != 0 && (openMode & FS_NO_CREATE_DIRS) == 0 && !isStandardIOFile) { |
| 3459 |
char pDirPathStack[1024]; |
| 3460 |
char* pDirPathHeap = NULL; |
| 3461 |
char* pDirPath; |
| 3462 |
int dirPathLen; |
| 3463 |
|
| 3464 |
dirPathLen = fs_path_directory(pDirPathStack, sizeof(pDirPathStack), pFilePath, FS_NULL_TERMINATED); |
| 3465 |
if (dirPathLen >= (int)sizeof(pDirPathStack)) { |
| 3466 |
pDirPathHeap = (char*)fs_malloc(dirPathLen + 1, fs_get_allocation_callbacks(pFS)); |
| 3467 |
if (pDirPathHeap == NULL) { |
| 3468 |
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS)); |
| 3469 |
fs_file_free(ppFile); |
| 3470 |
return FS_OUT_OF_MEMORY; |
| 3471 |
} |
| 3472 |
|
| 3473 |
dirPathLen = fs_path_directory(pDirPathHeap, dirPathLen + 1, pFilePath, FS_NULL_TERMINATED); |
| 3474 |
if (dirPathLen < 0) { |
| 3475 |
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS)); |
| 3476 |
fs_file_free(ppFile); |
| 3477 |
fs_free(pDirPathHeap, fs_get_allocation_callbacks(pFS)); |
| 3478 |
return FS_ERROR; |
| 3479 |
} |
| 3480 |
|
| 3481 |
pDirPath = pDirPathHeap; |
| 3482 |
} else { |
| 3483 |
pDirPath = pDirPathStack; |
| 3484 |
} |
| 3485 |
|
| 3486 |
result = fs_mkdir(pFS, pDirPath, FS_IGNORE_MOUNTS); |
| 3487 |
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) { |
| 3488 |
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS)); |
| 3489 |
fs_file_free(ppFile); |
| 3490 |
return result; |
| 3491 |
} |
| 3492 |
} |
| 3493 |
|
| 3494 |
result = fs_backend_file_open(pBackend, pFS, (*ppFile)->pStreamForBackend, pFilePath, openMode, *ppFile); |
| 3495 |
|
| 3496 |
if (result != FS_SUCCESS) { |
| 3497 |
fs_stream_delete_duplicate((*ppFile)->pStreamForBackend, fs_get_allocation_callbacks(pFS)); |
| 3498 |
fs_file_free(ppFile); |
| 3499 |
goto try_opening_from_archive; |
| 3500 |
} |
| 3501 |
|
| 3502 |
|
| 3503 |
if (result == FS_SUCCESS && pInfo != NULL) { |
| 3504 |
fs_backend_file_info(pBackend, *ppFile, pInfo); |
| 3505 |
} |
| 3506 |
} else { |
| 3507 |
if (pInfo != NULL) { |
| 3508 |
result = fs_backend_info(pBackend, pFS, pFilePath, openMode, pInfo); |
| 3509 |
} else { |
| 3510 |
result = FS_INVALID_ARGS; |
| 3511 |
} |
| 3512 |
} |
| 3513 |
|
| 3514 |
try_opening_from_archive: |
| 3515 |
if (!FS_IS_OPAQUE(openMode) && (openMode & FS_WRITE) == 0 && !isStandardIOFile) { |
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
if (pFS != NULL && (result == FS_DOES_NOT_EXIST || result == FS_NOT_DIRECTORY)) { |
| 3522 |
fs_file_free(ppFile); |
| 3523 |
result = fs_open_or_info_from_archive(pFS, pFilePath, openMode, ppFile, pInfo); |
| 3524 |
} |
| 3525 |
} |
| 3526 |
|
| 3527 |
return result; |
| 3528 |
} |
| 3529 |
|
| 3530 |
static fs_result fs_validate_path(const char* pPath, size_t pathLen, int mode) |
| 3531 |
{ |
| 3532 |
if ((mode & FS_NO_SPECIAL_DIRS) != 0) { |
| 3533 |
fs_path_iterator iPathSeg; |
| 3534 |
fs_result result; |
| 3535 |
|
| 3536 |
for (result = fs_path_first(pPath, pathLen, &iPathSeg); result == FS_SUCCESS; result = fs_path_next(&iPathSeg)) { |
| 3537 |
if (fs_strncmp(iPathSeg.pFullPath, ".", iPathSeg.segmentLength) == 0) { |
| 3538 |
return FS_INVALID_ARGS; |
| 3539 |
} |
| 3540 |
|
| 3541 |
if (fs_strncmp(iPathSeg.pFullPath, "..", iPathSeg.segmentLength) == 0) { |
| 3542 |
return FS_INVALID_ARGS; |
| 3543 |
} |
| 3544 |
} |
| 3545 |
} |
| 3546 |
|
| 3547 |
return FS_SUCCESS; |
| 3548 |
} |
| 3549 |
|
| 3550 |
static fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile, fs_file_info* pInfo) |
| 3551 |
{ |
| 3552 |
fs_result result; |
| 3553 |
fs_result mountPointIerationResult; |
| 3554 |
|
| 3555 |
if (ppFile == NULL && pInfo == NULL) { |
| 3556 |
return FS_INVALID_ARGS; |
| 3557 |
} |
| 3558 |
|
| 3559 |
if (pFilePath == NULL) { |
| 3560 |
return FS_INVALID_ARGS; |
| 3561 |
} |
| 3562 |
|
| 3563 |
|
| 3564 |
if (ppFile != NULL && openMode == 0) { |
| 3565 |
return FS_INVALID_ARGS; |
| 3566 |
} |
| 3567 |
|
| 3568 |
|
| 3569 |
if (pFilePath == FS_STDIN || pFilePath == FS_STDOUT || pFilePath == FS_STDERR) { |
| 3570 |
return fs_file_alloc_and_open_or_info(pFS, pFilePath, openMode, ppFile, pInfo); |
| 3571 |
} |
| 3572 |
|
| 3573 |
result = fs_validate_path(pFilePath, FS_NULL_TERMINATED, openMode); |
| 3574 |
if (result != FS_SUCCESS) { |
| 3575 |
return result; |
| 3576 |
} |
| 3577 |
|
| 3578 |
if ((openMode & FS_WRITE) != 0) { |
| 3579 |
|
| 3580 |
if (pFS != NULL && (openMode & FS_IGNORE_MOUNTS) == 0) { |
| 3581 |
fs_mount_point* pBestMountPoint = NULL; |
| 3582 |
fs_string fileRealPath; |
| 3583 |
|
| 3584 |
pBestMountPoint = fs_find_best_write_mount_point(pFS, pFilePath, openMode, &fileRealPath); |
| 3585 |
if (pBestMountPoint != NULL) { |
| 3586 |
|
| 3587 |
result = fs_file_alloc_and_open_or_info(pFS, fs_string_cstr(&fileRealPath), openMode, ppFile, pInfo); |
| 3588 |
fs_string_free(&fileRealPath, fs_get_allocation_callbacks(pFS)); |
| 3589 |
|
| 3590 |
if (result != FS_SUCCESS) { |
| 3591 |
return result; |
| 3592 |
} |
| 3593 |
|
| 3594 |
return FS_SUCCESS; |
| 3595 |
} else { |
| 3596 |
return FS_DOES_NOT_EXIST; |
| 3597 |
} |
| 3598 |
} else { |
| 3599 |
|
| 3600 |
|
| 3601 |
|
| 3602 |
|
| 3603 |
if ((openMode & FS_ONLY_MOUNTS) == 0) { |
| 3604 |
return fs_file_alloc_and_open_or_info(pFS, pFilePath, openMode, ppFile, pInfo); |
| 3605 |
} else { |
| 3606 |
|
| 3607 |
|
| 3608 |
|
| 3609 |
|
| 3610 |
return FS_DOES_NOT_EXIST; |
| 3611 |
} |
| 3612 |
} |
| 3613 |
} else { |
| 3614 |
|
| 3615 |
fs_mount_list_iterator iMountPoint; |
| 3616 |
|
| 3617 |
if (pFS != NULL && (openMode & FS_IGNORE_MOUNTS) == 0) { |
| 3618 |
for (mountPointIerationResult = fs_mount_list_first(pFS->pReadMountPoints, &iMountPoint); mountPointIerationResult == FS_SUCCESS; mountPointIerationResult = fs_mount_list_next(&iMountPoint)) { |
| 3619 |
|
| 3620 |
if (iMountPoint.pArchive != NULL) { |
| 3621 |
|
| 3622 |
fs_string fileSubPath; |
| 3623 |
|
| 3624 |
result = fs_resolve_sub_path_from_mount_point(pFS, iMountPoint.internal.pMountPoint, pFilePath, openMode, &fileSubPath); |
| 3625 |
if (result != FS_SUCCESS) { |
| 3626 |
continue; |
| 3627 |
} |
| 3628 |
|
| 3629 |
result = fs_file_open_or_info(iMountPoint.pArchive, fs_string_cstr(&fileSubPath), openMode, ppFile, pInfo); |
| 3630 |
fs_string_free(&fileSubPath, fs_get_allocation_callbacks(pFS)); |
| 3631 |
} else { |
| 3632 |
|
| 3633 |
fs_string fileRealPath; |
| 3634 |
|
| 3635 |
result = fs_resolve_real_path_from_mount_point(pFS, iMountPoint.internal.pMountPoint, pFilePath, openMode, &fileRealPath); |
| 3636 |
if (result != FS_SUCCESS) { |
| 3637 |
continue; |
| 3638 |
} |
| 3639 |
|
| 3640 |
result = fs_file_alloc_and_open_or_info(pFS, fs_string_cstr(&fileRealPath), openMode, ppFile, pInfo); |
| 3641 |
fs_string_free(&fileRealPath, fs_get_allocation_callbacks(pFS)); |
| 3642 |
} |
| 3643 |
|
| 3644 |
if (result == FS_SUCCESS) { |
| 3645 |
return FS_SUCCESS; |
| 3646 |
} else { |
| 3647 |
|
| 3648 |
} |
| 3649 |
} |
| 3650 |
} |
| 3651 |
|
| 3652 |
|
| 3653 |
if ((openMode & FS_ONLY_MOUNTS) == 0) { |
| 3654 |
result = fs_file_alloc_and_open_or_info(pFS, pFilePath, openMode, ppFile, pInfo); |
| 3655 |
if (result == FS_SUCCESS) { |
| 3656 |
return FS_SUCCESS; |
| 3657 |
} |
| 3658 |
} else { |
| 3659 |
|
| 3660 |
|
| 3661 |
|
| 3662 |
|
| 3663 |
|
| 3664 |
result = FS_DOES_NOT_EXIST; |
| 3665 |
} |
| 3666 |
|
| 3667 |
|
| 3668 |
if (ppFile != NULL) { |
| 3669 |
fs_file_free(ppFile); |
| 3670 |
} |
| 3671 |
} |
| 3672 |
|
| 3673 |
FS_ASSERT(result != FS_SUCCESS); |
| 3674 |
return result; |
| 3675 |
} |
| 3676 |
|
| 3677 |
FS_API fs_result fs_file_open(fs* pFS, const char* pFilePath, int openMode, fs_file** ppFile) |
| 3678 |
{ |
| 3679 |
if (ppFile == NULL) { |
| 3680 |
return FS_INVALID_ARGS; |
| 3681 |
} |
| 3682 |
|
| 3683 |
*ppFile = NULL; |
| 3684 |
|
| 3685 |
return fs_file_open_or_info(pFS, pFilePath, openMode, ppFile, NULL); |
| 3686 |
} |
| 3687 |
|
| 3688 |
static void fs_file_uninit(fs_file* pFile) |
| 3689 |
{ |
| 3690 |
fs_backend_file_close(fs_get_backend_or_default(fs_file_get_fs(pFile)), pFile); |
| 3691 |
} |
| 3692 |
|
| 3693 |
FS_API void fs_file_close(fs_file* pFile) |
| 3694 |
{ |
| 3695 |
const fs_backend* pBackend = fs_file_get_backend(pFile); |
| 3696 |
|
| 3697 |
FS_ASSERT(pBackend != NULL); |
| 3698 |
(void)pBackend; |
| 3699 |
|
| 3700 |
if (pFile == NULL) { |
| 3701 |
return; |
| 3702 |
} |
| 3703 |
|
| 3704 |
fs_file_uninit(pFile); |
| 3705 |
|
| 3706 |
if (pFile->pStreamForBackend != NULL) { |
| 3707 |
fs_stream_delete_duplicate(pFile->pStreamForBackend, fs_get_allocation_callbacks(pFile->pFS)); |
| 3708 |
} |
| 3709 |
|
| 3710 |
fs_file_free(&pFile); |
| 3711 |
} |
| 3712 |
|
| 3713 |
FS_API fs_result fs_file_read(fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 3714 |
{ |
| 3715 |
fs_result result; |
| 3716 |
size_t bytesRead; |
| 3717 |
const fs_backend* pBackend; |
| 3718 |
|
| 3719 |
if (pBytesRead != NULL) { |
| 3720 |
*pBytesRead = 0; |
| 3721 |
} |
| 3722 |
|
| 3723 |
if (pFile == NULL || pDst == NULL) { |
| 3724 |
return FS_INVALID_ARGS; |
| 3725 |
} |
| 3726 |
|
| 3727 |
pBackend = fs_file_get_backend(pFile); |
| 3728 |
FS_ASSERT(pBackend != NULL); |
| 3729 |
|
| 3730 |
bytesRead = 0; |
| 3731 |
result = fs_backend_file_read(pBackend, pFile, pDst, bytesToRead, &bytesRead); |
| 3732 |
|
| 3733 |
if (pBytesRead != NULL) { |
| 3734 |
*pBytesRead = bytesRead; |
| 3735 |
} |
| 3736 |
|
| 3737 |
if (result != FS_SUCCESS) { |
| 3738 |
|
| 3739 |
if (result == FS_AT_END) { |
| 3740 |
if (bytesRead > 0) { |
| 3741 |
result = FS_SUCCESS; |
| 3742 |
} |
| 3743 |
} |
| 3744 |
|
| 3745 |
return result; |
| 3746 |
} |
| 3747 |
|
| 3748 |
|
| 3749 |
|
| 3750 |
|
| 3751 |
|
| 3752 |
if (pBytesRead == NULL) { |
| 3753 |
if (bytesRead != bytesToRead) { |
| 3754 |
return FS_ERROR; |
| 3755 |
} |
| 3756 |
} |
| 3757 |
|
| 3758 |
return FS_SUCCESS; |
| 3759 |
} |
| 3760 |
|
| 3761 |
FS_API fs_result fs_file_write(fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 3762 |
{ |
| 3763 |
fs_result result; |
| 3764 |
size_t bytesWritten; |
| 3765 |
const fs_backend* pBackend; |
| 3766 |
|
| 3767 |
if (pBytesWritten != NULL) { |
| 3768 |
*pBytesWritten = 0; |
| 3769 |
} |
| 3770 |
|
| 3771 |
if (pFile == NULL || pSrc == NULL) { |
| 3772 |
return FS_INVALID_ARGS; |
| 3773 |
} |
| 3774 |
|
| 3775 |
pBackend = fs_file_get_backend(pFile); |
| 3776 |
FS_ASSERT(pBackend != NULL); |
| 3777 |
|
| 3778 |
bytesWritten = 0; |
| 3779 |
result = fs_backend_file_write(pBackend, pFile, pSrc, bytesToWrite, &bytesWritten); |
| 3780 |
|
| 3781 |
if (pBytesWritten != NULL) { |
| 3782 |
*pBytesWritten = bytesWritten; |
| 3783 |
} |
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
|
| 3789 |
if (pBytesWritten == NULL) { |
| 3790 |
if (bytesWritten != bytesToWrite) { |
| 3791 |
return FS_ERROR; |
| 3792 |
} |
| 3793 |
} |
| 3794 |
|
| 3795 |
return result; |
| 3796 |
} |
| 3797 |
|
| 3798 |
FS_API fs_result fs_file_writef(fs_file* pFile, const char* fmt, ...) |
| 3799 |
{ |
| 3800 |
va_list args; |
| 3801 |
fs_result result; |
| 3802 |
|
| 3803 |
va_start(args, fmt); |
| 3804 |
result = fs_file_writefv(pFile, fmt, args); |
| 3805 |
va_end(args); |
| 3806 |
|
| 3807 |
return result; |
| 3808 |
} |
| 3809 |
|
| 3810 |
FS_API fs_result fs_file_writefv(fs_file* pFile, const char* fmt, va_list args) |
| 3811 |
{ |
| 3812 |
return fs_stream_writefv(fs_file_get_stream(pFile), fmt, args); |
| 3813 |
} |
| 3814 |
|
| 3815 |
FS_API fs_result fs_file_seek(fs_file* pFile, fs_int64 offset, fs_seek_origin origin) |
| 3816 |
{ |
| 3817 |
const fs_backend* pBackend; |
| 3818 |
|
| 3819 |
if (pFile == NULL) { |
| 3820 |
return FS_INVALID_ARGS; |
| 3821 |
} |
| 3822 |
|
| 3823 |
pBackend = fs_file_get_backend(pFile); |
| 3824 |
FS_ASSERT(pBackend != NULL); |
| 3825 |
|
| 3826 |
return fs_backend_file_seek(pBackend, pFile, offset, origin); |
| 3827 |
} |
| 3828 |
|
| 3829 |
FS_API fs_result fs_file_tell(fs_file* pFile, fs_int64* pOffset) |
| 3830 |
{ |
| 3831 |
const fs_backend* pBackend; |
| 3832 |
|
| 3833 |
if (pOffset == NULL) { |
| 3834 |
return FS_INVALID_ARGS; |
| 3835 |
} |
| 3836 |
|
| 3837 |
*pOffset = 0; |
| 3838 |
|
| 3839 |
if (pFile == NULL) { |
| 3840 |
return FS_INVALID_ARGS; |
| 3841 |
} |
| 3842 |
|
| 3843 |
pBackend = fs_file_get_backend(pFile); |
| 3844 |
FS_ASSERT(pBackend != NULL); |
| 3845 |
|
| 3846 |
return fs_backend_file_tell(pBackend, pFile, pOffset); |
| 3847 |
} |
| 3848 |
|
| 3849 |
FS_API fs_result fs_file_flush(fs_file* pFile) |
| 3850 |
{ |
| 3851 |
const fs_backend* pBackend; |
| 3852 |
|
| 3853 |
if (pFile == NULL) { |
| 3854 |
return FS_INVALID_ARGS; |
| 3855 |
} |
| 3856 |
|
| 3857 |
pBackend = fs_file_get_backend(pFile); |
| 3858 |
FS_ASSERT(pBackend != NULL); |
| 3859 |
|
| 3860 |
return fs_backend_file_flush(pBackend, pFile); |
| 3861 |
} |
| 3862 |
|
| 3863 |
FS_API fs_result fs_file_truncate(fs_file* pFile) |
| 3864 |
{ |
| 3865 |
const fs_backend* pBackend; |
| 3866 |
|
| 3867 |
if (pFile == NULL) { |
| 3868 |
return FS_INVALID_ARGS; |
| 3869 |
} |
| 3870 |
|
| 3871 |
pBackend = fs_file_get_backend(pFile); |
| 3872 |
FS_ASSERT(pBackend != NULL); |
| 3873 |
|
| 3874 |
return fs_backend_file_truncate(pBackend, pFile); |
| 3875 |
} |
| 3876 |
|
| 3877 |
FS_API fs_result fs_file_get_info(fs_file* pFile, fs_file_info* pInfo) |
| 3878 |
{ |
| 3879 |
const fs_backend* pBackend; |
| 3880 |
|
| 3881 |
if (pInfo == NULL) { |
| 3882 |
return FS_INVALID_ARGS; |
| 3883 |
} |
| 3884 |
|
| 3885 |
memset(pInfo, 0, sizeof(*pInfo)); |
| 3886 |
|
| 3887 |
if (pFile == NULL) { |
| 3888 |
return FS_INVALID_ARGS; |
| 3889 |
} |
| 3890 |
|
| 3891 |
pBackend = fs_file_get_backend(pFile); |
| 3892 |
FS_ASSERT(pBackend != NULL); |
| 3893 |
|
| 3894 |
return fs_backend_file_info(pBackend, pFile, pInfo); |
| 3895 |
} |
| 3896 |
|
| 3897 |
FS_API fs_result fs_file_duplicate(fs_file* pFile, fs_file** ppDuplicate) |
| 3898 |
{ |
| 3899 |
fs_result result; |
| 3900 |
|
| 3901 |
if (ppDuplicate == NULL) { |
| 3902 |
return FS_INVALID_ARGS; |
| 3903 |
} |
| 3904 |
|
| 3905 |
*ppDuplicate = NULL; |
| 3906 |
|
| 3907 |
if (pFile == NULL) { |
| 3908 |
return FS_INVALID_ARGS; |
| 3909 |
} |
| 3910 |
|
| 3911 |
result = fs_file_alloc(pFile->pFS, ppDuplicate); |
| 3912 |
if (result != FS_SUCCESS) { |
| 3913 |
return result; |
| 3914 |
} |
| 3915 |
|
| 3916 |
result = fs_backend_file_duplicate(fs_get_backend_or_default(fs_file_get_fs(pFile)), pFile, *ppDuplicate); |
| 3917 |
if (result != FS_SUCCESS) { |
| 3918 |
fs_file_free(ppDuplicate); |
| 3919 |
return result; |
| 3920 |
} |
| 3921 |
|
| 3922 |
return result; |
| 3923 |
} |
| 3924 |
|
| 3925 |
FS_API void* fs_file_get_backend_data(fs_file* pFile) |
| 3926 |
{ |
| 3927 |
if (pFile == NULL) { |
| 3928 |
return NULL; |
| 3929 |
} |
| 3930 |
|
| 3931 |
return FS_OFFSET_PTR(pFile, sizeof(fs_file)); |
| 3932 |
} |
| 3933 |
|
| 3934 |
FS_API size_t fs_file_get_backend_data_size(fs_file* pFile) |
| 3935 |
{ |
| 3936 |
if (pFile == NULL) { |
| 3937 |
return 0; |
| 3938 |
} |
| 3939 |
|
| 3940 |
return pFile->backendDataSize; |
| 3941 |
} |
| 3942 |
|
| 3943 |
FS_API fs_stream* fs_file_get_stream(fs_file* pFile) |
| 3944 |
{ |
| 3945 |
return (fs_stream*)pFile; |
| 3946 |
} |
| 3947 |
|
| 3948 |
FS_API fs* fs_file_get_fs(fs_file* pFile) |
| 3949 |
{ |
| 3950 |
if (pFile == NULL) { |
| 3951 |
return NULL; |
| 3952 |
} |
| 3953 |
|
| 3954 |
return pFile->pFS; |
| 3955 |
} |
| 3956 |
|
| 3957 |
|
| 3958 |
typedef struct fs_iterator_item |
| 3959 |
{ |
| 3960 |
size_t nameLen; |
| 3961 |
fs_file_info info; |
| 3962 |
} fs_iterator_item; |
| 3963 |
|
| 3964 |
typedef struct fs_iterator_internal |
| 3965 |
{ |
| 3966 |
fs_iterator base; |
| 3967 |
size_t itemIndex; |
| 3968 |
size_t itemCount; |
| 3969 |
size_t itemDataSize; |
| 3970 |
size_t dataSize; |
| 3971 |
size_t allocSize; |
| 3972 |
fs_iterator_item** ppItems; |
| 3973 |
} fs_iterator_internal; |
| 3974 |
|
| 3975 |
static size_t fs_iterator_item_sizeof(size_t nameLen) |
| 3976 |
{ |
| 3977 |
return FS_ALIGN(sizeof(fs_iterator_item) + nameLen + 1, FS_SIZEOF_PTR); |
| 3978 |
} |
| 3979 |
|
| 3980 |
static char* fs_iterator_item_name(fs_iterator_item* pItem) |
| 3981 |
{ |
| 3982 |
return (char*)pItem + sizeof(*pItem); |
| 3983 |
} |
| 3984 |
|
| 3985 |
static void fs_iterator_internal_resolve_public_members(fs_iterator_internal* pIterator) |
| 3986 |
{ |
| 3987 |
FS_ASSERT(pIterator != NULL); |
| 3988 |
|
| 3989 |
pIterator->base.pName = fs_iterator_item_name(pIterator->ppItems[pIterator->itemIndex]); |
| 3990 |
pIterator->base.nameLen = pIterator->ppItems[pIterator->itemIndex]->nameLen; |
| 3991 |
pIterator->base.info = pIterator->ppItems[pIterator->itemIndex]->info; |
| 3992 |
} |
| 3993 |
|
| 3994 |
static fs_iterator_item* fs_iterator_internal_find(fs_iterator_internal* pIterator, const char* pName) |
| 3995 |
{ |
| 3996 |
|
| 3997 |
|
| 3998 |
|
| 3999 |
|
| 4000 |
size_t iItem; |
| 4001 |
size_t cursor = 0; |
| 4002 |
|
| 4003 |
for (iItem = 0; iItem < pIterator->itemCount; iItem += 1) { |
| 4004 |
fs_iterator_item* pItem = (fs_iterator_item*)FS_OFFSET_PTR(pIterator, sizeof(fs_iterator_internal) + cursor); |
| 4005 |
if (fs_strncmp(fs_iterator_item_name(pItem), pName, pItem->nameLen) == 0) { |
| 4006 |
return pItem; |
| 4007 |
} |
| 4008 |
|
| 4009 |
cursor += fs_iterator_item_sizeof(pItem->nameLen); |
| 4010 |
} |
| 4011 |
|
| 4012 |
return NULL; |
| 4013 |
} |
| 4014 |
|
| 4015 |
static fs_iterator_internal* fs_iterator_internal_append(fs_iterator_internal* pIterator, fs_iterator* pOther, fs* pFS, int mode) |
| 4016 |
{ |
| 4017 |
size_t newItemSize; |
| 4018 |
fs_iterator_item* pNewItem; |
| 4019 |
|
| 4020 |
FS_ASSERT(pOther != NULL); |
| 4021 |
|
| 4022 |
|
| 4023 |
if ((pOther->pName[0] == '.' && pOther->pName[1] == 0) || (pOther->pName[0] == '.' && pOther->pName[1] == '.' && pOther->pName[2] == 0)) { |
| 4024 |
return pIterator; |
| 4025 |
} |
| 4026 |
|
| 4027 |
|
| 4028 |
|
| 4029 |
(void)mode; |
| 4030 |
|
| 4031 |
|
| 4032 |
|
| 4033 |
if (pIterator != NULL) { |
| 4034 |
pNewItem = fs_iterator_internal_find(pIterator, pOther->pName); |
| 4035 |
if (pNewItem != NULL) { |
| 4036 |
return pIterator; |
| 4037 |
} |
| 4038 |
} |
| 4039 |
|
| 4040 |
|
| 4041 |
newItemSize = fs_iterator_item_sizeof(pOther->nameLen); |
| 4042 |
if (pIterator == NULL || pIterator->dataSize + newItemSize + sizeof(fs_iterator_item*) > pIterator->allocSize) { |
| 4043 |
fs_iterator_internal* pNewIterator; |
| 4044 |
size_t newAllocSize; |
| 4045 |
|
| 4046 |
if (pIterator == NULL) { |
| 4047 |
newAllocSize = 4096; |
| 4048 |
if (newAllocSize < (sizeof(*pIterator) + newItemSize + sizeof(fs_iterator_item*))) { |
| 4049 |
newAllocSize = (sizeof(*pIterator) + newItemSize + sizeof(fs_iterator_item*)); |
| 4050 |
} |
| 4051 |
} else { |
| 4052 |
newAllocSize = pIterator->allocSize * 2; |
| 4053 |
if (newAllocSize < (pIterator->dataSize + newItemSize + sizeof(fs_iterator_item*))) { |
| 4054 |
newAllocSize = (pIterator->dataSize + newItemSize + sizeof(fs_iterator_item*)); |
| 4055 |
} |
| 4056 |
} |
| 4057 |
|
| 4058 |
pNewIterator = (fs_iterator_internal*)fs_realloc(pIterator, newAllocSize, fs_get_allocation_callbacks(pFS)); |
| 4059 |
if (pNewIterator == NULL) { |
| 4060 |
return pIterator; |
| 4061 |
} |
| 4062 |
|
| 4063 |
if (pIterator == NULL) { |
| 4064 |
FS_ZERO_MEMORY(pNewIterator, sizeof(fs_iterator_internal)); |
| 4065 |
pNewIterator->dataSize = sizeof(fs_iterator_internal); |
| 4066 |
} |
| 4067 |
|
| 4068 |
pIterator = pNewIterator; |
| 4069 |
pIterator->allocSize = newAllocSize; |
| 4070 |
} |
| 4071 |
|
| 4072 |
|
| 4073 |
pNewItem = (fs_iterator_item*)FS_OFFSET_PTR(pIterator, sizeof(fs_iterator_internal) + pIterator->itemDataSize); |
| 4074 |
FS_COPY_MEMORY(fs_iterator_item_name(pNewItem), pOther->pName, pOther->nameLen + 1); |
| 4075 |
pNewItem->nameLen = pOther->nameLen; |
| 4076 |
pNewItem->info = pOther->info; |
| 4077 |
|
| 4078 |
pIterator->itemDataSize += newItemSize; |
| 4079 |
pIterator->dataSize += newItemSize + sizeof(fs_iterator_item*); |
| 4080 |
pIterator->itemCount += 1; |
| 4081 |
|
| 4082 |
return pIterator; |
| 4083 |
} |
| 4084 |
|
| 4085 |
|
| 4086 |
static int fs_iterator_item_compare(void* pUserData, const void* pA, const void* pB) |
| 4087 |
{ |
| 4088 |
fs_iterator_item* pItemA = *(fs_iterator_item**)pA; |
| 4089 |
fs_iterator_item* pItemB = *(fs_iterator_item**)pB; |
| 4090 |
const char* pNameA = fs_iterator_item_name(pItemA); |
| 4091 |
const char* pNameB = fs_iterator_item_name(pItemB); |
| 4092 |
int compareResult; |
| 4093 |
|
| 4094 |
(void)pUserData; |
| 4095 |
|
| 4096 |
compareResult = fs_strncmp(pNameA, pNameB, FS_MIN(pItemA->nameLen, pItemB->nameLen)); |
| 4097 |
if (compareResult == 0) { |
| 4098 |
if (pItemA->nameLen < pItemB->nameLen) { |
| 4099 |
compareResult = -1; |
| 4100 |
} else if (pItemA->nameLen > pItemB->nameLen) { |
| 4101 |
compareResult = 1; |
| 4102 |
} |
| 4103 |
} |
| 4104 |
|
| 4105 |
return compareResult; |
| 4106 |
} |
| 4107 |
|
| 4108 |
static void fs_iterator_internal_sort(fs_iterator_internal* pIterator) |
| 4109 |
{ |
| 4110 |
fs_sort(pIterator->ppItems, pIterator->itemCount, sizeof(fs_iterator_item*), fs_iterator_item_compare, NULL); |
| 4111 |
} |
| 4112 |
|
| 4113 |
static fs_iterator_internal* fs_iterator_internal_gather(fs_iterator_internal* pIterator, const fs_backend* pBackend, fs* pFS, const char* pDirectoryPath, size_t directoryPathLen, int mode) |
| 4114 |
{ |
| 4115 |
fs_result result; |
| 4116 |
fs_iterator* pInnerIterator; |
| 4117 |
|
| 4118 |
FS_ASSERT(pBackend != NULL); |
| 4119 |
|
| 4120 |
|
| 4121 |
for (pInnerIterator = fs_backend_first(pBackend, pFS, pDirectoryPath, directoryPathLen); pInnerIterator != NULL; pInnerIterator = fs_backend_next(pBackend, pInnerIterator)) { |
| 4122 |
pIterator = fs_iterator_internal_append(pIterator, pInnerIterator, pFS, mode); |
| 4123 |
} |
| 4124 |
|
| 4125 |
|
| 4126 |
if (pFS != NULL && !FS_IS_OPAQUE(mode)) { |
| 4127 |
fs_path_iterator iDirPathSeg; |
| 4128 |
|
| 4129 |
|
| 4130 |
|
| 4131 |
|
| 4132 |
|
| 4133 |
|
| 4134 |
|
| 4135 |
mode |= FS_IGNORE_MOUNTS; |
| 4136 |
mode &= ~FS_ONLY_MOUNTS; |
| 4137 |
|
| 4138 |
|
| 4139 |
if (pFS->archiveTypesAllocSize == 0) { |
| 4140 |
return pIterator; |
| 4141 |
} |
| 4142 |
|
| 4143 |
|
| 4144 |
|
| 4145 |
|
| 4146 |
|
| 4147 |
|
| 4148 |
|
| 4149 |
if (fs_path_first(pDirectoryPath, directoryPathLen, &iDirPathSeg) != FS_SUCCESS) { |
| 4150 |
return pIterator; |
| 4151 |
} |
| 4152 |
|
| 4153 |
do |
| 4154 |
{ |
| 4155 |
fs_result backendIteratorResult; |
| 4156 |
fs_registered_backend_iterator iBackend; |
| 4157 |
fs_bool32 isArchive = FS_FALSE; |
| 4158 |
size_t dirPathRemainingLen; |
| 4159 |
|
| 4160 |
|
| 4161 |
if (fs_strncmp(iDirPathSeg.pFullPath, ".", iDirPathSeg.segmentLength) == 0) { |
| 4162 |
continue; |
| 4163 |
} |
| 4164 |
if (fs_strncmp(iDirPathSeg.pFullPath, "..", iDirPathSeg.segmentLength) == 0) { |
| 4165 |
continue; |
| 4166 |
} |
| 4167 |
|
| 4168 |
if (fs_path_is_last(&iDirPathSeg)) { |
| 4169 |
dirPathRemainingLen = 0; |
| 4170 |
} else { |
| 4171 |
if (directoryPathLen == FS_NULL_TERMINATED) { |
| 4172 |
dirPathRemainingLen = FS_NULL_TERMINATED; |
| 4173 |
} else { |
| 4174 |
dirPathRemainingLen = directoryPathLen - (iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength + 1); |
| 4175 |
} |
| 4176 |
} |
| 4177 |
|
| 4178 |
|
| 4179 |
for (backendIteratorResult = fs_first_registered_backend(pFS, &iBackend); backendIteratorResult == FS_SUCCESS; backendIteratorResult = fs_next_registered_backend(&iBackend)) { |
| 4180 |
if (fs_path_extension_equal(iDirPathSeg.pFullPath + iDirPathSeg.segmentOffset, iDirPathSeg.segmentLength, iBackend.pExtension, iBackend.extensionLen)) { |
| 4181 |
fs* pArchive; |
| 4182 |
fs_iterator* pArchiveIterator; |
| 4183 |
|
| 4184 |
isArchive = FS_TRUE; |
| 4185 |
|
| 4186 |
result = fs_open_archive_ex(pFS, iBackend.pBackend, iBackend.pBackendConfig, iDirPathSeg.pFullPath, iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength, FS_READ | (mode & ~FS_WRITE), &pArchive); |
| 4187 |
if (result != FS_SUCCESS) { |
| 4188 |
|
| 4189 |
|
| 4190 |
|
| 4191 |
|
| 4192 |
if (result != FS_DOES_NOT_EXIST) { |
| 4193 |
fs_close_archive(pArchive); |
| 4194 |
return pIterator; |
| 4195 |
} else { |
| 4196 |
continue; |
| 4197 |
} |
| 4198 |
} |
| 4199 |
|
| 4200 |
if (dirPathRemainingLen == 0) { |
| 4201 |
pArchiveIterator = fs_first_ex(pArchive, "", 0, mode); |
| 4202 |
} else { |
| 4203 |
pArchiveIterator = fs_first_ex(pArchive, iDirPathSeg.pFullPath + iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength + 1, dirPathRemainingLen, mode); |
| 4204 |
} |
| 4205 |
|
| 4206 |
while (pArchiveIterator != NULL) { |
| 4207 |
pIterator = fs_iterator_internal_append(pIterator, pArchiveIterator, pFS, mode); |
| 4208 |
pArchiveIterator = fs_next(pArchiveIterator); |
| 4209 |
} |
| 4210 |
|
| 4211 |
fs_close_archive(pArchive); |
| 4212 |
break; |
| 4213 |
} |
| 4214 |
} |
| 4215 |
|
| 4216 |
|
| 4217 |
if (isArchive) { |
| 4218 |
return pIterator; |
| 4219 |
} |
| 4220 |
|
| 4221 |
|
| 4222 |
|
| 4223 |
|
| 4224 |
|
| 4225 |
if (FS_IS_VERBOSE(mode)) { |
| 4226 |
|
| 4227 |
|
| 4228 |
|
| 4229 |
|
| 4230 |
|
| 4231 |
continue; |
| 4232 |
} else { |
| 4233 |
|
| 4234 |
|
| 4235 |
|
| 4236 |
|
| 4237 |
|
| 4238 |
|
| 4239 |
|
| 4240 |
for (pInnerIterator = fs_backend_first(pBackend, pFS, iDirPathSeg.pFullPath, iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength); pInnerIterator != NULL; pInnerIterator = fs_backend_next(pBackend, pInnerIterator)) { |
| 4241 |
for (backendIteratorResult = fs_first_registered_backend(pFS, &iBackend); backendIteratorResult == FS_SUCCESS; backendIteratorResult = fs_next_registered_backend(&iBackend)) { |
| 4242 |
if (fs_path_extension_equal(pInnerIterator->pName, pInnerIterator->nameLen, iBackend.pExtension, iBackend.extensionLen)) { |
| 4243 |
|
| 4244 |
fs* pArchive; |
| 4245 |
fs_iterator* pArchiveIterator; |
| 4246 |
fs_string archivePath; |
| 4247 |
|
| 4248 |
result = fs_string_alloc(iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength + 1 + pInnerIterator->nameLen, fs_get_allocation_callbacks(pFS), &archivePath); |
| 4249 |
if (result != FS_SUCCESS) { |
| 4250 |
fs_backend_free_iterator(pBackend, pInnerIterator); |
| 4251 |
return pIterator; |
| 4252 |
} |
| 4253 |
|
| 4254 |
fs_string_append_preallocated(&archivePath, iDirPathSeg.pFullPath, iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength); |
| 4255 |
fs_string_append_preallocated(&archivePath, "/", FS_NULL_TERMINATED); |
| 4256 |
fs_string_append_preallocated(&archivePath, pInnerIterator->pName, pInnerIterator->nameLen); |
| 4257 |
|
| 4258 |
|
| 4259 |
result = fs_open_archive_ex(pFS, iBackend.pBackend, iBackend.pBackendConfig, fs_string_cstr(&archivePath), FS_NULL_TERMINATED, FS_READ | (mode & ~FS_WRITE), &pArchive); |
| 4260 |
fs_string_free(&archivePath, fs_get_allocation_callbacks(pFS)); |
| 4261 |
|
| 4262 |
if (result != FS_SUCCESS) { |
| 4263 |
continue; |
| 4264 |
} |
| 4265 |
|
| 4266 |
if (dirPathRemainingLen == 0) { |
| 4267 |
pArchiveIterator = fs_first_ex(pArchive, "", 0, mode); |
| 4268 |
} else { |
| 4269 |
pArchiveIterator = fs_first_ex(pArchive, iDirPathSeg.pFullPath + iDirPathSeg.segmentOffset + iDirPathSeg.segmentLength + 1, dirPathRemainingLen, mode); |
| 4270 |
} |
| 4271 |
|
| 4272 |
while (pArchiveIterator != NULL) { |
| 4273 |
pIterator = fs_iterator_internal_append(pIterator, pArchiveIterator, pFS, mode); |
| 4274 |
pArchiveIterator = fs_next(pArchiveIterator); |
| 4275 |
} |
| 4276 |
|
| 4277 |
fs_close_archive(pArchive); |
| 4278 |
break; |
| 4279 |
} |
| 4280 |
} |
| 4281 |
} |
| 4282 |
} |
| 4283 |
} while (fs_path_next(&iDirPathSeg) == FS_SUCCESS); |
| 4284 |
} |
| 4285 |
|
| 4286 |
return pIterator; |
| 4287 |
} |
| 4288 |
|
| 4289 |
FS_API fs_iterator* fs_first_ex(fs* pFS, const char* pDirectoryPath, size_t directoryPathLen, int mode) |
| 4290 |
{ |
| 4291 |
fs_iterator_internal* pIterator = NULL; |
| 4292 |
const fs_backend* pBackend; |
| 4293 |
fs_iterator* pBackendIterator; |
| 4294 |
fs_result result; |
| 4295 |
size_t cursor; |
| 4296 |
size_t iItem; |
| 4297 |
|
| 4298 |
if (pDirectoryPath == NULL) { |
| 4299 |
pDirectoryPath = ""; |
| 4300 |
} |
| 4301 |
|
| 4302 |
result = fs_validate_path(pDirectoryPath, directoryPathLen, mode); |
| 4303 |
if (result != FS_SUCCESS) { |
| 4304 |
return NULL; |
| 4305 |
} |
| 4306 |
|
| 4307 |
pBackend = fs_get_backend_or_default(pFS); |
| 4308 |
if (pBackend == NULL) { |
| 4309 |
return NULL; |
| 4310 |
} |
| 4311 |
|
| 4312 |
if (directoryPathLen == FS_NULL_TERMINATED) { |
| 4313 |
directoryPathLen = strlen(pDirectoryPath); |
| 4314 |
} |
| 4315 |
|
| 4316 |
|
| 4317 |
if (pFS == NULL) { |
| 4318 |
mode |= FS_IGNORE_MOUNTS; |
| 4319 |
mode &= ~FS_ONLY_MOUNTS; |
| 4320 |
} |
| 4321 |
|
| 4322 |
|
| 4323 |
|
| 4324 |
|
| 4325 |
|
| 4326 |
|
| 4327 |
|
| 4328 |
|
| 4329 |
|
| 4330 |
|
| 4331 |
|
| 4332 |
|
| 4333 |
if ((mode & FS_WRITE) != 0) { |
| 4334 |
|
| 4335 |
if (pFS != NULL && (mode & FS_IGNORE_MOUNTS) == 0) { |
| 4336 |
fs_mount_point* pBestMountPoint = NULL; |
| 4337 |
fs_string fileRealPath; |
| 4338 |
|
| 4339 |
pBestMountPoint = fs_find_best_write_mount_point(pFS, pDirectoryPath, mode, &fileRealPath); |
| 4340 |
if (pBestMountPoint != NULL) { |
| 4341 |
pIterator = fs_iterator_internal_gather(pIterator, pBackend, pFS, fs_string_cstr(&fileRealPath), fs_string_len(&fileRealPath), mode); |
| 4342 |
fs_string_free(&fileRealPath, fs_get_allocation_callbacks(pFS)); |
| 4343 |
} |
| 4344 |
} else { |
| 4345 |
|
| 4346 |
if ((mode & FS_ONLY_MOUNTS) == 0) { |
| 4347 |
pIterator = fs_iterator_internal_gather(pIterator, pBackend, pFS, pDirectoryPath, directoryPathLen, mode); |
| 4348 |
} |
| 4349 |
} |
| 4350 |
} else { |
| 4351 |
|
| 4352 |
fs_result mountPointIerationResult; |
| 4353 |
fs_mount_list_iterator iMountPoint; |
| 4354 |
|
| 4355 |
|
| 4356 |
if (pFS != NULL && (mode & FS_IGNORE_MOUNTS) == 0) { |
| 4357 |
for (mountPointIerationResult = fs_mount_list_first(pFS->pReadMountPoints, &iMountPoint); mountPointIerationResult == FS_SUCCESS; mountPointIerationResult = fs_mount_list_next(&iMountPoint)) { |
| 4358 |
if (iMountPoint.pArchive != NULL) { |
| 4359 |
fs_string dirSubPath; |
| 4360 |
|
| 4361 |
result = fs_resolve_sub_path_from_mount_point(pFS, iMountPoint.internal.pMountPoint, pDirectoryPath, mode, &dirSubPath); |
| 4362 |
if (result != FS_SUCCESS) { |
| 4363 |
continue; |
| 4364 |
} |
| 4365 |
|
| 4366 |
pBackendIterator = fs_first_ex(iMountPoint.pArchive, fs_string_cstr(&dirSubPath), fs_string_len(&dirSubPath), mode); |
| 4367 |
while (pBackendIterator != NULL) { |
| 4368 |
pIterator = fs_iterator_internal_append(pIterator, pBackendIterator, pFS, mode); |
| 4369 |
pBackendIterator = fs_next(pBackendIterator); |
| 4370 |
} |
| 4371 |
|
| 4372 |
fs_string_free(&dirSubPath, fs_get_allocation_callbacks(pFS)); |
| 4373 |
} else { |
| 4374 |
fs_string dirRealPath; |
| 4375 |
|
| 4376 |
result = fs_resolve_real_path_from_mount_point(pFS, iMountPoint.internal.pMountPoint, pDirectoryPath, mode, &dirRealPath); |
| 4377 |
if (result != FS_SUCCESS) { |
| 4378 |
continue; |
| 4379 |
} |
| 4380 |
|
| 4381 |
pIterator = fs_iterator_internal_gather(pIterator, pBackend, pFS, fs_string_cstr(&dirRealPath), fs_string_len(&dirRealPath), mode); |
| 4382 |
fs_string_free(&dirRealPath, fs_get_allocation_callbacks(pFS)); |
| 4383 |
} |
| 4384 |
} |
| 4385 |
} |
| 4386 |
|
| 4387 |
|
| 4388 |
if ((mode & FS_ONLY_MOUNTS) == 0) { |
| 4389 |
pIterator = fs_iterator_internal_gather(pIterator, pBackend, pFS, pDirectoryPath, directoryPathLen, mode); |
| 4390 |
} |
| 4391 |
} |
| 4392 |
|
| 4393 |
|
| 4394 |
if (pIterator == NULL) { |
| 4395 |
return NULL; |
| 4396 |
} |
| 4397 |
|
| 4398 |
|
| 4399 |
pIterator->ppItems = (fs_iterator_item**)FS_OFFSET_PTR(pIterator, pIterator->dataSize - (pIterator->itemCount * sizeof(fs_iterator_item*))); |
| 4400 |
|
| 4401 |
cursor = 0; |
| 4402 |
for (iItem = 0; iItem < pIterator->itemCount; iItem += 1) { |
| 4403 |
pIterator->ppItems[iItem] = (fs_iterator_item*)FS_OFFSET_PTR(pIterator, sizeof(fs_iterator_internal) + cursor); |
| 4404 |
cursor += fs_iterator_item_sizeof(pIterator->ppItems[iItem]->nameLen); |
| 4405 |
} |
| 4406 |
|
| 4407 |
|
| 4408 |
fs_iterator_internal_sort(pIterator); |
| 4409 |
|
| 4410 |
|
| 4411 |
pIterator->base.pFS = pFS; |
| 4412 |
pIterator->itemIndex = 0; |
| 4413 |
fs_iterator_internal_resolve_public_members(pIterator); |
| 4414 |
|
| 4415 |
return (fs_iterator*)pIterator; |
| 4416 |
} |
| 4417 |
|
| 4418 |
FS_API fs_iterator* fs_first(fs* pFS, const char* pDirectoryPath, int mode) |
| 4419 |
{ |
| 4420 |
return fs_first_ex(pFS, pDirectoryPath, FS_NULL_TERMINATED, mode); |
| 4421 |
} |
| 4422 |
|
| 4423 |
FS_API fs_iterator* fs_next(fs_iterator* pIterator) |
| 4424 |
{ |
| 4425 |
fs_iterator_internal* pIteratorInternal = (fs_iterator_internal*)pIterator; |
| 4426 |
|
| 4427 |
if (pIteratorInternal == NULL) { |
| 4428 |
return NULL; |
| 4429 |
} |
| 4430 |
|
| 4431 |
pIteratorInternal->itemIndex += 1; |
| 4432 |
|
| 4433 |
if (pIteratorInternal->itemIndex == pIteratorInternal->itemCount) { |
| 4434 |
fs_free_iterator(pIterator); |
| 4435 |
return NULL; |
| 4436 |
} |
| 4437 |
|
| 4438 |
fs_iterator_internal_resolve_public_members(pIteratorInternal); |
| 4439 |
|
| 4440 |
return pIterator; |
| 4441 |
} |
| 4442 |
|
| 4443 |
FS_API void fs_free_iterator(fs_iterator* pIterator) |
| 4444 |
{ |
| 4445 |
if (pIterator == NULL) { |
| 4446 |
return; |
| 4447 |
} |
| 4448 |
|
| 4449 |
fs_free(pIterator, fs_get_allocation_callbacks(pIterator->pFS)); |
| 4450 |
} |
| 4451 |
|
| 4452 |
|
| 4453 |
|
| 4454 |
static void fs_on_refcount_changed_internal(void* pUserData, fs* pFS, fs_uint32 newRefCount, fs_uint32 oldRefCount) |
| 4455 |
{ |
| 4456 |
fs* pOwnerFS; |
| 4457 |
|
| 4458 |
(void)pUserData; |
| 4459 |
(void)pFS; |
| 4460 |
(void)newRefCount; |
| 4461 |
(void)oldRefCount; |
| 4462 |
|
| 4463 |
pOwnerFS = (fs*)pUserData; |
| 4464 |
FS_ASSERT(pOwnerFS != NULL); |
| 4465 |
|
| 4466 |
if (newRefCount == 1) { |
| 4467 |
|
| 4468 |
fs_gc_archives(pOwnerFS, FS_GC_POLICY_THRESHOLD); |
| 4469 |
} |
| 4470 |
} |
| 4471 |
|
| 4472 |
static fs_result fs_open_archive_nolock(fs* pFS, const fs_backend* pBackend, const void* pBackendConfig, const char* pArchivePath, size_t archivePathLen, int openMode, fs** ppArchive) |
| 4473 |
{ |
| 4474 |
fs_result result; |
| 4475 |
fs* pArchive; |
| 4476 |
fs_config archiveConfig; |
| 4477 |
fs_file* pArchiveFile; |
| 4478 |
char pArchivePathNTStack[1024]; |
| 4479 |
char* pArchivePathNTHeap = NULL; |
| 4480 |
char* pArchivePathNT; |
| 4481 |
fs_opened_archive* pOpenedArchive; |
| 4482 |
|
| 4483 |
|
| 4484 |
|
| 4485 |
|
| 4486 |
|
| 4487 |
pOpenedArchive = fs_find_opened_archive(pFS, pArchivePath, archivePathLen); |
| 4488 |
if (pOpenedArchive != NULL) { |
| 4489 |
pArchive = pOpenedArchive->pArchive; |
| 4490 |
} else { |
| 4491 |
|
| 4492 |
|
| 4493 |
|
| 4494 |
|
| 4495 |
|
| 4496 |
if (archivePathLen == FS_NULL_TERMINATED) { |
| 4497 |
pArchivePathNT = (char*)pArchivePath; |
| 4498 |
} else { |
| 4499 |
if (archivePathLen >= sizeof(pArchivePathNTStack)) { |
| 4500 |
pArchivePathNTHeap = (char*)fs_malloc(archivePathLen + 1, fs_get_allocation_callbacks(pFS)); |
| 4501 |
if (pArchivePathNTHeap == NULL) { |
| 4502 |
return FS_OUT_OF_MEMORY; |
| 4503 |
} |
| 4504 |
|
| 4505 |
pArchivePathNT = pArchivePathNTHeap; |
| 4506 |
} else { |
| 4507 |
pArchivePathNT = pArchivePathNTStack; |
| 4508 |
} |
| 4509 |
|
| 4510 |
FS_COPY_MEMORY(pArchivePathNT, pArchivePath, archivePathLen); |
| 4511 |
pArchivePathNT[archivePathLen] = '\0'; |
| 4512 |
} |
| 4513 |
|
| 4514 |
result = fs_file_open(pFS, pArchivePathNT, openMode, &pArchiveFile); |
| 4515 |
if (result != FS_SUCCESS) { |
| 4516 |
fs_free(pArchivePathNTHeap, fs_get_allocation_callbacks(pFS)); |
| 4517 |
return result; |
| 4518 |
} |
| 4519 |
|
| 4520 |
archiveConfig = fs_config_init(pBackend, pBackendConfig, fs_file_get_stream(pArchiveFile)); |
| 4521 |
archiveConfig.pAllocationCallbacks = fs_get_allocation_callbacks(pFS); |
| 4522 |
archiveConfig.onRefCountChanged = fs_on_refcount_changed_internal; |
| 4523 |
archiveConfig.pRefCountChangedUserData = pFS; |
| 4524 |
|
| 4525 |
result = fs_init(&archiveConfig, &pArchive); |
| 4526 |
fs_free(pArchivePathNTHeap, fs_get_allocation_callbacks(pFS)); |
| 4527 |
|
| 4528 |
if (result != FS_SUCCESS) { |
| 4529 |
fs_file_close(pArchiveFile); |
| 4530 |
return result; |
| 4531 |
} |
| 4532 |
|
| 4533 |
|
| 4534 |
|
| 4535 |
|
| 4536 |
|
| 4537 |
|
| 4538 |
|
| 4539 |
pArchive->pArchiveTypes = pFS->pArchiveTypes; |
| 4540 |
pArchive->archiveTypesAllocSize = pFS->archiveTypesAllocSize; |
| 4541 |
pArchive->isOwnerOfArchiveTypes = FS_FALSE; |
| 4542 |
|
| 4543 |
|
| 4544 |
result = fs_add_opened_archive(pFS, pArchive, pArchivePath, archivePathLen); |
| 4545 |
if (result != FS_SUCCESS) { |
| 4546 |
fs_uninit(pArchive); |
| 4547 |
fs_file_close(pArchiveFile); |
| 4548 |
return result; |
| 4549 |
} |
| 4550 |
} |
| 4551 |
|
| 4552 |
FS_ASSERT(pArchive != NULL); |
| 4553 |
|
| 4554 |
*ppArchive = ((openMode & FS_NO_INCREMENT_REFCOUNT) == 0) ? fs_ref(pArchive) : pArchive; |
| 4555 |
return FS_SUCCESS; |
| 4556 |
} |
| 4557 |
|
| 4558 |
FS_API fs_result fs_open_archive_ex(fs* pFS, const fs_backend* pBackend, const void* pBackendConfig, const char* pArchivePath, size_t archivePathLen, int openMode, fs** ppArchive) |
| 4559 |
{ |
| 4560 |
fs_result result; |
| 4561 |
|
| 4562 |
if (ppArchive == NULL) { |
| 4563 |
return FS_INVALID_ARGS; |
| 4564 |
} |
| 4565 |
|
| 4566 |
*ppArchive = NULL; |
| 4567 |
|
| 4568 |
if (pFS == NULL || pBackend == NULL || pArchivePath == NULL || archivePathLen == 0) { |
| 4569 |
return FS_INVALID_ARGS; |
| 4570 |
} |
| 4571 |
|
| 4572 |
|
| 4573 |
|
| 4574 |
|
| 4575 |
|
| 4576 |
|
| 4577 |
|
| 4578 |
|
| 4579 |
|
| 4580 |
|
| 4581 |
fs_mtx_lock(&pFS->archiveLock); |
| 4582 |
{ |
| 4583 |
result = fs_open_archive_nolock(pFS, pBackend, pBackendConfig, pArchivePath, archivePathLen, openMode, ppArchive); |
| 4584 |
} |
| 4585 |
fs_mtx_unlock(&pFS->archiveLock); |
| 4586 |
|
| 4587 |
return result; |
| 4588 |
} |
| 4589 |
|
| 4590 |
FS_API fs_result fs_open_archive(fs* pFS, const char* pArchivePath, int openMode, fs** ppArchive) |
| 4591 |
{ |
| 4592 |
fs_result backendIteratorResult; |
| 4593 |
fs_registered_backend_iterator iBackend; |
| 4594 |
fs_result result; |
| 4595 |
|
| 4596 |
if (ppArchive == NULL) { |
| 4597 |
return FS_INVALID_ARGS; |
| 4598 |
} |
| 4599 |
|
| 4600 |
*ppArchive = NULL; |
| 4601 |
|
| 4602 |
if (pFS == NULL || pArchivePath == NULL) { |
| 4603 |
return FS_INVALID_ARGS; |
| 4604 |
} |
| 4605 |
|
| 4606 |
|
| 4607 |
|
| 4608 |
|
| 4609 |
|
| 4610 |
result = FS_NO_BACKEND; |
| 4611 |
for (backendIteratorResult = fs_first_registered_backend(pFS, &iBackend); backendIteratorResult == FS_SUCCESS; backendIteratorResult = fs_next_registered_backend(&iBackend)) { |
| 4612 |
if (fs_path_extension_equal(pArchivePath, FS_NULL_TERMINATED, iBackend.pExtension, iBackend.extensionLen)) { |
| 4613 |
result = fs_open_archive_ex(pFS, iBackend.pBackend, iBackend.pBackendConfig, pArchivePath, FS_NULL_TERMINATED, openMode, ppArchive); |
| 4614 |
if (result == FS_SUCCESS) { |
| 4615 |
return FS_SUCCESS; |
| 4616 |
} |
| 4617 |
} |
| 4618 |
} |
| 4619 |
|
| 4620 |
|
| 4621 |
return result; |
| 4622 |
} |
| 4623 |
|
| 4624 |
FS_API void fs_close_archive(fs* pArchive) |
| 4625 |
{ |
| 4626 |
fs_uint32 newRefCount; |
| 4627 |
|
| 4628 |
if (pArchive == NULL) { |
| 4629 |
return; |
| 4630 |
} |
| 4631 |
|
| 4632 |
|
| 4633 |
newRefCount = fs_unref(pArchive); |
| 4634 |
|
| 4635 |
|
| 4636 |
|
| 4637 |
|
| 4638 |
|
| 4639 |
if (newRefCount == 1) { |
| 4640 |
|
| 4641 |
|
| 4642 |
|
| 4643 |
|
| 4644 |
|
| 4645 |
fs* pArchiveOwnerFS = (fs*)pArchive->pRefCountChangedUserData; |
| 4646 |
FS_ASSERT(pArchiveOwnerFS != NULL); |
| 4647 |
|
| 4648 |
fs_gc(pArchiveOwnerFS, FS_GC_POLICY_FULL, pArchive); |
| 4649 |
} |
| 4650 |
} |
| 4651 |
|
| 4652 |
static void fs_gc_nolock(fs* pFS, int policy, fs* pSpecificArchive) |
| 4653 |
{ |
| 4654 |
size_t unreferencedCount = 0; |
| 4655 |
size_t collectionCount = 0; |
| 4656 |
size_t cursor = 0; |
| 4657 |
|
| 4658 |
FS_ASSERT(pFS != NULL); |
| 4659 |
|
| 4660 |
|
| 4661 |
|
| 4662 |
|
| 4663 |
|
| 4664 |
if ((policy & FS_GC_POLICY_FULL) != 0) { |
| 4665 |
|
| 4666 |
if (pSpecificArchive != NULL) { |
| 4667 |
fs_gc_archives(pSpecificArchive, FS_GC_POLICY_FULL); |
| 4668 |
} else { |
| 4669 |
cursor = 0; |
| 4670 |
while (cursor < pFS->openedArchivesSize) { |
| 4671 |
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); |
| 4672 |
FS_ASSERT(pOpenedArchive != NULL); |
| 4673 |
|
| 4674 |
fs_gc_archives(pOpenedArchive->pArchive, policy); |
| 4675 |
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 4676 |
} |
| 4677 |
} |
| 4678 |
} |
| 4679 |
|
| 4680 |
|
| 4681 |
cursor = 0; |
| 4682 |
while (cursor < pFS->openedArchivesSize) { |
| 4683 |
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); |
| 4684 |
|
| 4685 |
if (fs_refcount(pOpenedArchive->pArchive) == 1) { |
| 4686 |
unreferencedCount += 1; |
| 4687 |
} |
| 4688 |
|
| 4689 |
|
| 4690 |
if (pSpecificArchive != NULL && pSpecificArchive == pOpenedArchive->pArchive) { |
| 4691 |
if (fs_refcount(pSpecificArchive) == 1) { |
| 4692 |
break; |
| 4693 |
} else { |
| 4694 |
|
| 4695 |
return; |
| 4696 |
} |
| 4697 |
} |
| 4698 |
|
| 4699 |
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 4700 |
} |
| 4701 |
|
| 4702 |
|
| 4703 |
if ((policy & FS_GC_POLICY_THRESHOLD) != 0) { |
| 4704 |
if (unreferencedCount > fs_get_archive_gc_threshold(pFS)) { |
| 4705 |
collectionCount = unreferencedCount - fs_get_archive_gc_threshold(pFS); |
| 4706 |
} else { |
| 4707 |
collectionCount = 0; |
| 4708 |
} |
| 4709 |
} else if ((policy & FS_GC_POLICY_FULL) != 0) { |
| 4710 |
collectionCount = unreferencedCount; |
| 4711 |
} else { |
| 4712 |
FS_ASSERT(!"Invalid GC policy."); |
| 4713 |
} |
| 4714 |
|
| 4715 |
|
| 4716 |
cursor = 0; |
| 4717 |
while (collectionCount > 0 && cursor < pFS->openedArchivesSize) { |
| 4718 |
fs_opened_archive* pOpenedArchive = (fs_opened_archive*)FS_OFFSET_PTR(pFS->pOpenedArchives, cursor); |
| 4719 |
|
| 4720 |
if (fs_refcount(pOpenedArchive->pArchive) == 1 && (pSpecificArchive == NULL || pSpecificArchive == pOpenedArchive->pArchive)) { |
| 4721 |
fs_file* pArchiveFile = (fs_file*)pOpenedArchive->pArchive->pStream; |
| 4722 |
FS_ASSERT(pArchiveFile != NULL); |
| 4723 |
|
| 4724 |
fs_uninit(pOpenedArchive->pArchive); |
| 4725 |
fs_file_close(pArchiveFile); |
| 4726 |
fs_remove_opened_archive(pFS, pOpenedArchive); |
| 4727 |
|
| 4728 |
collectionCount -= 1; |
| 4729 |
|
| 4730 |
} else { |
| 4731 |
cursor += FS_ALIGN(sizeof(fs*) + sizeof(size_t) + strlen(pOpenedArchive->pPath) + 1, FS_SIZEOF_PTR); |
| 4732 |
} |
| 4733 |
} |
| 4734 |
} |
| 4735 |
|
| 4736 |
static void fs_gc(fs* pFS, int policy, fs* pSpecificArchive) |
| 4737 |
{ |
| 4738 |
if (pFS == NULL) { |
| 4739 |
return; |
| 4740 |
} |
| 4741 |
|
| 4742 |
if (policy == 0 || ((policy & FS_GC_POLICY_THRESHOLD) != 0 && (policy & FS_GC_POLICY_FULL) != 0)) { |
| 4743 |
return; |
| 4744 |
} |
| 4745 |
|
| 4746 |
fs_mtx_lock(&pFS->archiveLock); |
| 4747 |
{ |
| 4748 |
fs_gc_nolock(pFS, policy, pSpecificArchive); |
| 4749 |
} |
| 4750 |
fs_mtx_unlock(&pFS->archiveLock); |
| 4751 |
} |
| 4752 |
|
| 4753 |
FS_API void fs_gc_archives(fs* pFS, int policy) |
| 4754 |
{ |
| 4755 |
fs_gc(pFS, policy, NULL); |
| 4756 |
} |
| 4757 |
|
| 4758 |
FS_API void fs_set_archive_gc_threshold(fs* pFS, size_t threshold) |
| 4759 |
{ |
| 4760 |
if (pFS == NULL) { |
| 4761 |
return; |
| 4762 |
} |
| 4763 |
|
| 4764 |
pFS->archiveGCThreshold = threshold; |
| 4765 |
} |
| 4766 |
|
| 4767 |
FS_API size_t fs_get_archive_gc_threshold(fs* pFS) |
| 4768 |
{ |
| 4769 |
if (pFS == NULL) { |
| 4770 |
return 0; |
| 4771 |
} |
| 4772 |
|
| 4773 |
return pFS->archiveGCThreshold; |
| 4774 |
} |
| 4775 |
|
| 4776 |
|
| 4777 |
static fs_result fs_unmount_read(fs* pFS, const char* pActualPath, int options) |
| 4778 |
{ |
| 4779 |
fs_result iteratorResult; |
| 4780 |
fs_mount_list_iterator iterator; |
| 4781 |
|
| 4782 |
if (pFS == NULL || pActualPath == NULL) { |
| 4783 |
return FS_INVALID_ARGS; |
| 4784 |
} |
| 4785 |
|
| 4786 |
FS_UNUSED(options); |
| 4787 |
|
| 4788 |
for (iteratorResult = fs_mount_list_first(pFS->pReadMountPoints, &iterator); iteratorResult == FS_SUCCESS && !fs_mount_list_at_end(&iterator); ) { |
| 4789 |
if (strcmp(pActualPath, iterator.pPath) == 0) { |
| 4790 |
if (iterator.internal.pMountPoint->closeArchiveOnUnmount) { |
| 4791 |
fs_close_archive(iterator.pArchive); |
| 4792 |
} |
| 4793 |
|
| 4794 |
fs_mount_list_remove(pFS->pReadMountPoints, iterator.internal.pMountPoint); |
| 4795 |
|
| 4796 |
|
| 4797 |
|
| 4798 |
|
| 4799 |
|
| 4800 |
fs_mount_list_iterator_resolve_members(&iterator, iterator.internal.cursor); |
| 4801 |
} else { |
| 4802 |
iteratorResult = fs_mount_list_next(&iterator); |
| 4803 |
} |
| 4804 |
} |
| 4805 |
|
| 4806 |
return FS_SUCCESS; |
| 4807 |
} |
| 4808 |
|
| 4809 |
static fs_result fs_mount_read(fs* pFS, const char* pActualPath, const char* pVirtualPath, int options) |
| 4810 |
{ |
| 4811 |
fs_result result; |
| 4812 |
fs_mount_list_iterator iterator; |
| 4813 |
fs_result iteratorResult; |
| 4814 |
fs_mount_list* pMountPoints; |
| 4815 |
fs_mount_point* pNewMountPoint; |
| 4816 |
fs_file_info fileInfo; |
| 4817 |
|
| 4818 |
FS_ASSERT(pFS != NULL); |
| 4819 |
FS_ASSERT(pActualPath != NULL); |
| 4820 |
FS_ASSERT(pVirtualPath != NULL); |
| 4821 |
FS_ASSERT((options & FS_READ) == FS_READ); |
| 4822 |
|
| 4823 |
|
| 4824 |
|
| 4825 |
|
| 4826 |
|
| 4827 |
|
| 4828 |
for (iteratorResult = fs_mount_list_first(pFS->pReadMountPoints, &iterator); iteratorResult == FS_SUCCESS; iteratorResult = fs_mount_list_next(&iterator)) { |
| 4829 |
if (strcmp(pActualPath, iterator.pPath) == 0 && strcmp(pVirtualPath, iterator.pMountPointPath) == 0) { |
| 4830 |
return FS_SUCCESS; |
| 4831 |
} |
| 4832 |
} |
| 4833 |
|
| 4834 |
|
| 4835 |
|
| 4836 |
|
| 4837 |
|
| 4838 |
pMountPoints = fs_mount_list_alloc(pFS->pReadMountPoints, pActualPath, pVirtualPath, ((options & FS_LOWEST_PRIORITY) == FS_LOWEST_PRIORITY) ? FS_MOUNT_PRIORITY_LOWEST : FS_MOUNT_PRIORITY_HIGHEST, fs_get_allocation_callbacks(pFS), &pNewMountPoint); |
| 4839 |
if (pMountPoints == NULL) { |
| 4840 |
return FS_OUT_OF_MEMORY; |
| 4841 |
} |
| 4842 |
|
| 4843 |
pNewMountPoint->pArchive = NULL; |
| 4844 |
pNewMountPoint->closeArchiveOnUnmount = FS_FALSE; |
| 4845 |
|
| 4846 |
pFS->pReadMountPoints = pMountPoints; |
| 4847 |
|
| 4848 |
|
| 4849 |
|
| 4850 |
|
| 4851 |
|
| 4852 |
|
| 4853 |
|
| 4854 |
result = fs_backend_info(fs_get_backend_or_default(pFS), pFS, (pActualPath[0] != '\0') ? pActualPath : ".", FS_IGNORE_MOUNTS, &fileInfo); |
| 4855 |
if (result != FS_SUCCESS) { |
| 4856 |
fs_unmount_read(pFS, pActualPath, options); |
| 4857 |
return result; |
| 4858 |
} |
| 4859 |
|
| 4860 |
|
| 4861 |
if (!fileInfo.directory) { |
| 4862 |
result = fs_open_archive(pFS, pActualPath, FS_READ | FS_VERBOSE, &pNewMountPoint->pArchive); |
| 4863 |
if (result != FS_SUCCESS) { |
| 4864 |
fs_unmount_read(pFS, pActualPath, options); |
| 4865 |
return result; |
| 4866 |
} |
| 4867 |
|
| 4868 |
pNewMountPoint->closeArchiveOnUnmount = FS_TRUE; |
| 4869 |
} |
| 4870 |
|
| 4871 |
return FS_SUCCESS; |
| 4872 |
} |
| 4873 |
|
| 4874 |
|
| 4875 |
static fs_result fs_unmount_write(fs* pFS, const char* pActualPath, int options) |
| 4876 |
{ |
| 4877 |
fs_result iteratorResult; |
| 4878 |
fs_mount_list_iterator iterator; |
| 4879 |
|
| 4880 |
FS_ASSERT(pFS != NULL); |
| 4881 |
FS_ASSERT(pActualPath != NULL); |
| 4882 |
|
| 4883 |
FS_UNUSED(options); |
| 4884 |
|
| 4885 |
for (iteratorResult = fs_mount_list_first(pFS->pWriteMountPoints, &iterator); iteratorResult == FS_SUCCESS && !fs_mount_list_at_end(&iterator); ) { |
| 4886 |
if (strcmp(pActualPath, iterator.pPath) == 0) { |
| 4887 |
fs_mount_list_remove(pFS->pWriteMountPoints, iterator.internal.pMountPoint); |
| 4888 |
|
| 4889 |
|
| 4890 |
|
| 4891 |
|
| 4892 |
|
| 4893 |
fs_mount_list_iterator_resolve_members(&iterator, iterator.internal.cursor); |
| 4894 |
} else { |
| 4895 |
iteratorResult = fs_mount_list_next(&iterator); |
| 4896 |
} |
| 4897 |
} |
| 4898 |
|
| 4899 |
return FS_SUCCESS; |
| 4900 |
} |
| 4901 |
|
| 4902 |
static fs_result fs_mount_write(fs* pFS, const char* pActualPath, const char* pVirtualPath, int options) |
| 4903 |
{ |
| 4904 |
fs_result result; |
| 4905 |
fs_mount_list_iterator iterator; |
| 4906 |
fs_result iteratorResult; |
| 4907 |
fs_mount_point* pNewMountPoint; |
| 4908 |
fs_mount_list* pMountList; |
| 4909 |
fs_file_info fileInfo; |
| 4910 |
|
| 4911 |
if (pFS == NULL || pActualPath == NULL) { |
| 4912 |
return FS_INVALID_ARGS; |
| 4913 |
} |
| 4914 |
|
| 4915 |
if (pVirtualPath == NULL) { |
| 4916 |
pVirtualPath = ""; |
| 4917 |
} |
| 4918 |
|
| 4919 |
|
| 4920 |
for (iteratorResult = fs_mount_list_first(pFS->pWriteMountPoints, &iterator); iteratorResult == FS_SUCCESS; iteratorResult = fs_mount_list_next(&iterator)) { |
| 4921 |
if (strcmp(pActualPath, iterator.pPath) == 0 && strcmp(pVirtualPath, iterator.pMountPointPath) == 0) { |
| 4922 |
return FS_SUCCESS; |
| 4923 |
} |
| 4924 |
} |
| 4925 |
|
| 4926 |
|
| 4927 |
pMountList = fs_mount_list_alloc(pFS->pWriteMountPoints, pActualPath, pVirtualPath, ((options & FS_LOWEST_PRIORITY) == FS_LOWEST_PRIORITY) ? FS_MOUNT_PRIORITY_LOWEST : FS_MOUNT_PRIORITY_HIGHEST, fs_get_allocation_callbacks(pFS), &pNewMountPoint); |
| 4928 |
if (pMountList == NULL) { |
| 4929 |
return FS_OUT_OF_MEMORY; |
| 4930 |
} |
| 4931 |
|
| 4932 |
pFS->pWriteMountPoints = pMountList; |
| 4933 |
|
| 4934 |
|
| 4935 |
|
| 4936 |
|
| 4937 |
|
| 4938 |
pNewMountPoint->pArchive = NULL; |
| 4939 |
pNewMountPoint->closeArchiveOnUnmount = FS_FALSE; |
| 4940 |
|
| 4941 |
|
| 4942 |
result = fs_backend_info(fs_get_backend_or_default(pFS), pFS, (pActualPath[0] != '\0') ? pActualPath : ".", FS_IGNORE_MOUNTS, &fileInfo); |
| 4943 |
if (result != FS_SUCCESS && result != FS_DOES_NOT_EXIST) { |
| 4944 |
fs_unmount_write(pFS, pActualPath, options); |
| 4945 |
return result; |
| 4946 |
} |
| 4947 |
|
| 4948 |
if (!fileInfo.directory && result != FS_DOES_NOT_EXIST) { |
| 4949 |
fs_unmount_write(pFS, pActualPath, options); |
| 4950 |
return FS_INVALID_ARGS; |
| 4951 |
} |
| 4952 |
|
| 4953 |
|
| 4954 |
if ((options & FS_NO_CREATE_DIRS) == 0) { |
| 4955 |
fs_result result = fs_mkdir(pFS, pActualPath, FS_IGNORE_MOUNTS); |
| 4956 |
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) { |
| 4957 |
fs_unmount_write(pFS, pActualPath, options); |
| 4958 |
return result; |
| 4959 |
} |
| 4960 |
} |
| 4961 |
|
| 4962 |
return FS_SUCCESS; |
| 4963 |
} |
| 4964 |
|
| 4965 |
|
| 4966 |
FS_API fs_result fs_mount(fs* pFS, const char* pActualPath, const char* pVirtualPath, int options) |
| 4967 |
{ |
| 4968 |
if (pFS == NULL || pActualPath == NULL) { |
| 4969 |
return FS_INVALID_ARGS; |
| 4970 |
} |
| 4971 |
|
| 4972 |
if (pVirtualPath == NULL) { |
| 4973 |
pVirtualPath = ""; |
| 4974 |
} |
| 4975 |
|
| 4976 |
|
| 4977 |
if ((options & (FS_READ | FS_WRITE)) == 0) { |
| 4978 |
return FS_INVALID_ARGS; |
| 4979 |
} |
| 4980 |
|
| 4981 |
if ((options & FS_WRITE) == FS_WRITE) { |
| 4982 |
fs_result result = fs_mount_write(pFS, pActualPath, pVirtualPath, options); |
| 4983 |
if (result != FS_SUCCESS) { |
| 4984 |
return result; |
| 4985 |
} |
| 4986 |
} |
| 4987 |
|
| 4988 |
if ((options & FS_READ) == FS_READ) { |
| 4989 |
fs_result result = fs_mount_read(pFS, pActualPath, pVirtualPath, options); |
| 4990 |
if (result != FS_SUCCESS) { |
| 4991 |
return result; |
| 4992 |
} |
| 4993 |
} |
| 4994 |
|
| 4995 |
return FS_SUCCESS; |
| 4996 |
} |
| 4997 |
|
| 4998 |
FS_API fs_result fs_unmount(fs* pFS, const char* pActualPath, int options) |
| 4999 |
{ |
| 5000 |
fs_result result; |
| 5001 |
|
| 5002 |
if (pFS == NULL || pActualPath == NULL) { |
| 5003 |
return FS_INVALID_ARGS; |
| 5004 |
} |
| 5005 |
|
| 5006 |
if ((options & FS_READ) == FS_READ) { |
| 5007 |
result = fs_unmount_read(pFS, pActualPath, options); |
| 5008 |
if (result != FS_SUCCESS) { |
| 5009 |
return result; |
| 5010 |
} |
| 5011 |
} |
| 5012 |
|
| 5013 |
if ((options & FS_WRITE) == FS_WRITE) { |
| 5014 |
result = fs_unmount_write(pFS, pActualPath, options); |
| 5015 |
if (result != FS_SUCCESS) { |
| 5016 |
return result; |
| 5017 |
} |
| 5018 |
} |
| 5019 |
|
| 5020 |
return FS_SUCCESS; |
| 5021 |
} |
| 5022 |
|
| 5023 |
static size_t fs_sysdir_append(fs_sysdir_type type, char* pDst, size_t dstCap, const char* pSubDir) |
| 5024 |
{ |
| 5025 |
size_t sysDirLen; |
| 5026 |
size_t subDirLen; |
| 5027 |
size_t totalLen; |
| 5028 |
|
| 5029 |
if (pDst == NULL || pSubDir == NULL) { |
| 5030 |
return 0; |
| 5031 |
} |
| 5032 |
|
| 5033 |
sysDirLen = fs_sysdir(type, pDst, dstCap); |
| 5034 |
if (sysDirLen == 0) { |
| 5035 |
return 0; |
| 5036 |
} |
| 5037 |
|
| 5038 |
subDirLen = strlen(pSubDir); |
| 5039 |
|
| 5040 |
totalLen = sysDirLen + 1 + subDirLen; |
| 5041 |
if (totalLen < dstCap) { |
| 5042 |
pDst[sysDirLen] = '/'; |
| 5043 |
FS_COPY_MEMORY(pDst + sysDirLen + 1, pSubDir, subDirLen); |
| 5044 |
pDst[totalLen] = '\0'; |
| 5045 |
} |
| 5046 |
|
| 5047 |
return totalLen; |
| 5048 |
} |
| 5049 |
|
| 5050 |
FS_API fs_result fs_mount_sysdir(fs* pFS, fs_sysdir_type type, const char* pSubDir, const char* pVirtualPath, int options) |
| 5051 |
{ |
| 5052 |
char pPathToMountStack[1024]; |
| 5053 |
char* pPathToMountHeap = NULL; |
| 5054 |
char* pPathToMount; |
| 5055 |
size_t pathToMountLen; |
| 5056 |
fs_result result; |
| 5057 |
|
| 5058 |
if (pFS == NULL) { |
| 5059 |
return FS_INVALID_ARGS; |
| 5060 |
} |
| 5061 |
|
| 5062 |
if (pVirtualPath == NULL) { |
| 5063 |
pVirtualPath = ""; |
| 5064 |
} |
| 5065 |
|
| 5066 |
|
| 5067 |
|
| 5068 |
|
| 5069 |
|
| 5070 |
if (pSubDir == NULL || pSubDir[0] == '\0') { |
| 5071 |
return FS_INVALID_ARGS; |
| 5072 |
} |
| 5073 |
|
| 5074 |
pathToMountLen = fs_sysdir_append(type, pPathToMountStack, sizeof(pPathToMountStack), pSubDir); |
| 5075 |
if (pathToMountLen == 0) { |
| 5076 |
return FS_ERROR; |
| 5077 |
} |
| 5078 |
|
| 5079 |
if (pathToMountLen < sizeof(pPathToMountStack)) { |
| 5080 |
pPathToMount = pPathToMountStack; |
| 5081 |
} else { |
| 5082 |
pathToMountLen += 1; |
| 5083 |
|
| 5084 |
pPathToMountHeap = (char*)fs_malloc(pathToMountLen, fs_get_allocation_callbacks(pFS)); |
| 5085 |
if (pPathToMountHeap == NULL) { |
| 5086 |
return FS_OUT_OF_MEMORY; |
| 5087 |
} |
| 5088 |
|
| 5089 |
fs_sysdir_append(type, pPathToMountHeap, pathToMountLen, pSubDir); |
| 5090 |
pPathToMount = pPathToMountHeap; |
| 5091 |
} |
| 5092 |
|
| 5093 |
|
| 5094 |
result = fs_mount(pFS, pPathToMount, pVirtualPath, options); |
| 5095 |
fs_free(pPathToMountHeap, fs_get_allocation_callbacks(pFS)); |
| 5096 |
|
| 5097 |
return result; |
| 5098 |
} |
| 5099 |
|
| 5100 |
FS_API fs_result fs_unmount_sysdir(fs* pFS, fs_sysdir_type type, const char* pSubDir, int options) |
| 5101 |
{ |
| 5102 |
char pPathToMountStack[1024]; |
| 5103 |
char* pPathToMountHeap = NULL; |
| 5104 |
char* pPathToMount; |
| 5105 |
size_t pathToMountLen; |
| 5106 |
fs_result result; |
| 5107 |
|
| 5108 |
if (pFS == NULL) { |
| 5109 |
return FS_INVALID_ARGS; |
| 5110 |
} |
| 5111 |
|
| 5112 |
|
| 5113 |
|
| 5114 |
|
| 5115 |
|
| 5116 |
if (pSubDir == NULL || pSubDir[0] == '\0') { |
| 5117 |
return FS_INVALID_ARGS; |
| 5118 |
} |
| 5119 |
|
| 5120 |
pathToMountLen = fs_sysdir_append(type, pPathToMountStack, sizeof(pPathToMountStack), pSubDir); |
| 5121 |
if (pathToMountLen == 0) { |
| 5122 |
return FS_ERROR; |
| 5123 |
} |
| 5124 |
|
| 5125 |
if (pathToMountLen < sizeof(pPathToMountStack)) { |
| 5126 |
pPathToMount = pPathToMountStack; |
| 5127 |
} else { |
| 5128 |
pathToMountLen += 1; |
| 5129 |
|
| 5130 |
pPathToMountHeap = (char*)fs_malloc(pathToMountLen, fs_get_allocation_callbacks(pFS)); |
| 5131 |
if (pPathToMountHeap == NULL) { |
| 5132 |
return FS_OUT_OF_MEMORY; |
| 5133 |
} |
| 5134 |
|
| 5135 |
fs_sysdir_append(type, pPathToMountHeap, pathToMountLen, pSubDir); |
| 5136 |
pPathToMount = pPathToMountHeap; |
| 5137 |
} |
| 5138 |
|
| 5139 |
|
| 5140 |
result = fs_unmount(pFS, pPathToMount, options); |
| 5141 |
|
| 5142 |
fs_free(pPathToMountHeap, fs_get_allocation_callbacks(pFS)); |
| 5143 |
return result; |
| 5144 |
} |
| 5145 |
|
| 5146 |
FS_API fs_result fs_mount_fs(fs* pFS, fs* pOtherFS, const char* pVirtualPath, int options) |
| 5147 |
{ |
| 5148 |
fs_result iteratorResult; |
| 5149 |
fs_mount_list_iterator iterator; |
| 5150 |
fs_mount_list* pMountPoints; |
| 5151 |
fs_mount_point* pNewMountPoint; |
| 5152 |
|
| 5153 |
if (pFS == NULL || pOtherFS == NULL) { |
| 5154 |
return FS_INVALID_ARGS; |
| 5155 |
} |
| 5156 |
|
| 5157 |
if (pVirtualPath == NULL) { |
| 5158 |
pVirtualPath = ""; |
| 5159 |
} |
| 5160 |
|
| 5161 |
|
| 5162 |
if ((options & FS_WRITE) == FS_WRITE) { |
| 5163 |
return FS_INVALID_ARGS; |
| 5164 |
} |
| 5165 |
|
| 5166 |
|
| 5167 |
|
| 5168 |
|
| 5169 |
|
| 5170 |
for (iteratorResult = fs_mount_list_first(pFS->pReadMountPoints, &iterator); iteratorResult == FS_SUCCESS; iteratorResult = fs_mount_list_next(&iterator)) { |
| 5171 |
if (pOtherFS == iterator.pArchive && strcmp(pVirtualPath, iterator.pMountPointPath) == 0) { |
| 5172 |
|
| 5173 |
fs_ref(pOtherFS); |
| 5174 |
return FS_SUCCESS; |
| 5175 |
} |
| 5176 |
} |
| 5177 |
|
| 5178 |
|
| 5179 |
|
| 5180 |
|
| 5181 |
|
| 5182 |
pMountPoints = fs_mount_list_alloc(pFS->pReadMountPoints, "", pVirtualPath, ((options & FS_LOWEST_PRIORITY) == FS_LOWEST_PRIORITY) ? FS_MOUNT_PRIORITY_LOWEST : FS_MOUNT_PRIORITY_HIGHEST, fs_get_allocation_callbacks(pFS), &pNewMountPoint); |
| 5183 |
if (pMountPoints == NULL) { |
| 5184 |
return FS_OUT_OF_MEMORY; |
| 5185 |
} |
| 5186 |
|
| 5187 |
pFS->pReadMountPoints = pMountPoints; |
| 5188 |
|
| 5189 |
pNewMountPoint->pArchive = fs_ref(pOtherFS); |
| 5190 |
pNewMountPoint->closeArchiveOnUnmount = FS_FALSE; |
| 5191 |
|
| 5192 |
return FS_SUCCESS; |
| 5193 |
} |
| 5194 |
|
| 5195 |
FS_API fs_result fs_unmount_fs(fs* pFS, fs* pOtherFS, int options) |
| 5196 |
{ |
| 5197 |
fs_result iteratorResult; |
| 5198 |
fs_mount_list_iterator iterator; |
| 5199 |
|
| 5200 |
if (pFS == NULL || pOtherFS == NULL) { |
| 5201 |
return FS_INVALID_ARGS; |
| 5202 |
} |
| 5203 |
|
| 5204 |
FS_UNUSED(options); |
| 5205 |
|
| 5206 |
for (iteratorResult = fs_mount_list_first(pFS->pReadMountPoints, &iterator); iteratorResult == FS_SUCCESS; iteratorResult = fs_mount_list_next(&iterator)) { |
| 5207 |
if (iterator.pArchive == pOtherFS) { |
| 5208 |
fs_mount_list_remove(pFS->pReadMountPoints, iterator.internal.pMountPoint); |
| 5209 |
fs_unref(pOtherFS); |
| 5210 |
return FS_SUCCESS; |
| 5211 |
} |
| 5212 |
} |
| 5213 |
|
| 5214 |
return FS_SUCCESS; |
| 5215 |
} |
| 5216 |
|
| 5217 |
|
| 5218 |
FS_API fs_result fs_file_read_to_end(fs_file* pFile, fs_format format, void** ppData, size_t* pDataSize) |
| 5219 |
{ |
| 5220 |
return fs_stream_read_to_end(fs_file_get_stream(pFile), format, fs_get_allocation_callbacks(fs_file_get_fs(pFile)), ppData, pDataSize); |
| 5221 |
} |
| 5222 |
|
| 5223 |
FS_API fs_result fs_file_open_and_read(fs* pFS, const char* pFilePath, fs_format format, void** ppData, size_t* pDataSize) |
| 5224 |
{ |
| 5225 |
fs_result result; |
| 5226 |
fs_file* pFile; |
| 5227 |
|
| 5228 |
if (pFilePath == NULL || ppData == NULL || (pDataSize == NULL && format != FS_FORMAT_TEXT)) { |
| 5229 |
return FS_INVALID_ARGS; |
| 5230 |
} |
| 5231 |
|
| 5232 |
result = fs_file_open(pFS, pFilePath, FS_READ, &pFile); |
| 5233 |
if (result != FS_SUCCESS) { |
| 5234 |
return result; |
| 5235 |
} |
| 5236 |
|
| 5237 |
result = fs_file_read_to_end(pFile, format, ppData, pDataSize); |
| 5238 |
|
| 5239 |
fs_file_close(pFile); |
| 5240 |
|
| 5241 |
return result; |
| 5242 |
} |
| 5243 |
|
| 5244 |
FS_API fs_result fs_file_open_and_write(fs* pFS, const char* pFilePath, const void* pData, size_t dataSize) |
| 5245 |
{ |
| 5246 |
fs_result result; |
| 5247 |
fs_file* pFile; |
| 5248 |
|
| 5249 |
if (pFilePath == NULL) { |
| 5250 |
return FS_INVALID_ARGS; |
| 5251 |
} |
| 5252 |
|
| 5253 |
|
| 5254 |
if (pData == NULL && dataSize > 0) { |
| 5255 |
return FS_INVALID_ARGS; |
| 5256 |
} |
| 5257 |
|
| 5258 |
result = fs_file_open(pFS, pFilePath, FS_WRITE | FS_TRUNCATE, &pFile); |
| 5259 |
if (result != FS_SUCCESS) { |
| 5260 |
return result; |
| 5261 |
} |
| 5262 |
|
| 5263 |
if (dataSize > 0) { |
| 5264 |
result = fs_file_write(pFile, pData, dataSize, NULL); |
| 5265 |
} |
| 5266 |
|
| 5267 |
fs_file_close(pFile); |
| 5268 |
|
| 5269 |
return result; |
| 5270 |
} |
| 5271 |
|
| 5272 |
|
| 5273 |
|
| 5274 |
#define FS_SERIALIZED_SIG_0 0x52535346 |
| 5275 |
#define FS_SERIALIZED_SIG_1 0x00315A4C |
| 5276 |
|
| 5277 |
static fs_result fs_serialize_directory(fs* pFS, const char* pDirectoryPath, const char* pBasePath, int options, fs_stream* pOutputStream, fs_stream* pTOCStream, fs_uint32* pTOCEntryCount, fs_uint64* pRunningFileOffset) |
| 5278 |
{ |
| 5279 |
fs_result result; |
| 5280 |
fs_iterator* pIterator; |
| 5281 |
char padding[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 5282 |
|
| 5283 |
FS_ASSERT(pTOCEntryCount != NULL); |
| 5284 |
|
| 5285 |
for (pIterator = fs_first(pFS, pDirectoryPath, options); pIterator != NULL; pIterator = fs_next(pIterator)) { |
| 5286 |
fs_string path = fs_string_new(); |
| 5287 |
int pathLen; |
| 5288 |
const char* pTrimmedPath; |
| 5289 |
size_t trimmedPathLen; |
| 5290 |
fs_uint32 fileFlags; |
| 5291 |
fs_uint64 fileSize; |
| 5292 |
fs_uint64 fileOffset; |
| 5293 |
|
| 5294 |
*pTOCEntryCount += 1; |
| 5295 |
|
| 5296 |
|
| 5297 |
fileFlags = 0; |
| 5298 |
if (pIterator->info.directory) { |
| 5299 |
fileFlags |= 0x1; |
| 5300 |
} |
| 5301 |
|
| 5302 |
result = fs_stream_write(pTOCStream, &fileFlags, 4, NULL); |
| 5303 |
if (result != FS_SUCCESS) { |
| 5304 |
return result; |
| 5305 |
} |
| 5306 |
|
| 5307 |
|
| 5308 |
|
| 5309 |
pathLen = fs_path_append(path.stack, sizeof(path.stack), pDirectoryPath, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen); |
| 5310 |
if (pathLen < 0) { |
| 5311 |
return FS_ERROR; |
| 5312 |
} |
| 5313 |
|
| 5314 |
path.len = (size_t)pathLen; |
| 5315 |
|
| 5316 |
if (path.len > sizeof(path.stack)) { |
| 5317 |
result = fs_string_alloc(pathLen, fs_get_allocation_callbacks(pFS), &path); |
| 5318 |
if (result != FS_SUCCESS) { |
| 5319 |
return result; |
| 5320 |
} |
| 5321 |
|
| 5322 |
fs_path_append(path.heap, path.len + 1, pDirectoryPath, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen); |
| 5323 |
} |
| 5324 |
|
| 5325 |
pTrimmedPath = fs_path_trim_base(fs_string_cstr(&path), fs_string_len(&path), pBasePath, FS_NULL_TERMINATED); |
| 5326 |
if (pTrimmedPath == NULL) { |
| 5327 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5328 |
return FS_ERROR; |
| 5329 |
} |
| 5330 |
|
| 5331 |
trimmedPathLen = strlen(pTrimmedPath); |
| 5332 |
|
| 5333 |
|
| 5334 |
result = fs_stream_write(pTOCStream, &trimmedPathLen, 4, NULL); |
| 5335 |
if (result != FS_SUCCESS) { |
| 5336 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5337 |
return result; |
| 5338 |
} |
| 5339 |
|
| 5340 |
|
| 5341 |
result = fs_stream_write(pTOCStream, pTrimmedPath, trimmedPathLen, NULL); |
| 5342 |
if (result != FS_SUCCESS) { |
| 5343 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5344 |
return result; |
| 5345 |
} |
| 5346 |
|
| 5347 |
|
| 5348 |
result = fs_stream_write(pTOCStream, "\0", 1, NULL); |
| 5349 |
if (result != FS_SUCCESS) { |
| 5350 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5351 |
return result; |
| 5352 |
} |
| 5353 |
|
| 5354 |
|
| 5355 |
result = fs_stream_write(pTOCStream, padding, FS_ALIGN(trimmedPathLen + 1, 8) - (trimmedPathLen + 1), NULL); |
| 5356 |
if (result != FS_SUCCESS) { |
| 5357 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5358 |
return result; |
| 5359 |
} |
| 5360 |
|
| 5361 |
|
| 5362 |
if (pIterator->info.directory) { |
| 5363 |
fileOffset = 0; |
| 5364 |
fileSize = 0; |
| 5365 |
} else { |
| 5366 |
fileOffset = *pRunningFileOffset; |
| 5367 |
fileSize = 0; |
| 5368 |
|
| 5369 |
|
| 5370 |
{ |
| 5371 |
fs_file* pFile; |
| 5372 |
char buffer[4096]; |
| 5373 |
size_t bytesRead; |
| 5374 |
|
| 5375 |
result = fs_file_open(pFS, fs_string_cstr(&path), FS_READ | options, &pFile); |
| 5376 |
if (result != FS_SUCCESS) { |
| 5377 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5378 |
return result; |
| 5379 |
} |
| 5380 |
|
| 5381 |
for (;;) { |
| 5382 |
result = fs_file_read(pFile, buffer, sizeof(buffer), &bytesRead); |
| 5383 |
if (result != FS_SUCCESS && result != FS_AT_END) { |
| 5384 |
fs_file_close(pFile); |
| 5385 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5386 |
return result; |
| 5387 |
} |
| 5388 |
|
| 5389 |
if (bytesRead == 0) { |
| 5390 |
break; |
| 5391 |
} |
| 5392 |
|
| 5393 |
result = fs_stream_write(pOutputStream, buffer, bytesRead, NULL); |
| 5394 |
if (result != FS_SUCCESS) { |
| 5395 |
fs_file_close(pFile); |
| 5396 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5397 |
return result; |
| 5398 |
} |
| 5399 |
|
| 5400 |
fileSize += bytesRead; |
| 5401 |
|
| 5402 |
if (result == FS_AT_END) { |
| 5403 |
break; |
| 5404 |
} |
| 5405 |
} |
| 5406 |
|
| 5407 |
fs_file_close(pFile); |
| 5408 |
pFile = NULL; |
| 5409 |
} |
| 5410 |
*pRunningFileOffset += fileSize; |
| 5411 |
|
| 5412 |
|
| 5413 |
result = fs_stream_write(pOutputStream, padding, (size_t)(FS_ALIGN(*pRunningFileOffset, 8) - *pRunningFileOffset), NULL); |
| 5414 |
if (result != FS_SUCCESS) { |
| 5415 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5416 |
return result; |
| 5417 |
} |
| 5418 |
*pRunningFileOffset = FS_ALIGN(*pRunningFileOffset, 8); |
| 5419 |
} |
| 5420 |
|
| 5421 |
|
| 5422 |
|
| 5423 |
result = fs_stream_write(pTOCStream, &fileSize, 8, NULL); |
| 5424 |
if (result != FS_SUCCESS) { |
| 5425 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5426 |
return result; |
| 5427 |
} |
| 5428 |
|
| 5429 |
|
| 5430 |
result = fs_stream_write(pTOCStream, &fileOffset, 8, NULL); |
| 5431 |
if (result != FS_SUCCESS) { |
| 5432 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5433 |
return result; |
| 5434 |
} |
| 5435 |
|
| 5436 |
|
| 5437 |
|
| 5438 |
if (pIterator->info.directory) { |
| 5439 |
result = fs_serialize_directory(pFS, fs_string_cstr(&path), pBasePath, options, pOutputStream, pTOCStream, pTOCEntryCount, pRunningFileOffset); |
| 5440 |
if (result != FS_SUCCESS) { |
| 5441 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5442 |
return result; |
| 5443 |
} |
| 5444 |
} |
| 5445 |
|
| 5446 |
|
| 5447 |
|
| 5448 |
fs_string_free(&path, fs_get_allocation_callbacks(pFS)); |
| 5449 |
} |
| 5450 |
|
| 5451 |
return FS_SUCCESS; |
| 5452 |
} |
| 5453 |
|
| 5454 |
FS_API fs_result fs_serialize(fs* pFS, const char* pDirectoryPath, int options, fs_stream* pOutputStream) |
| 5455 |
{ |
| 5456 |
fs_result result; |
| 5457 |
fs_uint32 sig[2] = { FS_SERIALIZED_SIG_0, FS_SERIALIZED_SIG_1 }; |
| 5458 |
fs_memory_stream toc; |
| 5459 |
fs_uint32 tocEntryCount = 0; |
| 5460 |
fs_uint64 runningOffset = 0; |
| 5461 |
fs_uint64 tocOffset; |
| 5462 |
fs_int64 initialPos; |
| 5463 |
|
| 5464 |
if (pOutputStream == NULL) { |
| 5465 |
return FS_INVALID_ARGS; |
| 5466 |
} |
| 5467 |
|
| 5468 |
if (pDirectoryPath == NULL) { |
| 5469 |
pDirectoryPath = ""; |
| 5470 |
} |
| 5471 |
|
| 5472 |
|
| 5473 |
|
| 5474 |
result = fs_memory_stream_init_write(fs_get_allocation_callbacks(pFS), &toc); |
| 5475 |
if (result != FS_SUCCESS) { |
| 5476 |
return result; |
| 5477 |
} |
| 5478 |
|
| 5479 |
|
| 5480 |
|
| 5481 |
|
| 5482 |
|
| 5483 |
|
| 5484 |
result = fs_stream_tell(pOutputStream, &initialPos); |
| 5485 |
if (result != FS_SUCCESS) { |
| 5486 |
fs_memory_stream_uninit(&toc); |
| 5487 |
return result; |
| 5488 |
} |
| 5489 |
|
| 5490 |
result = fs_stream_write(pOutputStream, "\0\0\0\0\0\0\0\0", (size_t)(FS_ALIGN(initialPos, 8) - initialPos), NULL); |
| 5491 |
if (result != FS_SUCCESS) { |
| 5492 |
fs_memory_stream_uninit(&toc); |
| 5493 |
return result; |
| 5494 |
} |
| 5495 |
|
| 5496 |
|
| 5497 |
|
| 5498 |
result = fs_serialize_directory(pFS, pDirectoryPath, pDirectoryPath, options, pOutputStream, (fs_stream*)&toc, &tocEntryCount, &runningOffset); |
| 5499 |
if (result != FS_SUCCESS) { |
| 5500 |
fs_memory_stream_uninit(&toc); |
| 5501 |
return result; |
| 5502 |
} |
| 5503 |
|
| 5504 |
|
| 5505 |
{ |
| 5506 |
void* pTOCData; |
| 5507 |
size_t tocDataSize; |
| 5508 |
|
| 5509 |
|
| 5510 |
tocOffset = runningOffset; |
| 5511 |
|
| 5512 |
pTOCData = fs_memory_stream_take_ownership(&toc, &tocDataSize); |
| 5513 |
FS_ASSERT(pTOCData != NULL || tocDataSize == 0); |
| 5514 |
|
| 5515 |
fs_memory_stream_uninit(&toc); |
| 5516 |
|
| 5517 |
result = fs_stream_write(pOutputStream, pTOCData, tocDataSize, NULL); |
| 5518 |
fs_free(pTOCData, fs_get_allocation_callbacks(pFS)); |
| 5519 |
|
| 5520 |
if (result != FS_SUCCESS) { |
| 5521 |
fs_memory_stream_uninit(&toc); |
| 5522 |
return result; |
| 5523 |
} |
| 5524 |
|
| 5525 |
runningOffset += tocDataSize; |
| 5526 |
} |
| 5527 |
|
| 5528 |
|
| 5529 |
{ |
| 5530 |
char padding[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 5531 |
fs_int64 baseOffset; |
| 5532 |
fs_uint32 reserved; |
| 5533 |
|
| 5534 |
|
| 5535 |
result = fs_stream_write(pOutputStream, padding, (size_t)(FS_ALIGN(runningOffset, 8) - runningOffset), NULL); |
| 5536 |
if (result != FS_SUCCESS) { |
| 5537 |
return result; |
| 5538 |
} |
| 5539 |
runningOffset = FS_ALIGN(runningOffset, 8); |
| 5540 |
|
| 5541 |
|
| 5542 |
|
| 5543 |
|
| 5544 |
|
| 5545 |
|
| 5546 |
|
| 5547 |
|
| 5548 |
|
| 5549 |
|
| 5550 |
|
| 5551 |
baseOffset = -(fs_int64)(runningOffset + 32); |
| 5552 |
|
| 5553 |
|
| 5554 |
result = fs_stream_write(pOutputStream, sig, 8, NULL); |
| 5555 |
if (result != FS_SUCCESS) { |
| 5556 |
return result; |
| 5557 |
} |
| 5558 |
|
| 5559 |
|
| 5560 |
result = fs_stream_write(pOutputStream, &baseOffset, 8, NULL); |
| 5561 |
if (result != FS_SUCCESS) { |
| 5562 |
return result; |
| 5563 |
} |
| 5564 |
|
| 5565 |
|
| 5566 |
result = fs_stream_write(pOutputStream, &tocOffset, 8, NULL); |
| 5567 |
if (result != FS_SUCCESS) { |
| 5568 |
return result; |
| 5569 |
} |
| 5570 |
|
| 5571 |
|
| 5572 |
result = fs_stream_write(pOutputStream, &tocEntryCount, 4, NULL); |
| 5573 |
if (result != FS_SUCCESS) { |
| 5574 |
return result; |
| 5575 |
} |
| 5576 |
|
| 5577 |
|
| 5578 |
reserved = 0; |
| 5579 |
result = fs_stream_write(pOutputStream, &reserved, 4, NULL); |
| 5580 |
if (result != FS_SUCCESS) { |
| 5581 |
return result; |
| 5582 |
} |
| 5583 |
} |
| 5584 |
|
| 5585 |
return FS_SUCCESS; |
| 5586 |
} |
| 5587 |
|
| 5588 |
|
| 5589 |
|
| 5590 |
static fs_result fs_stream_read_u32_le(fs_stream* pStream, fs_uint32* pResult) |
| 5591 |
{ |
| 5592 |
fs_result result; |
| 5593 |
fs_uint8 bytes[4]; |
| 5594 |
|
| 5595 |
result = fs_stream_read(pStream, bytes, 4, NULL); |
| 5596 |
if (result != FS_SUCCESS) { |
| 5597 |
return result; |
| 5598 |
} |
| 5599 |
|
| 5600 |
*pResult = ((fs_uint32)bytes[0] << 0) | ((fs_uint32)bytes[1] << 8) | ((fs_uint32)bytes[2] << 16) | ((fs_uint32)bytes[3] << 24); |
| 5601 |
|
| 5602 |
return FS_SUCCESS; |
| 5603 |
} |
| 5604 |
|
| 5605 |
static fs_result fs_stream_read_u64_le(fs_stream* pStream, fs_uint64* pResult) |
| 5606 |
{ |
| 5607 |
fs_result result; |
| 5608 |
fs_uint8 bytes[8]; |
| 5609 |
|
| 5610 |
result = fs_stream_read(pStream, bytes, 8, NULL); |
| 5611 |
if (result != FS_SUCCESS) { |
| 5612 |
return result; |
| 5613 |
} |
| 5614 |
|
| 5615 |
*pResult = |
| 5616 |
((fs_uint64)bytes[0] << 0) | ((fs_uint64)bytes[1] << 8) | ((fs_uint64)bytes[2] << 16) | ((fs_uint64)bytes[3] << 24) | |
| 5617 |
((fs_uint64)bytes[4] << 32) | ((fs_uint64)bytes[5] << 40) | ((fs_uint64)bytes[6] << 48) | ((fs_uint64)bytes[7] << 56); |
| 5618 |
|
| 5619 |
return FS_SUCCESS; |
| 5620 |
} |
| 5621 |
|
| 5622 |
static fs_result fs_stream_read_s64_le(fs_stream* pStream, fs_int64* pResult) |
| 5623 |
{ |
| 5624 |
fs_result result; |
| 5625 |
fs_uint64 temp; |
| 5626 |
|
| 5627 |
result = fs_stream_read_u64_le(pStream, &temp); |
| 5628 |
if (result != FS_SUCCESS) { |
| 5629 |
return result; |
| 5630 |
} |
| 5631 |
|
| 5632 |
*pResult = (fs_int64)temp; |
| 5633 |
|
| 5634 |
return FS_SUCCESS; |
| 5635 |
} |
| 5636 |
|
| 5637 |
FS_API fs_result fs_deserialize(fs* pFS, const char* pDirectoryPath, int options, fs_stream* pInputStream) |
| 5638 |
{ |
| 5639 |
fs_result result; |
| 5640 |
fs_uint32 sig[2]; |
| 5641 |
fs_int64 baseOffset; |
| 5642 |
fs_uint64 tocOffset; |
| 5643 |
fs_uint32 tocEntryCount; |
| 5644 |
fs_uint32 reserved; |
| 5645 |
fs_uint32 iEntry; |
| 5646 |
|
| 5647 |
if (pInputStream == NULL) { |
| 5648 |
return FS_INVALID_ARGS; |
| 5649 |
} |
| 5650 |
|
| 5651 |
if (pDirectoryPath == NULL) { |
| 5652 |
pDirectoryPath = ""; |
| 5653 |
} |
| 5654 |
|
| 5655 |
|
| 5656 |
result = fs_stream_seek(pInputStream, -32, FS_SEEK_END); |
| 5657 |
if (result != FS_SUCCESS) { |
| 5658 |
return result; |
| 5659 |
} |
| 5660 |
|
| 5661 |
|
| 5662 |
result = fs_stream_read_u32_le(pInputStream, &sig[0]); |
| 5663 |
if (result != FS_SUCCESS) { |
| 5664 |
return result; |
| 5665 |
} |
| 5666 |
|
| 5667 |
result = fs_stream_read_u32_le(pInputStream, &sig[1]); |
| 5668 |
if (result != FS_SUCCESS) { |
| 5669 |
return result; |
| 5670 |
} |
| 5671 |
|
| 5672 |
if (sig[0] != FS_SERIALIZED_SIG_0 || sig[1] != FS_SERIALIZED_SIG_1) { |
| 5673 |
return FS_INVALID_DATA; |
| 5674 |
} |
| 5675 |
|
| 5676 |
|
| 5677 |
result = fs_stream_read_s64_le(pInputStream, &baseOffset); |
| 5678 |
if (result != FS_SUCCESS) { |
| 5679 |
return result; |
| 5680 |
} |
| 5681 |
|
| 5682 |
|
| 5683 |
result = fs_stream_read_u64_le(pInputStream, &tocOffset); |
| 5684 |
if (result != FS_SUCCESS) { |
| 5685 |
return result; |
| 5686 |
} |
| 5687 |
|
| 5688 |
|
| 5689 |
result = fs_stream_read_u32_le(pInputStream, &tocEntryCount); |
| 5690 |
if (result != FS_SUCCESS) { |
| 5691 |
return result; |
| 5692 |
} |
| 5693 |
|
| 5694 |
|
| 5695 |
result = fs_stream_read_u32_le(pInputStream, &reserved); |
| 5696 |
if (result != FS_SUCCESS) { |
| 5697 |
return result; |
| 5698 |
} |
| 5699 |
|
| 5700 |
|
| 5701 |
result = fs_stream_seek(pInputStream, baseOffset + (fs_int64)tocOffset, FS_SEEK_END); |
| 5702 |
if (result != FS_SUCCESS) { |
| 5703 |
return result; |
| 5704 |
} |
| 5705 |
|
| 5706 |
|
| 5707 |
for (iEntry = 0; iEntry < tocEntryCount; iEntry += 1) { |
| 5708 |
fs_uint32 flags; |
| 5709 |
fs_uint32 localPathLen; |
| 5710 |
fs_string localPath; |
| 5711 |
fs_string fullPath; |
| 5712 |
int fullPathLen; |
| 5713 |
fs_uint64 fileSize; |
| 5714 |
fs_uint64 fileOffset; |
| 5715 |
fs_int64 currentOffset; |
| 5716 |
fs_file* pFile = NULL; |
| 5717 |
|
| 5718 |
|
| 5719 |
result = fs_stream_read_u32_le(pInputStream, &flags); |
| 5720 |
if (result != FS_SUCCESS) { |
| 5721 |
return result; |
| 5722 |
} |
| 5723 |
|
| 5724 |
|
| 5725 |
result = fs_stream_read_u32_le(pInputStream, &localPathLen); |
| 5726 |
if (result != FS_SUCCESS) { |
| 5727 |
return result; |
| 5728 |
} |
| 5729 |
|
| 5730 |
|
| 5731 |
result = fs_string_alloc(localPathLen + 1, fs_get_allocation_callbacks(pFS), &localPath); |
| 5732 |
if (result != FS_SUCCESS) { |
| 5733 |
return result; |
| 5734 |
} |
| 5735 |
|
| 5736 |
result = fs_stream_read(pInputStream, fs_string_cstr_mutable(&localPath), localPathLen + 1, NULL); |
| 5737 |
if (result != FS_SUCCESS) { |
| 5738 |
fs_string_free(&localPath, fs_get_allocation_callbacks(pFS)); |
| 5739 |
return result; |
| 5740 |
} |
| 5741 |
|
| 5742 |
if (fs_string_cstr(&localPath)[localPathLen] != '\0') { |
| 5743 |
fs_string_free(&localPath, fs_get_allocation_callbacks(pFS)); |
| 5744 |
return FS_INVALID_DATA; |
| 5745 |
} |
| 5746 |
|
| 5747 |
localPath.len = (size_t)localPathLen; |
| 5748 |
|
| 5749 |
fullPath = fs_string_new(); |
| 5750 |
fullPathLen = fs_path_append(fullPath.stack, sizeof(fullPath.stack), pDirectoryPath, FS_NULL_TERMINATED, fs_string_cstr(&localPath), fs_string_len(&localPath)); |
| 5751 |
if (fullPathLen < 0) { |
| 5752 |
fs_string_free(&localPath, fs_get_allocation_callbacks(pFS)); |
| 5753 |
return FS_ERROR; |
| 5754 |
} |
| 5755 |
|
| 5756 |
fullPath.len = (size_t)fullPathLen; |
| 5757 |
|
| 5758 |
if (fullPath.len > sizeof(fullPath.stack)) { |
| 5759 |
result = fs_string_alloc(fullPathLen, fs_get_allocation_callbacks(pFS), &fullPath); |
| 5760 |
if (result != FS_SUCCESS) { |
| 5761 |
fs_string_free(&localPath, fs_get_allocation_callbacks(pFS)); |
| 5762 |
return result; |
| 5763 |
} |
| 5764 |
|
| 5765 |
fs_path_append(fullPath.heap, fullPath.len + 1, pDirectoryPath, FS_NULL_TERMINATED, fs_string_cstr(&localPath), fs_string_len(&localPath)); |
| 5766 |
} |
| 5767 |
|
| 5768 |
fs_string_free(&localPath, fs_get_allocation_callbacks(pFS)); |
| 5769 |
|
| 5770 |
|
| 5771 |
|
| 5772 |
result = fs_stream_seek(pInputStream, FS_ALIGN(localPathLen + 1, 8) - (localPathLen + 1), FS_SEEK_CUR); |
| 5773 |
if (result != FS_SUCCESS) { |
| 5774 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5775 |
return result; |
| 5776 |
} |
| 5777 |
|
| 5778 |
|
| 5779 |
result = fs_stream_read_u64_le(pInputStream, &fileSize); |
| 5780 |
if (result != FS_SUCCESS) { |
| 5781 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5782 |
return result; |
| 5783 |
} |
| 5784 |
|
| 5785 |
|
| 5786 |
result = fs_stream_read_u64_le(pInputStream, &fileOffset); |
| 5787 |
if (result != FS_SUCCESS) { |
| 5788 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5789 |
return result; |
| 5790 |
} |
| 5791 |
|
| 5792 |
|
| 5793 |
|
| 5794 |
result = fs_stream_tell(pInputStream, ¤tOffset); |
| 5795 |
if (result != FS_SUCCESS) { |
| 5796 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5797 |
return result; |
| 5798 |
} |
| 5799 |
|
| 5800 |
if ((flags & 0x1) != 0) { |
| 5801 |
|
| 5802 |
result = fs_mkdir(pFS, fs_string_cstr(&fullPath), options); |
| 5803 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5804 |
|
| 5805 |
if (result != FS_SUCCESS && result != FS_ALREADY_EXISTS) { |
| 5806 |
return result; |
| 5807 |
} |
| 5808 |
} else { |
| 5809 |
|
| 5810 |
fs_uint64 bytesRemaining; |
| 5811 |
|
| 5812 |
result = fs_file_open(pFS, fs_string_cstr(&fullPath), FS_WRITE | FS_TRUNCATE | options, &pFile); |
| 5813 |
fs_string_free(&fullPath, fs_get_allocation_callbacks(pFS)); |
| 5814 |
|
| 5815 |
if (result != FS_SUCCESS) { |
| 5816 |
return result; |
| 5817 |
} |
| 5818 |
|
| 5819 |
|
| 5820 |
result = fs_stream_seek(pInputStream, baseOffset + (fs_int64)fileOffset, FS_SEEK_END); |
| 5821 |
if (result != FS_SUCCESS) { |
| 5822 |
fs_file_close(pFile); |
| 5823 |
return result; |
| 5824 |
} |
| 5825 |
|
| 5826 |
|
| 5827 |
bytesRemaining = fileSize; |
| 5828 |
while (bytesRemaining > 0) { |
| 5829 |
char buffer[4096]; |
| 5830 |
size_t bytesRead; |
| 5831 |
fs_uint64 bytesToRead; |
| 5832 |
|
| 5833 |
bytesToRead = bytesRemaining; |
| 5834 |
if (bytesToRead > sizeof(buffer)) { |
| 5835 |
bytesToRead = sizeof(buffer); |
| 5836 |
} |
| 5837 |
|
| 5838 |
result = fs_stream_read(pInputStream, buffer, (size_t)bytesToRead, &bytesRead); |
| 5839 |
if (result != FS_SUCCESS && result != FS_AT_END) { |
| 5840 |
fs_file_close(pFile); |
| 5841 |
return result; |
| 5842 |
} |
| 5843 |
|
| 5844 |
if (bytesRead == 0) { |
| 5845 |
break; |
| 5846 |
} |
| 5847 |
|
| 5848 |
result = fs_file_write(pFile, buffer, bytesRead, NULL); |
| 5849 |
if (result != FS_SUCCESS) { |
| 5850 |
fs_file_close(pFile); |
| 5851 |
return result; |
| 5852 |
} |
| 5853 |
|
| 5854 |
bytesRemaining -= bytesRead; |
| 5855 |
|
| 5856 |
if (result == FS_AT_END) { |
| 5857 |
break; |
| 5858 |
} |
| 5859 |
} |
| 5860 |
|
| 5861 |
fs_file_close(pFile); |
| 5862 |
pFile = NULL; |
| 5863 |
} |
| 5864 |
|
| 5865 |
|
| 5866 |
result = fs_stream_seek(pInputStream, currentOffset, FS_SEEK_SET); |
| 5867 |
if (result != FS_SUCCESS) { |
| 5868 |
return result; |
| 5869 |
} |
| 5870 |
} |
| 5871 |
|
| 5872 |
return FS_SUCCESS; |
| 5873 |
} |
| 5874 |
|
| 5875 |
|
| 5876 |
|
| 5877 |
#if !defined(_WIN32) |
| 5878 |
#define FS_SUPPORTS_POSIX |
| 5879 |
#endif |
| 5880 |
|
| 5881 |
#if !defined(FS_NO_POSIX) && defined(FS_SUPPORTS_POSIX) |
| 5882 |
#define FS_HAS_POSIX |
| 5883 |
#endif |
| 5884 |
|
| 5885 |
#if defined(FS_HAS_POSIX) |
| 5886 |
|
| 5887 |
|
| 5888 |
#ifndef _FILE_OFFSET_BITS |
| 5889 |
#define _FILE_OFFSET_BITS 64 |
| 5890 |
#endif |
| 5891 |
|
| 5892 |
#include <string.h> |
| 5893 |
#include <stdio.h> |
| 5894 |
#include <errno.h> |
| 5895 |
#include <fcntl.h> |
| 5896 |
#include <unistd.h> |
| 5897 |
#include <dirent.h> |
| 5898 |
#include <sys/stat.h> |
| 5899 |
|
| 5900 |
|
| 5901 |
static size_t fs_alloc_size_posix(const void* pBackendConfig) |
| 5902 |
{ |
| 5903 |
(void)pBackendConfig; |
| 5904 |
return 0; |
| 5905 |
} |
| 5906 |
|
| 5907 |
static fs_result fs_init_posix(fs* pFS, const void* pBackendConfig, fs_stream* pStream) |
| 5908 |
{ |
| 5909 |
(void)pFS; |
| 5910 |
(void)pBackendConfig; |
| 5911 |
(void)pStream; |
| 5912 |
|
| 5913 |
return FS_SUCCESS; |
| 5914 |
} |
| 5915 |
|
| 5916 |
static void fs_uninit_posix(fs* pFS) |
| 5917 |
{ |
| 5918 |
(void)pFS; |
| 5919 |
} |
| 5920 |
|
| 5921 |
static fs_result fs_remove_posix(fs* pFS, const char* pFilePath) |
| 5922 |
{ |
| 5923 |
int result = remove(pFilePath); |
| 5924 |
if (result < 0) { |
| 5925 |
return fs_result_from_errno(errno); |
| 5926 |
} |
| 5927 |
|
| 5928 |
(void)pFS; |
| 5929 |
return FS_SUCCESS; |
| 5930 |
} |
| 5931 |
|
| 5932 |
static fs_result fs_rename_posix(fs* pFS, const char* pOldPath, const char* pNewPath) |
| 5933 |
{ |
| 5934 |
int result = rename(pOldPath, pNewPath); |
| 5935 |
if (result < 0) { |
| 5936 |
return fs_result_from_errno(errno); |
| 5937 |
} |
| 5938 |
|
| 5939 |
(void)pFS; |
| 5940 |
return FS_SUCCESS; |
| 5941 |
} |
| 5942 |
|
| 5943 |
static fs_result fs_mkdir_posix(fs* pFS, const char* pPath) |
| 5944 |
{ |
| 5945 |
int result = mkdir(pPath, S_IRWXU); |
| 5946 |
if (result < 0) { |
| 5947 |
return fs_result_from_errno(errno); |
| 5948 |
} |
| 5949 |
|
| 5950 |
(void)pFS; |
| 5951 |
return FS_SUCCESS; |
| 5952 |
} |
| 5953 |
|
| 5954 |
|
| 5955 |
static fs_file_info fs_file_info_from_stat_posix(struct stat* pStat) |
| 5956 |
{ |
| 5957 |
fs_file_info info; |
| 5958 |
|
| 5959 |
memset(&info, 0, sizeof(info)); |
| 5960 |
info.size = pStat->st_size; |
| 5961 |
info.lastAccessTime = pStat->st_atime; |
| 5962 |
info.lastModifiedTime = pStat->st_mtime; |
| 5963 |
info.directory = S_ISDIR(pStat->st_mode) != 0; |
| 5964 |
info.symlink = S_ISLNK(pStat->st_mode) != 0; |
| 5965 |
|
| 5966 |
return info; |
| 5967 |
} |
| 5968 |
|
| 5969 |
static fs_result fs_info_posix(fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo) |
| 5970 |
{ |
| 5971 |
struct stat info; |
| 5972 |
int result; |
| 5973 |
|
| 5974 |
if (pPath == FS_STDIN ) { |
| 5975 |
result = fstat(STDIN_FILENO, &info); |
| 5976 |
} else if (pPath == FS_STDOUT) { |
| 5977 |
result = fstat(STDOUT_FILENO, &info); |
| 5978 |
} else if (pPath == FS_STDERR) { |
| 5979 |
result = fstat(STDERR_FILENO, &info); |
| 5980 |
} else { |
| 5981 |
result = stat(pPath, &info); |
| 5982 |
} |
| 5983 |
|
| 5984 |
if (result != 0) { |
| 5985 |
return fs_result_from_errno(errno); |
| 5986 |
} |
| 5987 |
|
| 5988 |
*pInfo = fs_file_info_from_stat_posix(&info); |
| 5989 |
|
| 5990 |
(void)pFS; |
| 5991 |
(void)openMode; |
| 5992 |
return FS_SUCCESS; |
| 5993 |
} |
| 5994 |
|
| 5995 |
|
| 5996 |
typedef struct fs_file_posix |
| 5997 |
{ |
| 5998 |
int fd; |
| 5999 |
fs_bool32 isStandardHandle; |
| 6000 |
int openMode; |
| 6001 |
char* pFilePath; |
| 6002 |
char pFilePathStack[128]; |
| 6003 |
char* pFilePathHeap; |
| 6004 |
} fs_file_posix; |
| 6005 |
|
| 6006 |
static size_t fs_file_alloc_size_posix(fs* pFS) |
| 6007 |
{ |
| 6008 |
(void)pFS; |
| 6009 |
return sizeof(fs_file_posix); |
| 6010 |
} |
| 6011 |
|
| 6012 |
static fs_result fs_file_open_posix(fs* pFS, fs_stream* pStream, const char* pFilePath, int openMode, fs_file* pFile) |
| 6013 |
{ |
| 6014 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6015 |
int fd; |
| 6016 |
int flags = 0; |
| 6017 |
size_t filePathLen; |
| 6018 |
|
| 6019 |
if ((openMode & FS_READ) != 0) { |
| 6020 |
if ((openMode & FS_WRITE) != 0) { |
| 6021 |
flags |= O_RDWR | O_CREAT; |
| 6022 |
} else { |
| 6023 |
flags |= O_RDONLY; |
| 6024 |
} |
| 6025 |
} else if ((openMode & FS_WRITE) != 0) { |
| 6026 |
flags |= O_WRONLY | O_CREAT; |
| 6027 |
} |
| 6028 |
|
| 6029 |
if ((openMode & FS_WRITE) != 0) { |
| 6030 |
if ((openMode & FS_EXCLUSIVE) != 0) { |
| 6031 |
flags |= O_EXCL; |
| 6032 |
} else if ((openMode & FS_APPEND) != 0) { |
| 6033 |
flags |= O_APPEND; |
| 6034 |
} else if ((openMode & FS_TRUNCATE) != 0) { |
| 6035 |
flags |= O_TRUNC; |
| 6036 |
} |
| 6037 |
} |
| 6038 |
|
| 6039 |
|
| 6040 |
|
| 6041 |
#if defined(O_LARGEFILE) |
| 6042 |
flags |= O_LARGEFILE; |
| 6043 |
#endif |
| 6044 |
|
| 6045 |
if (pFilePath == FS_STDIN ) { |
| 6046 |
fd = STDIN_FILENO; |
| 6047 |
pFilePosix->isStandardHandle = FS_TRUE; |
| 6048 |
} else if (pFilePath == FS_STDOUT) { |
| 6049 |
fd = STDOUT_FILENO; |
| 6050 |
pFilePosix->isStandardHandle = FS_TRUE; |
| 6051 |
} else if (pFilePath == FS_STDERR) { |
| 6052 |
fd = STDERR_FILENO; |
| 6053 |
pFilePosix->isStandardHandle = FS_TRUE; |
| 6054 |
} else { |
| 6055 |
fd = open(pFilePath, flags, 0666); |
| 6056 |
} |
| 6057 |
|
| 6058 |
if (fd < 0) { |
| 6059 |
return fs_result_from_errno(errno); |
| 6060 |
} |
| 6061 |
|
| 6062 |
pFilePosix->fd = fd; |
| 6063 |
|
| 6064 |
|
| 6065 |
|
| 6066 |
|
| 6067 |
|
| 6068 |
|
| 6069 |
if (!pFilePosix->isStandardHandle) { |
| 6070 |
pFilePosix->openMode = openMode; |
| 6071 |
|
| 6072 |
filePathLen = strlen(pFilePath); |
| 6073 |
|
| 6074 |
if (filePathLen < FS_COUNTOF(pFilePosix->pFilePathStack)) { |
| 6075 |
pFilePosix->pFilePath = pFilePosix->pFilePathStack; |
| 6076 |
} else { |
| 6077 |
pFilePosix->pFilePathHeap = (char*)fs_malloc(filePathLen + 1, fs_get_allocation_callbacks(pFS)); |
| 6078 |
if (pFilePosix->pFilePathHeap == NULL) { |
| 6079 |
close(fd); |
| 6080 |
return FS_OUT_OF_MEMORY; |
| 6081 |
} |
| 6082 |
|
| 6083 |
pFilePosix->pFilePath = pFilePosix->pFilePathHeap; |
| 6084 |
} |
| 6085 |
|
| 6086 |
fs_strcpy(pFilePosix->pFilePath, pFilePath); |
| 6087 |
} |
| 6088 |
|
| 6089 |
|
| 6090 |
(void)pFS; |
| 6091 |
(void)pStream; |
| 6092 |
return FS_SUCCESS; |
| 6093 |
} |
| 6094 |
|
| 6095 |
static void fs_file_close_posix(fs_file* pFile) |
| 6096 |
{ |
| 6097 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6098 |
|
| 6099 |
|
| 6100 |
if (pFilePosix->isStandardHandle) { |
| 6101 |
return; |
| 6102 |
} |
| 6103 |
|
| 6104 |
close(pFilePosix->fd); |
| 6105 |
fs_free(pFilePosix->pFilePathHeap, fs_get_allocation_callbacks(fs_file_get_fs(pFile))); |
| 6106 |
} |
| 6107 |
|
| 6108 |
static fs_result fs_file_read_posix(fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 6109 |
{ |
| 6110 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6111 |
ssize_t bytesRead; |
| 6112 |
|
| 6113 |
bytesRead = read(pFilePosix->fd, pDst, bytesToRead); |
| 6114 |
if (bytesRead < 0) { |
| 6115 |
return fs_result_from_errno(errno); |
| 6116 |
} |
| 6117 |
|
| 6118 |
*pBytesRead = (size_t)bytesRead; |
| 6119 |
|
| 6120 |
if (*pBytesRead == 0) { |
| 6121 |
return FS_AT_END; |
| 6122 |
} |
| 6123 |
|
| 6124 |
return FS_SUCCESS; |
| 6125 |
} |
| 6126 |
|
| 6127 |
static fs_result fs_file_write_posix(fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 6128 |
{ |
| 6129 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6130 |
ssize_t bytesWritten; |
| 6131 |
|
| 6132 |
bytesWritten = write(pFilePosix->fd, pSrc, bytesToWrite); |
| 6133 |
if (bytesWritten < 0) { |
| 6134 |
return fs_result_from_errno(errno); |
| 6135 |
} |
| 6136 |
|
| 6137 |
*pBytesWritten = (size_t)bytesWritten; |
| 6138 |
return FS_SUCCESS; |
| 6139 |
} |
| 6140 |
|
| 6141 |
static fs_result fs_file_seek_posix(fs_file* pFile, fs_int64 offset, fs_seek_origin origin) |
| 6142 |
{ |
| 6143 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6144 |
int whence; |
| 6145 |
off_t result; |
| 6146 |
|
| 6147 |
if (origin == FS_SEEK_SET) { |
| 6148 |
whence = SEEK_SET; |
| 6149 |
} else if (origin == FS_SEEK_END) { |
| 6150 |
whence = SEEK_END; |
| 6151 |
} else { |
| 6152 |
whence = SEEK_CUR; |
| 6153 |
} |
| 6154 |
|
| 6155 |
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 |
| 6156 |
{ |
| 6157 |
result = lseek(pFilePosix->fd, (off_t)offset, whence); |
| 6158 |
} |
| 6159 |
#else |
| 6160 |
{ |
| 6161 |
if (offset < -2147483648 || offset > 2147483647) { |
| 6162 |
return FS_BAD_SEEK; |
| 6163 |
} |
| 6164 |
|
| 6165 |
result = lseek(pFilePosix->fd, (off_t)(int)offset, whence); |
| 6166 |
} |
| 6167 |
#endif |
| 6168 |
|
| 6169 |
if (result < 0) { |
| 6170 |
return fs_result_from_errno(errno); |
| 6171 |
} |
| 6172 |
|
| 6173 |
return FS_SUCCESS; |
| 6174 |
} |
| 6175 |
|
| 6176 |
static fs_result fs_file_tell_posix(fs_file* pFile, fs_int64* pCursor) |
| 6177 |
{ |
| 6178 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6179 |
fs_int64 cursor; |
| 6180 |
|
| 6181 |
cursor = (fs_int64)lseek(pFilePosix->fd, 0, SEEK_CUR); |
| 6182 |
if (cursor < 0) { |
| 6183 |
return fs_result_from_errno(errno); |
| 6184 |
} |
| 6185 |
|
| 6186 |
*pCursor = cursor; |
| 6187 |
return FS_SUCCESS; |
| 6188 |
} |
| 6189 |
|
| 6190 |
static fs_result fs_file_flush_posix(fs_file* pFile) |
| 6191 |
{ |
| 6192 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6193 |
int result; |
| 6194 |
|
| 6195 |
result = fsync(pFilePosix->fd); |
| 6196 |
if (result < 0) { |
| 6197 |
return fs_result_from_errno(errno); |
| 6198 |
} |
| 6199 |
|
| 6200 |
return FS_SUCCESS; |
| 6201 |
} |
| 6202 |
|
| 6203 |
|
| 6204 |
|
| 6205 |
|
| 6206 |
|
| 6207 |
|
| 6208 |
#if !defined(FS_HAS_FTRUNCATE) && (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) |
| 6209 |
#define FS_HAS_FTRUNCATE |
| 6210 |
#endif |
| 6211 |
#if !defined(FS_HAS_FTRUNCATE) && !defined(__STRICT_ANSI__) |
| 6212 |
#define FS_HAS_FTRUNCATE |
| 6213 |
#endif |
| 6214 |
|
| 6215 |
static fs_result fs_file_truncate_posix(fs_file* pFile) |
| 6216 |
{ |
| 6217 |
#if defined(FS_HAS_FTRUNCATE) |
| 6218 |
{ |
| 6219 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6220 |
off_t currentPos; |
| 6221 |
|
| 6222 |
|
| 6223 |
currentPos = lseek(pFilePosix->fd, 0, SEEK_CUR); |
| 6224 |
if (currentPos < 0) { |
| 6225 |
return fs_result_from_errno(errno); |
| 6226 |
} |
| 6227 |
|
| 6228 |
if (ftruncate(pFilePosix->fd, currentPos) < 0) { |
| 6229 |
return fs_result_from_errno(errno); |
| 6230 |
} |
| 6231 |
|
| 6232 |
return FS_SUCCESS; |
| 6233 |
} |
| 6234 |
#else |
| 6235 |
{ |
| 6236 |
(void)pFile; |
| 6237 |
return FS_NOT_IMPLEMENTED; |
| 6238 |
} |
| 6239 |
#endif |
| 6240 |
} |
| 6241 |
|
| 6242 |
static fs_result fs_file_info_posix(fs_file* pFile, fs_file_info* pInfo) |
| 6243 |
{ |
| 6244 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6245 |
struct stat info; |
| 6246 |
|
| 6247 |
if (fstat(pFilePosix->fd, &info) < 0) { |
| 6248 |
return fs_result_from_errno(errno); |
| 6249 |
} |
| 6250 |
|
| 6251 |
*pInfo = fs_file_info_from_stat_posix(&info); |
| 6252 |
|
| 6253 |
return FS_SUCCESS; |
| 6254 |
} |
| 6255 |
|
| 6256 |
static fs_result fs_file_duplicate_posix(fs_file* pFile, fs_file* pDuplicate) |
| 6257 |
{ |
| 6258 |
fs_file_posix* pFilePosix = (fs_file_posix*)fs_file_get_backend_data(pFile); |
| 6259 |
fs_file_posix* pDuplicatePosix = (fs_file_posix*)fs_file_get_backend_data(pDuplicate); |
| 6260 |
fs_result result; |
| 6261 |
struct stat st1, st2; |
| 6262 |
|
| 6263 |
|
| 6264 |
if (pFilePosix->isStandardHandle) { |
| 6265 |
pDuplicatePosix->fd = pFilePosix->fd; |
| 6266 |
pDuplicatePosix->isStandardHandle = FS_TRUE; |
| 6267 |
|
| 6268 |
return FS_SUCCESS; |
| 6269 |
} |
| 6270 |
|
| 6271 |
|
| 6272 |
|
| 6273 |
|
| 6274 |
|
| 6275 |
|
| 6276 |
if ((pFilePosix->openMode & FS_WRITE) != 0) { |
| 6277 |
return FS_INVALID_OPERATION; |
| 6278 |
} |
| 6279 |
|
| 6280 |
result = fs_file_open_posix(fs_file_get_fs(pFile), NULL, pFilePosix->pFilePath, pFilePosix->openMode, pDuplicate); |
| 6281 |
if (result != FS_SUCCESS) { |
| 6282 |
return result; |
| 6283 |
} |
| 6284 |
|
| 6285 |
|
| 6286 |
if (fstat(pFilePosix->fd, &st1) < 0) { |
| 6287 |
fs_file_close_posix(pDuplicate); |
| 6288 |
return fs_result_from_errno(errno); |
| 6289 |
} |
| 6290 |
|
| 6291 |
if (fstat(pDuplicatePosix->fd, &st2) < 0) { |
| 6292 |
fs_file_close_posix(pDuplicate); |
| 6293 |
return fs_result_from_errno(errno); |
| 6294 |
} |
| 6295 |
|
| 6296 |
if (st1.st_ino != st2.st_ino || st1.st_dev != st2.st_dev) { |
| 6297 |
fs_file_close_posix(pDuplicate); |
| 6298 |
return FS_INVALID_OPERATION; |
| 6299 |
} |
| 6300 |
|
| 6301 |
return FS_SUCCESS; |
| 6302 |
} |
| 6303 |
|
| 6304 |
|
| 6305 |
#define FS_POSIX_MIN_ITERATOR_ALLOCATION_SIZE 1024 |
| 6306 |
|
| 6307 |
typedef struct fs_iterator_posix |
| 6308 |
{ |
| 6309 |
fs_iterator iterator; |
| 6310 |
DIR* pDir; |
| 6311 |
char* pFullFilePath; |
| 6312 |
size_t directoryPathLen; |
| 6313 |
} fs_iterator_posix; |
| 6314 |
|
| 6315 |
static void fs_free_iterator_posix(fs_iterator* pIterator); |
| 6316 |
|
| 6317 |
static fs_iterator* fs_first_posix(fs* pFS, const char* pDirectoryPath, size_t directoryPathLen) |
| 6318 |
{ |
| 6319 |
fs_iterator_posix* pIteratorPosix; |
| 6320 |
struct dirent* info; |
| 6321 |
struct stat statInfo; |
| 6322 |
size_t fileNameLen; |
| 6323 |
|
| 6324 |
|
| 6325 |
|
| 6326 |
|
| 6327 |
|
| 6328 |
|
| 6329 |
|
| 6330 |
|
| 6331 |
|
| 6332 |
|
| 6333 |
|
| 6334 |
|
| 6335 |
|
| 6336 |
if (directoryPathLen == 0 || pDirectoryPath[0] == '\0') { |
| 6337 |
directoryPathLen = 1; |
| 6338 |
pDirectoryPath = "."; |
| 6339 |
} |
| 6340 |
|
| 6341 |
|
| 6342 |
if (directoryPathLen == (size_t)-1) { |
| 6343 |
directoryPathLen = strlen(pDirectoryPath); |
| 6344 |
} |
| 6345 |
|
| 6346 |
|
| 6347 |
|
| 6348 |
|
| 6349 |
|
| 6350 |
|
| 6351 |
pIteratorPosix = (fs_iterator_posix*)fs_malloc(FS_MAX(sizeof(*pIteratorPosix) + directoryPathLen + 1, FS_POSIX_MIN_ITERATOR_ALLOCATION_SIZE), fs_get_allocation_callbacks(pFS)); |
| 6352 |
if (pIteratorPosix == NULL) { |
| 6353 |
return NULL; |
| 6354 |
} |
| 6355 |
|
| 6356 |
|
| 6357 |
pIteratorPosix->pFullFilePath = (char*)pIteratorPosix + sizeof(*pIteratorPosix); |
| 6358 |
pIteratorPosix->directoryPathLen = directoryPathLen; |
| 6359 |
|
| 6360 |
|
| 6361 |
fs_strncpy_s(pIteratorPosix->pFullFilePath, directoryPathLen + 1, pDirectoryPath, directoryPathLen); |
| 6362 |
|
| 6363 |
|
| 6364 |
pIteratorPosix->pDir = opendir(pIteratorPosix->pFullFilePath); |
| 6365 |
if (pIteratorPosix->pDir == NULL) { |
| 6366 |
fs_free(pIteratorPosix, fs_get_allocation_callbacks(pFS)); |
| 6367 |
return NULL; |
| 6368 |
} |
| 6369 |
|
| 6370 |
|
| 6371 |
info = readdir(pIteratorPosix->pDir); |
| 6372 |
if (info == NULL) { |
| 6373 |
closedir(pIteratorPosix->pDir); |
| 6374 |
fs_free(pIteratorPosix, fs_get_allocation_callbacks(pFS)); |
| 6375 |
return NULL; |
| 6376 |
} |
| 6377 |
|
| 6378 |
fileNameLen = strlen(info->d_name); |
| 6379 |
|
| 6380 |
|
| 6381 |
|
| 6382 |
|
| 6383 |
|
| 6384 |
|
| 6385 |
{ |
| 6386 |
fs_iterator_posix* pNewIteratorPosix= (fs_iterator_posix*)fs_realloc(pIteratorPosix, FS_MAX(sizeof(*pIteratorPosix) + directoryPathLen + 1 + fileNameLen + 1, FS_POSIX_MIN_ITERATOR_ALLOCATION_SIZE), fs_get_allocation_callbacks(pFS)); |
| 6387 |
if (pNewIteratorPosix == NULL) { |
| 6388 |
closedir(pIteratorPosix->pDir); |
| 6389 |
fs_free(pIteratorPosix, fs_get_allocation_callbacks(pFS)); |
| 6390 |
return NULL; |
| 6391 |
} |
| 6392 |
|
| 6393 |
pIteratorPosix = pNewIteratorPosix; |
| 6394 |
} |
| 6395 |
|
| 6396 |
|
| 6397 |
pIteratorPosix->pFullFilePath = (char*)pIteratorPosix + sizeof(*pIteratorPosix); |
| 6398 |
pIteratorPosix->pFullFilePath[directoryPathLen] = '/'; |
| 6399 |
fs_strcpy(pIteratorPosix->pFullFilePath + directoryPathLen + 1, info->d_name); |
| 6400 |
|
| 6401 |
|
| 6402 |
pIteratorPosix->iterator.pName = pIteratorPosix->pFullFilePath + directoryPathLen + 1; |
| 6403 |
pIteratorPosix->iterator.nameLen = fileNameLen; |
| 6404 |
|
| 6405 |
|
| 6406 |
if (stat(pIteratorPosix->pFullFilePath, &statInfo) != 0) { |
| 6407 |
closedir(pIteratorPosix->pDir); |
| 6408 |
fs_free(pIteratorPosix, fs_get_allocation_callbacks(pFS)); |
| 6409 |
return NULL; |
| 6410 |
} |
| 6411 |
|
| 6412 |
pIteratorPosix->iterator.info = fs_file_info_from_stat_posix(&statInfo); |
| 6413 |
|
| 6414 |
return (fs_iterator*)pIteratorPosix; |
| 6415 |
} |
| 6416 |
|
| 6417 |
static fs_iterator* fs_next_posix(fs_iterator* pIterator) |
| 6418 |
{ |
| 6419 |
fs_iterator_posix* pIteratorPosix = (fs_iterator_posix*)pIterator; |
| 6420 |
struct dirent* info; |
| 6421 |
struct stat statInfo; |
| 6422 |
size_t fileNameLen; |
| 6423 |
|
| 6424 |
|
| 6425 |
info = readdir(pIteratorPosix->pDir); |
| 6426 |
if (info == NULL) { |
| 6427 |
fs_free_iterator_posix((fs_iterator*)pIteratorPosix); |
| 6428 |
return NULL; |
| 6429 |
} |
| 6430 |
|
| 6431 |
fileNameLen = strlen(info->d_name); |
| 6432 |
|
| 6433 |
|
| 6434 |
{ |
| 6435 |
fs_iterator_posix* pNewIteratorPosix = (fs_iterator_posix*)fs_realloc(pIteratorPosix, FS_MAX(sizeof(*pIteratorPosix) + pIteratorPosix->directoryPathLen + 1 + fileNameLen + 1, FS_POSIX_MIN_ITERATOR_ALLOCATION_SIZE), fs_get_allocation_callbacks(pIterator->pFS)); |
| 6436 |
if (pNewIteratorPosix == NULL) { |
| 6437 |
fs_free_iterator_posix((fs_iterator*)pIteratorPosix); |
| 6438 |
return NULL; |
| 6439 |
} |
| 6440 |
|
| 6441 |
pIteratorPosix = pNewIteratorPosix; |
| 6442 |
} |
| 6443 |
|
| 6444 |
|
| 6445 |
pIteratorPosix->pFullFilePath = (char*)pIteratorPosix + sizeof(*pIteratorPosix); |
| 6446 |
fs_strcpy(pIteratorPosix->pFullFilePath + pIteratorPosix->directoryPathLen + 1, info->d_name); |
| 6447 |
|
| 6448 |
|
| 6449 |
pIteratorPosix->iterator.pName = pIteratorPosix->pFullFilePath + pIteratorPosix->directoryPathLen + 1; |
| 6450 |
pIteratorPosix->iterator.nameLen = fileNameLen; |
| 6451 |
|
| 6452 |
|
| 6453 |
if (stat(pIteratorPosix->pFullFilePath, &statInfo) != 0) { |
| 6454 |
fs_free_iterator_posix((fs_iterator*)pIteratorPosix); |
| 6455 |
return NULL; |
| 6456 |
} |
| 6457 |
|
| 6458 |
pIteratorPosix->iterator.info = fs_file_info_from_stat_posix(&statInfo); |
| 6459 |
|
| 6460 |
return (fs_iterator*)pIteratorPosix; |
| 6461 |
} |
| 6462 |
|
| 6463 |
static void fs_free_iterator_posix(fs_iterator* pIterator) |
| 6464 |
{ |
| 6465 |
fs_iterator_posix* pIteratorPosix = (fs_iterator_posix*)pIterator; |
| 6466 |
|
| 6467 |
closedir(pIteratorPosix->pDir); |
| 6468 |
fs_free(pIteratorPosix, fs_get_allocation_callbacks(pIterator->pFS)); |
| 6469 |
} |
| 6470 |
|
| 6471 |
static fs_backend fs_posix_backend = |
| 6472 |
{ |
| 6473 |
fs_alloc_size_posix, |
| 6474 |
fs_init_posix, |
| 6475 |
fs_uninit_posix, |
| 6476 |
fs_remove_posix, |
| 6477 |
fs_rename_posix, |
| 6478 |
fs_mkdir_posix, |
| 6479 |
fs_info_posix, |
| 6480 |
fs_file_alloc_size_posix, |
| 6481 |
fs_file_open_posix, |
| 6482 |
fs_file_close_posix, |
| 6483 |
fs_file_read_posix, |
| 6484 |
fs_file_write_posix, |
| 6485 |
fs_file_seek_posix, |
| 6486 |
fs_file_tell_posix, |
| 6487 |
fs_file_flush_posix, |
| 6488 |
fs_file_truncate_posix, |
| 6489 |
fs_file_info_posix, |
| 6490 |
fs_file_duplicate_posix, |
| 6491 |
fs_first_posix, |
| 6492 |
fs_next_posix, |
| 6493 |
fs_free_iterator_posix |
| 6494 |
}; |
| 6495 |
|
| 6496 |
const fs_backend* FS_BACKEND_POSIX = &fs_posix_backend; |
| 6497 |
#else |
| 6498 |
const fs_backend* FS_BACKEND_POSIX = NULL; |
| 6499 |
#endif |
| 6500 |
|
| 6501 |
|
| 6502 |
|
| 6503 |
|
| 6504 |
#if defined(_WIN32) |
| 6505 |
#define FS_SUPPORTS_WIN32 |
| 6506 |
#endif |
| 6507 |
|
| 6508 |
#if !defined(FS_NO_WIN32) && defined(FS_SUPPORTS_WIN32) |
| 6509 |
#define FS_HAS_WIN32 |
| 6510 |
#endif |
| 6511 |
|
| 6512 |
#if defined(_WIN32) |
| 6513 |
#include <windows.h> |
| 6514 |
|
| 6515 |
#if defined(UNICODE) || defined(_UNICODE) |
| 6516 |
#define fs_win32_char wchar_t |
| 6517 |
#else |
| 6518 |
#define fs_win32_char char |
| 6519 |
#endif |
| 6520 |
|
| 6521 |
typedef struct |
| 6522 |
{ |
| 6523 |
size_t len; |
| 6524 |
fs_win32_char* path; |
| 6525 |
fs_win32_char pathStack[256]; |
| 6526 |
fs_win32_char* pathHeap; |
| 6527 |
} fs_win32_path; |
| 6528 |
|
| 6529 |
static void fs_win32_path_init_internal(fs_win32_path* pPath) |
| 6530 |
{ |
| 6531 |
pPath->len = 0; |
| 6532 |
pPath->path = pPath->pathStack; |
| 6533 |
pPath->pathStack[0] = '\0'; |
| 6534 |
pPath->pathHeap = NULL; |
| 6535 |
} |
| 6536 |
|
| 6537 |
static fs_result fs_win32_path_init(fs_win32_path* pPath, const char* pPathUTF8, size_t pathUTF8Len, const fs_allocation_callbacks* pAllocationCallbacks) |
| 6538 |
{ |
| 6539 |
size_t i; |
| 6540 |
|
| 6541 |
fs_win32_path_init_internal(pPath); |
| 6542 |
|
| 6543 |
#if defined(UNICODE) || defined(_UNICODE) |
| 6544 |
{ |
| 6545 |
int wideCharLen; |
| 6546 |
int cbMultiByte; |
| 6547 |
|
| 6548 |
if (pathUTF8Len == (size_t)-1) { |
| 6549 |
cbMultiByte = (int)-1; |
| 6550 |
} else { |
| 6551 |
cbMultiByte = (int)pathUTF8Len + 1; |
| 6552 |
} |
| 6553 |
|
| 6554 |
wideCharLen = MultiByteToWideChar(CP_UTF8, 0, pPathUTF8, cbMultiByte, NULL, 0); |
| 6555 |
if (wideCharLen == 0) { |
| 6556 |
return FS_ERROR; |
| 6557 |
} |
| 6558 |
|
| 6559 |
|
| 6560 |
if (wideCharLen <= (int)FS_COUNTOF(pPath->pathStack)) { |
| 6561 |
pPath->path = pPath->pathStack; |
| 6562 |
} else { |
| 6563 |
pPath->pathHeap = (fs_win32_char*)fs_malloc(sizeof(fs_win32_char) * wideCharLen, pAllocationCallbacks); |
| 6564 |
if (pPath->pathHeap == NULL) { |
| 6565 |
return FS_OUT_OF_MEMORY; |
| 6566 |
} |
| 6567 |
|
| 6568 |
pPath->path = pPath->pathHeap; |
| 6569 |
} |
| 6570 |
|
| 6571 |
MultiByteToWideChar(CP_UTF8, 0, pPathUTF8, cbMultiByte, pPath->path, wideCharLen); |
| 6572 |
pPath->len = wideCharLen - 1; |
| 6573 |
|
| 6574 |
|
| 6575 |
for (i = 0; i < pPath->len; i += 1) { |
| 6576 |
if (pPath->path[i] == '/') { |
| 6577 |
pPath->path[i] = '\\'; |
| 6578 |
} |
| 6579 |
} |
| 6580 |
|
| 6581 |
return FS_SUCCESS; |
| 6582 |
} |
| 6583 |
#else |
| 6584 |
{ |
| 6585 |
|
| 6586 |
|
| 6587 |
|
| 6588 |
|
| 6589 |
if (pathUTF8Len == (size_t)-1) { |
| 6590 |
pPath->len = strlen(pPathUTF8); |
| 6591 |
} else { |
| 6592 |
pPath->len = pathUTF8Len; |
| 6593 |
} |
| 6594 |
|
| 6595 |
if (pPath->len >= sizeof(pPath->pathStack)) { |
| 6596 |
pPath->pathHeap = (fs_win32_char*)fs_malloc(sizeof(fs_win32_char) * (pPath->len + 1), pAllocationCallbacks); |
| 6597 |
if (pPath->pathHeap == NULL) { |
| 6598 |
return FS_OUT_OF_MEMORY; |
| 6599 |
} |
| 6600 |
|
| 6601 |
pPath->path = pPath->pathHeap; |
| 6602 |
} |
| 6603 |
|
| 6604 |
fs_strcpy(pPath->path, pPathUTF8); |
| 6605 |
for (i = 0; i < pPath->len; i += 1) { |
| 6606 |
if (pPath->path[i] == '/') { |
| 6607 |
pPath->path[i] = '\\'; |
| 6608 |
} |
| 6609 |
} |
| 6610 |
|
| 6611 |
return FS_SUCCESS; |
| 6612 |
} |
| 6613 |
#endif |
| 6614 |
} |
| 6615 |
|
| 6616 |
|
| 6617 |
|
| 6618 |
static fs_result fs_win32_path_append(fs_win32_path* pPath, const char* pAppendUTF8, const fs_allocation_callbacks* pAllocationCallbacks) |
| 6619 |
{ |
| 6620 |
fs_result result; |
| 6621 |
fs_win32_path append; |
| 6622 |
size_t newLen; |
| 6623 |
|
| 6624 |
result = fs_win32_path_init(&append, pAppendUTF8, (size_t)-1, pAllocationCallbacks); |
| 6625 |
if (result != FS_SUCCESS) { |
| 6626 |
return result; |
| 6627 |
} |
| 6628 |
|
| 6629 |
newLen = pPath->len + append.len; |
| 6630 |
|
| 6631 |
if (pPath->path == pPath->pathHeap) { |
| 6632 |
|
| 6633 |
fs_win32_char* pNewHeap = (fs_win32_char*)fs_realloc(pPath->pathHeap, sizeof(fs_win32_char) * (newLen + 1), pAllocationCallbacks); |
| 6634 |
if (pNewHeap == NULL) { |
| 6635 |
return FS_OUT_OF_MEMORY; |
| 6636 |
} |
| 6637 |
|
| 6638 |
pPath->pathHeap = pNewHeap; |
| 6639 |
pPath->path = pNewHeap; |
| 6640 |
} else { |
| 6641 |
|
| 6642 |
if (newLen >= FS_COUNTOF(pPath->pathStack)) { |
| 6643 |
|
| 6644 |
pPath->pathHeap = (fs_win32_char*)fs_malloc(sizeof(fs_win32_char) * (newLen + 1), pAllocationCallbacks); |
| 6645 |
if (pPath->pathHeap == NULL) { |
| 6646 |
return FS_OUT_OF_MEMORY; |
| 6647 |
} |
| 6648 |
|
| 6649 |
memcpy(pPath->pathHeap, pPath->pathStack, sizeof(fs_win32_char) * (pPath->len + 1)); |
| 6650 |
pPath->path = pPath->pathHeap; |
| 6651 |
} else { |
| 6652 |
|
| 6653 |
} |
| 6654 |
} |
| 6655 |
|
| 6656 |
|
| 6657 |
memcpy(pPath->path + pPath->len, append.path, sizeof(fs_win32_char) * (append.len + 1)); |
| 6658 |
pPath->len = newLen; |
| 6659 |
|
| 6660 |
return FS_SUCCESS; |
| 6661 |
} |
| 6662 |
|
| 6663 |
|
| 6664 |
|
| 6665 |
static void fs_win32_path_uninit(fs_win32_path* pPath, const fs_allocation_callbacks* pAllocationCallbacks) |
| 6666 |
{ |
| 6667 |
if (pPath->pathHeap) { |
| 6668 |
fs_free(pPath->pathHeap, pAllocationCallbacks); |
| 6669 |
pPath->pathHeap = NULL; |
| 6670 |
} |
| 6671 |
} |
| 6672 |
|
| 6673 |
|
| 6674 |
FS_API fs_uint64 fs_FILETIME_to_unix(const FILETIME* pFT) |
| 6675 |
{ |
| 6676 |
ULARGE_INTEGER li; |
| 6677 |
|
| 6678 |
li.HighPart = pFT->dwHighDateTime; |
| 6679 |
li.LowPart = pFT->dwLowDateTime; |
| 6680 |
|
| 6681 |
return (fs_uint64)(li.QuadPart / 10000000UL - ((fs_uint64)116444736UL * 100UL)); |
| 6682 |
} |
| 6683 |
|
| 6684 |
static fs_file_info fs_file_info_from_WIN32_FIND_DATA(const WIN32_FIND_DATA* pFD) |
| 6685 |
{ |
| 6686 |
fs_file_info info; |
| 6687 |
|
| 6688 |
FS_ZERO_OBJECT(&info); |
| 6689 |
info.size = ((fs_uint64)pFD->nFileSizeHigh << 32) | (fs_uint64)pFD->nFileSizeLow; |
| 6690 |
info.lastModifiedTime = fs_FILETIME_to_unix(&pFD->ftLastWriteTime); |
| 6691 |
info.lastAccessTime = fs_FILETIME_to_unix(&pFD->ftLastAccessTime); |
| 6692 |
info.directory = (pFD->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 6693 |
info.symlink = ((pFD->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) && (pFD->dwReserved0 == 0xA000000C); |
| 6694 |
|
| 6695 |
return info; |
| 6696 |
} |
| 6697 |
|
| 6698 |
static fs_file_info fs_file_info_from_HANDLE_FILE_INFORMATION(const BY_HANDLE_FILE_INFORMATION* pFileInfo) |
| 6699 |
{ |
| 6700 |
fs_file_info info; |
| 6701 |
|
| 6702 |
FS_ZERO_OBJECT(&info); |
| 6703 |
info.size = ((fs_uint64)pFileInfo->nFileSizeHigh << 32) | (fs_uint64)pFileInfo->nFileSizeLow; |
| 6704 |
info.lastModifiedTime = fs_FILETIME_to_unix(&pFileInfo->ftLastWriteTime); |
| 6705 |
info.lastAccessTime = fs_FILETIME_to_unix(&pFileInfo->ftLastAccessTime); |
| 6706 |
info.directory = (pFileInfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 6707 |
|
| 6708 |
return info; |
| 6709 |
} |
| 6710 |
|
| 6711 |
|
| 6712 |
static size_t fs_alloc_size_win32(const void* pBackendConfig) |
| 6713 |
{ |
| 6714 |
(void)pBackendConfig; |
| 6715 |
return 0; |
| 6716 |
} |
| 6717 |
|
| 6718 |
static fs_result fs_init_win32(fs* pFS, const void* pBackendConfig, fs_stream* pStream) |
| 6719 |
{ |
| 6720 |
(void)pFS; |
| 6721 |
(void)pBackendConfig; |
| 6722 |
(void)pStream; |
| 6723 |
|
| 6724 |
return FS_SUCCESS; |
| 6725 |
} |
| 6726 |
|
| 6727 |
static void fs_uninit_win32(fs* pFS) |
| 6728 |
{ |
| 6729 |
(void)pFS; |
| 6730 |
} |
| 6731 |
|
| 6732 |
static fs_result fs_remove_win32(fs* pFS, const char* pFilePath) |
| 6733 |
{ |
| 6734 |
BOOL resultWin32; |
| 6735 |
fs_result result; |
| 6736 |
fs_win32_path path; |
| 6737 |
|
| 6738 |
result = fs_win32_path_init(&path, pFilePath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6739 |
if (result != FS_SUCCESS) { |
| 6740 |
return result; |
| 6741 |
} |
| 6742 |
|
| 6743 |
resultWin32 = DeleteFile(path.path); |
| 6744 |
if (resultWin32 == FS_FALSE) { |
| 6745 |
|
| 6746 |
DWORD error = GetLastError(); |
| 6747 |
if (error == ERROR_ACCESS_DENIED || error == ERROR_FILE_NOT_FOUND) { |
| 6748 |
DWORD attributes = GetFileAttributes(path.path); |
| 6749 |
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 6750 |
resultWin32 = RemoveDirectory(path.path); |
| 6751 |
if (resultWin32 == FS_FALSE) { |
| 6752 |
result = fs_result_from_GetLastError(); |
| 6753 |
goto done; |
| 6754 |
} else { |
| 6755 |
return FS_SUCCESS; |
| 6756 |
} |
| 6757 |
} else { |
| 6758 |
result = fs_result_from_GetLastError(); |
| 6759 |
goto done; |
| 6760 |
} |
| 6761 |
} else { |
| 6762 |
result = fs_result_from_GetLastError(); |
| 6763 |
goto done; |
| 6764 |
} |
| 6765 |
|
| 6766 |
result = fs_result_from_GetLastError(); |
| 6767 |
goto done; |
| 6768 |
} else { |
| 6769 |
result = FS_SUCCESS; |
| 6770 |
} |
| 6771 |
|
| 6772 |
done: |
| 6773 |
fs_win32_path_uninit(&path, fs_get_allocation_callbacks(pFS)); |
| 6774 |
|
| 6775 |
(void)pFS; |
| 6776 |
return result; |
| 6777 |
} |
| 6778 |
|
| 6779 |
static fs_result fs_rename_win32(fs* pFS, const char* pOldPath, const char* pNewPath) |
| 6780 |
{ |
| 6781 |
BOOL resultWin32; |
| 6782 |
fs_result result; |
| 6783 |
fs_win32_path pathOld; |
| 6784 |
fs_win32_path pathNew; |
| 6785 |
|
| 6786 |
result = fs_win32_path_init(&pathOld, pOldPath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6787 |
if (result != FS_SUCCESS) { |
| 6788 |
return result; |
| 6789 |
} |
| 6790 |
|
| 6791 |
result = fs_win32_path_init(&pathNew, pNewPath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6792 |
if (result != FS_SUCCESS) { |
| 6793 |
fs_win32_path_uninit(&pathOld, fs_get_allocation_callbacks(pFS)); |
| 6794 |
return result; |
| 6795 |
} |
| 6796 |
|
| 6797 |
resultWin32 = MoveFile(pathOld.path, pathNew.path); |
| 6798 |
if (resultWin32 == FS_FALSE) { |
| 6799 |
result = fs_result_from_GetLastError(); |
| 6800 |
} |
| 6801 |
|
| 6802 |
fs_win32_path_uninit(&pathOld, fs_get_allocation_callbacks(pFS)); |
| 6803 |
fs_win32_path_uninit(&pathNew, fs_get_allocation_callbacks(pFS)); |
| 6804 |
|
| 6805 |
(void)pFS; |
| 6806 |
return result; |
| 6807 |
} |
| 6808 |
|
| 6809 |
static fs_result fs_mkdir_win32(fs* pFS, const char* pPath) |
| 6810 |
{ |
| 6811 |
BOOL resultWin32; |
| 6812 |
fs_result result; |
| 6813 |
fs_win32_path path; |
| 6814 |
|
| 6815 |
|
| 6816 |
if ((pPath[0] >= 'a' && pPath[0] <= 'z') || (pPath[0] >= 'A' && pPath[0] <= 'Z')) { |
| 6817 |
if (pPath[1] == ':' && pPath[2] == '\0') { |
| 6818 |
return FS_SUCCESS; |
| 6819 |
} |
| 6820 |
} |
| 6821 |
|
| 6822 |
result = fs_win32_path_init(&path, pPath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6823 |
if (result != FS_SUCCESS) { |
| 6824 |
return result; |
| 6825 |
} |
| 6826 |
|
| 6827 |
resultWin32 = CreateDirectory(path.path, NULL); |
| 6828 |
if (resultWin32 == FS_FALSE) { |
| 6829 |
result = fs_result_from_GetLastError(); |
| 6830 |
goto done; |
| 6831 |
} |
| 6832 |
|
| 6833 |
done: |
| 6834 |
fs_win32_path_uninit(&path, fs_get_allocation_callbacks(pFS)); |
| 6835 |
|
| 6836 |
(void)pFS; |
| 6837 |
return result; |
| 6838 |
} |
| 6839 |
|
| 6840 |
static fs_result fs_info_from_stdio_win32(HANDLE hFile, fs_file_info* pInfo) |
| 6841 |
{ |
| 6842 |
BY_HANDLE_FILE_INFORMATION fileInfo; |
| 6843 |
|
| 6844 |
if (GetFileInformationByHandle(hFile, &fileInfo) == FS_FALSE) { |
| 6845 |
return fs_result_from_GetLastError(); |
| 6846 |
} |
| 6847 |
|
| 6848 |
*pInfo = fs_file_info_from_HANDLE_FILE_INFORMATION(&fileInfo); |
| 6849 |
|
| 6850 |
return FS_SUCCESS; |
| 6851 |
} |
| 6852 |
|
| 6853 |
static fs_result fs_info_win32(fs* pFS, const char* pPath, int openMode, fs_file_info* pInfo) |
| 6854 |
{ |
| 6855 |
HANDLE hFind; |
| 6856 |
WIN32_FIND_DATA fd; |
| 6857 |
fs_result result; |
| 6858 |
fs_win32_path path; |
| 6859 |
|
| 6860 |
|
| 6861 |
if (pPath == FS_STDIN ) { |
| 6862 |
return fs_info_from_stdio_win32(GetStdHandle(STD_INPUT_HANDLE ), pInfo); |
| 6863 |
} else if (pPath == FS_STDOUT) { |
| 6864 |
return fs_info_from_stdio_win32(GetStdHandle(STD_OUTPUT_HANDLE), pInfo); |
| 6865 |
} else if (pPath == FS_STDERR) { |
| 6866 |
return fs_info_from_stdio_win32(GetStdHandle(STD_ERROR_HANDLE ), pInfo); |
| 6867 |
} |
| 6868 |
|
| 6869 |
result = fs_win32_path_init(&path, pPath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6870 |
if (result != FS_SUCCESS) { |
| 6871 |
return result; |
| 6872 |
} |
| 6873 |
|
| 6874 |
hFind = FindFirstFile(path.path, &fd); |
| 6875 |
|
| 6876 |
fs_win32_path_uninit(&path, fs_get_allocation_callbacks(pFS)); |
| 6877 |
|
| 6878 |
if (hFind == INVALID_HANDLE_VALUE) { |
| 6879 |
result = fs_result_from_GetLastError(); |
| 6880 |
goto done; |
| 6881 |
} else { |
| 6882 |
result = FS_SUCCESS; |
| 6883 |
} |
| 6884 |
|
| 6885 |
FindClose(hFind); |
| 6886 |
hFind = NULL; |
| 6887 |
|
| 6888 |
*pInfo = fs_file_info_from_WIN32_FIND_DATA(&fd); |
| 6889 |
|
| 6890 |
done: |
| 6891 |
(void)openMode; |
| 6892 |
(void)pFS; |
| 6893 |
return result; |
| 6894 |
} |
| 6895 |
|
| 6896 |
|
| 6897 |
typedef struct fs_file_win32 |
| 6898 |
{ |
| 6899 |
HANDLE hFile; |
| 6900 |
fs_bool32 isStandardHandle; |
| 6901 |
int openMode; |
| 6902 |
char* pFilePath; |
| 6903 |
char pFilePathStack[256]; |
| 6904 |
char* pFilePathHeap; |
| 6905 |
} fs_file_win32; |
| 6906 |
|
| 6907 |
static size_t fs_file_alloc_size_win32(fs* pFS) |
| 6908 |
{ |
| 6909 |
(void)pFS; |
| 6910 |
return sizeof(fs_file_win32); |
| 6911 |
} |
| 6912 |
|
| 6913 |
static fs_result fs_file_open_win32(fs* pFS, fs_stream* pStream, const char* pFilePath, int openMode, fs_file* pFile) |
| 6914 |
{ |
| 6915 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 6916 |
fs_result result; |
| 6917 |
fs_win32_path path; |
| 6918 |
HANDLE hFile; |
| 6919 |
DWORD dwDesiredAccess = 0; |
| 6920 |
DWORD dwShareMode = 0; |
| 6921 |
DWORD dwCreationDisposition = OPEN_EXISTING; |
| 6922 |
|
| 6923 |
if (pFilePath == FS_STDIN ) { |
| 6924 |
hFile = GetStdHandle(STD_INPUT_HANDLE); |
| 6925 |
pFileWin32->isStandardHandle = FS_TRUE; |
| 6926 |
} else if (pFilePath == FS_STDOUT) { |
| 6927 |
hFile = GetStdHandle(STD_OUTPUT_HANDLE); |
| 6928 |
pFileWin32->isStandardHandle = FS_TRUE; |
| 6929 |
} else if (pFilePath == FS_STDERR) { |
| 6930 |
hFile = GetStdHandle(STD_ERROR_HANDLE); |
| 6931 |
pFileWin32->isStandardHandle = FS_TRUE; |
| 6932 |
} else { |
| 6933 |
pFileWin32->isStandardHandle = FS_FALSE; |
| 6934 |
} |
| 6935 |
|
| 6936 |
if (pFileWin32->isStandardHandle) { |
| 6937 |
pFileWin32->hFile = hFile; |
| 6938 |
return FS_SUCCESS; |
| 6939 |
} |
| 6940 |
|
| 6941 |
|
| 6942 |
if ((openMode & FS_READ) != 0) { |
| 6943 |
dwDesiredAccess |= GENERIC_READ; |
| 6944 |
dwShareMode |= FILE_SHARE_READ; |
| 6945 |
dwCreationDisposition = OPEN_EXISTING; |
| 6946 |
} |
| 6947 |
|
| 6948 |
if ((openMode & FS_WRITE) != 0) { |
| 6949 |
dwShareMode |= FILE_SHARE_WRITE; |
| 6950 |
|
| 6951 |
if ((openMode & FS_EXCLUSIVE) != 0) { |
| 6952 |
dwDesiredAccess |= GENERIC_WRITE; |
| 6953 |
dwCreationDisposition = CREATE_NEW; |
| 6954 |
} else if ((openMode & FS_APPEND) != 0) { |
| 6955 |
dwDesiredAccess |= FILE_APPEND_DATA; |
| 6956 |
dwCreationDisposition = OPEN_ALWAYS; |
| 6957 |
} else if ((openMode & FS_TRUNCATE) != 0) { |
| 6958 |
dwDesiredAccess |= GENERIC_WRITE; |
| 6959 |
dwCreationDisposition = CREATE_ALWAYS; |
| 6960 |
} else { |
| 6961 |
dwDesiredAccess |= GENERIC_WRITE; |
| 6962 |
dwCreationDisposition = OPEN_ALWAYS; |
| 6963 |
} |
| 6964 |
} |
| 6965 |
|
| 6966 |
|
| 6967 |
if (dwDesiredAccess == 0) { |
| 6968 |
return FS_INVALID_ARGS; |
| 6969 |
} |
| 6970 |
|
| 6971 |
result = fs_win32_path_init(&path, pFilePath, (size_t)-1, fs_get_allocation_callbacks(pFS)); |
| 6972 |
if (result != FS_SUCCESS) { |
| 6973 |
return result; |
| 6974 |
} |
| 6975 |
|
| 6976 |
hFile = CreateFile(path.path, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); |
| 6977 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 6978 |
result = fs_result_from_GetLastError(); |
| 6979 |
} else { |
| 6980 |
result = FS_SUCCESS; |
| 6981 |
pFileWin32->hFile = hFile; |
| 6982 |
} |
| 6983 |
|
| 6984 |
if (result != FS_SUCCESS) { |
| 6985 |
return result; |
| 6986 |
} |
| 6987 |
|
| 6988 |
|
| 6989 |
pFileWin32->openMode = openMode; |
| 6990 |
|
| 6991 |
|
| 6992 |
if (path.len < FS_COUNTOF(pFileWin32->pFilePathStack)) { |
| 6993 |
pFileWin32->pFilePath = pFileWin32->pFilePathStack; |
| 6994 |
} else { |
| 6995 |
pFileWin32->pFilePathHeap = (char*)fs_malloc(path.len + 1, fs_get_allocation_callbacks(pFS)); |
| 6996 |
if (pFileWin32->pFilePathHeap == NULL) { |
| 6997 |
result = FS_OUT_OF_MEMORY; |
| 6998 |
if (pFileWin32->isStandardHandle == FS_FALSE) { |
| 6999 |
CloseHandle(pFileWin32->hFile); |
| 7000 |
} |
| 7001 |
|
| 7002 |
fs_win32_path_uninit(&path, fs_get_allocation_callbacks(pFS)); |
| 7003 |
return result; |
| 7004 |
} |
| 7005 |
|
| 7006 |
pFileWin32->pFilePath = pFileWin32->pFilePathHeap; |
| 7007 |
} |
| 7008 |
|
| 7009 |
fs_strcpy(pFileWin32->pFilePath, pFilePath); |
| 7010 |
|
| 7011 |
|
| 7012 |
|
| 7013 |
fs_win32_path_uninit(&path, fs_get_allocation_callbacks(pFS)); |
| 7014 |
|
| 7015 |
(void)pFS; |
| 7016 |
(void)pStream; |
| 7017 |
return FS_SUCCESS; |
| 7018 |
} |
| 7019 |
|
| 7020 |
static void fs_file_close_win32(fs_file* pFile) |
| 7021 |
{ |
| 7022 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7023 |
|
| 7024 |
if (pFileWin32->isStandardHandle == FS_FALSE) { |
| 7025 |
CloseHandle(pFileWin32->hFile); |
| 7026 |
fs_free(pFileWin32->pFilePathHeap, fs_get_allocation_callbacks(fs_file_get_fs(pFile))); |
| 7027 |
} |
| 7028 |
} |
| 7029 |
|
| 7030 |
static fs_result fs_file_read_win32(fs_file* pFile, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 7031 |
{ |
| 7032 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7033 |
BOOL resultWin32; |
| 7034 |
size_t bytesRemaining = bytesToRead; |
| 7035 |
char* pRunningDst = (char*)pDst; |
| 7036 |
|
| 7037 |
|
| 7038 |
|
| 7039 |
|
| 7040 |
|
| 7041 |
while (bytesRemaining > 0) { |
| 7042 |
DWORD bytesToReadNow = (DWORD)FS_MIN(bytesRemaining, (size_t)0xFFFFFFFF); |
| 7043 |
DWORD bytesReadNow; |
| 7044 |
|
| 7045 |
resultWin32 = ReadFile(pFileWin32->hFile, pRunningDst, bytesToReadNow, &bytesReadNow, NULL); |
| 7046 |
if (resultWin32 == FS_FALSE) { |
| 7047 |
return fs_result_from_GetLastError(); |
| 7048 |
} |
| 7049 |
|
| 7050 |
if (bytesReadNow == 0) { |
| 7051 |
break; |
| 7052 |
} |
| 7053 |
|
| 7054 |
bytesRemaining -= bytesReadNow; |
| 7055 |
pRunningDst += bytesReadNow; |
| 7056 |
} |
| 7057 |
|
| 7058 |
*pBytesRead = bytesToRead - bytesRemaining; |
| 7059 |
|
| 7060 |
if (*pBytesRead == 0) { |
| 7061 |
return FS_AT_END; |
| 7062 |
} |
| 7063 |
|
| 7064 |
return FS_SUCCESS; |
| 7065 |
} |
| 7066 |
|
| 7067 |
static fs_result fs_file_write_win32(fs_file* pFile, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 7068 |
{ |
| 7069 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7070 |
BOOL resultWin32; |
| 7071 |
size_t bytesRemaining = bytesToWrite; |
| 7072 |
const char* pRunningSrc = (const char*)pSrc; |
| 7073 |
|
| 7074 |
|
| 7075 |
|
| 7076 |
|
| 7077 |
|
| 7078 |
while (bytesRemaining > 0) { |
| 7079 |
DWORD bytesToWriteNow = (DWORD)FS_MIN(bytesRemaining, (size_t)0xFFFFFFFF); |
| 7080 |
DWORD bytesWrittenNow; |
| 7081 |
|
| 7082 |
resultWin32 = WriteFile(pFileWin32->hFile, pRunningSrc, bytesToWriteNow, &bytesWrittenNow, NULL); |
| 7083 |
if (resultWin32 == FS_FALSE) { |
| 7084 |
return fs_result_from_GetLastError(); |
| 7085 |
} |
| 7086 |
|
| 7087 |
if (bytesWrittenNow == 0) { |
| 7088 |
break; |
| 7089 |
} |
| 7090 |
|
| 7091 |
bytesRemaining -= bytesWrittenNow; |
| 7092 |
pRunningSrc += bytesWrittenNow; |
| 7093 |
} |
| 7094 |
|
| 7095 |
*pBytesWritten = bytesToWrite - bytesRemaining; |
| 7096 |
|
| 7097 |
return FS_SUCCESS; |
| 7098 |
} |
| 7099 |
|
| 7100 |
static fs_result fs_file_seek_win32(fs_file* pFile, fs_int64 offset, fs_seek_origin origin) |
| 7101 |
{ |
| 7102 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7103 |
LARGE_INTEGER liDistanceToMove; |
| 7104 |
DWORD dwMoveMethod; |
| 7105 |
|
| 7106 |
switch (origin) { |
| 7107 |
case FS_SEEK_SET: |
| 7108 |
dwMoveMethod = FILE_BEGIN; |
| 7109 |
liDistanceToMove.QuadPart = offset; |
| 7110 |
break; |
| 7111 |
case FS_SEEK_CUR: |
| 7112 |
dwMoveMethod = FILE_CURRENT; |
| 7113 |
liDistanceToMove.QuadPart = offset; |
| 7114 |
break; |
| 7115 |
case FS_SEEK_END: |
| 7116 |
dwMoveMethod = FILE_END; |
| 7117 |
liDistanceToMove.QuadPart = offset; |
| 7118 |
break; |
| 7119 |
default: |
| 7120 |
return FS_INVALID_ARGS; |
| 7121 |
} |
| 7122 |
|
| 7123 |
|
| 7124 |
|
| 7125 |
|
| 7126 |
|
| 7127 |
|
| 7128 |
|
| 7129 |
|
| 7130 |
if (SetFilePointer(pFileWin32->hFile, liDistanceToMove.LowPart, (liDistanceToMove.HighPart == 0 ? NULL : &liDistanceToMove.HighPart), dwMoveMethod) == INVALID_SET_FILE_POINTER) { |
| 7131 |
return fs_result_from_GetLastError(); |
| 7132 |
} |
| 7133 |
|
| 7134 |
return FS_SUCCESS; |
| 7135 |
} |
| 7136 |
|
| 7137 |
static fs_result fs_file_tell_win32(fs_file* pFile, fs_int64* pCursor) |
| 7138 |
{ |
| 7139 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7140 |
LARGE_INTEGER liCursor; |
| 7141 |
|
| 7142 |
liCursor.HighPart = 0; |
| 7143 |
liCursor.LowPart = SetFilePointer(pFileWin32->hFile, 0, &liCursor.HighPart, FILE_CURRENT); |
| 7144 |
|
| 7145 |
if (liCursor.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
| 7146 |
return fs_result_from_GetLastError(); |
| 7147 |
} |
| 7148 |
|
| 7149 |
*pCursor = liCursor.QuadPart; |
| 7150 |
|
| 7151 |
return FS_SUCCESS; |
| 7152 |
} |
| 7153 |
|
| 7154 |
static fs_result fs_file_flush_win32(fs_file* pFile) |
| 7155 |
{ |
| 7156 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7157 |
|
| 7158 |
if (FlushFileBuffers(pFileWin32->hFile) == FS_FALSE) { |
| 7159 |
return fs_result_from_GetLastError(); |
| 7160 |
} |
| 7161 |
|
| 7162 |
return FS_SUCCESS; |
| 7163 |
} |
| 7164 |
|
| 7165 |
static fs_result fs_file_truncate_win32(fs_file* pFile) |
| 7166 |
{ |
| 7167 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7168 |
|
| 7169 |
if (SetEndOfFile(pFileWin32->hFile) == FS_FALSE) { |
| 7170 |
return fs_result_from_GetLastError(); |
| 7171 |
} |
| 7172 |
|
| 7173 |
return FS_SUCCESS; |
| 7174 |
} |
| 7175 |
|
| 7176 |
static fs_result fs_file_info_win32(fs_file* pFile, fs_file_info* pInfo) |
| 7177 |
{ |
| 7178 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7179 |
BY_HANDLE_FILE_INFORMATION fileInfo; |
| 7180 |
|
| 7181 |
if (GetFileInformationByHandle(pFileWin32->hFile, &fileInfo) == FS_FALSE) { |
| 7182 |
return fs_result_from_GetLastError(); |
| 7183 |
} |
| 7184 |
|
| 7185 |
*pInfo = fs_file_info_from_HANDLE_FILE_INFORMATION(&fileInfo); |
| 7186 |
|
| 7187 |
return FS_SUCCESS; |
| 7188 |
} |
| 7189 |
|
| 7190 |
static fs_result fs_file_duplicate_win32(fs_file* pFile, fs_file* pDuplicate) |
| 7191 |
{ |
| 7192 |
fs_file_win32* pFileWin32 = (fs_file_win32*)fs_file_get_backend_data(pFile); |
| 7193 |
fs_file_win32* pDuplicateWin32 = (fs_file_win32*)fs_file_get_backend_data(pDuplicate); |
| 7194 |
fs_result result; |
| 7195 |
BY_HANDLE_FILE_INFORMATION info1, info2; |
| 7196 |
|
| 7197 |
if (pFileWin32->isStandardHandle) { |
| 7198 |
pDuplicateWin32->hFile = pFileWin32->hFile; |
| 7199 |
pDuplicateWin32->isStandardHandle = FS_TRUE; |
| 7200 |
|
| 7201 |
return FS_SUCCESS; |
| 7202 |
} |
| 7203 |
|
| 7204 |
|
| 7205 |
|
| 7206 |
|
| 7207 |
|
| 7208 |
|
| 7209 |
if ((pFileWin32->openMode & FS_WRITE) != 0) { |
| 7210 |
return FS_INVALID_OPERATION; |
| 7211 |
} |
| 7212 |
|
| 7213 |
result = fs_file_open_win32(fs_file_get_fs(pFile), NULL, pFileWin32->pFilePath, pFileWin32->openMode, pDuplicate); |
| 7214 |
if (result != FS_SUCCESS) { |
| 7215 |
return result; |
| 7216 |
} |
| 7217 |
|
| 7218 |
|
| 7219 |
if (GetFileInformationByHandle(pFileWin32->hFile, &info1) == FS_FALSE) { |
| 7220 |
fs_file_close_win32(pDuplicate); |
| 7221 |
return fs_result_from_GetLastError(); |
| 7222 |
} |
| 7223 |
if (GetFileInformationByHandle(pDuplicateWin32->hFile, &info2) == FS_FALSE) { |
| 7224 |
fs_file_close_win32(pDuplicate); |
| 7225 |
return fs_result_from_GetLastError(); |
| 7226 |
} |
| 7227 |
|
| 7228 |
if ((info1.dwVolumeSerialNumber != info2.dwVolumeSerialNumber) || (info1.nFileIndexLow != info2.nFileIndexLow) || (info1.nFileIndexHigh != info2.nFileIndexHigh)) { |
| 7229 |
fs_file_close_win32(pDuplicate); |
| 7230 |
return FS_INVALID_OPERATION; |
| 7231 |
} |
| 7232 |
|
| 7233 |
return FS_SUCCESS; |
| 7234 |
} |
| 7235 |
|
| 7236 |
|
| 7237 |
#define FS_WIN32_MIN_ITERATOR_ALLOCATION_SIZE 1024 |
| 7238 |
|
| 7239 |
typedef struct fs_iterator_win32 |
| 7240 |
{ |
| 7241 |
fs_iterator iterator; |
| 7242 |
HANDLE hFind; |
| 7243 |
WIN32_FIND_DATAA findData; |
| 7244 |
char* pFullFilePath; |
| 7245 |
size_t directoryPathLen; |
| 7246 |
} fs_iterator_win32; |
| 7247 |
|
| 7248 |
static void fs_free_iterator_win32(fs_iterator* pIterator); |
| 7249 |
|
| 7250 |
static fs_iterator* fs_iterator_win32_resolve(fs_iterator_win32* pIteratorWin32, fs* pFS, HANDLE hFind, const WIN32_FIND_DATA* pFD) |
| 7251 |
{ |
| 7252 |
fs_iterator_win32* pNewIteratorWin32; |
| 7253 |
size_t allocSize; |
| 7254 |
int nameLenIncludingNullTerminator; |
| 7255 |
|
| 7256 |
|
| 7257 |
|
| 7258 |
|
| 7259 |
|
| 7260 |
#if defined(UNICODE) || defined(_UNICODE) |
| 7261 |
{ |
| 7262 |
nameLenIncludingNullTerminator = WideCharToMultiByte(CP_UTF8, 0, pFD->cFileName, -1, NULL, 0, NULL, NULL); |
| 7263 |
if (nameLenIncludingNullTerminator == 0) { |
| 7264 |
fs_free_iterator_win32((fs_iterator*)pIteratorWin32); |
| 7265 |
return NULL; |
| 7266 |
} |
| 7267 |
} |
| 7268 |
#else |
| 7269 |
{ |
| 7270 |
nameLenIncludingNullTerminator = (int)strlen(pFD->cFileName) + 1; |
| 7271 |
} |
| 7272 |
#endif |
| 7273 |
|
| 7274 |
allocSize = FS_MAX(sizeof(fs_iterator_win32) + nameLenIncludingNullTerminator, FS_WIN32_MIN_ITERATOR_ALLOCATION_SIZE); |
| 7275 |
|
| 7276 |
pNewIteratorWin32 = (fs_iterator_win32*)fs_realloc(pIteratorWin32, allocSize, fs_get_allocation_callbacks(pFS)); |
| 7277 |
if (pNewIteratorWin32 == NULL) { |
| 7278 |
fs_free_iterator_win32((fs_iterator*)pIteratorWin32); |
| 7279 |
return NULL; |
| 7280 |
} |
| 7281 |
|
| 7282 |
pNewIteratorWin32->iterator.pFS = pFS; |
| 7283 |
pNewIteratorWin32->hFind = hFind; |
| 7284 |
|
| 7285 |
|
| 7286 |
pNewIteratorWin32->iterator.pName = (char*)pNewIteratorWin32 + sizeof(fs_iterator_win32); |
| 7287 |
pNewIteratorWin32->iterator.nameLen = (size_t)nameLenIncludingNullTerminator - 1; |
| 7288 |
|
| 7289 |
#if defined(UNICODE) || defined(_UNICODE) |
| 7290 |
{ |
| 7291 |
WideCharToMultiByte(CP_UTF8, 0, pFD->cFileName, -1, (char*)pNewIteratorWin32->iterator.pName, nameLenIncludingNullTerminator, NULL, NULL); |
| 7292 |
} |
| 7293 |
#else |
| 7294 |
{ |
| 7295 |
fs_strcpy((char*)pNewIteratorWin32->iterator.pName, pFD->cFileName); |
| 7296 |
} |
| 7297 |
#endif |
| 7298 |
|
| 7299 |
|
| 7300 |
pNewIteratorWin32->iterator.info = fs_file_info_from_WIN32_FIND_DATA(pFD); |
| 7301 |
|
| 7302 |
return (fs_iterator*)pNewIteratorWin32; |
| 7303 |
} |
| 7304 |
|
| 7305 |
static fs_iterator* fs_first_win32(fs* pFS, const char* pDirectoryPath, size_t directoryPathLen) |
| 7306 |
{ |
| 7307 |
HANDLE hFind; |
| 7308 |
WIN32_FIND_DATA fd; |
| 7309 |
fs_result result; |
| 7310 |
fs_win32_path query; |
| 7311 |
|
| 7312 |
|
| 7313 |
if (pDirectoryPath == NULL || pDirectoryPath[0] == '\0') { |
| 7314 |
pDirectoryPath = "."; |
| 7315 |
directoryPathLen = 1; |
| 7316 |
} |
| 7317 |
|
| 7318 |
result = fs_win32_path_init(&query, pDirectoryPath, directoryPathLen, fs_get_allocation_callbacks(pFS)); |
| 7319 |
if (result != FS_SUCCESS) { |
| 7320 |
return NULL; |
| 7321 |
} |
| 7322 |
|
| 7323 |
|
| 7324 |
|
| 7325 |
|
| 7326 |
|
| 7327 |
if (query.len > 0 && query.path[query.len - 1] == '\\') { |
| 7328 |
query.len -= 1; |
| 7329 |
query.path[query.len] = '\0'; |
| 7330 |
} |
| 7331 |
|
| 7332 |
result = fs_win32_path_append(&query, "\\*", fs_get_allocation_callbacks(pFS)); |
| 7333 |
if (result != FS_SUCCESS) { |
| 7334 |
fs_win32_path_uninit(&query, fs_get_allocation_callbacks(pFS)); |
| 7335 |
return NULL; |
| 7336 |
} |
| 7337 |
|
| 7338 |
hFind = FindFirstFile(query.path, &fd); |
| 7339 |
fs_win32_path_uninit(&query, fs_get_allocation_callbacks(pFS)); |
| 7340 |
|
| 7341 |
if (hFind == INVALID_HANDLE_VALUE) { |
| 7342 |
return NULL; |
| 7343 |
} |
| 7344 |
|
| 7345 |
return fs_iterator_win32_resolve(NULL, pFS, hFind, &fd); |
| 7346 |
} |
| 7347 |
|
| 7348 |
static fs_iterator* fs_next_win32(fs_iterator* pIterator) |
| 7349 |
{ |
| 7350 |
fs_iterator_win32* pIteratorWin32 = (fs_iterator_win32*)pIterator; |
| 7351 |
WIN32_FIND_DATA fd; |
| 7352 |
|
| 7353 |
if (!FindNextFile(pIteratorWin32->hFind, &fd)) { |
| 7354 |
fs_free_iterator_win32(pIterator); |
| 7355 |
return NULL; |
| 7356 |
} |
| 7357 |
|
| 7358 |
return fs_iterator_win32_resolve(pIteratorWin32, pIterator->pFS, pIteratorWin32->hFind, &fd); |
| 7359 |
} |
| 7360 |
|
| 7361 |
static void fs_free_iterator_win32(fs_iterator* pIterator) |
| 7362 |
{ |
| 7363 |
fs_iterator_win32* pIteratorWin32 = (fs_iterator_win32*)pIterator; |
| 7364 |
|
| 7365 |
FindClose(pIteratorWin32->hFind); |
| 7366 |
fs_free(pIteratorWin32, fs_get_allocation_callbacks(pIterator->pFS)); |
| 7367 |
} |
| 7368 |
|
| 7369 |
static fs_backend fs_win32_backend = |
| 7370 |
{ |
| 7371 |
fs_alloc_size_win32, |
| 7372 |
fs_init_win32, |
| 7373 |
fs_uninit_win32, |
| 7374 |
fs_remove_win32, |
| 7375 |
fs_rename_win32, |
| 7376 |
fs_mkdir_win32, |
| 7377 |
fs_info_win32, |
| 7378 |
fs_file_alloc_size_win32, |
| 7379 |
fs_file_open_win32, |
| 7380 |
fs_file_close_win32, |
| 7381 |
fs_file_read_win32, |
| 7382 |
fs_file_write_win32, |
| 7383 |
fs_file_seek_win32, |
| 7384 |
fs_file_tell_win32, |
| 7385 |
fs_file_flush_win32, |
| 7386 |
fs_file_truncate_win32, |
| 7387 |
fs_file_info_win32, |
| 7388 |
fs_file_duplicate_win32, |
| 7389 |
fs_first_win32, |
| 7390 |
fs_next_win32, |
| 7391 |
fs_free_iterator_win32 |
| 7392 |
}; |
| 7393 |
|
| 7394 |
const fs_backend* FS_BACKEND_WIN32 = &fs_win32_backend; |
| 7395 |
#else |
| 7396 |
const fs_backend* FS_BACKEND_WIN32 = NULL; |
| 7397 |
#endif |
| 7398 |
|
| 7399 |
|
| 7400 |
|
| 7401 |
|
| 7402 |
|
| 7403 |
#if defined(_WIN32) |
| 7404 |
#include <shlobj.h> |
| 7405 |
|
| 7406 |
#ifndef CSIDL_LOCAL_APPDATA |
| 7407 |
#define CSIDL_LOCAL_APPDATA 0x001C |
| 7408 |
#endif |
| 7409 |
#ifndef CSIDL_PROFILE |
| 7410 |
#define CSIDL_PROFILE 0x0028 |
| 7411 |
#endif |
| 7412 |
|
| 7413 |
|
| 7414 |
|
| 7415 |
|
| 7416 |
|
| 7417 |
|
| 7418 |
HRESULT fs_get_executable_directory_win32(char* pPath) |
| 7419 |
{ |
| 7420 |
DWORD result; |
| 7421 |
|
| 7422 |
result = GetModuleFileNameA(NULL, pPath, 260); |
| 7423 |
if (result == 260) { |
| 7424 |
return ERROR_INSUFFICIENT_BUFFER; |
| 7425 |
} |
| 7426 |
|
| 7427 |
fs_path_directory(pPath, 260, pPath, result); |
| 7428 |
|
| 7429 |
return ERROR_SUCCESS; |
| 7430 |
} |
| 7431 |
|
| 7432 |
|
| 7433 |
|
| 7434 |
|
| 7435 |
|
| 7436 |
|
| 7437 |
|
| 7438 |
HRESULT fs_get_folder_path_win32(char* pPath, int nFolder) |
| 7439 |
{ |
| 7440 |
HRESULT hr; |
| 7441 |
|
| 7442 |
FS_ASSERT(pPath != NULL); |
| 7443 |
|
| 7444 |
|
| 7445 |
|
| 7446 |
|
| 7447 |
|
| 7448 |
|
| 7449 |
|
| 7450 |
|
| 7451 |
|
| 7452 |
|
| 7453 |
|
| 7454 |
|
| 7455 |
|
| 7456 |
|
| 7457 |
hr = SHGetSpecialFolderPathA(NULL, pPath, nFolder, 0); |
| 7458 |
if (FAILED(hr)) { |
| 7459 |
|
| 7460 |
|
| 7461 |
|
| 7462 |
|
| 7463 |
if (nFolder == CSIDL_LOCAL_APPDATA) { |
| 7464 |
hr = SHGetSpecialFolderPathA(NULL, pPath, CSIDL_APPDATA, 0); |
| 7465 |
if (FAILED(hr)) { |
| 7466 |
hr = fs_get_executable_directory_win32(pPath); |
| 7467 |
} |
| 7468 |
} else if (nFolder == CSIDL_PROFILE) { |
| 7469 |
|
| 7470 |
|
| 7471 |
|
| 7472 |
|
| 7473 |
hr = fs_get_executable_directory_win32(pPath); |
| 7474 |
} |
| 7475 |
} |
| 7476 |
|
| 7477 |
return hr; |
| 7478 |
} |
| 7479 |
#else |
| 7480 |
#include <pwd.h> |
| 7481 |
#include <unistd.h> |
| 7482 |
|
| 7483 |
static const char* fs_sysdir_home(void) |
| 7484 |
{ |
| 7485 |
const char* pHome; |
| 7486 |
struct passwd* pPasswd; |
| 7487 |
|
| 7488 |
pHome = getenv("HOME"); |
| 7489 |
if (pHome != NULL) { |
| 7490 |
return pHome; |
| 7491 |
} |
| 7492 |
|
| 7493 |
|
| 7494 |
pPasswd = getpwuid(getuid()); |
| 7495 |
if (pPasswd != NULL) { |
| 7496 |
return pPasswd->pw_dir; |
| 7497 |
} |
| 7498 |
|
| 7499 |
return NULL; |
| 7500 |
} |
| 7501 |
|
| 7502 |
static size_t fs_sysdir_home_subdir(const char* pSubDir, char* pDst, size_t dstCap) |
| 7503 |
{ |
| 7504 |
const char* pHome = fs_sysdir_home(); |
| 7505 |
if (pHome != NULL) { |
| 7506 |
size_t homeLen = strlen(pHome); |
| 7507 |
size_t subDirLen = strlen(pSubDir); |
| 7508 |
size_t fullLength = homeLen + 1 + subDirLen; |
| 7509 |
|
| 7510 |
if (fullLength < dstCap) { |
| 7511 |
FS_COPY_MEMORY(pDst, pHome, homeLen); |
| 7512 |
pDst[homeLen] = '/'; |
| 7513 |
FS_COPY_MEMORY(pDst + homeLen + 1, pSubDir, subDirLen); |
| 7514 |
pDst[fullLength] = '\0'; |
| 7515 |
} |
| 7516 |
|
| 7517 |
return fullLength; |
| 7518 |
} |
| 7519 |
|
| 7520 |
return 0; |
| 7521 |
} |
| 7522 |
#endif |
| 7523 |
|
| 7524 |
FS_API size_t fs_sysdir(fs_sysdir_type type, char* pDst, size_t dstCap) |
| 7525 |
{ |
| 7526 |
size_t fullLength = 0; |
| 7527 |
|
| 7528 |
#if defined(_WIN32) |
| 7529 |
{ |
| 7530 |
HRESULT hr; |
| 7531 |
char pPath[260]; |
| 7532 |
|
| 7533 |
switch (type) |
| 7534 |
{ |
| 7535 |
case FS_SYSDIR_HOME: |
| 7536 |
{ |
| 7537 |
hr = fs_get_folder_path_win32(pPath, CSIDL_PROFILE); |
| 7538 |
if (SUCCEEDED(hr)) { |
| 7539 |
fullLength = strlen(pPath); |
| 7540 |
if (pDst != NULL && fullLength < dstCap) { |
| 7541 |
FS_COPY_MEMORY(pDst, pPath, fullLength); |
| 7542 |
pDst[fullLength] = '\0'; |
| 7543 |
} |
| 7544 |
} |
| 7545 |
} break; |
| 7546 |
|
| 7547 |
case FS_SYSDIR_TEMP: |
| 7548 |
{ |
| 7549 |
fullLength = GetTempPathA(sizeof(pPath), pPath); |
| 7550 |
if (fullLength > 0) { |
| 7551 |
fullLength -= 1; |
| 7552 |
|
| 7553 |
if (pDst != NULL && fullLength < dstCap) { |
| 7554 |
FS_COPY_MEMORY(pDst, pPath, fullLength); |
| 7555 |
pDst[fullLength] = '\0'; |
| 7556 |
} |
| 7557 |
} |
| 7558 |
} break; |
| 7559 |
|
| 7560 |
case FS_SYSDIR_CONFIG: |
| 7561 |
{ |
| 7562 |
hr = fs_get_folder_path_win32(pPath, CSIDL_APPDATA); |
| 7563 |
if (SUCCEEDED(hr)) { |
| 7564 |
fullLength = strlen(pPath); |
| 7565 |
if (pDst != NULL && fullLength < dstCap) { |
| 7566 |
FS_COPY_MEMORY(pDst, pPath, fullLength); |
| 7567 |
pDst[fullLength] = '\0'; |
| 7568 |
} |
| 7569 |
} |
| 7570 |
} break; |
| 7571 |
|
| 7572 |
case FS_SYSDIR_DATA: |
| 7573 |
{ |
| 7574 |
hr = fs_get_folder_path_win32(pPath, CSIDL_LOCAL_APPDATA); |
| 7575 |
if (SUCCEEDED(hr)) { |
| 7576 |
fullLength = strlen(pPath); |
| 7577 |
if (pDst != NULL && fullLength < dstCap) { |
| 7578 |
FS_COPY_MEMORY(pDst, pPath, fullLength); |
| 7579 |
pDst[fullLength] = '\0'; |
| 7580 |
} |
| 7581 |
} |
| 7582 |
} break; |
| 7583 |
|
| 7584 |
case FS_SYSDIR_CACHE: |
| 7585 |
{ |
| 7586 |
|
| 7587 |
hr = fs_get_folder_path_win32(pPath, CSIDL_LOCAL_APPDATA); |
| 7588 |
if (SUCCEEDED(hr)) { |
| 7589 |
const char* pCacheSuffix = "\\Cache"; |
| 7590 |
size_t localAppDataLen = strlen(pPath); |
| 7591 |
size_t cacheSuffixLen = strlen(pCacheSuffix); |
| 7592 |
fullLength = localAppDataLen + cacheSuffixLen; |
| 7593 |
|
| 7594 |
if (pDst != NULL && fullLength < dstCap) { |
| 7595 |
FS_COPY_MEMORY(pDst, pPath, localAppDataLen); |
| 7596 |
FS_COPY_MEMORY(pDst + localAppDataLen, pCacheSuffix, cacheSuffixLen); |
| 7597 |
pDst[fullLength] = '\0'; |
| 7598 |
} |
| 7599 |
} |
| 7600 |
} break; |
| 7601 |
|
| 7602 |
default: |
| 7603 |
{ |
| 7604 |
FS_ASSERT(!"Unknown system directory type."); |
| 7605 |
} break; |
| 7606 |
} |
| 7607 |
|
| 7608 |
|
| 7609 |
if (pDst != NULL && fullLength < dstCap) { |
| 7610 |
size_t i; |
| 7611 |
|
| 7612 |
for (i = 0; i < fullLength; i += 1) { |
| 7613 |
if (pDst[i] == '\\') { |
| 7614 |
pDst[i] = '/'; |
| 7615 |
} |
| 7616 |
} |
| 7617 |
} |
| 7618 |
} |
| 7619 |
#else |
| 7620 |
{ |
| 7621 |
switch (type) |
| 7622 |
{ |
| 7623 |
case FS_SYSDIR_HOME: |
| 7624 |
{ |
| 7625 |
const char* pHome = fs_sysdir_home(); |
| 7626 |
if (pHome != NULL) { |
| 7627 |
fullLength = strlen(pHome); |
| 7628 |
if (pDst != NULL && fullLength < dstCap) { |
| 7629 |
FS_COPY_MEMORY(pDst, pHome, fullLength); |
| 7630 |
pDst[fullLength] = '\0'; |
| 7631 |
} |
| 7632 |
} |
| 7633 |
} break; |
| 7634 |
|
| 7635 |
case FS_SYSDIR_TEMP: |
| 7636 |
{ |
| 7637 |
const char* pTemp = getenv("TMPDIR"); |
| 7638 |
if (pTemp != NULL) { |
| 7639 |
fullLength = strlen(pTemp); |
| 7640 |
if (pDst != NULL && fullLength < dstCap) { |
| 7641 |
FS_COPY_MEMORY(pDst, pTemp, fullLength); |
| 7642 |
pDst[fullLength] = '\0'; |
| 7643 |
} |
| 7644 |
} else { |
| 7645 |
|
| 7646 |
const char* pTmp = "/tmp"; |
| 7647 |
fullLength = strlen(pTmp); |
| 7648 |
if (pDst != NULL && fullLength < dstCap) { |
| 7649 |
FS_COPY_MEMORY(pDst, pTmp, fullLength); |
| 7650 |
pDst[fullLength] = '\0'; |
| 7651 |
} |
| 7652 |
} |
| 7653 |
} break; |
| 7654 |
|
| 7655 |
case FS_SYSDIR_CONFIG: |
| 7656 |
{ |
| 7657 |
const char* pConfig = getenv("XDG_CONFIG_HOME"); |
| 7658 |
if (pConfig != NULL) { |
| 7659 |
fullLength = strlen(pConfig); |
| 7660 |
if (pDst != NULL && fullLength < dstCap) { |
| 7661 |
FS_COPY_MEMORY(pDst, pConfig, fullLength); |
| 7662 |
pDst[fullLength] = '\0'; |
| 7663 |
} |
| 7664 |
} else { |
| 7665 |
|
| 7666 |
fullLength = fs_sysdir_home_subdir(".config", pDst, dstCap); |
| 7667 |
} |
| 7668 |
} break; |
| 7669 |
|
| 7670 |
case FS_SYSDIR_DATA: |
| 7671 |
{ |
| 7672 |
const char* pData = getenv("XDG_DATA_HOME"); |
| 7673 |
if (pData != NULL) { |
| 7674 |
fullLength = strlen(pData); |
| 7675 |
if (pDst != NULL && fullLength < dstCap) { |
| 7676 |
FS_COPY_MEMORY(pDst, pData, fullLength); |
| 7677 |
pDst[fullLength] = '\0'; |
| 7678 |
} |
| 7679 |
} else { |
| 7680 |
|
| 7681 |
fullLength = fs_sysdir_home_subdir(".local/share", pDst, dstCap); |
| 7682 |
} |
| 7683 |
} break; |
| 7684 |
|
| 7685 |
case FS_SYSDIR_CACHE: |
| 7686 |
{ |
| 7687 |
const char* pCache = getenv("XDG_CACHE_HOME"); |
| 7688 |
if (pCache != NULL) { |
| 7689 |
fullLength = strlen(pCache); |
| 7690 |
if (pDst != NULL && fullLength < dstCap) { |
| 7691 |
FS_COPY_MEMORY(pDst, pCache, fullLength); |
| 7692 |
pDst[fullLength] = '\0'; |
| 7693 |
} |
| 7694 |
} else { |
| 7695 |
|
| 7696 |
fullLength = fs_sysdir_home_subdir(".cache", pDst, dstCap); |
| 7697 |
} |
| 7698 |
} break; |
| 7699 |
|
| 7700 |
default: |
| 7701 |
{ |
| 7702 |
FS_ASSERT(!"Unknown system directory type."); |
| 7703 |
} break; |
| 7704 |
} |
| 7705 |
} |
| 7706 |
#endif |
| 7707 |
|
| 7708 |
|
| 7709 |
if (pDst != NULL && fullLength < dstCap && fullLength > 0) { |
| 7710 |
if (pDst[fullLength - 1] == '/' || pDst[fullLength - 1] == '\\') { |
| 7711 |
pDst[fullLength - 1] = '\0'; |
| 7712 |
} |
| 7713 |
} |
| 7714 |
|
| 7715 |
return fullLength; |
| 7716 |
} |
| 7717 |
|
| 7718 |
|
| 7719 |
|
| 7720 |
|
| 7721 |
#ifndef _WIN32 |
| 7722 |
|
| 7723 |
|
| 7724 |
|
| 7725 |
|
| 7726 |
|
| 7727 |
|
| 7728 |
|
| 7729 |
|
| 7730 |
|
| 7731 |
|
| 7732 |
#if !defined(FS_USE_MKDTEMP_FALLBACK) |
| 7733 |
#if !defined(FS_HAS_MKDTEMP) && (defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) |
| 7734 |
#define FS_HAS_MKDTEMP |
| 7735 |
#endif |
| 7736 |
#if !defined(FS_HAS_MKDTEMP) && defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L |
| 7737 |
#define FS_HAS_MKDTEMP |
| 7738 |
#endif |
| 7739 |
#if !defined(FS_HAS_MKDTEMP) && defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700 |
| 7740 |
#define FS_HAS_MKDTEMP |
| 7741 |
#endif |
| 7742 |
#if !defined(FS_HAS_MKDTEMP) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && !defined(__STRICT_ANSI__) |
| 7743 |
#define FS_HAS_MKDTEMP |
| 7744 |
#endif |
| 7745 |
#endif |
| 7746 |
|
| 7747 |
#if !defined(FS_HAS_MKDTEMP) |
| 7748 |
#include <time.h> |
| 7749 |
#include <sys/stat.h> |
| 7750 |
#include <fcntl.h> |
| 7751 |
|
| 7752 |
static int fs_get_random_bytes(unsigned char* pBytes, size_t count) |
| 7753 |
{ |
| 7754 |
int fd; |
| 7755 |
ssize_t bytesRead; |
| 7756 |
|
| 7757 |
|
| 7758 |
fd = open("/dev/urandom", O_RDONLY); |
| 7759 |
if (fd >= 0) { |
| 7760 |
bytesRead = read(fd, pBytes, count); |
| 7761 |
close(fd); |
| 7762 |
|
| 7763 |
if ((size_t)bytesRead == count) { |
| 7764 |
return 0; |
| 7765 |
} |
| 7766 |
|
| 7767 |
|
| 7768 |
} |
| 7769 |
|
| 7770 |
|
| 7771 |
{ |
| 7772 |
unsigned long stackAddress = 0; |
| 7773 |
unsigned long seed; |
| 7774 |
size_t i; |
| 7775 |
|
| 7776 |
|
| 7777 |
seed = (unsigned long)time(NULL); |
| 7778 |
seed ^= (unsigned long)getpid(); |
| 7779 |
seed ^= (unsigned long)(size_t)&stackAddress; |
| 7780 |
seed ^= (unsigned long)clock(); |
| 7781 |
|
| 7782 |
|
| 7783 |
for (i = 0; i < count; i += 1) { |
| 7784 |
seed = seed * 1103515245U + 12345U; |
| 7785 |
pBytes[i] = (unsigned char)(seed >> 16); |
| 7786 |
} |
| 7787 |
|
| 7788 |
return 0; |
| 7789 |
} |
| 7790 |
} |
| 7791 |
|
| 7792 |
static char* fs_mkdtemp_fallback(char* pTemplate) |
| 7793 |
{ |
| 7794 |
size_t templateLen; |
| 7795 |
char* pXXXXXX; |
| 7796 |
int attempts; |
| 7797 |
int i; |
| 7798 |
|
| 7799 |
if (pTemplate == NULL) { |
| 7800 |
return NULL; |
| 7801 |
} |
| 7802 |
|
| 7803 |
|
| 7804 |
templateLen = strlen(pTemplate); |
| 7805 |
if (templateLen < 6) { |
| 7806 |
return NULL; |
| 7807 |
} |
| 7808 |
|
| 7809 |
pXXXXXX = pTemplate + templateLen - 6; |
| 7810 |
for (i = 0; i < 6; i += 1) { |
| 7811 |
if (pXXXXXX[i] != 'X') { |
| 7812 |
return NULL; |
| 7813 |
} |
| 7814 |
} |
| 7815 |
|
| 7816 |
|
| 7817 |
for (attempts = 0; attempts < 100; attempts += 1) { |
| 7818 |
unsigned char randomBytes[6]; |
| 7819 |
|
| 7820 |
if (fs_get_random_bytes(randomBytes, 6) != 0) { |
| 7821 |
return NULL; |
| 7822 |
} |
| 7823 |
|
| 7824 |
|
| 7825 |
for (i = 0; i < 6; i += 1) { |
| 7826 |
int r = randomBytes[i] % 62; |
| 7827 |
if (r < 10) { |
| 7828 |
pXXXXXX[i] = '0' + r; |
| 7829 |
} else if (r < 36) { |
| 7830 |
pXXXXXX[i] = 'A' + (r - 10); |
| 7831 |
} else { |
| 7832 |
pXXXXXX[i] = 'a' + (r - 36); |
| 7833 |
} |
| 7834 |
} |
| 7835 |
|
| 7836 |
|
| 7837 |
if (mkdir(pTemplate, S_IRWXU) == 0) { |
| 7838 |
return pTemplate; |
| 7839 |
} |
| 7840 |
|
| 7841 |
|
| 7842 |
|
| 7843 |
|
| 7844 |
|
| 7845 |
if (errno != EEXIST) { |
| 7846 |
return NULL; |
| 7847 |
} |
| 7848 |
} |
| 7849 |
|
| 7850 |
|
| 7851 |
return NULL; |
| 7852 |
} |
| 7853 |
|
| 7854 |
static int fs_mkstemp_fallback(char* pTemplate) |
| 7855 |
{ |
| 7856 |
size_t templateLen; |
| 7857 |
char* pXXXXXX; |
| 7858 |
int attempts; |
| 7859 |
int i; |
| 7860 |
int fd; |
| 7861 |
|
| 7862 |
if (pTemplate == NULL) { |
| 7863 |
return -1; |
| 7864 |
} |
| 7865 |
|
| 7866 |
|
| 7867 |
templateLen = strlen(pTemplate); |
| 7868 |
if (templateLen < 6) { |
| 7869 |
return -1; |
| 7870 |
} |
| 7871 |
|
| 7872 |
pXXXXXX = pTemplate + templateLen - 6; |
| 7873 |
for (i = 0; i < 6; i += 1) { |
| 7874 |
if (pXXXXXX[i] != 'X') { |
| 7875 |
return -1; |
| 7876 |
} |
| 7877 |
} |
| 7878 |
|
| 7879 |
|
| 7880 |
for (attempts = 0; attempts < 100; attempts += 1) { |
| 7881 |
unsigned char randomBytes[6]; |
| 7882 |
|
| 7883 |
if (fs_get_random_bytes(randomBytes, 6) != 0) { |
| 7884 |
return -1; |
| 7885 |
} |
| 7886 |
|
| 7887 |
|
| 7888 |
for (i = 0; i < 6; i += 1) { |
| 7889 |
int r = randomBytes[i] % 62; |
| 7890 |
if (r < 10) { |
| 7891 |
pXXXXXX[i] = '0' + r; |
| 7892 |
} else if (r < 36) { |
| 7893 |
pXXXXXX[i] = 'A' + (r - 10); |
| 7894 |
} else { |
| 7895 |
pXXXXXX[i] = 'a' + (r - 36); |
| 7896 |
} |
| 7897 |
} |
| 7898 |
|
| 7899 |
|
| 7900 |
fd = open(pTemplate, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); |
| 7901 |
if (fd >= 0) { |
| 7902 |
return fd; |
| 7903 |
} |
| 7904 |
|
| 7905 |
|
| 7906 |
|
| 7907 |
|
| 7908 |
|
| 7909 |
if (errno != EEXIST) { |
| 7910 |
return -1; |
| 7911 |
} |
| 7912 |
} |
| 7913 |
|
| 7914 |
|
| 7915 |
return -1; |
| 7916 |
} |
| 7917 |
#endif |
| 7918 |
|
| 7919 |
static char* fs_mkdtemp(char* pTemplate) |
| 7920 |
{ |
| 7921 |
if (pTemplate == NULL) { |
| 7922 |
return NULL; |
| 7923 |
} |
| 7924 |
|
| 7925 |
#if defined(FS_HAS_MKDTEMP) |
| 7926 |
{ |
| 7927 |
return mkdtemp(pTemplate); |
| 7928 |
} |
| 7929 |
#else |
| 7930 |
{ |
| 7931 |
return fs_mkdtemp_fallback(pTemplate); |
| 7932 |
} |
| 7933 |
#endif |
| 7934 |
} |
| 7935 |
|
| 7936 |
static int fs_mkstemp(char* pTemplate) |
| 7937 |
{ |
| 7938 |
if (pTemplate == NULL) { |
| 7939 |
return -1; |
| 7940 |
} |
| 7941 |
|
| 7942 |
#if defined(FS_HAS_MKDTEMP) |
| 7943 |
{ |
| 7944 |
return mkstemp(pTemplate); |
| 7945 |
} |
| 7946 |
#else |
| 7947 |
{ |
| 7948 |
return fs_mkstemp_fallback(pTemplate); |
| 7949 |
} |
| 7950 |
#endif |
| 7951 |
} |
| 7952 |
#endif |
| 7953 |
|
| 7954 |
FS_API fs_result fs_mktmp(const char* pPrefix, char* pTmpPath, size_t tmpPathCap, int options) |
| 7955 |
{ |
| 7956 |
size_t baseDirLen; |
| 7957 |
const char* pPrefixName; |
| 7958 |
const char* pPrefixDir; |
| 7959 |
size_t prefixDirLen; |
| 7960 |
|
| 7961 |
if (pTmpPath == NULL) { |
| 7962 |
return FS_INVALID_ARGS; |
| 7963 |
} |
| 7964 |
|
| 7965 |
pTmpPath[0] = '\0'; |
| 7966 |
|
| 7967 |
if (tmpPathCap == 0) { |
| 7968 |
return FS_INVALID_ARGS; |
| 7969 |
} |
| 7970 |
|
| 7971 |
if (pPrefix == NULL) { |
| 7972 |
pPrefix = ""; |
| 7973 |
} |
| 7974 |
|
| 7975 |
if (pPrefix[0] == '\0') { |
| 7976 |
pPrefix = "fs"; |
| 7977 |
} |
| 7978 |
|
| 7979 |
|
| 7980 |
if ((options & (FS_MKTMP_DIR | FS_MKTMP_FILE)) == 0) { |
| 7981 |
return FS_INVALID_ARGS; |
| 7982 |
} |
| 7983 |
|
| 7984 |
|
| 7985 |
if ((options & FS_MKTMP_DIR) != 0 && (options & FS_MKTMP_FILE) != 0) { |
| 7986 |
return FS_INVALID_ARGS; |
| 7987 |
} |
| 7988 |
|
| 7989 |
|
| 7990 |
if (strstr(pPrefix, "..") != NULL || pPrefix[0] == '/') { |
| 7991 |
return FS_INVALID_ARGS; |
| 7992 |
} |
| 7993 |
|
| 7994 |
|
| 7995 |
baseDirLen = fs_sysdir(FS_SYSDIR_TEMP, pTmpPath, tmpPathCap); |
| 7996 |
if (baseDirLen == 0) { |
| 7997 |
return FS_ERROR; |
| 7998 |
} |
| 7999 |
|
| 8000 |
|
| 8001 |
pPrefixName = fs_path_file_name(pPrefix, FS_NULL_TERMINATED); |
| 8002 |
FS_ASSERT(pPrefixName != NULL); |
| 8003 |
|
| 8004 |
if (pPrefixName == pPrefix) { |
| 8005 |
|
| 8006 |
pPrefixDir = ""; |
| 8007 |
prefixDirLen = 0; |
| 8008 |
} else { |
| 8009 |
|
| 8010 |
pPrefixDir = pPrefix; |
| 8011 |
prefixDirLen = (size_t)(pPrefixName - pPrefix); |
| 8012 |
prefixDirLen -= 1; |
| 8013 |
} |
| 8014 |
|
| 8015 |
if (prefixDirLen > 0) { |
| 8016 |
if (fs_strcat_s(pTmpPath, tmpPathCap, "/") != 0) { |
| 8017 |
return FS_PATH_TOO_LONG; |
| 8018 |
} |
| 8019 |
} |
| 8020 |
|
| 8021 |
if (fs_strncat_s(pTmpPath, tmpPathCap, pPrefixDir, prefixDirLen) != 0) { |
| 8022 |
return FS_PATH_TOO_LONG; |
| 8023 |
} |
| 8024 |
|
| 8025 |
|
| 8026 |
if ((options & FS_NO_CREATE_DIRS) == 0) { |
| 8027 |
fs_mkdir(NULL, pTmpPath, FS_IGNORE_MOUNTS); |
| 8028 |
} |
| 8029 |
|
| 8030 |
|
| 8031 |
if (fs_strcat_s(pTmpPath, tmpPathCap, "/") != 0) { |
| 8032 |
return FS_PATH_TOO_LONG; |
| 8033 |
} |
| 8034 |
|
| 8035 |
|
| 8036 |
#if defined(_WIN32) |
| 8037 |
{ |
| 8038 |
|
| 8039 |
|
| 8040 |
|
| 8041 |
|
| 8042 |
|
| 8043 |
char pTmpPathWin[MAX_PATH]; |
| 8044 |
size_t i; |
| 8045 |
|
| 8046 |
for (i = 0; pTmpPath[i] != '\0'; i += 1) { |
| 8047 |
if (pTmpPath[i] == '/') { |
| 8048 |
pTmpPath[i] = '\\'; |
| 8049 |
} |
| 8050 |
} |
| 8051 |
|
| 8052 |
if (GetTempFileNameA(pTmpPath, pPrefixName, 0, pTmpPathWin) == 0) { |
| 8053 |
return fs_result_from_GetLastError(); |
| 8054 |
} |
| 8055 |
|
| 8056 |
|
| 8057 |
|
| 8058 |
|
| 8059 |
|
| 8060 |
|
| 8061 |
if (fs_strcpy_s(pTmpPath, tmpPathCap, pTmpPathWin) != 0) { |
| 8062 |
DeleteFileA(pTmpPathWin); |
| 8063 |
return FS_PATH_TOO_LONG; |
| 8064 |
} |
| 8065 |
|
| 8066 |
|
| 8067 |
|
| 8068 |
|
| 8069 |
|
| 8070 |
if ((options & FS_MKTMP_DIR) != 0) { |
| 8071 |
|
| 8072 |
DeleteFileA(pTmpPathWin); |
| 8073 |
|
| 8074 |
if (CreateDirectoryA(pTmpPathWin, NULL) == 0) { |
| 8075 |
return fs_result_from_GetLastError(); |
| 8076 |
} |
| 8077 |
} else { |
| 8078 |
|
| 8079 |
} |
| 8080 |
|
| 8081 |
|
| 8082 |
for (i = 0; pTmpPath[i] != '\0'; i += 1) { |
| 8083 |
if (pTmpPath[i] == '\\') { |
| 8084 |
pTmpPath[i] = '/'; |
| 8085 |
} |
| 8086 |
} |
| 8087 |
} |
| 8088 |
#else |
| 8089 |
{ |
| 8090 |
|
| 8091 |
if (fs_strcat_s(pTmpPath, tmpPathCap, pPrefixName) != 0) { |
| 8092 |
return FS_PATH_TOO_LONG; |
| 8093 |
} |
| 8094 |
|
| 8095 |
|
| 8096 |
if (fs_strcat_s(pTmpPath, tmpPathCap, "XXXXXX") != 0) { |
| 8097 |
return FS_PATH_TOO_LONG; |
| 8098 |
} |
| 8099 |
|
| 8100 |
|
| 8101 |
if ((options & FS_MKTMP_DIR) != 0) { |
| 8102 |
|
| 8103 |
if (fs_mkdtemp(pTmpPath) == NULL) { |
| 8104 |
return fs_result_from_errno(errno); |
| 8105 |
} |
| 8106 |
} else { |
| 8107 |
|
| 8108 |
int fd = fs_mkstemp(pTmpPath); |
| 8109 |
if (fd == -1) { |
| 8110 |
return fs_result_from_errno(errno); |
| 8111 |
} |
| 8112 |
|
| 8113 |
close(fd); |
| 8114 |
} |
| 8115 |
} |
| 8116 |
#endif |
| 8117 |
|
| 8118 |
return FS_SUCCESS; |
| 8119 |
} |
| 8120 |
|
| 8121 |
|
| 8122 |
|
| 8123 |
|
| 8124 |
|
| 8125 |
FS_API fs_result fs_path_first(const char* pPath, size_t pathLen, fs_path_iterator* pIterator) |
| 8126 |
{ |
| 8127 |
if (pIterator == NULL) { |
| 8128 |
return FS_INVALID_ARGS; |
| 8129 |
} |
| 8130 |
|
| 8131 |
FS_ZERO_OBJECT(pIterator); |
| 8132 |
|
| 8133 |
if (pPath == NULL || pPath[0] == '\0' || pathLen == 0) { |
| 8134 |
return FS_INVALID_ARGS; |
| 8135 |
} |
| 8136 |
|
| 8137 |
pIterator->pFullPath = pPath; |
| 8138 |
pIterator->fullPathLength = pathLen; |
| 8139 |
pIterator->segmentOffset = 0; |
| 8140 |
pIterator->segmentLength = 0; |
| 8141 |
|
| 8142 |
|
| 8143 |
while (pIterator->segmentLength < pathLen && pPath[pIterator->segmentLength] != '\0' && (pPath[pIterator->segmentLength] != '\\' && pPath[pIterator->segmentLength] != '/')) { |
| 8144 |
pIterator->segmentLength += 1; |
| 8145 |
} |
| 8146 |
|
| 8147 |
return FS_SUCCESS; |
| 8148 |
} |
| 8149 |
|
| 8150 |
FS_API fs_result fs_path_last(const char* pPath, size_t pathLen, fs_path_iterator* pIterator) |
| 8151 |
{ |
| 8152 |
if (pIterator == NULL) { |
| 8153 |
return FS_INVALID_ARGS; |
| 8154 |
} |
| 8155 |
|
| 8156 |
FS_ZERO_OBJECT(pIterator); |
| 8157 |
|
| 8158 |
if (pathLen == 0 || pPath == NULL || pPath[0] == '\0') { |
| 8159 |
return FS_INVALID_ARGS; |
| 8160 |
} |
| 8161 |
|
| 8162 |
if (pathLen == (size_t)-1) { |
| 8163 |
pathLen = strlen(pPath); |
| 8164 |
} |
| 8165 |
|
| 8166 |
|
| 8167 |
pIterator->pFullPath = pPath; |
| 8168 |
pIterator->fullPathLength = pathLen; |
| 8169 |
pIterator->segmentOffset = pathLen; |
| 8170 |
pIterator->segmentLength = 0; |
| 8171 |
|
| 8172 |
|
| 8173 |
while (pIterator->segmentLength < pathLen && pPath[pIterator->segmentOffset - 1] != '\0' && (pPath[pIterator->segmentOffset - 1] != '\\' && pPath[pIterator->segmentOffset - 1] != '/')) { |
| 8174 |
pIterator->segmentOffset -= 1; |
| 8175 |
pIterator->segmentLength += 1; |
| 8176 |
} |
| 8177 |
|
| 8178 |
return FS_SUCCESS; |
| 8179 |
} |
| 8180 |
|
| 8181 |
FS_API fs_result fs_path_next(fs_path_iterator* pIterator) |
| 8182 |
{ |
| 8183 |
if (pIterator == NULL) { |
| 8184 |
return FS_INVALID_ARGS; |
| 8185 |
} |
| 8186 |
|
| 8187 |
FS_ASSERT(pIterator->pFullPath != NULL); |
| 8188 |
|
| 8189 |
|
| 8190 |
pIterator->segmentOffset = pIterator->segmentOffset + pIterator->segmentLength; |
| 8191 |
pIterator->segmentLength = 0; |
| 8192 |
|
| 8193 |
|
| 8194 |
if (pIterator->segmentOffset >= pIterator->fullPathLength || pIterator->pFullPath[pIterator->segmentOffset] == '\0') { |
| 8195 |
return FS_AT_END; |
| 8196 |
} |
| 8197 |
|
| 8198 |
|
| 8199 |
pIterator->segmentOffset += 1; |
| 8200 |
|
| 8201 |
|
| 8202 |
for (;;) { |
| 8203 |
if (pIterator->segmentOffset + pIterator->segmentLength >= pIterator->fullPathLength || pIterator->pFullPath[pIterator->segmentOffset + pIterator->segmentLength] == '\0') { |
| 8204 |
break; |
| 8205 |
} |
| 8206 |
|
| 8207 |
if (pIterator->pFullPath[pIterator->segmentOffset + pIterator->segmentLength] == '\\' || pIterator->pFullPath[pIterator->segmentOffset + pIterator->segmentLength] == '/') { |
| 8208 |
break; |
| 8209 |
} |
| 8210 |
|
| 8211 |
pIterator->segmentLength += 1; |
| 8212 |
} |
| 8213 |
|
| 8214 |
return FS_SUCCESS; |
| 8215 |
} |
| 8216 |
|
| 8217 |
FS_API fs_result fs_path_prev(fs_path_iterator* pIterator) |
| 8218 |
{ |
| 8219 |
if (pIterator == NULL) { |
| 8220 |
return FS_INVALID_ARGS; |
| 8221 |
} |
| 8222 |
|
| 8223 |
FS_ASSERT(pIterator->pFullPath != NULL); |
| 8224 |
|
| 8225 |
if (pIterator->segmentOffset == 0) { |
| 8226 |
return FS_AT_END; |
| 8227 |
} |
| 8228 |
|
| 8229 |
pIterator->segmentLength = 0; |
| 8230 |
|
| 8231 |
|
| 8232 |
|
| 8233 |
|
| 8234 |
|
| 8235 |
pIterator->segmentOffset = pIterator->segmentOffset - 1; |
| 8236 |
pIterator->segmentLength = 0; |
| 8237 |
|
| 8238 |
|
| 8239 |
for (;;) { |
| 8240 |
if (pIterator->segmentOffset == 0) { |
| 8241 |
break; |
| 8242 |
} |
| 8243 |
|
| 8244 |
if (pIterator->pFullPath[pIterator->segmentOffset - 1] == '\\' || pIterator->pFullPath[pIterator->segmentOffset - 1] == '/') { |
| 8245 |
break; |
| 8246 |
} |
| 8247 |
|
| 8248 |
pIterator->segmentOffset -= 1; |
| 8249 |
pIterator->segmentLength += 1; |
| 8250 |
} |
| 8251 |
|
| 8252 |
return FS_SUCCESS; |
| 8253 |
} |
| 8254 |
|
| 8255 |
FS_API fs_bool32 fs_path_is_first(const fs_path_iterator* pIterator) |
| 8256 |
{ |
| 8257 |
if (pIterator == NULL) { |
| 8258 |
return FS_FALSE; |
| 8259 |
} |
| 8260 |
|
| 8261 |
return pIterator->segmentOffset == 0; |
| 8262 |
|
| 8263 |
} |
| 8264 |
|
| 8265 |
FS_API fs_bool32 fs_path_is_last(const fs_path_iterator* pIterator) |
| 8266 |
{ |
| 8267 |
if (pIterator == NULL) { |
| 8268 |
return FS_FALSE; |
| 8269 |
} |
| 8270 |
|
| 8271 |
if (pIterator->fullPathLength == FS_NULL_TERMINATED) { |
| 8272 |
return pIterator->pFullPath[pIterator->segmentOffset + pIterator->segmentLength] == '\0'; |
| 8273 |
} else { |
| 8274 |
return pIterator->segmentOffset + pIterator->segmentLength == pIterator->fullPathLength; |
| 8275 |
} |
| 8276 |
} |
| 8277 |
|
| 8278 |
FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB) |
| 8279 |
{ |
| 8280 |
FS_ASSERT(pIteratorA != NULL); |
| 8281 |
FS_ASSERT(pIteratorB != NULL); |
| 8282 |
|
| 8283 |
if (pIteratorA->pFullPath == pIteratorB->pFullPath && pIteratorA->segmentOffset == pIteratorB->segmentOffset && pIteratorA->segmentLength == pIteratorB->segmentLength) { |
| 8284 |
return 0; |
| 8285 |
} |
| 8286 |
|
| 8287 |
return fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength)); |
| 8288 |
} |
| 8289 |
|
| 8290 |
FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen) |
| 8291 |
{ |
| 8292 |
fs_path_iterator iPathA; |
| 8293 |
fs_path_iterator iPathB; |
| 8294 |
fs_result result; |
| 8295 |
|
| 8296 |
if (pPathA == NULL && pPathB == NULL) { |
| 8297 |
return 0; |
| 8298 |
} |
| 8299 |
|
| 8300 |
if (pPathA == NULL) { |
| 8301 |
return -1; |
| 8302 |
} |
| 8303 |
if (pPathB == NULL) { |
| 8304 |
return +1; |
| 8305 |
} |
| 8306 |
|
| 8307 |
result = fs_path_first(pPathA, pathALen, &iPathA); |
| 8308 |
if (result != FS_SUCCESS) { |
| 8309 |
return -1; |
| 8310 |
} |
| 8311 |
|
| 8312 |
result = fs_path_first(pPathB, pathBLen, &iPathB); |
| 8313 |
if (result != FS_SUCCESS) { |
| 8314 |
return +1; |
| 8315 |
} |
| 8316 |
|
| 8317 |
|
| 8318 |
for (;;) { |
| 8319 |
int cmp; |
| 8320 |
|
| 8321 |
cmp = fs_path_iterators_compare(&iPathA, &iPathB); |
| 8322 |
if (cmp != 0) { |
| 8323 |
return cmp; |
| 8324 |
} |
| 8325 |
|
| 8326 |
if (fs_path_is_last(&iPathA) && fs_path_is_last(&iPathB)) { |
| 8327 |
return 0; |
| 8328 |
} |
| 8329 |
|
| 8330 |
result = fs_path_next(&iPathA); |
| 8331 |
if (result != FS_SUCCESS) { |
| 8332 |
return -1; |
| 8333 |
} |
| 8334 |
|
| 8335 |
result = fs_path_next(&iPathB); |
| 8336 |
if (result != FS_SUCCESS) { |
| 8337 |
return +1; |
| 8338 |
} |
| 8339 |
} |
| 8340 |
|
| 8341 |
return 0; |
| 8342 |
} |
| 8343 |
|
| 8344 |
FS_API const char* fs_path_file_name(const char* pPath, size_t pathLen) |
| 8345 |
{ |
| 8346 |
|
| 8347 |
fs_result result; |
| 8348 |
fs_path_iterator last; |
| 8349 |
|
| 8350 |
result = fs_path_last(pPath, pathLen, &last); |
| 8351 |
if (result != FS_SUCCESS) { |
| 8352 |
return NULL; |
| 8353 |
} |
| 8354 |
|
| 8355 |
if (last.segmentLength == 0) { |
| 8356 |
return NULL; |
| 8357 |
} |
| 8358 |
|
| 8359 |
return last.pFullPath + last.segmentOffset; |
| 8360 |
} |
| 8361 |
|
| 8362 |
FS_API int fs_path_directory(char* pDst, size_t dstCap, const char* pPath, size_t pathLen) |
| 8363 |
{ |
| 8364 |
const char* pFileName; |
| 8365 |
|
| 8366 |
pFileName = fs_path_file_name(pPath, pathLen); |
| 8367 |
if (pFileName == NULL) { |
| 8368 |
return -1; |
| 8369 |
} |
| 8370 |
|
| 8371 |
if (pFileName == pPath) { |
| 8372 |
if (pDst != NULL && dstCap > 0) { |
| 8373 |
pDst[0] = '\0'; |
| 8374 |
} |
| 8375 |
|
| 8376 |
return 0; |
| 8377 |
} else { |
| 8378 |
const char* pDirEnd = pFileName - 1; |
| 8379 |
size_t dirLen = (size_t)(pDirEnd - pPath); |
| 8380 |
|
| 8381 |
if (pDst != NULL && dstCap > 0) { |
| 8382 |
size_t bytesToCopy = FS_MIN(dstCap - 1, dirLen); |
| 8383 |
if (bytesToCopy > 0 && pDst != pPath) { |
| 8384 |
FS_MOVE_MEMORY(pDst, pPath, bytesToCopy); |
| 8385 |
} |
| 8386 |
|
| 8387 |
pDst[bytesToCopy] = '\0'; |
| 8388 |
} |
| 8389 |
|
| 8390 |
if (dirLen > (size_t)-1) { |
| 8391 |
return -1; |
| 8392 |
} |
| 8393 |
|
| 8394 |
return (int)dirLen; |
| 8395 |
} |
| 8396 |
} |
| 8397 |
|
| 8398 |
FS_API const char* fs_path_extension(const char* pPath, size_t pathLen) |
| 8399 |
{ |
| 8400 |
const char* pDot = NULL; |
| 8401 |
const char* pLastSlash = NULL; |
| 8402 |
size_t i; |
| 8403 |
|
| 8404 |
if (pPath == NULL) { |
| 8405 |
return NULL; |
| 8406 |
} |
| 8407 |
|
| 8408 |
|
| 8409 |
for (i = 0; i < pathLen; ++i) { |
| 8410 |
if (pPath[i] == '\0') { |
| 8411 |
break; |
| 8412 |
} |
| 8413 |
|
| 8414 |
if (pPath[i] == '.') { |
| 8415 |
pDot = pPath + i; |
| 8416 |
} else if (pPath[i] == '\\' || pPath[i] == '/') { |
| 8417 |
pLastSlash = pPath + i; |
| 8418 |
} |
| 8419 |
} |
| 8420 |
|
| 8421 |
|
| 8422 |
if (pDot != NULL && pDot > pLastSlash) { |
| 8423 |
return pDot + 1; |
| 8424 |
} else { |
| 8425 |
return NULL; |
| 8426 |
} |
| 8427 |
} |
| 8428 |
|
| 8429 |
FS_API fs_bool32 fs_path_extension_equal(const char* pPath, size_t pathLen, const char* pExtension, size_t extensionLen) |
| 8430 |
{ |
| 8431 |
if (pPath == NULL || pExtension == NULL) { |
| 8432 |
return FS_FALSE; |
| 8433 |
} |
| 8434 |
|
| 8435 |
if (extensionLen == FS_NULL_TERMINATED) { |
| 8436 |
extensionLen = strlen(pExtension); |
| 8437 |
} |
| 8438 |
|
| 8439 |
if (pathLen == FS_NULL_TERMINATED) { |
| 8440 |
pathLen = strlen(pPath); |
| 8441 |
} |
| 8442 |
|
| 8443 |
if (extensionLen >= pathLen) { |
| 8444 |
return FS_FALSE; |
| 8445 |
} |
| 8446 |
|
| 8447 |
if (pPath[pathLen - extensionLen - 1] != '.') { |
| 8448 |
return FS_FALSE; |
| 8449 |
} |
| 8450 |
|
| 8451 |
return fs_strnicmp(pPath + pathLen - extensionLen, pExtension, extensionLen) == 0; |
| 8452 |
} |
| 8453 |
|
| 8454 |
FS_API const char* fs_path_trim_base(const char* pPath, size_t pathLen, const char* pBasePath, size_t basePathLen) |
| 8455 |
{ |
| 8456 |
fs_path_iterator iPath; |
| 8457 |
fs_path_iterator iBase; |
| 8458 |
fs_result result; |
| 8459 |
|
| 8460 |
if (basePathLen != FS_NULL_TERMINATED && pathLen < basePathLen) { |
| 8461 |
return NULL; |
| 8462 |
} |
| 8463 |
|
| 8464 |
if (basePathLen == 0 || pBasePath == NULL || pBasePath[0] == '\0') { |
| 8465 |
return pPath; |
| 8466 |
} |
| 8467 |
|
| 8468 |
result = fs_path_first(pPath, pathLen, &iPath); |
| 8469 |
if (result != FS_SUCCESS) { |
| 8470 |
return NULL; |
| 8471 |
} |
| 8472 |
|
| 8473 |
result = fs_path_first(pBasePath, basePathLen, &iBase); |
| 8474 |
if (result != FS_SUCCESS) { |
| 8475 |
return NULL; |
| 8476 |
} |
| 8477 |
|
| 8478 |
|
| 8479 |
for (;;) { |
| 8480 |
if (iPath.segmentLength != iBase.segmentLength || fs_strncmp(iPath.pFullPath + iPath.segmentOffset, iBase.pFullPath + iBase.segmentOffset, iPath.segmentLength) != 0) { |
| 8481 |
return NULL; |
| 8482 |
} |
| 8483 |
|
| 8484 |
result = fs_path_next(&iBase); |
| 8485 |
if (result != FS_SUCCESS || (iBase.segmentLength == 0 && fs_path_is_last(&iBase))) { |
| 8486 |
fs_path_next(&iPath); |
| 8487 |
break; |
| 8488 |
} |
| 8489 |
|
| 8490 |
result = fs_path_next(&iPath); |
| 8491 |
if (result != FS_SUCCESS) { |
| 8492 |
return NULL; |
| 8493 |
} |
| 8494 |
} |
| 8495 |
|
| 8496 |
|
| 8497 |
return iPath.pFullPath + iPath.segmentOffset; |
| 8498 |
} |
| 8499 |
|
| 8500 |
FS_API fs_bool32 fs_path_begins_with(const char* pPath, size_t pathLen, const char* pBasePath, size_t basePathLen) |
| 8501 |
{ |
| 8502 |
return fs_path_trim_base(pPath, pathLen, pBasePath, basePathLen) != NULL; |
| 8503 |
} |
| 8504 |
|
| 8505 |
FS_API int fs_path_append(char* pDst, size_t dstCap, const char* pBasePath, size_t basePathLen, const char* pPathToAppend, size_t pathToAppendLen) |
| 8506 |
{ |
| 8507 |
size_t dstLen = 0; |
| 8508 |
|
| 8509 |
if (pBasePath == NULL) { |
| 8510 |
pBasePath = ""; |
| 8511 |
basePathLen = 0; |
| 8512 |
} |
| 8513 |
|
| 8514 |
if (pPathToAppend == NULL) { |
| 8515 |
pPathToAppend = ""; |
| 8516 |
pathToAppendLen = 0; |
| 8517 |
} |
| 8518 |
|
| 8519 |
if (basePathLen == FS_NULL_TERMINATED) { |
| 8520 |
basePathLen = strlen(pBasePath); |
| 8521 |
} |
| 8522 |
|
| 8523 |
if (pathToAppendLen == FS_NULL_TERMINATED) { |
| 8524 |
pathToAppendLen = strlen(pPathToAppend); |
| 8525 |
} |
| 8526 |
|
| 8527 |
|
| 8528 |
|
| 8529 |
if (basePathLen > 0 && (pBasePath[basePathLen - 1] == '\\' || pBasePath[basePathLen - 1] == '/')) { |
| 8530 |
basePathLen -= 1; |
| 8531 |
} |
| 8532 |
|
| 8533 |
|
| 8534 |
|
| 8535 |
|
| 8536 |
|
| 8537 |
|
| 8538 |
if (basePathLen > 0) { |
| 8539 |
|
| 8540 |
if (pDst != NULL) { |
| 8541 |
size_t bytesToCopy = FS_MIN(basePathLen, dstCap); |
| 8542 |
|
| 8543 |
if (bytesToCopy > 0) { |
| 8544 |
if (bytesToCopy == dstCap) { |
| 8545 |
bytesToCopy -= 1; |
| 8546 |
} |
| 8547 |
|
| 8548 |
|
| 8549 |
if (pDst != pBasePath) { |
| 8550 |
FS_COPY_MEMORY(pDst, pBasePath, bytesToCopy); |
| 8551 |
} |
| 8552 |
} |
| 8553 |
|
| 8554 |
pDst += bytesToCopy; |
| 8555 |
dstCap -= bytesToCopy; |
| 8556 |
} |
| 8557 |
dstLen += basePathLen; |
| 8558 |
|
| 8559 |
|
| 8560 |
if (pDst != NULL) { |
| 8561 |
if (dstCap > 1) { |
| 8562 |
pDst[0] = '/'; |
| 8563 |
pDst += 1; |
| 8564 |
dstCap -= 1; |
| 8565 |
} |
| 8566 |
} |
| 8567 |
dstLen += 1; |
| 8568 |
} |
| 8569 |
|
| 8570 |
|
| 8571 |
|
| 8572 |
if (pDst != NULL) { |
| 8573 |
size_t bytesToCopy = FS_MIN(pathToAppendLen, dstCap); |
| 8574 |
|
| 8575 |
if (bytesToCopy > 0) { |
| 8576 |
if (bytesToCopy == dstCap) { |
| 8577 |
bytesToCopy -= 1; |
| 8578 |
} |
| 8579 |
|
| 8580 |
FS_COPY_MEMORY(pDst, pPathToAppend, bytesToCopy); |
| 8581 |
} |
| 8582 |
|
| 8583 |
pDst += bytesToCopy; |
| 8584 |
dstCap -= bytesToCopy; |
| 8585 |
} |
| 8586 |
dstLen += pathToAppendLen; |
| 8587 |
|
| 8588 |
|
| 8589 |
|
| 8590 |
if (pDst != NULL) { |
| 8591 |
if (dstCap > 0) { |
| 8592 |
pDst[0] = '\0'; |
| 8593 |
} |
| 8594 |
} |
| 8595 |
|
| 8596 |
|
| 8597 |
if (dstLen > 0x7FFFFFFF) { |
| 8598 |
return -1; |
| 8599 |
} |
| 8600 |
|
| 8601 |
return (int)dstLen; |
| 8602 |
} |
| 8603 |
|
| 8604 |
FS_API int fs_path_normalize(char* pDst, size_t dstCap, const char* pPath, size_t pathLen, unsigned int options) |
| 8605 |
{ |
| 8606 |
fs_path_iterator iPath; |
| 8607 |
fs_result result; |
| 8608 |
fs_bool32 allowLeadingBackNav = FS_TRUE; |
| 8609 |
fs_path_iterator stack[256]; |
| 8610 |
int top = 0; |
| 8611 |
int leadingBackNavCount = 0; |
| 8612 |
int dstLen = 0; |
| 8613 |
|
| 8614 |
if (pPath == NULL) { |
| 8615 |
pPath = ""; |
| 8616 |
pathLen = 0; |
| 8617 |
} |
| 8618 |
|
| 8619 |
if (pDst != NULL && dstCap > 0) { |
| 8620 |
pDst[0] = '\0'; |
| 8621 |
} |
| 8622 |
|
| 8623 |
|
| 8624 |
if (pathLen == 0 || pPath[0] == '\0') { |
| 8625 |
return 0; |
| 8626 |
} |
| 8627 |
|
| 8628 |
result = fs_path_first(pPath, pathLen, &iPath); |
| 8629 |
if (result != FS_SUCCESS) { |
| 8630 |
return -1; |
| 8631 |
} |
| 8632 |
|
| 8633 |
|
| 8634 |
if (iPath.segmentLength == 0) { |
| 8635 |
allowLeadingBackNav = FS_FALSE; |
| 8636 |
|
| 8637 |
if (pDst != NULL && dstCap > 0) { |
| 8638 |
pDst[0] = '/'; |
| 8639 |
pDst += 1; |
| 8640 |
dstCap -= 1; |
| 8641 |
} |
| 8642 |
dstLen += 1; |
| 8643 |
|
| 8644 |
|
| 8645 |
result = fs_path_next(&iPath); |
| 8646 |
if (result != FS_SUCCESS) { |
| 8647 |
return dstLen; |
| 8648 |
} |
| 8649 |
} |
| 8650 |
|
| 8651 |
if ((options & FS_NO_ABOVE_ROOT_NAVIGATION) != 0) { |
| 8652 |
allowLeadingBackNav = FS_FALSE; |
| 8653 |
} |
| 8654 |
|
| 8655 |
for (;;) { |
| 8656 |
|
| 8657 |
{ |
| 8658 |
if (iPath.segmentLength == 0 || (iPath.segmentLength == 1 && iPath.pFullPath[iPath.segmentOffset] == '.')) { |
| 8659 |
|
| 8660 |
goto next_segment; |
| 8661 |
} else if (iPath.segmentLength == 2 && iPath.pFullPath[iPath.segmentOffset] == '.' && iPath.pFullPath[iPath.segmentOffset + 1] == '.') { |
| 8662 |
|
| 8663 |
if (top > leadingBackNavCount) { |
| 8664 |
top -= 1; |
| 8665 |
goto next_segment; |
| 8666 |
} else { |
| 8667 |
|
| 8668 |
if (!allowLeadingBackNav) { |
| 8669 |
return -1; |
| 8670 |
} |
| 8671 |
|
| 8672 |
leadingBackNavCount += 1; |
| 8673 |
goto push_segment; |
| 8674 |
} |
| 8675 |
} else { |
| 8676 |
|
| 8677 |
goto push_segment; |
| 8678 |
} |
| 8679 |
} |
| 8680 |
|
| 8681 |
push_segment: |
| 8682 |
if (top < (int)FS_COUNTOF(stack)) { |
| 8683 |
stack[top] = iPath; |
| 8684 |
top += 1; |
| 8685 |
} else { |
| 8686 |
return -1; |
| 8687 |
} |
| 8688 |
|
| 8689 |
next_segment: |
| 8690 |
result = fs_path_next(&iPath); |
| 8691 |
if (result != FS_SUCCESS) { |
| 8692 |
break; |
| 8693 |
} |
| 8694 |
} |
| 8695 |
|
| 8696 |
|
| 8697 |
{ |
| 8698 |
int i = 0; |
| 8699 |
for (i = 0; i < top; i += 1) { |
| 8700 |
size_t segLen = stack[i].segmentLength; |
| 8701 |
|
| 8702 |
if (pDst != NULL && dstCap > segLen) { |
| 8703 |
FS_COPY_MEMORY(pDst, stack[i].pFullPath + stack[i].segmentOffset, segLen); |
| 8704 |
pDst += segLen; |
| 8705 |
dstCap -= segLen; |
| 8706 |
} |
| 8707 |
dstLen += (int)segLen; |
| 8708 |
|
| 8709 |
|
| 8710 |
if (i + 1 < top) { |
| 8711 |
if (pDst != NULL && dstCap > 0) { |
| 8712 |
pDst[0] = '/'; |
| 8713 |
pDst += 1; |
| 8714 |
dstCap -= 1; |
| 8715 |
} |
| 8716 |
dstLen += 1; |
| 8717 |
} |
| 8718 |
} |
| 8719 |
} |
| 8720 |
|
| 8721 |
|
| 8722 |
if (pDst != NULL && dstCap > 0) { |
| 8723 |
pDst[0] = '\0'; |
| 8724 |
} |
| 8725 |
|
| 8726 |
return dstLen; |
| 8727 |
} |
| 8728 |
|
| 8729 |
|
| 8730 |
|
| 8731 |
|
| 8732 |
|
| 8733 |
static fs_result fs_memory_stream_read_internal(fs_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 8734 |
{ |
| 8735 |
return fs_memory_stream_read((fs_memory_stream*)pStream, pDst, bytesToRead, pBytesRead); |
| 8736 |
} |
| 8737 |
|
| 8738 |
static fs_result fs_memory_stream_write_internal(fs_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 8739 |
{ |
| 8740 |
return fs_memory_stream_write((fs_memory_stream*)pStream, pSrc, bytesToWrite, pBytesWritten); |
| 8741 |
} |
| 8742 |
|
| 8743 |
static fs_result fs_memory_stream_seek_internal(fs_stream* pStream, fs_int64 offset, fs_seek_origin origin) |
| 8744 |
{ |
| 8745 |
return fs_memory_stream_seek((fs_memory_stream*)pStream, offset, origin); |
| 8746 |
} |
| 8747 |
|
| 8748 |
static fs_result fs_memory_stream_tell_internal(fs_stream* pStream, fs_int64* pCursor) |
| 8749 |
{ |
| 8750 |
fs_result result; |
| 8751 |
size_t cursor; |
| 8752 |
|
| 8753 |
result = fs_memory_stream_tell((fs_memory_stream*)pStream, &cursor); |
| 8754 |
if (result != FS_SUCCESS) { |
| 8755 |
return result; |
| 8756 |
} |
| 8757 |
|
| 8758 |
#if defined(__clang__) || defined(__GNUC__) |
| 8759 |
#pragma GCC diagnostic push |
| 8760 |
#if defined(__clang__) |
| 8761 |
#pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" |
| 8762 |
#elif defined(__GNUC__) |
| 8763 |
#pragma GCC diagnostic ignored "-Wtype-limits" |
| 8764 |
#endif |
| 8765 |
#endif |
| 8766 |
if (cursor > FS_INT64_MAX) { |
| 8767 |
return FS_ERROR; |
| 8768 |
} |
| 8769 |
#if defined(__clang__) || defined(__GNUC__) |
| 8770 |
#pragma GCC diagnostic pop |
| 8771 |
#endif |
| 8772 |
|
| 8773 |
*pCursor = (fs_int64)cursor; |
| 8774 |
|
| 8775 |
return FS_SUCCESS; |
| 8776 |
} |
| 8777 |
|
| 8778 |
static size_t fs_memory_stream_duplicate_alloc_size_internal(fs_stream* pStream) |
| 8779 |
{ |
| 8780 |
(void)pStream; |
| 8781 |
return sizeof(fs_memory_stream); |
| 8782 |
} |
| 8783 |
|
| 8784 |
static fs_result fs_memory_stream_duplicate_internal(fs_stream* pStream, fs_stream* pDuplicatedStream) |
| 8785 |
{ |
| 8786 |
fs_memory_stream* pMemoryStream; |
| 8787 |
|
| 8788 |
pMemoryStream = (fs_memory_stream*)pStream; |
| 8789 |
FS_ASSERT(pMemoryStream != NULL); |
| 8790 |
|
| 8791 |
*pDuplicatedStream = *pStream; |
| 8792 |
|
| 8793 |
|
| 8794 |
if (pMemoryStream->write.pData != NULL) { |
| 8795 |
void* pNewData = fs_malloc(pMemoryStream->write.dataCap, &pMemoryStream->allocationCallbacks); |
| 8796 |
if (pNewData == NULL) { |
| 8797 |
return FS_OUT_OF_MEMORY; |
| 8798 |
} |
| 8799 |
|
| 8800 |
FS_COPY_MEMORY(pNewData, pMemoryStream->write.pData, pMemoryStream->write.dataSize); |
| 8801 |
|
| 8802 |
pMemoryStream->write.pData = pNewData; |
| 8803 |
|
| 8804 |
pMemoryStream->ppData = &pMemoryStream->write.pData; |
| 8805 |
pMemoryStream->pDataSize = &pMemoryStream->write.dataSize; |
| 8806 |
} else { |
| 8807 |
pMemoryStream->ppData = (void**)&pMemoryStream->readonly.pData; |
| 8808 |
pMemoryStream->pDataSize = &pMemoryStream->readonly.dataSize; |
| 8809 |
} |
| 8810 |
|
| 8811 |
return FS_SUCCESS; |
| 8812 |
} |
| 8813 |
|
| 8814 |
static void fs_memory_stream_uninit_internal(fs_stream* pStream) |
| 8815 |
{ |
| 8816 |
fs_memory_stream_uninit((fs_memory_stream*)pStream); |
| 8817 |
} |
| 8818 |
|
| 8819 |
static fs_stream_vtable fs_gStreamVTableMemory = |
| 8820 |
{ |
| 8821 |
fs_memory_stream_read_internal, |
| 8822 |
fs_memory_stream_write_internal, |
| 8823 |
fs_memory_stream_seek_internal, |
| 8824 |
fs_memory_stream_tell_internal, |
| 8825 |
fs_memory_stream_duplicate_alloc_size_internal, |
| 8826 |
fs_memory_stream_duplicate_internal, |
| 8827 |
fs_memory_stream_uninit_internal |
| 8828 |
}; |
| 8829 |
|
| 8830 |
|
| 8831 |
FS_API fs_result fs_memory_stream_init_write(const fs_allocation_callbacks* pAllocationCallbacks, fs_memory_stream* pStream) |
| 8832 |
{ |
| 8833 |
fs_result result; |
| 8834 |
|
| 8835 |
if (pStream == NULL) { |
| 8836 |
return FS_INVALID_ARGS; |
| 8837 |
} |
| 8838 |
|
| 8839 |
FS_ZERO_OBJECT(pStream); |
| 8840 |
|
| 8841 |
result = fs_stream_init(&fs_gStreamVTableMemory, &pStream->base); |
| 8842 |
if (result != FS_SUCCESS) { |
| 8843 |
return result; |
| 8844 |
} |
| 8845 |
|
| 8846 |
pStream->write.pData = NULL; |
| 8847 |
pStream->write.dataSize = 0; |
| 8848 |
pStream->write.dataCap = 0; |
| 8849 |
pStream->allocationCallbacks = fs_allocation_callbacks_init_copy(pAllocationCallbacks); |
| 8850 |
|
| 8851 |
pStream->ppData = &pStream->write.pData; |
| 8852 |
pStream->pDataSize = &pStream->write.dataSize; |
| 8853 |
|
| 8854 |
return FS_SUCCESS; |
| 8855 |
} |
| 8856 |
|
| 8857 |
FS_API fs_result fs_memory_stream_init_readonly(const void* pData, size_t dataSize, fs_memory_stream* pStream) |
| 8858 |
{ |
| 8859 |
fs_result result; |
| 8860 |
|
| 8861 |
if (pStream == NULL) { |
| 8862 |
return FS_INVALID_ARGS; |
| 8863 |
} |
| 8864 |
|
| 8865 |
FS_ZERO_OBJECT(pStream); |
| 8866 |
|
| 8867 |
if (pData == NULL) { |
| 8868 |
return FS_INVALID_ARGS; |
| 8869 |
} |
| 8870 |
|
| 8871 |
result = fs_stream_init(&fs_gStreamVTableMemory, &pStream->base); |
| 8872 |
if (result != FS_SUCCESS) { |
| 8873 |
return result; |
| 8874 |
} |
| 8875 |
|
| 8876 |
pStream->readonly.pData = pData; |
| 8877 |
pStream->readonly.dataSize = dataSize; |
| 8878 |
|
| 8879 |
pStream->ppData = (void**)&pStream->readonly.pData; |
| 8880 |
pStream->pDataSize = &pStream->readonly.dataSize; |
| 8881 |
|
| 8882 |
return FS_SUCCESS; |
| 8883 |
} |
| 8884 |
|
| 8885 |
FS_API void fs_memory_stream_uninit(fs_memory_stream* pStream) |
| 8886 |
{ |
| 8887 |
if (pStream == NULL) { |
| 8888 |
return; |
| 8889 |
} |
| 8890 |
|
| 8891 |
if (pStream->write.pData != NULL) { |
| 8892 |
fs_free(pStream->write.pData, &pStream->allocationCallbacks); |
| 8893 |
} |
| 8894 |
} |
| 8895 |
|
| 8896 |
FS_API fs_result fs_memory_stream_read(fs_memory_stream* pStream, void* pDst, size_t bytesToRead, size_t* pBytesRead) |
| 8897 |
{ |
| 8898 |
size_t bytesAvailable; |
| 8899 |
size_t bytesRead; |
| 8900 |
|
| 8901 |
if (pBytesRead != NULL) { |
| 8902 |
*pBytesRead = 0; |
| 8903 |
} |
| 8904 |
|
| 8905 |
if (pStream == NULL) { |
| 8906 |
return FS_INVALID_ARGS; |
| 8907 |
} |
| 8908 |
|
| 8909 |
FS_ASSERT(pStream->cursor <= *pStream->pDataSize); |
| 8910 |
|
| 8911 |
bytesAvailable = *pStream->pDataSize - pStream->cursor; |
| 8912 |
if (bytesAvailable == 0) { |
| 8913 |
return FS_AT_END; |
| 8914 |
} |
| 8915 |
|
| 8916 |
bytesRead = FS_MIN(bytesAvailable, bytesToRead); |
| 8917 |
|
| 8918 |
|
| 8919 |
if (pDst != NULL) { |
| 8920 |
FS_COPY_MEMORY(pDst, FS_OFFSET_PTR(*pStream->ppData, pStream->cursor), bytesRead); |
| 8921 |
} |
| 8922 |
|
| 8923 |
pStream->cursor += bytesRead; |
| 8924 |
|
| 8925 |
if (pBytesRead != NULL) { |
| 8926 |
*pBytesRead = bytesRead; |
| 8927 |
} |
| 8928 |
|
| 8929 |
return FS_SUCCESS; |
| 8930 |
} |
| 8931 |
|
| 8932 |
FS_API fs_result fs_memory_stream_write(fs_memory_stream* pStream, const void* pSrc, size_t bytesToWrite, size_t* pBytesWritten) |
| 8933 |
{ |
| 8934 |
size_t writeEndPosition; |
| 8935 |
size_t newDataSize; |
| 8936 |
|
| 8937 |
if (pBytesWritten != NULL) { |
| 8938 |
*pBytesWritten = 0; |
| 8939 |
} |
| 8940 |
|
| 8941 |
if (pStream == NULL || pSrc == NULL) { |
| 8942 |
return FS_INVALID_ARGS; |
| 8943 |
} |
| 8944 |
|
| 8945 |
|
| 8946 |
if (pStream->readonly.pData != NULL) { |
| 8947 |
return FS_INVALID_OPERATION; |
| 8948 |
} |
| 8949 |
|
| 8950 |
|
| 8951 |
writeEndPosition = pStream->cursor + bytesToWrite; |
| 8952 |
if (writeEndPosition > pStream->write.dataCap) { |
| 8953 |
void* pNewBuffer; |
| 8954 |
size_t newCap; |
| 8955 |
|
| 8956 |
newCap = FS_MAX(writeEndPosition, pStream->write.dataCap * 2); |
| 8957 |
pNewBuffer = fs_realloc(*pStream->ppData, newCap, &pStream->allocationCallbacks); |
| 8958 |
if (pNewBuffer == NULL) { |
| 8959 |
return FS_OUT_OF_MEMORY; |
| 8960 |
} |
| 8961 |
|
| 8962 |
*pStream->ppData = pNewBuffer; |
| 8963 |
pStream->write.dataCap = newCap; |
| 8964 |
} |
| 8965 |
|
| 8966 |
FS_ASSERT(writeEndPosition <= pStream->write.dataCap); |
| 8967 |
|
| 8968 |
|
| 8969 |
FS_COPY_MEMORY(FS_OFFSET_PTR(*pStream->ppData, pStream->cursor), pSrc, bytesToWrite); |
| 8970 |
pStream->cursor = writeEndPosition; |
| 8971 |
|
| 8972 |
|
| 8973 |
newDataSize = FS_MAX(*pStream->pDataSize, writeEndPosition); |
| 8974 |
*pStream->pDataSize = newDataSize; |
| 8975 |
|
| 8976 |
if (pBytesWritten != NULL) { |
| 8977 |
*pBytesWritten = bytesToWrite; |
| 8978 |
} |
| 8979 |
|
| 8980 |
return FS_SUCCESS; |
| 8981 |
} |
| 8982 |
|
| 8983 |
FS_API fs_result fs_memory_stream_seek(fs_memory_stream* pStream, fs_int64 offset, int origin) |
| 8984 |
{ |
| 8985 |
fs_int64 newCursor; |
| 8986 |
|
| 8987 |
if (pStream == NULL) { |
| 8988 |
return FS_INVALID_ARGS; |
| 8989 |
} |
| 8990 |
|
| 8991 |
if ((fs_uint64)FS_ABS(offset) > FS_SIZE_MAX) { |
| 8992 |
return FS_INVALID_ARGS; |
| 8993 |
} |
| 8994 |
|
| 8995 |
if (origin == FS_SEEK_SET) { |
| 8996 |
newCursor = 0; |
| 8997 |
} else if (origin == FS_SEEK_CUR) { |
| 8998 |
newCursor = (fs_int64)pStream->cursor; |
| 8999 |
} else if (origin == FS_SEEK_END) { |
| 9000 |
newCursor = (fs_int64)*pStream->pDataSize; |
| 9001 |
} else { |
| 9002 |
FS_ASSERT(!"Invalid seek origin"); |
| 9003 |
return FS_INVALID_ARGS; |
| 9004 |
} |
| 9005 |
|
| 9006 |
newCursor += offset; |
| 9007 |
|
| 9008 |
if (newCursor < 0) { |
| 9009 |
return FS_BAD_SEEK; |
| 9010 |
} |
| 9011 |
if ((size_t)newCursor > *pStream->pDataSize) { |
| 9012 |
return FS_BAD_SEEK; |
| 9013 |
} |
| 9014 |
|
| 9015 |
pStream->cursor = (size_t)newCursor; |
| 9016 |
|
| 9017 |
return FS_SUCCESS; |
| 9018 |
} |
| 9019 |
|
| 9020 |
FS_API fs_result fs_memory_stream_tell(fs_memory_stream* pStream, size_t* pCursor) |
| 9021 |
{ |
| 9022 |
if (pCursor == NULL) { |
| 9023 |
return FS_INVALID_ARGS; |
| 9024 |
} |
| 9025 |
|
| 9026 |
*pCursor = 0; |
| 9027 |
|
| 9028 |
if (pStream == NULL) { |
| 9029 |
return FS_INVALID_ARGS; |
| 9030 |
} |
| 9031 |
|
| 9032 |
*pCursor = pStream->cursor; |
| 9033 |
|
| 9034 |
return FS_SUCCESS; |
| 9035 |
} |
| 9036 |
|
| 9037 |
FS_API fs_result fs_memory_stream_remove(fs_memory_stream* pStream, size_t offset, size_t size) |
| 9038 |
{ |
| 9039 |
void* pDst; |
| 9040 |
void* pSrc; |
| 9041 |
size_t tailSize; |
| 9042 |
|
| 9043 |
if (pStream == NULL) { |
| 9044 |
return FS_INVALID_ARGS; |
| 9045 |
} |
| 9046 |
|
| 9047 |
if ((offset + size) > *pStream->pDataSize) { |
| 9048 |
return FS_INVALID_ARGS; |
| 9049 |
} |
| 9050 |
|
| 9051 |
|
| 9052 |
if (pStream->cursor > offset) { |
| 9053 |
if (pStream->cursor >= (offset + size)) { |
| 9054 |
pStream->cursor -= size; |
| 9055 |
} else { |
| 9056 |
pStream->cursor = offset; |
| 9057 |
} |
| 9058 |
} |
| 9059 |
|
| 9060 |
pDst = FS_OFFSET_PTR(*pStream->ppData, offset); |
| 9061 |
pSrc = FS_OFFSET_PTR(*pStream->ppData, offset + size); |
| 9062 |
tailSize = *pStream->pDataSize - (offset + size); |
| 9063 |
|
| 9064 |
FS_MOVE_MEMORY(pDst, pSrc, tailSize); |
| 9065 |
*pStream->pDataSize -= size; |
| 9066 |
|
| 9067 |
return FS_SUCCESS; |
| 9068 |
} |
| 9069 |
|
| 9070 |
FS_API fs_result fs_memory_stream_truncate(fs_memory_stream* pStream) |
| 9071 |
{ |
| 9072 |
if (pStream == NULL) { |
| 9073 |
return FS_INVALID_ARGS; |
| 9074 |
} |
| 9075 |
|
| 9076 |
return fs_memory_stream_remove(pStream, pStream->cursor, (*pStream->pDataSize - pStream->cursor)); |
| 9077 |
} |
| 9078 |
|
| 9079 |
FS_API void* fs_memory_stream_take_ownership(fs_memory_stream* pStream, size_t* pSize) |
| 9080 |
{ |
| 9081 |
void* pData; |
| 9082 |
|
| 9083 |
if (pStream == NULL) { |
| 9084 |
return NULL; |
| 9085 |
} |
| 9086 |
|
| 9087 |
pData = *pStream->ppData; |
| 9088 |
if (pSize != NULL) { |
| 9089 |
*pSize = *pStream->pDataSize; |
| 9090 |
} |
| 9091 |
|
| 9092 |
pStream->write.pData = NULL; |
| 9093 |
pStream->write.dataSize = 0; |
| 9094 |
pStream->write.dataCap = 0; |
| 9095 |
|
| 9096 |
return pData; |
| 9097 |
|
| 9098 |
} |
| 9099 |
|
| 9100 |
|
| 9101 |
|
| 9102 |
|
| 9103 |
|
| 9104 |
static FS_INLINE void fs_swap(void* a, void* b, size_t sz) |
| 9105 |
{ |
| 9106 |
char* _a = (char*)a; |
| 9107 |
char* _b = (char*)b; |
| 9108 |
|
| 9109 |
while (sz > 0) { |
| 9110 |
char temp = *_a; |
| 9111 |
*_a++ = *_b; |
| 9112 |
*_b++ = temp; |
| 9113 |
sz -= 1; |
| 9114 |
} |
| 9115 |
} |
| 9116 |
|
| 9117 |
FS_API void fs_sort(void* pBase, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData) |
| 9118 |
{ |
| 9119 |
|
| 9120 |
size_t i; |
| 9121 |
size_t j; |
| 9122 |
|
| 9123 |
for (i = 1; i < count; i += 1) { |
| 9124 |
for (j = i; j > 0; j -= 1) { |
| 9125 |
void* pA = (char*)pBase + (j - 1) * stride; |
| 9126 |
void* pB = (char*)pBase + j * stride; |
| 9127 |
|
| 9128 |
if (compareProc(pUserData, pA, pB) <= 0) { |
| 9129 |
break; |
| 9130 |
} |
| 9131 |
|
| 9132 |
fs_swap(pA, pB, stride); |
| 9133 |
} |
| 9134 |
} |
| 9135 |
} |
| 9136 |
|
| 9137 |
FS_API void* fs_binary_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData) |
| 9138 |
{ |
| 9139 |
size_t iStart; |
| 9140 |
size_t iEnd; |
| 9141 |
size_t iMid; |
| 9142 |
|
| 9143 |
if (count == 0) { |
| 9144 |
return NULL; |
| 9145 |
} |
| 9146 |
|
| 9147 |
iStart = 0; |
| 9148 |
iEnd = count - 1; |
| 9149 |
|
| 9150 |
while (iStart <= iEnd) { |
| 9151 |
int compareResult; |
| 9152 |
|
| 9153 |
iMid = iStart + (iEnd - iStart) / 2; |
| 9154 |
|
| 9155 |
compareResult = compareProc(pUserData, pKey, (char*)pList + (iMid * stride)); |
| 9156 |
if (compareResult < 0) { |
| 9157 |
iEnd = iMid - 1; |
| 9158 |
} else if (compareResult > 0) { |
| 9159 |
iStart = iMid + 1; |
| 9160 |
} else { |
| 9161 |
return (void*)((char*)pList + (iMid * stride)); |
| 9162 |
} |
| 9163 |
} |
| 9164 |
|
| 9165 |
return NULL; |
| 9166 |
} |
| 9167 |
|
| 9168 |
FS_API void* fs_linear_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData) |
| 9169 |
{ |
| 9170 |
size_t i; |
| 9171 |
|
| 9172 |
for (i = 0; i < count; i+= 1) { |
| 9173 |
int compareResult = compareProc(pUserData, pKey, (char*)pList + (i * stride)); |
| 9174 |
if (compareResult == 0) { |
| 9175 |
return (void*)((char*)pList + (i * stride)); |
| 9176 |
} |
| 9177 |
} |
| 9178 |
|
| 9179 |
return NULL; |
| 9180 |
} |
| 9181 |
|
| 9182 |
FS_API void* fs_sorted_search(const void* pKey, const void* pList, size_t count, size_t stride, int (*compareProc)(void*, const void*, const void*), void* pUserData) |
| 9183 |
{ |
| 9184 |
const size_t threshold = 10; |
| 9185 |
|
| 9186 |
if (count < threshold) { |
| 9187 |
return fs_linear_search(pKey, pList, count, stride, compareProc, pUserData); |
| 9188 |
} else { |
| 9189 |
return fs_binary_search(pKey, pList, count, stride, compareProc, pUserData); |
| 9190 |
} |
| 9191 |
} |
| 9192 |
|
| 9193 |
|
| 9194 |
|
| 9195 |
|
| 9196 |
|
| 9197 |
|
| 9198 |
|
| 9199 |
|
| 9200 |
typedef char* fs_sprintf_callback(const char* buf, void* user, size_t len); |
| 9201 |
|
| 9202 |
|
| 9203 |
|
| 9204 |
|
| 9205 |
|
| 9206 |
#ifndef FS_SPRINTF_NOUNALIGNED |
| 9207 |
#define FS_SPRINTF_NOUNALIGNED |
| 9208 |
#endif |
| 9209 |
|
| 9210 |
|
| 9211 |
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) |
| 9212 |
#pragma GCC diagnostic push |
| 9213 |
#pragma GCC diagnostic ignored "-Wlong-long" |
| 9214 |
#endif |
| 9215 |
|
| 9216 |
|
| 9217 |
#if defined(__GNUC__) && (__GNUC__ >= 7 || (__GNUC__ == 6 && __GNUC_MINOR__ >= 1)) |
| 9218 |
#pragma GCC diagnostic push |
| 9219 |
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" |
| 9220 |
#endif |
| 9221 |
|
| 9222 |
|
| 9223 |
#if defined(__clang__) |
| 9224 |
#if defined(__has_feature) && defined(__has_attribute) |
| 9225 |
#if __has_feature(address_sanitizer) |
| 9226 |
#if __has_attribute(__no_sanitize__) |
| 9227 |
#define FS_ASAN __attribute__((__no_sanitize__("address"))) |
| 9228 |
#elif __has_attribute(__no_sanitize_address__) |
| 9229 |
#define FS_ASAN __attribute__((__no_sanitize_address__)) |
| 9230 |
#elif __has_attribute(__no_address_safety_analysis__) |
| 9231 |
#define FS_ASAN __attribute__((__no_address_safety_analysis__)) |
| 9232 |
#endif |
| 9233 |
#endif |
| 9234 |
#endif |
| 9235 |
#elif defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) |
| 9236 |
#if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ |
| 9237 |
#define FS_ASAN __attribute__((__no_sanitize_address__)) |
| 9238 |
#endif |
| 9239 |
#elif defined(_MSC_VER) |
| 9240 |
#if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ |
| 9241 |
#define FS_ASAN __declspec(no_sanitize_address) |
| 9242 |
#endif |
| 9243 |
#endif |
| 9244 |
|
| 9245 |
#ifndef FS_ASAN |
| 9246 |
#define FS_ASAN |
| 9247 |
#endif |
| 9248 |
|
| 9249 |
#ifndef FS_API_SPRINTF_DEF |
| 9250 |
#define FS_API_SPRINTF_DEF FS_API FS_ASAN |
| 9251 |
#endif |
| 9252 |
|
| 9253 |
#ifndef FS_SPRINTF_MIN |
| 9254 |
#define FS_SPRINTF_MIN 512 |
| 9255 |
#endif |
| 9256 |
|
| 9257 |
#ifndef FS_SPRINTF_MSVC_MODE |
| 9258 |
#if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 9259 |
#define FS_SPRINTF_MSVC_MODE |
| 9260 |
#endif |
| 9261 |
#endif |
| 9262 |
|
| 9263 |
#ifdef FS_SPRINTF_NOUNALIGNED |
| 9264 |
#define FS_UNALIGNED(code) |
| 9265 |
#else |
| 9266 |
#define FS_UNALIGNED(code) code |
| 9267 |
#endif |
| 9268 |
|
| 9269 |
#ifndef FS_SPRINTF_NOFLOAT |
| 9270 |
|
| 9271 |
static fs_int32 fs_real_to_str(char const* *start, fs_uint32 *len, char* out, fs_int32 *decimal_pos, double value, fs_uint32 frac_digits); |
| 9272 |
static fs_int32 fs_real_to_parts(fs_int64 *bits, fs_int32 *expo, double value); |
| 9273 |
#define FS_SPECIAL 0x7000 |
| 9274 |
#endif |
| 9275 |
|
| 9276 |
static char fs_period = '.'; |
| 9277 |
static char fs_comma = ','; |
| 9278 |
static struct |
| 9279 |
{ |
| 9280 |
short temp; |
| 9281 |
char pair[201]; |
| 9282 |
} fs_digitpair = |
| 9283 |
{ |
| 9284 |
0, |
| 9285 |
"00010203040506070809101112131415161718192021222324" |
| 9286 |
"25262728293031323334353637383940414243444546474849" |
| 9287 |
"50515253545556575859606162636465666768697071727374" |
| 9288 |
"75767778798081828384858687888990919293949596979899" |
| 9289 |
}; |
| 9290 |
|
| 9291 |
FS_API_SPRINTF_DEF void fs_set_sprintf_separators(char pcomma, char pperiod) |
| 9292 |
{ |
| 9293 |
fs_period = pperiod; |
| 9294 |
fs_comma = pcomma; |
| 9295 |
} |
| 9296 |
|
| 9297 |
#define FS_LEFTJUST 1 |
| 9298 |
#define FS_LEADINGPLUS 2 |
| 9299 |
#define FS_LEADINGSPACE 4 |
| 9300 |
#define FS_LEADING_0X 8 |
| 9301 |
#define FS_LEADINGZERO 16 |
| 9302 |
#define FS_INTMAX 32 |
| 9303 |
#define FS_TRIPLET_COMMA 64 |
| 9304 |
#define FS_NEGATIVE 128 |
| 9305 |
#define FS_METRIC_SUFFIX 256 |
| 9306 |
#define FS_HALFWIDTH 512 |
| 9307 |
#define FS_METRIC_NOSPACE 1024 |
| 9308 |
#define FS_METRIC_1024 2048 |
| 9309 |
#define FS_METRIC_JEDEC 4096 |
| 9310 |
|
| 9311 |
static void fs_lead_sign(fs_uint32 fl, char* sign) |
| 9312 |
{ |
| 9313 |
sign[0] = 0; |
| 9314 |
if (fl & FS_NEGATIVE) { |
| 9315 |
sign[0] = 1; |
| 9316 |
sign[1] = '-'; |
| 9317 |
} else if (fl & FS_LEADINGSPACE) { |
| 9318 |
sign[0] = 1; |
| 9319 |
sign[1] = ' '; |
| 9320 |
} else if (fl & FS_LEADINGPLUS) { |
| 9321 |
sign[0] = 1; |
| 9322 |
sign[1] = '+'; |
| 9323 |
} |
| 9324 |
} |
| 9325 |
|
| 9326 |
static FS_ASAN fs_uint32 fs_strlen_limited(char const* s, fs_uint32 limit) |
| 9327 |
{ |
| 9328 |
char const* sn = s; |
| 9329 |
|
| 9330 |
|
| 9331 |
for (;;) { |
| 9332 |
if (((fs_uintptr)sn & 3) == 0) |
| 9333 |
break; |
| 9334 |
|
| 9335 |
if (!limit || *sn == 0) |
| 9336 |
return (fs_uint32)(sn - s); |
| 9337 |
|
| 9338 |
++sn; |
| 9339 |
--limit; |
| 9340 |
} |
| 9341 |
|
| 9342 |
|
| 9343 |
|
| 9344 |
|
| 9345 |
|
| 9346 |
|
| 9347 |
while (limit >= 4) { |
| 9348 |
fs_uint32 v = *(fs_uint32 *)sn; |
| 9349 |
|
| 9350 |
if ((v - 0x01010101) & (~v) & 0x80808080UL) |
| 9351 |
break; |
| 9352 |
|
| 9353 |
sn += 4; |
| 9354 |
limit -= 4; |
| 9355 |
} |
| 9356 |
|
| 9357 |
|
| 9358 |
while (limit && *sn) { |
| 9359 |
++sn; |
| 9360 |
--limit; |
| 9361 |
} |
| 9362 |
|
| 9363 |
return (fs_uint32)(sn - s); |
| 9364 |
} |
| 9365 |
|
| 9366 |
FS_API_SPRINTF_DEF int fs_vsprintfcb(fs_sprintf_callback* callback, void* user, char* buf, char const* fmt, va_list va) |
| 9367 |
{ |
| 9368 |
static char hex[] = "0123456789abcdefxp"; |
| 9369 |
static char hexu[] = "0123456789ABCDEFXP"; |
| 9370 |
char* bf; |
| 9371 |
char const* f; |
| 9372 |
int tlen = 0; |
| 9373 |
|
| 9374 |
bf = buf; |
| 9375 |
f = fmt; |
| 9376 |
for (;;) { |
| 9377 |
fs_int32 fw, pr, tz; |
| 9378 |
fs_uint32 fl; |
| 9379 |
|
| 9380 |
|
| 9381 |
#define fs_chk_cb_bufL(bytes) \ |
| 9382 |
{ \ |
| 9383 |
int len = (int)(bf - buf); \ |
| 9384 |
if ((len + (bytes)) >= FS_SPRINTF_MIN) { \ |
| 9385 |
tlen += len; \ |
| 9386 |
if (0 == (bf = buf = callback(buf, user, len))) \ |
| 9387 |
goto done; \ |
| 9388 |
} \ |
| 9389 |
} |
| 9390 |
#define fs_chk_cb_buf(bytes) \ |
| 9391 |
{ \ |
| 9392 |
if (callback) { \ |
| 9393 |
fs_chk_cb_bufL(bytes); \ |
| 9394 |
} \ |
| 9395 |
} |
| 9396 |
#define fs_flush_cb() \ |
| 9397 |
{ \ |
| 9398 |
fs_chk_cb_bufL(FS_SPRINTF_MIN - 1); \ |
| 9399 |
} |
| 9400 |
#define fs_cb_buf_clamp(cl, v) \ |
| 9401 |
cl = v; \ |
| 9402 |
if (callback) { \ |
| 9403 |
int lg = FS_SPRINTF_MIN - (int)(bf - buf); \ |
| 9404 |
if (cl > lg) \ |
| 9405 |
cl = lg; \ |
| 9406 |
} |
| 9407 |
|
| 9408 |
|
| 9409 |
for (;;) { |
| 9410 |
while (((fs_uintptr)f) & 3) { |
| 9411 |
schk1: |
| 9412 |
if (f[0] == '%') |
| 9413 |
goto scandd; |
| 9414 |
schk2: |
| 9415 |
if (f[0] == 0) |
| 9416 |
goto endfmt; |
| 9417 |
fs_chk_cb_buf(1); |
| 9418 |
*bf++ = f[0]; |
| 9419 |
++f; |
| 9420 |
} |
| 9421 |
for (;;) { |
| 9422 |
|
| 9423 |
|
| 9424 |
|
| 9425 |
fs_uint32 v, c; |
| 9426 |
v = *(fs_uint32 *)f; |
| 9427 |
c = (~v) & 0x80808080; |
| 9428 |
if (((v ^ 0x25252525) - 0x01010101) & c) |
| 9429 |
goto schk1; |
| 9430 |
if ((v - 0x01010101) & c) |
| 9431 |
goto schk2; |
| 9432 |
if (callback) |
| 9433 |
if ((FS_SPRINTF_MIN - (int)(bf - buf)) < 4) |
| 9434 |
goto schk1; |
| 9435 |
#ifdef FS_SPRINTF_NOUNALIGNED |
| 9436 |
if(((fs_uintptr)bf) & 3) { |
| 9437 |
bf[0] = f[0]; |
| 9438 |
bf[1] = f[1]; |
| 9439 |
bf[2] = f[2]; |
| 9440 |
bf[3] = f[3]; |
| 9441 |
} else |
| 9442 |
#endif |
| 9443 |
{ |
| 9444 |
*(fs_uint32 *)bf = v; |
| 9445 |
} |
| 9446 |
bf += 4; |
| 9447 |
f += 4; |
| 9448 |
} |
| 9449 |
} |
| 9450 |
scandd: |
| 9451 |
|
| 9452 |
++f; |
| 9453 |
|
| 9454 |
|
| 9455 |
fw = 0; |
| 9456 |
pr = -1; |
| 9457 |
fl = 0; |
| 9458 |
tz = 0; |
| 9459 |
|
| 9460 |
|
| 9461 |
for (;;) { |
| 9462 |
switch (f[0]) { |
| 9463 |
|
| 9464 |
case '-': |
| 9465 |
fl |= FS_LEFTJUST; |
| 9466 |
++f; |
| 9467 |
continue; |
| 9468 |
|
| 9469 |
case '+': |
| 9470 |
fl |= FS_LEADINGPLUS; |
| 9471 |
++f; |
| 9472 |
continue; |
| 9473 |
|
| 9474 |
case ' ': |
| 9475 |
fl |= FS_LEADINGSPACE; |
| 9476 |
++f; |
| 9477 |
continue; |
| 9478 |
|
| 9479 |
case '#': |
| 9480 |
fl |= FS_LEADING_0X; |
| 9481 |
++f; |
| 9482 |
continue; |
| 9483 |
|
| 9484 |
case '\'': |
| 9485 |
fl |= FS_TRIPLET_COMMA; |
| 9486 |
++f; |
| 9487 |
continue; |
| 9488 |
|
| 9489 |
case '$': |
| 9490 |
if (fl & FS_METRIC_SUFFIX) { |
| 9491 |
if (fl & FS_METRIC_1024) { |
| 9492 |
fl |= FS_METRIC_JEDEC; |
| 9493 |
} else { |
| 9494 |
fl |= FS_METRIC_1024; |
| 9495 |
} |
| 9496 |
} else { |
| 9497 |
fl |= FS_METRIC_SUFFIX; |
| 9498 |
} |
| 9499 |
++f; |
| 9500 |
continue; |
| 9501 |
|
| 9502 |
case '_': |
| 9503 |
fl |= FS_METRIC_NOSPACE; |
| 9504 |
++f; |
| 9505 |
continue; |
| 9506 |
|
| 9507 |
case '0': |
| 9508 |
fl |= FS_LEADINGZERO; |
| 9509 |
++f; |
| 9510 |
goto flags_done; |
| 9511 |
default: goto flags_done; |
| 9512 |
} |
| 9513 |
} |
| 9514 |
flags_done: |
| 9515 |
|
| 9516 |
|
| 9517 |
if (f[0] == '*') { |
| 9518 |
fw = va_arg(va, fs_uint32); |
| 9519 |
++f; |
| 9520 |
} else { |
| 9521 |
while ((f[0] >= '0') && (f[0] <= '9')) { |
| 9522 |
fw = fw * 10 + f[0] - '0'; |
| 9523 |
f++; |
| 9524 |
} |
| 9525 |
} |
| 9526 |
|
| 9527 |
if (f[0] == '.') { |
| 9528 |
++f; |
| 9529 |
if (f[0] == '*') { |
| 9530 |
pr = va_arg(va, fs_uint32); |
| 9531 |
++f; |
| 9532 |
} else { |
| 9533 |
pr = 0; |
| 9534 |
while ((f[0] >= '0') && (f[0] <= '9')) { |
| 9535 |
pr = pr * 10 + f[0] - '0'; |
| 9536 |
f++; |
| 9537 |
} |
| 9538 |
} |
| 9539 |
} |
| 9540 |
|
| 9541 |
|
| 9542 |
switch (f[0]) { |
| 9543 |
|
| 9544 |
case 'h': |
| 9545 |
fl |= FS_HALFWIDTH; |
| 9546 |
++f; |
| 9547 |
if (f[0] == 'h') |
| 9548 |
++f; |
| 9549 |
break; |
| 9550 |
|
| 9551 |
case 'l': |
| 9552 |
fl |= ((sizeof(long) == 8) ? FS_INTMAX : 0); |
| 9553 |
++f; |
| 9554 |
if (f[0] == 'l') { |
| 9555 |
fl |= FS_INTMAX; |
| 9556 |
++f; |
| 9557 |
} |
| 9558 |
break; |
| 9559 |
|
| 9560 |
case 'j': |
| 9561 |
fl |= (sizeof(size_t) == 8) ? FS_INTMAX : 0; |
| 9562 |
++f; |
| 9563 |
break; |
| 9564 |
|
| 9565 |
case 'z': |
| 9566 |
fl |= (sizeof(ptrdiff_t) == 8) ? FS_INTMAX : 0; |
| 9567 |
++f; |
| 9568 |
break; |
| 9569 |
case 't': |
| 9570 |
fl |= (sizeof(ptrdiff_t) == 8) ? FS_INTMAX : 0; |
| 9571 |
++f; |
| 9572 |
break; |
| 9573 |
|
| 9574 |
case 'I': |
| 9575 |
if ((f[1] == '6') && (f[2] == '4')) { |
| 9576 |
fl |= FS_INTMAX; |
| 9577 |
f += 3; |
| 9578 |
} else if ((f[1] == '3') && (f[2] == '2')) { |
| 9579 |
f += 3; |
| 9580 |
} else { |
| 9581 |
fl |= ((sizeof(void* ) == 8) ? FS_INTMAX : 0); |
| 9582 |
++f; |
| 9583 |
} |
| 9584 |
break; |
| 9585 |
default: break; |
| 9586 |
} |
| 9587 |
|
| 9588 |
|
| 9589 |
switch (f[0]) { |
| 9590 |
#define FS_NUMSZ 512 |
| 9591 |
char num[FS_NUMSZ]; |
| 9592 |
char lead[8]; |
| 9593 |
char tail[8]; |
| 9594 |
char* s; |
| 9595 |
char const* h; |
| 9596 |
fs_uint32 l, n, cs; |
| 9597 |
fs_uint64 n64; |
| 9598 |
#ifndef FS_SPRINTF_NOFLOAT |
| 9599 |
double fv; |
| 9600 |
#endif |
| 9601 |
fs_int32 dp; |
| 9602 |
char const* sn; |
| 9603 |
|
| 9604 |
case 's': |
| 9605 |
|
| 9606 |
s = va_arg(va, char* ); |
| 9607 |
if (s == 0) |
| 9608 |
s = (char* )"null"; |
| 9609 |
|
| 9610 |
|
| 9611 |
l = fs_strlen_limited(s, (pr >= 0) ? (fs_uint32)pr : ~0u); |
| 9612 |
lead[0] = 0; |
| 9613 |
tail[0] = 0; |
| 9614 |
pr = 0; |
| 9615 |
dp = 0; |
| 9616 |
cs = 0; |
| 9617 |
|
| 9618 |
goto scopy; |
| 9619 |
|
| 9620 |
case 'c': |
| 9621 |
|
| 9622 |
s = num + FS_NUMSZ - 1; |
| 9623 |
*s = (char)va_arg(va, int); |
| 9624 |
l = 1; |
| 9625 |
lead[0] = 0; |
| 9626 |
tail[0] = 0; |
| 9627 |
pr = 0; |
| 9628 |
dp = 0; |
| 9629 |
cs = 0; |
| 9630 |
goto scopy; |
| 9631 |
|
| 9632 |
case 'n': |
| 9633 |
{ |
| 9634 |
int *d = va_arg(va, int *); |
| 9635 |
*d = tlen + (int)(bf - buf); |
| 9636 |
} break; |
| 9637 |
|
| 9638 |
#ifdef FS_SPRINTF_NOFLOAT |
| 9639 |
case 'A': |
| 9640 |
case 'a': |
| 9641 |
case 'G': |
| 9642 |
case 'g': |
| 9643 |
case 'E': |
| 9644 |
case 'e': |
| 9645 |
case 'f': |
| 9646 |
va_arg(va, double); |
| 9647 |
s = (char* )"No float"; |
| 9648 |
l = 8; |
| 9649 |
lead[0] = 0; |
| 9650 |
tail[0] = 0; |
| 9651 |
pr = 0; |
| 9652 |
cs = 0; |
| 9653 |
FS_UNUSED(dp); |
| 9654 |
goto scopy; |
| 9655 |
#else |
| 9656 |
case 'A': |
| 9657 |
case 'a': |
| 9658 |
h = (f[0] == 'A') ? hexu : hex; |
| 9659 |
fv = va_arg(va, double); |
| 9660 |
if (pr == -1) |
| 9661 |
pr = 6; |
| 9662 |
|
| 9663 |
if (fs_real_to_parts((fs_int64 *)&n64, &dp, fv)) |
| 9664 |
fl |= FS_NEGATIVE; |
| 9665 |
|
| 9666 |
s = num + 64; |
| 9667 |
|
| 9668 |
fs_lead_sign(fl, lead); |
| 9669 |
|
| 9670 |
if (dp == -1023) |
| 9671 |
dp = (n64) ? -1022 : 0; |
| 9672 |
else |
| 9673 |
n64 |= (((fs_uint64)1) << 52); |
| 9674 |
n64 <<= (64 - 56); |
| 9675 |
if (pr < 15) |
| 9676 |
n64 += ((((fs_uint64)8) << 56) >> (pr * 4)); |
| 9677 |
|
| 9678 |
|
| 9679 |
#ifdef FS_SPRINTF_MSVC_MODE |
| 9680 |
*s++ = '0'; |
| 9681 |
*s++ = 'x'; |
| 9682 |
#else |
| 9683 |
lead[1 + lead[0]] = '0'; |
| 9684 |
lead[2 + lead[0]] = 'x'; |
| 9685 |
lead[0] += 2; |
| 9686 |
#endif |
| 9687 |
*s++ = h[(n64 >> 60) & 15]; |
| 9688 |
n64 <<= 4; |
| 9689 |
if (pr) |
| 9690 |
*s++ = fs_period; |
| 9691 |
sn = s; |
| 9692 |
|
| 9693 |
|
| 9694 |
n = pr; |
| 9695 |
if (n > 13) |
| 9696 |
n = 13; |
| 9697 |
if (pr > (fs_int32)n) |
| 9698 |
tz = pr - n; |
| 9699 |
pr = 0; |
| 9700 |
while (n--) { |
| 9701 |
*s++ = h[(n64 >> 60) & 15]; |
| 9702 |
n64 <<= 4; |
| 9703 |
} |
| 9704 |
|
| 9705 |
|
| 9706 |
tail[1] = h[17]; |
| 9707 |
if (dp < 0) { |
| 9708 |
tail[2] = '-'; |
| 9709 |
dp = -dp; |
| 9710 |
} else |
| 9711 |
tail[2] = '+'; |
| 9712 |
n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3)); |
| 9713 |
tail[0] = (char)n; |
| 9714 |
for (;;) { |
| 9715 |
tail[n] = '0' + dp % 10; |
| 9716 |
if (n <= 3) |
| 9717 |
break; |
| 9718 |
--n; |
| 9719 |
dp /= 10; |
| 9720 |
} |
| 9721 |
|
| 9722 |
dp = (int)(s - sn); |
| 9723 |
l = (int)(s - (num + 64)); |
| 9724 |
s = num + 64; |
| 9725 |
cs = 1 + (3 << 24); |
| 9726 |
goto scopy; |
| 9727 |
|
| 9728 |
case 'G': |
| 9729 |
case 'g': |
| 9730 |
h = (f[0] == 'G') ? hexu : hex; |
| 9731 |
fv = va_arg(va, double); |
| 9732 |
if (pr == -1) |
| 9733 |
pr = 6; |
| 9734 |
else if (pr == 0) |
| 9735 |
pr = 1; |
| 9736 |
|
| 9737 |
if (fs_real_to_str(&sn, &l, num, &dp, fv, (pr - 1) | 0x80000000)) |
| 9738 |
fl |= FS_NEGATIVE; |
| 9739 |
|
| 9740 |
|
| 9741 |
n = pr; |
| 9742 |
if (l > (fs_uint32)pr) |
| 9743 |
l = pr; |
| 9744 |
while ((l > 1) && (pr) && (sn[l - 1] == '0')) { |
| 9745 |
--pr; |
| 9746 |
--l; |
| 9747 |
} |
| 9748 |
|
| 9749 |
|
| 9750 |
if ((dp <= -4) || (dp > (fs_int32)n)) { |
| 9751 |
if (pr > (fs_int32)l) |
| 9752 |
pr = l - 1; |
| 9753 |
else if (pr) |
| 9754 |
--pr; |
| 9755 |
goto doexpfromg; |
| 9756 |
} |
| 9757 |
|
| 9758 |
if (dp > 0) { |
| 9759 |
pr = (dp < (fs_int32)l) ? l - dp : 0; |
| 9760 |
} else { |
| 9761 |
pr = -dp + ((pr > (fs_int32)l) ? (fs_int32) l : pr); |
| 9762 |
} |
| 9763 |
goto dofloatfromg; |
| 9764 |
|
| 9765 |
case 'E': |
| 9766 |
case 'e': |
| 9767 |
h = (f[0] == 'E') ? hexu : hex; |
| 9768 |
fv = va_arg(va, double); |
| 9769 |
if (pr == -1) |
| 9770 |
pr = 6; |
| 9771 |
|
| 9772 |
if (fs_real_to_str(&sn, &l, num, &dp, fv, pr | 0x80000000)) |
| 9773 |
fl |= FS_NEGATIVE; |
| 9774 |
doexpfromg: |
| 9775 |
tail[0] = 0; |
| 9776 |
fs_lead_sign(fl, lead); |
| 9777 |
if (dp == FS_SPECIAL) { |
| 9778 |
s = (char* )sn; |
| 9779 |
cs = 0; |
| 9780 |
pr = 0; |
| 9781 |
goto scopy; |
| 9782 |
} |
| 9783 |
s = num + 64; |
| 9784 |
|
| 9785 |
*s++ = sn[0]; |
| 9786 |
|
| 9787 |
if (pr) |
| 9788 |
*s++ = fs_period; |
| 9789 |
|
| 9790 |
|
| 9791 |
if ((l - 1) > (fs_uint32)pr) |
| 9792 |
l = pr + 1; |
| 9793 |
for (n = 1; n < l; n++) |
| 9794 |
*s++ = sn[n]; |
| 9795 |
|
| 9796 |
tz = pr - (l - 1); |
| 9797 |
pr = 0; |
| 9798 |
|
| 9799 |
tail[1] = h[0xe]; |
| 9800 |
dp -= 1; |
| 9801 |
if (dp < 0) { |
| 9802 |
tail[2] = '-'; |
| 9803 |
dp = -dp; |
| 9804 |
} else |
| 9805 |
tail[2] = '+'; |
| 9806 |
#ifdef FS_SPRINTF_MSVC_MODE |
| 9807 |
n = 5; |
| 9808 |
#else |
| 9809 |
n = (dp >= 100) ? 5 : 4; |
| 9810 |
#endif |
| 9811 |
tail[0] = (char)n; |
| 9812 |
for (;;) { |
| 9813 |
tail[n] = '0' + dp % 10; |
| 9814 |
if (n <= 3) |
| 9815 |
break; |
| 9816 |
--n; |
| 9817 |
dp /= 10; |
| 9818 |
} |
| 9819 |
cs = 1 + (3 << 24); |
| 9820 |
goto flt_lead; |
| 9821 |
|
| 9822 |
case 'f': |
| 9823 |
fv = va_arg(va, double); |
| 9824 |
doafloat: |
| 9825 |
|
| 9826 |
if (fl & FS_METRIC_SUFFIX) { |
| 9827 |
double divisor; |
| 9828 |
divisor = 1000.0f; |
| 9829 |
if (fl & FS_METRIC_1024) |
| 9830 |
divisor = 1024.0; |
| 9831 |
while (fl < 0x4000000) { |
| 9832 |
if ((fv < divisor) && (fv > -divisor)) |
| 9833 |
break; |
| 9834 |
fv /= divisor; |
| 9835 |
fl += 0x1000000; |
| 9836 |
} |
| 9837 |
} |
| 9838 |
if (pr == -1) |
| 9839 |
pr = 6; |
| 9840 |
|
| 9841 |
if (fs_real_to_str(&sn, &l, num, &dp, fv, pr)) |
| 9842 |
fl |= FS_NEGATIVE; |
| 9843 |
dofloatfromg: |
| 9844 |
tail[0] = 0; |
| 9845 |
fs_lead_sign(fl, lead); |
| 9846 |
if (dp == FS_SPECIAL) { |
| 9847 |
s = (char* )sn; |
| 9848 |
cs = 0; |
| 9849 |
pr = 0; |
| 9850 |
goto scopy; |
| 9851 |
} |
| 9852 |
s = num + 64; |
| 9853 |
|
| 9854 |
|
| 9855 |
if (dp <= 0) { |
| 9856 |
fs_int32 i; |
| 9857 |
|
| 9858 |
*s++ = '0'; |
| 9859 |
if (pr) |
| 9860 |
*s++ = fs_period; |
| 9861 |
n = -dp; |
| 9862 |
if ((fs_int32)n > pr) |
| 9863 |
n = pr; |
| 9864 |
i = n; |
| 9865 |
while (i) { |
| 9866 |
if ((((fs_uintptr)s) & 3) == 0) |
| 9867 |
break; |
| 9868 |
*s++ = '0'; |
| 9869 |
--i; |
| 9870 |
} |
| 9871 |
while (i >= 4) { |
| 9872 |
*(fs_uint32 *)s = 0x30303030; |
| 9873 |
s += 4; |
| 9874 |
i -= 4; |
| 9875 |
} |
| 9876 |
while (i) { |
| 9877 |
*s++ = '0'; |
| 9878 |
--i; |
| 9879 |
} |
| 9880 |
if ((fs_int32)(l + n) > pr) |
| 9881 |
l = pr - n; |
| 9882 |
i = l; |
| 9883 |
while (i) { |
| 9884 |
*s++ = *sn++; |
| 9885 |
--i; |
| 9886 |
} |
| 9887 |
tz = pr - (n + l); |
| 9888 |
cs = 1 + (3 << 24); |
| 9889 |
} else { |
| 9890 |
cs = (fl & FS_TRIPLET_COMMA) ? ((600 - (fs_uint32)dp) % 3) : 0; |
| 9891 |
if ((fs_uint32)dp >= l) { |
| 9892 |
|
| 9893 |
n = 0; |
| 9894 |
for (;;) { |
| 9895 |
if ((fl & FS_TRIPLET_COMMA) && (++cs == 4)) { |
| 9896 |
cs = 0; |
| 9897 |
*s++ = fs_comma; |
| 9898 |
} else { |
| 9899 |
*s++ = sn[n]; |
| 9900 |
++n; |
| 9901 |
if (n >= l) |
| 9902 |
break; |
| 9903 |
} |
| 9904 |
} |
| 9905 |
if (n < (fs_uint32)dp) { |
| 9906 |
n = dp - n; |
| 9907 |
if ((fl & FS_TRIPLET_COMMA) == 0) { |
| 9908 |
while (n) { |
| 9909 |
if ((((fs_uintptr)s) & 3) == 0) |
| 9910 |
break; |
| 9911 |
*s++ = '0'; |
| 9912 |
--n; |
| 9913 |
} |
| 9914 |
while (n >= 4) { |
| 9915 |
*(fs_uint32 *)s = 0x30303030; |
| 9916 |
s += 4; |
| 9917 |
n -= 4; |
| 9918 |
} |
| 9919 |
} |
| 9920 |
while (n) { |
| 9921 |
if ((fl & FS_TRIPLET_COMMA) && (++cs == 4)) { |
| 9922 |
cs = 0; |
| 9923 |
*s++ = fs_comma; |
| 9924 |
} else { |
| 9925 |
*s++ = '0'; |
| 9926 |
--n; |
| 9927 |
} |
| 9928 |
} |
| 9929 |
} |
| 9930 |
cs = (int)(s - (num + 64)) + (3 << 24); |
| 9931 |
if (pr) { |
| 9932 |
*s++ = fs_period; |
| 9933 |
tz = pr; |
| 9934 |
} |
| 9935 |
} else { |
| 9936 |
|
| 9937 |
n = 0; |
| 9938 |
for (;;) { |
| 9939 |
if ((fl & FS_TRIPLET_COMMA) && (++cs == 4)) { |
| 9940 |
cs = 0; |
| 9941 |
*s++ = fs_comma; |
| 9942 |
} else { |
| 9943 |
*s++ = sn[n]; |
| 9944 |
++n; |
| 9945 |
if (n >= (fs_uint32)dp) |
| 9946 |
break; |
| 9947 |
} |
| 9948 |
} |
| 9949 |
cs = (int)(s - (num + 64)) + (3 << 24); |
| 9950 |
if (pr) |
| 9951 |
*s++ = fs_period; |
| 9952 |
if ((l - dp) > (fs_uint32)pr) |
| 9953 |
l = pr + dp; |
| 9954 |
while (n < l) { |
| 9955 |
*s++ = sn[n]; |
| 9956 |
++n; |
| 9957 |
} |
| 9958 |
tz = pr - (l - dp); |
| 9959 |
} |
| 9960 |
} |
| 9961 |
pr = 0; |
| 9962 |
|
| 9963 |
|
| 9964 |
if (fl & FS_METRIC_SUFFIX) { |
| 9965 |
char idx; |
| 9966 |
idx = 1; |
| 9967 |
if (fl & FS_METRIC_NOSPACE) |
| 9968 |
idx = 0; |
| 9969 |
tail[0] = idx; |
| 9970 |
tail[1] = ' '; |
| 9971 |
{ |
| 9972 |
if (fl >> 24) { |
| 9973 |
if (fl & FS_METRIC_1024) |
| 9974 |
tail[idx + 1] = "_KMGT"[fl >> 24]; |
| 9975 |
else |
| 9976 |
tail[idx + 1] = "_kMGT"[fl >> 24]; |
| 9977 |
idx++; |
| 9978 |
|
| 9979 |
if (fl & FS_METRIC_1024 && !(fl & FS_METRIC_JEDEC)) { |
| 9980 |
tail[idx + 1] = 'i'; |
| 9981 |
idx++; |
| 9982 |
} |
| 9983 |
tail[0] = idx; |
| 9984 |
} |
| 9985 |
} |
| 9986 |
}; |
| 9987 |
|
| 9988 |
flt_lead: |
| 9989 |
|
| 9990 |
l = (fs_uint32)(s - (num + 64)); |
| 9991 |
s = num + 64; |
| 9992 |
goto scopy; |
| 9993 |
#endif |
| 9994 |
|
| 9995 |
case 'B': |
| 9996 |
case 'b': |
| 9997 |
h = (f[0] == 'B') ? hexu : hex; |
| 9998 |
lead[0] = 0; |
| 9999 |
if (fl & FS_LEADING_0X) { |
| 10000 |
lead[0] = 2; |
| 10001 |
lead[1] = '0'; |
| 10002 |
lead[2] = h[0xb]; |
| 10003 |
} |
| 10004 |
l = (8 << 4) | (1 << 8); |
| 10005 |
goto radixnum; |
| 10006 |
|
| 10007 |
case 'o': |
| 10008 |
h = hexu; |
| 10009 |
lead[0] = 0; |
| 10010 |
if (fl & FS_LEADING_0X) { |
| 10011 |
lead[0] = 1; |
| 10012 |
lead[1] = '0'; |
| 10013 |
} |
| 10014 |
l = (3 << 4) | (3 << 8); |
| 10015 |
goto radixnum; |
| 10016 |
|
| 10017 |
case 'p': |
| 10018 |
fl |= (sizeof(void* ) == 8) ? FS_INTMAX : 0; |
| 10019 |
pr = sizeof(void* ) * 2; |
| 10020 |
fl &= ~FS_LEADINGZERO; |
| 10021 |
|
| 10022 |
|
| 10023 |
case 'X': |
| 10024 |
case 'x': |
| 10025 |
h = (f[0] == 'X') ? hexu : hex; |
| 10026 |
l = (4 << 4) | (4 << 8); |
| 10027 |
lead[0] = 0; |
| 10028 |
if (fl & FS_LEADING_0X) { |
| 10029 |
lead[0] = 2; |
| 10030 |
lead[1] = '0'; |
| 10031 |
lead[2] = h[16]; |
| 10032 |
} |
| 10033 |
radixnum: |
| 10034 |
|
| 10035 |
if (fl & FS_INTMAX) |
| 10036 |
n64 = va_arg(va, fs_uint64); |
| 10037 |
else |
| 10038 |
n64 = va_arg(va, fs_uint32); |
| 10039 |
|
| 10040 |
s = num + FS_NUMSZ; |
| 10041 |
dp = 0; |
| 10042 |
|
| 10043 |
tail[0] = 0; |
| 10044 |
if (n64 == 0) { |
| 10045 |
lead[0] = 0; |
| 10046 |
if (pr == 0) { |
| 10047 |
l = 0; |
| 10048 |
cs = 0; |
| 10049 |
goto scopy; |
| 10050 |
} |
| 10051 |
} |
| 10052 |
|
| 10053 |
for (;;) { |
| 10054 |
*--s = h[n64 & ((1 << (l >> 8)) - 1)]; |
| 10055 |
n64 >>= (l >> 8); |
| 10056 |
if (!((n64) || ((fs_int32)((num + FS_NUMSZ) - s) < pr))) |
| 10057 |
break; |
| 10058 |
if (fl & FS_TRIPLET_COMMA) { |
| 10059 |
++l; |
| 10060 |
if ((l & 15) == ((l >> 4) & 15)) { |
| 10061 |
l &= ~15; |
| 10062 |
*--s = fs_comma; |
| 10063 |
} |
| 10064 |
} |
| 10065 |
}; |
| 10066 |
|
| 10067 |
cs = (fs_uint32)((num + FS_NUMSZ) - s) + ((((l >> 4) & 15)) << 24); |
| 10068 |
|
| 10069 |
l = (fs_uint32)((num + FS_NUMSZ) - s); |
| 10070 |
|
| 10071 |
goto scopy; |
| 10072 |
|
| 10073 |
case 'u': |
| 10074 |
case 'i': |
| 10075 |
case 'd': |
| 10076 |
|
| 10077 |
if (fl & FS_INTMAX) { |
| 10078 |
fs_int64 i64 = va_arg(va, fs_int64); |
| 10079 |
n64 = (fs_uint64)i64; |
| 10080 |
if ((f[0] != 'u') && (i64 < 0)) { |
| 10081 |
n64 = (fs_uint64)-i64; |
| 10082 |
fl |= FS_NEGATIVE; |
| 10083 |
} |
| 10084 |
} else { |
| 10085 |
fs_int32 i = va_arg(va, fs_int32); |
| 10086 |
n64 = (fs_uint32)i; |
| 10087 |
if ((f[0] != 'u') && (i < 0)) { |
| 10088 |
n64 = (fs_uint32)-i; |
| 10089 |
fl |= FS_NEGATIVE; |
| 10090 |
} |
| 10091 |
} |
| 10092 |
|
| 10093 |
#ifndef FS_SPRINTF_NOFLOAT |
| 10094 |
if (fl & FS_METRIC_SUFFIX) { |
| 10095 |
if (n64 < 1024) |
| 10096 |
pr = 0; |
| 10097 |
else if (pr == -1) |
| 10098 |
pr = 1; |
| 10099 |
fv = (double)(fs_int64)n64; |
| 10100 |
goto doafloat; |
| 10101 |
} |
| 10102 |
#endif |
| 10103 |
|
| 10104 |
|
| 10105 |
s = num + FS_NUMSZ; |
| 10106 |
l = 0; |
| 10107 |
|
| 10108 |
for (;;) { |
| 10109 |
|
| 10110 |
char* o = s - 8; |
| 10111 |
if (n64 >= 100000000) { |
| 10112 |
n = (fs_uint32)(n64 % 100000000); |
| 10113 |
n64 /= 100000000; |
| 10114 |
} else { |
| 10115 |
n = (fs_uint32)n64; |
| 10116 |
n64 = 0; |
| 10117 |
} |
| 10118 |
if ((fl & FS_TRIPLET_COMMA) == 0) { |
| 10119 |
do { |
| 10120 |
s -= 2; |
| 10121 |
*(fs_uint16 *)s = *(fs_uint16 *)&fs_digitpair.pair[(n % 100) * 2]; |
| 10122 |
n /= 100; |
| 10123 |
} while (n); |
| 10124 |
} |
| 10125 |
while (n) { |
| 10126 |
if ((fl & FS_TRIPLET_COMMA) && (l++ == 3)) { |
| 10127 |
l = 0; |
| 10128 |
*--s = fs_comma; |
| 10129 |
--o; |
| 10130 |
} else { |
| 10131 |
*--s = (char)(n % 10) + '0'; |
| 10132 |
n /= 10; |
| 10133 |
} |
| 10134 |
} |
| 10135 |
if (n64 == 0) { |
| 10136 |
if ((s[0] == '0') && (s != (num + FS_NUMSZ))) |
| 10137 |
++s; |
| 10138 |
break; |
| 10139 |
} |
| 10140 |
while (s != o) |
| 10141 |
if ((fl & FS_TRIPLET_COMMA) && (l++ == 3)) { |
| 10142 |
l = 0; |
| 10143 |
*--s = fs_comma; |
| 10144 |
--o; |
| 10145 |
} else { |
| 10146 |
*--s = '0'; |
| 10147 |
} |
| 10148 |
} |
| 10149 |
|
| 10150 |
tail[0] = 0; |
| 10151 |
fs_lead_sign(fl, lead); |
| 10152 |
|
| 10153 |
|
| 10154 |
l = (fs_uint32)((num + FS_NUMSZ) - s); |
| 10155 |
if (l == 0) { |
| 10156 |
*--s = '0'; |
| 10157 |
l = 1; |
| 10158 |
} |
| 10159 |
cs = l + (3 << 24); |
| 10160 |
if (pr < 0) |
| 10161 |
pr = 0; |
| 10162 |
|
| 10163 |
scopy: |
| 10164 |
|
| 10165 |
if (pr < (fs_int32)l) |
| 10166 |
pr = l; |
| 10167 |
n = pr + lead[0] + tail[0] + tz; |
| 10168 |
if (fw < (fs_int32)n) |
| 10169 |
fw = n; |
| 10170 |
fw -= n; |
| 10171 |
pr -= l; |
| 10172 |
|
| 10173 |
|
| 10174 |
if ((fl & FS_LEFTJUST) == 0) { |
| 10175 |
if (fl & FS_LEADINGZERO) |
| 10176 |
{ |
| 10177 |
pr = (fw > pr) ? fw : pr; |
| 10178 |
fw = 0; |
| 10179 |
} else { |
| 10180 |
fl &= ~FS_TRIPLET_COMMA; |
| 10181 |
} |
| 10182 |
} |
| 10183 |
|
| 10184 |
|
| 10185 |
if (fw + pr) { |
| 10186 |
fs_int32 i; |
| 10187 |
fs_uint32 c; |
| 10188 |
|
| 10189 |
|
| 10190 |
if ((fl & FS_LEFTJUST) == 0) |
| 10191 |
while (fw > 0) { |
| 10192 |
fs_cb_buf_clamp(i, fw); |
| 10193 |
fw -= i; |
| 10194 |
while (i) { |
| 10195 |
if ((((fs_uintptr)bf) & 3) == 0) |
| 10196 |
break; |
| 10197 |
*bf++ = ' '; |
| 10198 |
--i; |
| 10199 |
} |
| 10200 |
while (i >= 4) { |
| 10201 |
*(fs_uint32 *)bf = 0x20202020; |
| 10202 |
bf += 4; |
| 10203 |
i -= 4; |
| 10204 |
} |
| 10205 |
while (i) { |
| 10206 |
*bf++ = ' '; |
| 10207 |
--i; |
| 10208 |
} |
| 10209 |
fs_chk_cb_buf(1); |
| 10210 |
} |
| 10211 |
|
| 10212 |
|
| 10213 |
sn = lead + 1; |
| 10214 |
while (lead[0]) { |
| 10215 |
fs_cb_buf_clamp(i, lead[0]); |
| 10216 |
lead[0] -= (char)i; |
| 10217 |
while (i) { |
| 10218 |
FS_SUPPRESS_CLANG_ANALYZER *bf++ = *sn++; |
| 10219 |
--i; |
| 10220 |
} |
| 10221 |
fs_chk_cb_buf(1); |
| 10222 |
} |
| 10223 |
|
| 10224 |
|
| 10225 |
c = cs >> 24; |
| 10226 |
cs &= 0xffffff; |
| 10227 |
cs = (fl & FS_TRIPLET_COMMA) ? ((fs_uint32)(c - ((pr + cs) % (c + 1)))) : 0; |
| 10228 |
while (pr > 0) { |
| 10229 |
fs_cb_buf_clamp(i, pr); |
| 10230 |
pr -= i; |
| 10231 |
if ((fl & FS_TRIPLET_COMMA) == 0) { |
| 10232 |
while (i) { |
| 10233 |
if ((((fs_uintptr)bf) & 3) == 0) |
| 10234 |
break; |
| 10235 |
*bf++ = '0'; |
| 10236 |
--i; |
| 10237 |
} |
| 10238 |
while (i >= 4) { |
| 10239 |
*(fs_uint32 *)bf = 0x30303030; |
| 10240 |
bf += 4; |
| 10241 |
i -= 4; |
| 10242 |
} |
| 10243 |
} |
| 10244 |
while (i) { |
| 10245 |
if ((fl & FS_TRIPLET_COMMA) && (cs++ == c)) { |
| 10246 |
cs = 0; |
| 10247 |
*bf++ = fs_comma; |
| 10248 |
} else |
| 10249 |
*bf++ = '0'; |
| 10250 |
--i; |
| 10251 |
} |
| 10252 |
fs_chk_cb_buf(1); |
| 10253 |
} |
| 10254 |
} |
| 10255 |
|
| 10256 |
|
| 10257 |
sn = lead + 1; |
| 10258 |
while (lead[0]) { |
| 10259 |
fs_int32 i; |
| 10260 |
fs_cb_buf_clamp(i, lead[0]); |
| 10261 |
lead[0] -= (char)i; |
| 10262 |
while (i) { |
| 10263 |
*bf++ = *sn++; |
| 10264 |
--i; |
| 10265 |
} |
| 10266 |
fs_chk_cb_buf(1); |
| 10267 |
} |
| 10268 |
|
| 10269 |
|
| 10270 |
n = l; |
| 10271 |
while (n) { |
| 10272 |
fs_int32 i; |
| 10273 |
fs_cb_buf_clamp(i, n); |
| 10274 |
n -= i; |
| 10275 |
FS_UNALIGNED(while (i >= 4) { |
| 10276 |
*(fs_uint32 volatile *)bf = *(fs_uint32 volatile *)s; |
| 10277 |
bf += 4; |
| 10278 |
s += 4; |
| 10279 |
i -= 4; |
| 10280 |
}) |
| 10281 |
while (i) { |
| 10282 |
FS_SUPPRESS_CLANG_ANALYZER *bf++ = *s++; |
| 10283 |
--i; |
| 10284 |
} |
| 10285 |
fs_chk_cb_buf(1); |
| 10286 |
} |
| 10287 |
|
| 10288 |
|
| 10289 |
while (tz) { |
| 10290 |
fs_int32 i; |
| 10291 |
fs_cb_buf_clamp(i, tz); |
| 10292 |
tz -= i; |
| 10293 |
while (i) { |
| 10294 |
if ((((fs_uintptr)bf) & 3) == 0) |
| 10295 |
break; |
| 10296 |
*bf++ = '0'; |
| 10297 |
--i; |
| 10298 |
} |
| 10299 |
while (i >= 4) { |
| 10300 |
*(fs_uint32 *)bf = 0x30303030; |
| 10301 |
bf += 4; |
| 10302 |
i -= 4; |
| 10303 |
} |
| 10304 |
while (i) { |
| 10305 |
*bf++ = '0'; |
| 10306 |
--i; |
| 10307 |
} |
| 10308 |
fs_chk_cb_buf(1); |
| 10309 |
} |
| 10310 |
|
| 10311 |
|
| 10312 |
sn = tail + 1; |
| 10313 |
while (tail[0]) { |
| 10314 |
fs_int32 i; |
| 10315 |
fs_cb_buf_clamp(i, tail[0]); |
| 10316 |
tail[0] -= (char)i; |
| 10317 |
while (i) { |
| 10318 |
*bf++ = *sn++; |
| 10319 |
--i; |
| 10320 |
} |
| 10321 |
fs_chk_cb_buf(1); |
| 10322 |
} |
| 10323 |
|
| 10324 |
|
| 10325 |
if (fl & FS_LEFTJUST) |
| 10326 |
if (fw > 0) { |
| 10327 |
while (fw) { |
| 10328 |
fs_int32 i; |
| 10329 |
fs_cb_buf_clamp(i, fw); |
| 10330 |
fw -= i; |
| 10331 |
while (i) { |
| 10332 |
if ((((fs_uintptr)bf) & 3) == 0) |
| 10333 |
break; |
| 10334 |
*bf++ = ' '; |
| 10335 |
--i; |
| 10336 |
} |
| 10337 |
while (i >= 4) { |
| 10338 |
*(fs_uint32 *)bf = 0x20202020; |
| 10339 |
bf += 4; |
| 10340 |
i -= 4; |
| 10341 |
} |
| 10342 |
while (i--) |
| 10343 |
*bf++ = ' '; |
| 10344 |
fs_chk_cb_buf(1); |
| 10345 |
} |
| 10346 |
} |
| 10347 |
break; |
| 10348 |
|
| 10349 |
default: |
| 10350 |
s = num + FS_NUMSZ - 1; |
| 10351 |
*s = f[0]; |
| 10352 |
l = 1; |
| 10353 |
fw = fl = 0; |
| 10354 |
lead[0] = 0; |
| 10355 |
tail[0] = 0; |
| 10356 |
pr = 0; |
| 10357 |
dp = 0; |
| 10358 |
cs = 0; |
| 10359 |
goto scopy; |
| 10360 |
} |
| 10361 |
++f; |
| 10362 |
} |
| 10363 |
endfmt: |
| 10364 |
|
| 10365 |
if (!callback) |
| 10366 |
*bf = 0; |
| 10367 |
else |
| 10368 |
fs_flush_cb(); |
| 10369 |
|
| 10370 |
done: |
| 10371 |
return tlen + (int)(bf - buf); |
| 10372 |
} |
| 10373 |
|
| 10374 |
|
| 10375 |
#undef FS_LEFTJUST |
| 10376 |
#undef FS_LEADINGPLUS |
| 10377 |
#undef FS_LEADINGSPACE |
| 10378 |
#undef FS_LEADING_0X |
| 10379 |
#undef FS_LEADINGZERO |
| 10380 |
#undef FS_INTMAX |
| 10381 |
#undef FS_TRIPLET_COMMA |
| 10382 |
#undef FS_NEGATIVE |
| 10383 |
#undef FS_METRIC_SUFFIX |
| 10384 |
#undef FS_NUMSZ |
| 10385 |
#undef fs_chk_cb_bufL |
| 10386 |
#undef fs_chk_cb_buf |
| 10387 |
#undef fs_flush_cb |
| 10388 |
#undef fs_cb_buf_clamp |
| 10389 |
|
| 10390 |
|
| 10391 |
|
| 10392 |
|
| 10393 |
FS_API_SPRINTF_DEF int fs_sprintf(char* buf, char const* fmt, ...) |
| 10394 |
{ |
| 10395 |
int result; |
| 10396 |
va_list va; |
| 10397 |
va_start(va, fmt); |
| 10398 |
result = fs_vsprintfcb(0, 0, buf, fmt, va); |
| 10399 |
va_end(va); |
| 10400 |
return result; |
| 10401 |
} |
| 10402 |
|
| 10403 |
typedef struct fs_sprintf_context { |
| 10404 |
char* buf; |
| 10405 |
size_t count; |
| 10406 |
size_t length; |
| 10407 |
char tmp[FS_SPRINTF_MIN]; |
| 10408 |
} fs_sprintf_context; |
| 10409 |
|
| 10410 |
static char* fs_clamp_callback(const char* buf, void* user, size_t len) |
| 10411 |
{ |
| 10412 |
fs_sprintf_context *c = (fs_sprintf_context *)user; |
| 10413 |
c->length += len; |
| 10414 |
|
| 10415 |
if (len > c->count) |
| 10416 |
len = c->count; |
| 10417 |
|
| 10418 |
if (len) { |
| 10419 |
if (buf != c->buf && buf != NULL) { |
| 10420 |
const char* s, *se; |
| 10421 |
char* d; |
| 10422 |
d = c->buf; |
| 10423 |
s = buf; |
| 10424 |
se = buf + len; |
| 10425 |
do { |
| 10426 |
*d++ = *s++; |
| 10427 |
} while (s < se); |
| 10428 |
} |
| 10429 |
c->buf += len; |
| 10430 |
c->count -= len; |
| 10431 |
} |
| 10432 |
|
| 10433 |
if (c->count <= 0) |
| 10434 |
return c->tmp; |
| 10435 |
return (c->count >= FS_SPRINTF_MIN) ? c->buf : c->tmp; |
| 10436 |
} |
| 10437 |
|
| 10438 |
static char* fs_count_clamp_callback( const char* buf, void* user, size_t len ) |
| 10439 |
{ |
| 10440 |
fs_sprintf_context * c = (fs_sprintf_context*)user; |
| 10441 |
(void) sizeof(buf); |
| 10442 |
|
| 10443 |
c->length += len; |
| 10444 |
return c->tmp; |
| 10445 |
} |
| 10446 |
|
| 10447 |
FS_API_SPRINTF_DEF int fs_vsnprintf( char* buf, size_t count, char const* fmt, va_list va ) |
| 10448 |
{ |
| 10449 |
fs_sprintf_context c; |
| 10450 |
|
| 10451 |
if ( (count == 0) && !buf ) |
| 10452 |
{ |
| 10453 |
c.length = 0; |
| 10454 |
|
| 10455 |
fs_vsprintfcb( fs_count_clamp_callback, &c, c.tmp, fmt, va ); |
| 10456 |
} |
| 10457 |
else |
| 10458 |
{ |
| 10459 |
size_t l; |
| 10460 |
|
| 10461 |
c.buf = buf; |
| 10462 |
c.count = count; |
| 10463 |
c.length = 0; |
| 10464 |
|
| 10465 |
fs_vsprintfcb( fs_clamp_callback, &c, fs_clamp_callback(0,&c,0), fmt, va ); |
| 10466 |
|
| 10467 |
|
| 10468 |
l = (size_t)( c.buf - buf ); |
| 10469 |
if ( l >= count ) |
| 10470 |
l = count - 1; |
| 10471 |
buf[l] = 0; |
| 10472 |
} |
| 10473 |
|
| 10474 |
return (int)c.length; |
| 10475 |
} |
| 10476 |
|
| 10477 |
FS_API_SPRINTF_DEF int fs_snprintf(char* buf, size_t count, char const* fmt, ...) |
| 10478 |
{ |
| 10479 |
int result; |
| 10480 |
va_list va; |
| 10481 |
va_start(va, fmt); |
| 10482 |
|
| 10483 |
result = fs_vsnprintf(buf, count, fmt, va); |
| 10484 |
va_end(va); |
| 10485 |
|
| 10486 |
return result; |
| 10487 |
} |
| 10488 |
|
| 10489 |
FS_API_SPRINTF_DEF int fs_vsprintf(char* buf, char const* fmt, va_list va) |
| 10490 |
{ |
| 10491 |
return fs_vsprintfcb(0, 0, buf, fmt, va); |
| 10492 |
} |
| 10493 |
|
| 10494 |
|
| 10495 |
|
| 10496 |
|
| 10497 |
#ifndef FS_SPRINTF_NOFLOAT |
| 10498 |
|
| 10499 |
|
| 10500 |
#define FS_COPYFP(dest, src) \ |
| 10501 |
{ \ |
| 10502 |
int cn; \ |
| 10503 |
for (cn = 0; cn < 8; cn++) \ |
| 10504 |
((char* )&dest)[cn] = ((char* )&src)[cn]; \ |
| 10505 |
} |
| 10506 |
|
| 10507 |
|
| 10508 |
static fs_int32 fs_real_to_parts(fs_int64 *bits, fs_int32 *expo, double value) |
| 10509 |
{ |
| 10510 |
double d; |
| 10511 |
fs_int64 b = 0; |
| 10512 |
|
| 10513 |
|
| 10514 |
d = value; |
| 10515 |
|
| 10516 |
FS_COPYFP(b, d); |
| 10517 |
|
| 10518 |
*bits = b & ((((fs_uint64)1) << 52) - 1); |
| 10519 |
*expo = (fs_int32)(((b >> 52) & 2047) - 1023); |
| 10520 |
|
| 10521 |
return (fs_int32)((fs_uint64) b >> 63); |
| 10522 |
} |
| 10523 |
|
| 10524 |
static double const fs_bot[23] = { |
| 10525 |
1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011, |
| 10526 |
1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022 |
| 10527 |
}; |
| 10528 |
static double const fs_negbot[22] = { |
| 10529 |
1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011, |
| 10530 |
1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022 |
| 10531 |
}; |
| 10532 |
static double const fs_negboterr[22] = { |
| 10533 |
-5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023, |
| 10534 |
4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029, |
| 10535 |
-3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035, |
| 10536 |
2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039 |
| 10537 |
}; |
| 10538 |
static double const fs_top[13] = { |
| 10539 |
1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+299 |
| 10540 |
}; |
| 10541 |
static double const fs_negtop[13] = { |
| 10542 |
1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-299 |
| 10543 |
}; |
| 10544 |
static double const fs_toperr[13] = { |
| 10545 |
8388608, |
| 10546 |
6.8601809640529717e+028, |
| 10547 |
-7.253143638152921e+052, |
| 10548 |
-4.3377296974619174e+075, |
| 10549 |
-1.5559416129466825e+098, |
| 10550 |
-3.2841562489204913e+121, |
| 10551 |
-3.7745893248228135e+144, |
| 10552 |
-1.7356668416969134e+167, |
| 10553 |
-3.8893577551088374e+190, |
| 10554 |
-9.9566444326005119e+213, |
| 10555 |
6.3641293062232429e+236, |
| 10556 |
-5.2069140800249813e+259, |
| 10557 |
-5.2504760255204387e+282 |
| 10558 |
}; |
| 10559 |
static double const fs_negtoperr[13] = { |
| 10560 |
3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109, |
| 10561 |
-5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201, |
| 10562 |
7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293, |
| 10563 |
8.0970921678014997e-317 |
| 10564 |
}; |
| 10565 |
|
| 10566 |
#if defined(_MSC_VER) && (_MSC_VER <= 1200) |
| 10567 |
static fs_uint64 const fs_powten[20] = { |
| 10568 |
1, |
| 10569 |
10, |
| 10570 |
100, |
| 10571 |
1000, |
| 10572 |
10000, |
| 10573 |
100000, |
| 10574 |
1000000, |
| 10575 |
10000000, |
| 10576 |
100000000, |
| 10577 |
1000000000, |
| 10578 |
10000000000, |
| 10579 |
100000000000, |
| 10580 |
1000000000000, |
| 10581 |
10000000000000, |
| 10582 |
100000000000000, |
| 10583 |
1000000000000000, |
| 10584 |
10000000000000000, |
| 10585 |
100000000000000000, |
| 10586 |
1000000000000000000, |
| 10587 |
10000000000000000000U |
| 10588 |
}; |
| 10589 |
#define fs_tento19th ((fs_uint64)1000000000000000000) |
| 10590 |
#else |
| 10591 |
static fs_uint64 const fs_powten[20] = { |
| 10592 |
1, |
| 10593 |
10, |
| 10594 |
100, |
| 10595 |
1000, |
| 10596 |
10000, |
| 10597 |
100000, |
| 10598 |
1000000, |
| 10599 |
10000000, |
| 10600 |
100000000, |
| 10601 |
1000000000, |
| 10602 |
10000000000ULL, |
| 10603 |
100000000000ULL, |
| 10604 |
1000000000000ULL, |
| 10605 |
10000000000000ULL, |
| 10606 |
100000000000000ULL, |
| 10607 |
1000000000000000ULL, |
| 10608 |
10000000000000000ULL, |
| 10609 |
100000000000000000ULL, |
| 10610 |
1000000000000000000ULL, |
| 10611 |
10000000000000000000ULL |
| 10612 |
}; |
| 10613 |
#define fs_tento19th (1000000000000000000ULL) |
| 10614 |
#endif |
| 10615 |
|
| 10616 |
#define fs_ddmulthi(oh, ol, xh, yh) \ |
| 10617 |
{ \ |
| 10618 |
double ahi = 0, alo, bhi = 0, blo; \ |
| 10619 |
fs_int64 bt; \ |
| 10620 |
oh = xh * yh; \ |
| 10621 |
FS_COPYFP(bt, xh); \ |
| 10622 |
bt &= ((~(fs_uint64)0) << 27); \ |
| 10623 |
FS_COPYFP(ahi, bt); \ |
| 10624 |
alo = xh - ahi; \ |
| 10625 |
FS_COPYFP(bt, yh); \ |
| 10626 |
bt &= ((~(fs_uint64)0) << 27); \ |
| 10627 |
FS_COPYFP(bhi, bt); \ |
| 10628 |
blo = yh - bhi; \ |
| 10629 |
ol = ((ahi * bhi - oh) + ahi * blo + alo * bhi) + alo * blo; \ |
| 10630 |
} |
| 10631 |
|
| 10632 |
#define fs_ddtoS64(ob, xh, xl) \ |
| 10633 |
{ \ |
| 10634 |
double ahi = 0, alo, vh, t; \ |
| 10635 |
ob = (fs_int64)xh; \ |
| 10636 |
vh = (double)ob; \ |
| 10637 |
ahi = (xh - vh); \ |
| 10638 |
t = (ahi - xh); \ |
| 10639 |
alo = (xh - (ahi - t)) - (vh + t); \ |
| 10640 |
ob += (fs_int64)(ahi + alo + xl); \ |
| 10641 |
} |
| 10642 |
|
| 10643 |
#define fs_ddrenorm(oh, ol) \ |
| 10644 |
{ \ |
| 10645 |
double s; \ |
| 10646 |
s = oh + ol; \ |
| 10647 |
ol = ol - (s - oh); \ |
| 10648 |
oh = s; \ |
| 10649 |
} |
| 10650 |
|
| 10651 |
#define fs_ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh); |
| 10652 |
|
| 10653 |
#define fs_ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl); |
| 10654 |
|
| 10655 |
static void fs_raise_to_power10(double *ohi, double *olo, double d, fs_int32 power) |
| 10656 |
{ |
| 10657 |
double ph, pl; |
| 10658 |
if ((power >= 0) && (power <= 22)) { |
| 10659 |
fs_ddmulthi(ph, pl, d, fs_bot[power]); |
| 10660 |
} else { |
| 10661 |
fs_int32 e, et, eb; |
| 10662 |
double p2h, p2l; |
| 10663 |
|
| 10664 |
e = power; |
| 10665 |
if (power < 0) |
| 10666 |
e = -e; |
| 10667 |
et = (e * 0x2c9) >> 14; |
| 10668 |
if (et > 13) |
| 10669 |
et = 13; |
| 10670 |
eb = e - (et * 23); |
| 10671 |
|
| 10672 |
ph = d; |
| 10673 |
pl = 0.0; |
| 10674 |
if (power < 0) { |
| 10675 |
if (eb) { |
| 10676 |
--eb; |
| 10677 |
fs_ddmulthi(ph, pl, d, fs_negbot[eb]); |
| 10678 |
fs_ddmultlos(ph, pl, d, fs_negboterr[eb]); |
| 10679 |
} |
| 10680 |
if (et) { |
| 10681 |
fs_ddrenorm(ph, pl); |
| 10682 |
--et; |
| 10683 |
fs_ddmulthi(p2h, p2l, ph, fs_negtop[et]); |
| 10684 |
fs_ddmultlo(p2h, p2l, ph, pl, fs_negtop[et], fs_negtoperr[et]); |
| 10685 |
ph = p2h; |
| 10686 |
pl = p2l; |
| 10687 |
} |
| 10688 |
} else { |
| 10689 |
if (eb) { |
| 10690 |
e = eb; |
| 10691 |
if (eb > 22) |
| 10692 |
eb = 22; |
| 10693 |
e -= eb; |
| 10694 |
fs_ddmulthi(ph, pl, d, fs_bot[eb]); |
| 10695 |
if (e) { |
| 10696 |
fs_ddrenorm(ph, pl); |
| 10697 |
fs_ddmulthi(p2h, p2l, ph, fs_bot[e]); |
| 10698 |
fs_ddmultlos(p2h, p2l, fs_bot[e], pl); |
| 10699 |
ph = p2h; |
| 10700 |
pl = p2l; |
| 10701 |
} |
| 10702 |
} |
| 10703 |
if (et) { |
| 10704 |
fs_ddrenorm(ph, pl); |
| 10705 |
--et; |
| 10706 |
fs_ddmulthi(p2h, p2l, ph, fs_top[et]); |
| 10707 |
fs_ddmultlo(p2h, p2l, ph, pl, fs_top[et], fs_toperr[et]); |
| 10708 |
ph = p2h; |
| 10709 |
pl = p2l; |
| 10710 |
} |
| 10711 |
} |
| 10712 |
} |
| 10713 |
fs_ddrenorm(ph, pl); |
| 10714 |
*ohi = ph; |
| 10715 |
*olo = pl; |
| 10716 |
} |
| 10717 |
|
| 10718 |
|
| 10719 |
|
| 10720 |
|
| 10721 |
|
| 10722 |
static fs_int32 fs_real_to_str(char const* *start, fs_uint32 *len, char* out, fs_int32 *decimal_pos, double value, fs_uint32 frac_digits) |
| 10723 |
{ |
| 10724 |
double d; |
| 10725 |
fs_int64 bits = 0; |
| 10726 |
fs_int32 expo, e, ng, tens; |
| 10727 |
|
| 10728 |
d = value; |
| 10729 |
FS_COPYFP(bits, d); |
| 10730 |
expo = (fs_int32)((bits >> 52) & 2047); |
| 10731 |
ng = (fs_int32)((fs_uint64) bits >> 63); |
| 10732 |
if (ng) |
| 10733 |
d = -d; |
| 10734 |
|
| 10735 |
if (expo == 2047) |
| 10736 |
{ |
| 10737 |
*start = (bits & ((((fs_uint64)1) << 52) - 1)) ? "NaN" : "Inf"; |
| 10738 |
*decimal_pos = FS_SPECIAL; |
| 10739 |
*len = 3; |
| 10740 |
return ng; |
| 10741 |
} |
| 10742 |
|
| 10743 |
if (expo == 0) |
| 10744 |
{ |
| 10745 |
if (((fs_uint64) bits << 1) == 0) |
| 10746 |
{ |
| 10747 |
*decimal_pos = 1; |
| 10748 |
*start = out; |
| 10749 |
out[0] = '0'; |
| 10750 |
*len = 1; |
| 10751 |
return ng; |
| 10752 |
} |
| 10753 |
|
| 10754 |
{ |
| 10755 |
fs_int64 v = ((fs_uint64)1) << 51; |
| 10756 |
while ((bits & v) == 0) { |
| 10757 |
--expo; |
| 10758 |
v >>= 1; |
| 10759 |
} |
| 10760 |
} |
| 10761 |
} |
| 10762 |
|
| 10763 |
|
| 10764 |
{ |
| 10765 |
double ph, pl; |
| 10766 |
|
| 10767 |
|
| 10768 |
tens = expo - 1023; |
| 10769 |
tens = (tens < 0) ? ((tens * 617) / 2048) : (((tens * 1233) / 4096) + 1); |
| 10770 |
|
| 10771 |
|
| 10772 |
fs_raise_to_power10(&ph, &pl, d, 18 - tens); |
| 10773 |
|
| 10774 |
|
| 10775 |
fs_ddtoS64(bits, ph, pl); |
| 10776 |
|
| 10777 |
|
| 10778 |
if (((fs_uint64)bits) >= fs_tento19th) |
| 10779 |
++tens; |
| 10780 |
} |
| 10781 |
|
| 10782 |
|
| 10783 |
frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits); |
| 10784 |
if ((frac_digits < 24)) { |
| 10785 |
fs_uint32 dg = 1; |
| 10786 |
if ((fs_uint64)bits >= fs_powten[9]) |
| 10787 |
dg = 10; |
| 10788 |
while ((fs_uint64)bits >= fs_powten[dg]) { |
| 10789 |
++dg; |
| 10790 |
if (dg == 20) |
| 10791 |
goto noround; |
| 10792 |
} |
| 10793 |
if (frac_digits < dg) { |
| 10794 |
fs_uint64 r; |
| 10795 |
|
| 10796 |
e = dg - frac_digits; |
| 10797 |
if ((fs_uint32)e >= 24) |
| 10798 |
goto noround; |
| 10799 |
r = fs_powten[e]; |
| 10800 |
bits = bits + (r / 2); |
| 10801 |
if ((fs_uint64)bits >= fs_powten[dg]) |
| 10802 |
++tens; |
| 10803 |
bits /= r; |
| 10804 |
} |
| 10805 |
noround:; |
| 10806 |
} |
| 10807 |
|
| 10808 |
|
| 10809 |
if (bits) { |
| 10810 |
fs_uint32 n; |
| 10811 |
for (;;) { |
| 10812 |
if (bits <= 0xffffffff) |
| 10813 |
break; |
| 10814 |
if (bits % 1000) |
| 10815 |
goto donez; |
| 10816 |
bits /= 1000; |
| 10817 |
} |
| 10818 |
n = (fs_uint32)bits; |
| 10819 |
while ((n % 1000) == 0) |
| 10820 |
n /= 1000; |
| 10821 |
bits = n; |
| 10822 |
donez:; |
| 10823 |
} |
| 10824 |
|
| 10825 |
|
| 10826 |
out += 64; |
| 10827 |
e = 0; |
| 10828 |
for (;;) { |
| 10829 |
fs_uint32 n; |
| 10830 |
char* o = out - 8; |
| 10831 |
|
| 10832 |
if (bits >= 100000000) { |
| 10833 |
n = (fs_uint32)(bits % 100000000); |
| 10834 |
bits /= 100000000; |
| 10835 |
} else { |
| 10836 |
n = (fs_uint32)bits; |
| 10837 |
bits = 0; |
| 10838 |
} |
| 10839 |
while (n) { |
| 10840 |
out -= 2; |
| 10841 |
*(fs_uint16 *)out = *(fs_uint16 *)&fs_digitpair.pair[(n % 100) * 2]; |
| 10842 |
n /= 100; |
| 10843 |
e += 2; |
| 10844 |
} |
| 10845 |
if (bits == 0) { |
| 10846 |
if ((e) && (out[0] == '0')) { |
| 10847 |
++out; |
| 10848 |
--e; |
| 10849 |
} |
| 10850 |
break; |
| 10851 |
} |
| 10852 |
while (out != o) { |
| 10853 |
*--out = '0'; |
| 10854 |
++e; |
| 10855 |
} |
| 10856 |
} |
| 10857 |
|
| 10858 |
*decimal_pos = tens; |
| 10859 |
*start = out; |
| 10860 |
*len = e; |
| 10861 |
return ng; |
| 10862 |
} |
| 10863 |
|
| 10864 |
#undef fs_ddmulthi |
| 10865 |
#undef fs_ddrenorm |
| 10866 |
#undef fs_ddmultlo |
| 10867 |
#undef fs_ddmultlos |
| 10868 |
#undef FS_SPECIAL |
| 10869 |
#undef FS_COPYFP |
| 10870 |
|
| 10871 |
#endif |
| 10872 |
|
| 10873 |
|
| 10874 |
#undef FS_UNALIGNED |
| 10875 |
|
| 10876 |
|
| 10877 |
#if defined(__GNUC__) && (__GNUC__ >= 7 || (__GNUC__ == 6 && __GNUC_MINOR__ >= 1)) |
| 10878 |
#pragma GCC diagnostic pop |
| 10879 |
#endif |
| 10880 |
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) |
| 10881 |
#pragma GCC diagnostic pop |
| 10882 |
#endif |
| 10883 |
|
| 10884 |
|
| 10885 |
|
| 10886 |
#endif |
| 10887 |
|
| 10888 |
|
| 10889 |
|
| 10890 |
|
| 10891 |
|
| 10892 |
|
| 10893 |
|
| 10894 |
|
| 10895 |
|
| 10896 |
|
| 10897 |
|
| 10898 |
|
| 10899 |
|
| 10900 |
|
| 10901 |
|
| 10902 |
|
| 10903 |
|
| 10904 |
|
| 10905 |
|
| 10906 |
|
| 10907 |
|
| 10908 |
|
| 10909 |
|
| 10910 |
|
| 10911 |
|
| 10912 |
|
| 10913 |
|
| 10914 |
|
| 10915 |
|
| 10916 |
|
| 10917 |
|
| 10918 |
|
| 10919 |
|
| 10920 |
|
| 10921 |
|
| 10922 |
|
| 10923 |
|
| 10924 |
|
| 10925 |
|
| 10926 |
|
| 10927 |
|
| 10928 |
|
| 10929 |
|
| 10930 |
|
| 10931 |
|
| 10932 |
|
| 10933 |
|
| 10934 |
|
| 10935 |
|
| 10936 |
|