| 1590 | #endif |
| 1591 | |
| 1592 | static int jl_replace_atomic(const char *temp_path, const char *path, bool destination_exists) { |
| 1593 | #ifdef _WIN32 |
| 1594 | wchar_t *wide_temp = cbm_utf8_to_wide(temp_path); |
| 1595 | wchar_t *wide_path = cbm_utf8_to_wide(path); |
| 1596 | if (!wide_temp || !wide_path) { |
| 1597 | free(wide_temp); |
| 1598 | free(wide_path); |
| 1599 | return -1; |
| 1600 | } |
| 1601 | /* ReplaceFileW retains the destination's ACL and other mergeable metadata; |
| 1602 | * refusing merge errors is safer than silently dropping that metadata. */ |
| 1603 | BOOL replaced = destination_exists ? ReplaceFileW(wide_path, wide_temp, NULL, |
| 1604 | REPLACEFILE_WRITE_THROUGH, NULL, NULL) |
| 1605 | : MoveFileExW(wide_temp, wide_path, MOVEFILE_WRITE_THROUGH); |
| 1606 | free(wide_temp); |
| 1607 | free(wide_path); |
| 1608 | return replaced ? 0 : -1; |
| 1609 | #else |
| 1610 | if (!destination_exists) { |
| 1611 | if (link(temp_path, path) != 0) { |
| 1612 | return -1; |
| 1613 | } |
| 1614 | if (cbm_unlink(temp_path) != 0) { |
| 1615 | return -1; |
| 1616 | } |
| 1617 | return jl_sync_parent_directory(path); |
| 1618 | } |
| 1619 | if (rename(temp_path, path) != 0) { |
| 1620 | return -1; |
| 1621 | } |
| 1622 | return jl_sync_parent_directory(path); |
| 1623 | #endif |
| 1624 | } |
| 1625 | |
| 1626 | static const char *jl_temp_name(const char *temp_path) { |
| 1627 | const char *slash = strrchr(temp_path, '/'); |
no test coverage detected