rename() with overwrite semantics on every platform: POSIX rename already * replaces atomically; Windows rename fails with EEXIST when the target * exists, so use write-through MoveFileExW(MOVEFILE_REPLACE_EXISTING) there * (wide paths — raw MoveFileExA would re-mangle non-ASCII cache paths). */
| 1339 | * exists, so use write-through MoveFileExW(MOVEFILE_REPLACE_EXISTING) there |
| 1340 | * (wide paths — raw MoveFileExA would re-mangle non-ASCII cache paths). */ |
| 1341 | int cbm_rename_replace(const char *src, const char *dst) { |
| 1342 | #ifdef _WIN32 |
| 1343 | wchar_t *wsrc = cbm_path_to_wide(src); |
| 1344 | wchar_t *wdst = cbm_path_to_wide(dst); |
| 1345 | int ret = CBM_NOT_FOUND; |
| 1346 | if (wsrc && wdst) { |
| 1347 | if (MoveFileExW(wsrc, wdst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { |
| 1348 | ret = 0; |
| 1349 | } else { |
| 1350 | /* Translate the Win32 error into errno so callers can report WHY. |
| 1351 | * |
| 1352 | * Callers log `errno` after a failed rename (see |
| 1353 | * finalize.rename_failed in the pipeline). Without this the value |
| 1354 | * is whatever happened to be left there by an unrelated CRT call, |
| 1355 | * so on Windows the one field that should explain an atomic-publish |
| 1356 | * failure was noise. #1620 is exactly that: an ACL problem surfaced |
| 1357 | * to the user as "Pipeline failed. Check repo_path exists and |
| 1358 | * contains source files" — blaming their repository — because |
| 1359 | * ERROR_ACCESS_DENIED never reached the log. |
| 1360 | * |
| 1361 | * ERROR_ACCESS_DENIED is the interesting one here: MoveFileEx needs |
| 1362 | * DELETE on the destination, which a cache file created under an |
| 1363 | * empty or foreign DACL does not grant. */ |
| 1364 | DWORD error = GetLastError(); |
| 1365 | switch (error) { |
| 1366 | case ERROR_ACCESS_DENIED: |
| 1367 | case ERROR_WRITE_PROTECT: |
| 1368 | errno = EACCES; |
| 1369 | break; |
| 1370 | case ERROR_FILE_NOT_FOUND: |
| 1371 | case ERROR_PATH_NOT_FOUND: |
| 1372 | errno = ENOENT; |
| 1373 | break; |
| 1374 | case ERROR_SHARING_VIOLATION: |
| 1375 | case ERROR_LOCK_VIOLATION: |
| 1376 | case ERROR_USER_MAPPED_FILE: |
| 1377 | errno = EBUSY; |
| 1378 | break; |
| 1379 | case ERROR_NOT_SAME_DEVICE: |
| 1380 | errno = EXDEV; |
| 1381 | break; |
| 1382 | case ERROR_DISK_FULL: |
| 1383 | errno = ENOSPC; |
| 1384 | break; |
| 1385 | case ERROR_INVALID_NAME: |
| 1386 | case ERROR_FILENAME_EXCED_RANGE: |
| 1387 | errno = ENAMETOOLONG; |
| 1388 | break; |
| 1389 | default: |
| 1390 | errno = EIO; |
| 1391 | break; |
| 1392 | } |
| 1393 | ret = CBM_NOT_FOUND; |
| 1394 | } |
| 1395 | } |
| 1396 | free(wsrc); |
| 1397 | free(wdst); |
| 1398 | return ret; |