MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / toml_read_file

Function toml_read_file

src/cli/config_toml_edit.c:413–541  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

411#endif
412
413static int toml_read_file(const char *path, char **out_data, size_t *out_len,
414 toml_file_snapshot_t *snapshot_out) {
415 if (!path || !out_data || !out_len || !snapshot_out) {
416 return TOML_EDIT_ERR;
417 }
418 *out_data = NULL;
419 *out_len = 0;
420 memset(snapshot_out, 0, sizeof(*snapshot_out));
421
422#ifdef _WIN32
423 wchar_t *wide_path = cbm_utf8_to_wide(path);
424 if (!wide_path) {
425 return TOML_EDIT_ERR;
426 }
427 HANDLE handle = CreateFileW(
428 wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
429 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
430 free(wide_path);
431 if (handle == INVALID_HANDLE_VALUE) {
432 DWORD error = GetLastError();
433 if (error != ERROR_FILE_NOT_FOUND && error != ERROR_PATH_NOT_FOUND) {
434 return TOML_EDIT_ERR;
435 }
436 char *empty = (char *)malloc(1);
437 if (!empty) {
438 return TOML_EDIT_ERR;
439 }
440 empty[0] = '\0';
441 *out_data = empty;
442 return TOML_EDIT_OK;
443 }
444 toml_file_snapshot_t before;
445 if (toml_snapshot_from_handle(handle, &before) != TOML_EDIT_OK) {
446 CloseHandle(handle);
447 return TOML_EDIT_ERR;
448 }
449 size_t size = (size_t)before.size;
450 char *data = (char *)malloc(size + 1U);
451 if (!data) {
452 CloseHandle(handle);
453 return TOML_EDIT_ERR;
454 }
455 DWORD read_count = 0U;
456 BOOL read_ok = ReadFile(handle, data, (DWORD)size, &read_count, NULL);
457 toml_file_snapshot_t after;
458 int after_result = toml_snapshot_from_handle(handle, &after);
459 BOOL close_ok = CloseHandle(handle);
460 if (!read_ok || read_count != (DWORD)size || after_result != TOML_EDIT_OK || !close_ok ||
461 !toml_snapshot_equal(&before, &after)) {
462 free(data);
463 return TOML_EDIT_ERR;
464 }
465#else
466#ifndef O_NOFOLLOW
467 return TOML_EDIT_ERR;
468#else
469 int flags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK;
470#ifdef O_CLOEXEC

Calls 9

cbm_utf8_to_wideFunction · 0.85
toml_snapshot_equalFunction · 0.85
lstatFunction · 0.85
toml_snapshot_from_statFunction · 0.85
freadFunction · 0.85
openFunction · 0.50

Tested by

no test coverage detected