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

Function text_read_file

src/cli/config_text_edit.c:386–513  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

384}
385
386static int text_read_file(const char *path, char **data_out, size_t *len_out,
387 text_file_snapshot_t *snapshot_out) {
388 if (!path || !data_out || !len_out || !snapshot_out) {
389 return TEXT_ERROR;
390 }
391 *data_out = NULL;
392 *len_out = 0U;
393 memset(snapshot_out, 0, sizeof(*snapshot_out));
394
395#ifdef _WIN32
396 wchar_t *wide_path = cbm_utf8_to_wide(path);
397 if (!wide_path) {
398 return TEXT_ERROR;
399 }
400 HANDLE handle = CreateFileW(
401 wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
402 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
403 free(wide_path);
404 if (handle == INVALID_HANDLE_VALUE) {
405 DWORD error = GetLastError();
406 if (error != ERROR_FILE_NOT_FOUND && error != ERROR_PATH_NOT_FOUND) {
407 return TEXT_ERROR;
408 }
409 char *empty = (char *)calloc(1U, 1U);
410 if (!empty) {
411 return TEXT_ERROR;
412 }
413 *data_out = empty;
414 return TEXT_OK;
415 }
416 text_file_snapshot_t before;
417 if (text_snapshot_from_handle(handle, &before) != TEXT_OK) {
418 CloseHandle(handle);
419 return TEXT_ERROR;
420 }
421 size_t len = (size_t)before.size;
422 char *data = (char *)malloc(len + 1U);
423 if (!data) {
424 CloseHandle(handle);
425 return TEXT_ERROR;
426 }
427 DWORD read_count = 0U;
428 BOOL read_ok = ReadFile(handle, data, (DWORD)len, &read_count, NULL);
429 text_file_snapshot_t after;
430 int after_result = text_snapshot_from_handle(handle, &after);
431 BOOL close_ok = CloseHandle(handle);
432 if (!read_ok || read_count != (DWORD)len || after_result != TEXT_OK || !close_ok ||
433 !text_snapshot_equal(&before, &after)) {
434 free(data);
435 return TEXT_ERROR;
436 }
437#else
438#ifndef O_NOFOLLOW
439 (void)path;
440 return TEXT_ERROR;
441#else
442 int flags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK;
443#ifdef O_CLOEXEC

Calls 9

cbm_utf8_to_wideFunction · 0.85
text_snapshot_equalFunction · 0.85
lstatFunction · 0.85
text_snapshot_from_statFunction · 0.85
freadFunction · 0.85
openFunction · 0.50

Tested by

no test coverage detected