| 572 | #endif |
| 573 | |
| 574 | static int text_replace_file(const char *temp_path, const char *path, int destination_exists) { |
| 575 | #ifdef _WIN32 |
| 576 | wchar_t *wide_temp = cbm_utf8_to_wide(temp_path); |
| 577 | wchar_t *wide_path = cbm_utf8_to_wide(path); |
| 578 | if (!wide_temp || !wide_path) { |
| 579 | free(wide_temp); |
| 580 | free(wide_path); |
| 581 | return TEXT_ERROR; |
| 582 | } |
| 583 | BOOL replaced = destination_exists ? ReplaceFileW(wide_path, wide_temp, NULL, |
| 584 | REPLACEFILE_WRITE_THROUGH, NULL, NULL) |
| 585 | : MoveFileExW(wide_temp, wide_path, MOVEFILE_WRITE_THROUGH); |
| 586 | free(wide_temp); |
| 587 | free(wide_path); |
| 588 | return replaced ? TEXT_OK : TEXT_ERROR; |
| 589 | #else |
| 590 | if (!destination_exists) { |
| 591 | if (link(temp_path, path) != 0) { |
| 592 | return TEXT_ERROR; |
| 593 | } |
| 594 | if (cbm_unlink(temp_path) != 0) { |
| 595 | return TEXT_ERROR; |
| 596 | } |
| 597 | return text_sync_parent_directory(path); |
| 598 | } |
| 599 | if (rename(temp_path, path) != 0) { |
| 600 | return TEXT_ERROR; |
| 601 | } |
| 602 | return text_sync_parent_directory(path); |
| 603 | #endif |
| 604 | } |
| 605 | |
| 606 | static const char *text_temp_name(const char *temp_path) { |
| 607 | const char *slash = strrchr(temp_path, '/'); |
no test coverage detected