| 597 | #endif |
| 598 | |
| 599 | static int toml_replace_atomic(const char *temp_path, const char *path, int existed) { |
| 600 | #ifdef _WIN32 |
| 601 | wchar_t *wide_temp = cbm_utf8_to_wide(temp_path); |
| 602 | wchar_t *wide_path = cbm_utf8_to_wide(path); |
| 603 | if (!wide_temp || !wide_path) { |
| 604 | free(wide_temp); |
| 605 | free(wide_path); |
| 606 | return TOML_EDIT_ERR; |
| 607 | } |
| 608 | /* ReplaceFileW preserves the destination ACL and other mergeable metadata; |
| 609 | * merge failures stay fatal so metadata is never silently discarded. */ |
| 610 | BOOL replaced = |
| 611 | existed ? ReplaceFileW(wide_path, wide_temp, NULL, REPLACEFILE_WRITE_THROUGH, NULL, NULL) |
| 612 | : MoveFileExW(wide_temp, wide_path, MOVEFILE_WRITE_THROUGH); |
| 613 | free(wide_temp); |
| 614 | free(wide_path); |
| 615 | return replaced ? TOML_EDIT_OK : TOML_EDIT_ERR; |
| 616 | #else |
| 617 | if (!existed) { |
| 618 | if (link(temp_path, path) != 0) { |
| 619 | return TOML_EDIT_ERR; |
| 620 | } |
| 621 | if (cbm_unlink(temp_path) != 0) { |
| 622 | return TOML_EDIT_ERR; |
| 623 | } |
| 624 | return toml_sync_parent_directory(path); |
| 625 | } |
| 626 | if (rename(temp_path, path) != 0) { |
| 627 | return TOML_EDIT_ERR; |
| 628 | } |
| 629 | return toml_sync_parent_directory(path); |
| 630 | #endif |
| 631 | } |
| 632 | |
| 633 | static const char *toml_temp_name(const char *temp_path) { |
| 634 | const char *slash = strrchr(temp_path, '/'); |
no test coverage detected