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

Function read_file_alloc

src/pipeline/artifact.c:149–174  ·  view source on GitHub ↗

Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */

Source from the content-addressed store, hash-verified

147
148/* Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */
149static char *read_file_alloc(const char *path, size_t *out_len) {
150 FILE *fp = cbm_fopen(path, "rb");
151 if (!fp) {
152 return NULL;
153 }
154 (void)fseek(fp, 0, SEEK_END);
155 long sz = ftell(fp);
156 if (sz <= 0) {
157 (void)fclose(fp);
158 return NULL;
159 }
160 (void)fseek(fp, 0, SEEK_SET);
161 char *buf = malloc((size_t)sz);
162 if (!buf) {
163 (void)fclose(fp);
164 return NULL;
165 }
166 size_t rd = fread(buf, ART_NUL, (size_t)sz, fp);
167 (void)fclose(fp);
168 if ((long)rd != sz) {
169 free(buf);
170 return NULL;
171 }
172 *out_len = (size_t)sz;
173 return buf;
174}
175
176/* Write buffer to file atomically (write to tmp, rename). Returns 0 on success. */
177static int write_file_atomic(const char *path, const char *data, size_t len,

Callers 6

read_metadata_versionFunction · 0.85
prepare_snapshot_dbFunction · 0.85
cbm_artifact_importFunction · 0.85
cbm_artifact_commitFunction · 0.85

Calls 2

cbm_fopenFunction · 0.85
freadFunction · 0.85

Tested by

no test coverage detected