| 1389 | #endif |
| 1390 | |
| 1391 | static int jl_read_file(const char *path, char **content_out, size_t *length_out, bool *missing_out, |
| 1392 | jl_file_snapshot_t *snapshot_out) { |
| 1393 | *content_out = NULL; |
| 1394 | *length_out = 0; |
| 1395 | *missing_out = false; |
| 1396 | memset(snapshot_out, 0, sizeof(*snapshot_out)); |
| 1397 | #ifdef _WIN32 |
| 1398 | wchar_t *wide_path = cbm_utf8_to_wide(path); |
| 1399 | if (!wide_path) { |
| 1400 | return -1; |
| 1401 | } |
| 1402 | HANDLE handle = CreateFileW( |
| 1403 | wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 1404 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1405 | free(wide_path); |
| 1406 | if (handle == INVALID_HANDLE_VALUE) { |
| 1407 | DWORD error = GetLastError(); |
| 1408 | if (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) { |
| 1409 | *missing_out = true; |
| 1410 | return 0; |
| 1411 | } |
| 1412 | return -1; |
| 1413 | } |
| 1414 | jl_file_snapshot_t before; |
| 1415 | if (jl_snapshot_from_handle(handle, &before) != 0) { |
| 1416 | CloseHandle(handle); |
| 1417 | return -1; |
| 1418 | } |
| 1419 | size_t length = (size_t)before.size; |
| 1420 | char *content = malloc(length + 1U); |
| 1421 | if (!content) { |
| 1422 | CloseHandle(handle); |
| 1423 | return -1; |
| 1424 | } |
| 1425 | DWORD read_count = 0U; |
| 1426 | BOOL read_ok = ReadFile(handle, content, (DWORD)length, &read_count, NULL); |
| 1427 | jl_file_snapshot_t after; |
| 1428 | int after_result = jl_snapshot_from_handle(handle, &after); |
| 1429 | BOOL close_ok = CloseHandle(handle); |
| 1430 | if (!read_ok || read_count != (DWORD)length || after_result != 0 || !close_ok || |
| 1431 | !jl_snapshot_state_equal(&before, &after)) { |
| 1432 | free(content); |
| 1433 | return -1; |
| 1434 | } |
| 1435 | #else |
| 1436 | #ifndef O_NOFOLLOW |
| 1437 | (void)path; |
| 1438 | return -1; |
| 1439 | #else |
| 1440 | int flags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK; |
| 1441 | #ifdef O_CLOEXEC |
| 1442 | flags |= O_CLOEXEC; |
| 1443 | #endif |
| 1444 | /* A symlink the invoking user owns, inside an opted-in configuration |
| 1445 | * root, is read through the descriptor the helper validated on the |
| 1446 | * target's pinned parent directory (#1954); everything else is opened by |
| 1447 | * name with O_NOFOLLOW exactly as before. */ |
| 1448 | cbm_config_edit_target_t target; |
no test coverage detected