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

Function yaml_read_file

src/cli/config_yaml_edit.c:627–741  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

625#endif
626
627static int yaml_read_file(const char *path, char **out_data, size_t *out_len,
628 yaml_file_snapshot_t *out_snapshot) {
629 *out_data = NULL;
630 *out_len = 0U;
631 memset(out_snapshot, 0, sizeof(*out_snapshot));
632#ifdef _WIN32
633 wchar_t *wide_path = cbm_utf8_to_wide(path);
634 if (!wide_path) {
635 return YAML_ERROR;
636 }
637 HANDLE handle = CreateFileW(
638 wide_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
639 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
640 free(wide_path);
641 if (handle == INVALID_HANDLE_VALUE) {
642 DWORD error = GetLastError();
643 if (error != ERROR_FILE_NOT_FOUND && error != ERROR_PATH_NOT_FOUND) {
644 return YAML_ERROR;
645 }
646 char *empty = (char *)calloc(YAML_UNIT, YAML_UNIT);
647 if (!empty) {
648 return YAML_ERROR;
649 }
650 *out_data = empty;
651 return 0;
652 }
653 yaml_file_snapshot_t before;
654 if (yaml_snapshot_from_handle(handle, &before) != 0) {
655 CloseHandle(handle);
656 return YAML_ERROR;
657 }
658 size_t len = (size_t)before.size;
659 char *data = (char *)malloc(len + YAML_UNIT);
660 if (!data) {
661 CloseHandle(handle);
662 return YAML_ERROR;
663 }
664 DWORD read_count = 0U;
665 BOOL read_ok = ReadFile(handle, data, (DWORD)len, &read_count, NULL);
666 yaml_file_snapshot_t after;
667 int after_result = yaml_snapshot_from_handle(handle, &after);
668 BOOL close_ok = CloseHandle(handle);
669 if (!read_ok || read_count != (DWORD)len || after_result != 0 || !close_ok ||
670 !yaml_snapshot_state_equal(&before, &after)) {
671 free(data);
672 return YAML_ERROR;
673 }
674#else
675#ifndef O_NOFOLLOW
676 (void)path;
677 return YAML_ERROR;
678#else
679 int flags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK;
680#ifdef O_CLOEXEC
681 flags |= O_CLOEXEC;
682#endif
683 int descriptor = open(path, flags);
684 if (descriptor < 0) {

Calls 5

cbm_utf8_to_wideFunction · 0.85
yaml_snapshot_from_statFunction · 0.85
yaml_validate_text_bytesFunction · 0.85

Tested by

no test coverage detected