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

Function yaml_read_file

src/cli/config_yaml_edit.c:641–767  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Calls 5

cbm_utf8_to_wideFunction · 0.85
yaml_snapshot_from_statFunction · 0.85

Tested by

no test coverage detected