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

Function toml_write_atomic

src/cli/config_toml_edit.c:611–706  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

609}
610
611static int toml_write_atomic(const char *path, const char *old_data, size_t old_len,
612 const char *new_data, size_t new_len,
613 const toml_file_snapshot_t *snapshot) {
614 if (old_len > TOML_EDIT_MAX_BYTES || new_len > TOML_EDIT_MAX_BYTES) {
615 return TOML_EDIT_ERR;
616 }
617 if (old_len == new_len && (old_len == 0 || memcmp(old_data, new_data, old_len) == 0)) {
618 return TOML_EDIT_OK;
619 }
620 size_t path_len = strlen(path);
621 static const char suffix[] = ".XXXXXX";
622 if (path_len > SIZE_MAX - sizeof(suffix)) {
623 return TOML_EDIT_ERR;
624 }
625 char *temp_path = (char *)malloc(path_len + sizeof(suffix));
626 if (!temp_path) {
627 return TOML_EDIT_ERR;
628 }
629 memcpy(temp_path, path, path_len);
630 memcpy(temp_path + path_len, suffix, sizeof(suffix));
631
632 int fd = cbm_mkstemp(temp_path);
633 if (fd < 0) {
634 free(temp_path);
635 return TOML_EDIT_ERR;
636 }
637 FILE *file = toml_fdopen(fd, "wb");
638 if (!file) {
639 (void)toml_close(fd);
640 (void)cbm_unlink(temp_path);
641 free(temp_path);
642 return TOML_EDIT_ERR;
643 }
644
645 int failed = new_len != 0 && fwrite(new_data, 1, new_len, file) != new_len;
646 if (!failed && fflush(file) != 0) {
647 failed = 1;
648 }
649#ifndef _WIN32
650 if (!failed && snapshot->exists &&
651 fchown(cbm_fileno(file), snapshot->owner, snapshot->group) != 0) {
652 failed = 1;
653 }
654 mode_t mode = snapshot->exists ? snapshot->mode & 0777U : 0600U;
655 if (!failed && fchmod(cbm_fileno(file), mode) != 0) {
656 failed = 1;
657 }
658#endif
659 if (!failed && TOML_SYNC(cbm_fileno(file)) != 0) {
660 failed = 1;
661 }
662 if (fclose(file) != 0) {
663 failed = 1;
664 }
665 if (failed) {
666 (void)cbm_unlink(temp_path);
667 free(temp_path);
668 return TOML_EDIT_ERR;

Calls 5

cbm_mkstempFunction · 0.85
cbm_unlinkFunction · 0.85
toml_read_fileFunction · 0.85
toml_replace_atomicFunction · 0.85

Tested by

no test coverage detected