| 385 | } |
| 386 | |
| 387 | bool zeroFillFile(MMKVFileHandle_t file, size_t startPos, size_t size) { |
| 388 | if (file == INVALID_HANDLE_VALUE) { |
| 389 | return false; |
| 390 | } |
| 391 | if (size == 0) { |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | LARGE_INTEGER position = {}; |
| 396 | position.QuadPart = startPos; |
| 397 | if (!SetFilePointerEx(file, position, nullptr, FILE_BEGIN)) { |
| 398 | MMKVError("fail to lseek fd[%p], error:%d", file, GetLastError()); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | static const char zeros[4096] = {0}; |
| 403 | while (size >= sizeof(zeros)) { |
| 404 | DWORD bytesWritten = 0; |
| 405 | if (!WriteFile(file, zeros, sizeof(zeros), &bytesWritten, nullptr)) { |
| 406 | MMKVError("fail to write fd[%p], error:%d", file, GetLastError()); |
| 407 | return false; |
| 408 | } |
| 409 | size -= bytesWritten; |
| 410 | } |
| 411 | if (size > 0) { |
| 412 | DWORD bytesWritten = 0; |
| 413 | if (!WriteFile(file, zeros, (DWORD) size, &bytesWritten, nullptr)) { |
| 414 | MMKVError("fail to write fd[%p], error:%d", file, GetLastError()); |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | static bool ftruncate(MMKVFileHandle_t file, size_t size) { |
| 422 | LARGE_INTEGER large = {}; |