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

Function toml_read_file

src/cli/config_toml_edit.c:404–519  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Calls 4

cbm_utf8_to_wideFunction · 0.85
toml_snapshot_equalFunction · 0.85
toml_snapshot_from_statFunction · 0.85

Tested by

no test coverage detected