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

Function diag_write_file

src/foundation/diagnostics.c:351–388  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

349}
350
351static bool diag_write_file(const char *base_name, const char *data, size_t length, bool append) {
352 char path[CBM_PATH_MAX];
353 if (!diag_full_path(base_name, path, sizeof(path))) {
354 return false;
355 }
356 wchar_t *wide_path = cbm_path_to_wide(path);
357 DWORD access = append ? FILE_APPEND_DATA : GENERIC_WRITE;
358 HANDLE handle = wide_path
359 ? CreateFileW(wide_path, access, FILE_SHARE_READ, NULL, CREATE_NEW,
360 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL)
361 : INVALID_HANDLE_VALUE;
362 DWORD creation_error = handle == INVALID_HANDLE_VALUE ? GetLastError() : ERROR_SUCCESS;
363 if (handle == INVALID_HANDLE_VALUE && wide_path &&
364 (creation_error == ERROR_FILE_EXISTS || creation_error == ERROR_ALREADY_EXISTS)) {
365 handle = CreateFileW(wide_path, access, FILE_SHARE_READ, NULL,
366 append ? OPEN_EXISTING : TRUNCATE_EXISTING,
367 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
368 }
369 free(wide_path);
370 if (!diag_win_handle_valid(handle)) {
371 if (handle != INVALID_HANDLE_VALUE) {
372 (void)CloseHandle(handle);
373 }
374 return false;
375 }
376 bool ok = true;
377 size_t offset = 0;
378 while (offset < length) {
379 DWORD chunk = length - offset > MAXDWORD ? MAXDWORD : (DWORD)(length - offset);
380 DWORD bytes_written = 0;
381 if (!WriteFile(handle, data + offset, chunk, &bytes_written, NULL) || bytes_written == 0) {
382 ok = false;
383 break;
384 }
385 offset += bytes_written;
386 }
387 return CloseHandle(handle) != 0 && ok;
388}
389
390static bool diag_native_rename(const char *source_name, const char *destination_name) {
391 char source[CBM_PATH_MAX];

Callers 2

append_trajectoryFunction · 0.85
write_diagnosticsFunction · 0.85

Calls 6

diag_full_pathFunction · 0.85
cbm_path_to_wideFunction · 0.85
diag_win_handle_validFunction · 0.85
diag_posix_file_validFunction · 0.85
writeFunction · 0.70
closeFunction · 0.50

Tested by

no test coverage detected