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

Function ui_log_import_legacy

src/ui/http_server.c:744–821  ·  view source on GitHub ↗

One-time, bounded migration of old file-only info records. Preserve original * timestamps and leave project provenance unknown; no current project is inferred. * Read oldest archive before current file. The checkpoint and inserts commit * together, so restart/retry cannot import an unkeyed legacy row twice. */

Source from the content-addressed store, hash-verified

742 * Read oldest archive before current file. The checkpoint and inserts commit
743 * together, so restart/retry cannot import an unkeyed legacy row twice. */
744static bool ui_log_import_legacy(sqlite3 *db, const char *cache) {
745 sqlite3_stmt *st = NULL;
746 if (sqlite3_prepare_v2(db, "SELECT 1 FROM activity_meta WHERE key='ui_legacy_import'", -1, &st,
747 NULL) != SQLITE_OK)
748 return false;
749 int rc = sqlite3_step(st);
750 sqlite3_finalize(st);
751 if (rc == SQLITE_ROW)
752 return true;
753 if (rc != SQLITE_DONE || sqlite3_exec(db, "BEGIN IMMEDIATE", NULL, NULL, NULL) != SQLITE_OK)
754 return false;
755 bool ok = true, partial = false;
756 for (int archive = 1; archive >= 0 && ok; archive--) {
757 char path[CBM_SZ_2K];
758 int n = snprintf(path, sizeof(path), "%s/logs/ui.log%s", cache, archive ? ".1" : "");
759 if (n < 0 || (size_t)n >= sizeof(path)) {
760 ok = false;
761 break;
762 }
763 int64_t size = cbm_file_size(path);
764 if (size <= 0)
765 continue;
766 FILE *file = cbm_fopen(path, "rb");
767 if (!file) {
768 ok = false;
769 break;
770 }
771 size_t length = size > UI_LOG_TAIL_READ_BYTES ? UI_LOG_TAIL_READ_BYTES : (size_t)size;
772 char *data = cbm_alloc(CBM_MEM_CLASS_OTHER, length + 1);
773 if (!data || (size > (int64_t)length &&
774 fseek(file, (long)(size - (int64_t)length), SEEK_SET) != 0)) {
775 cbm_free(CBM_MEM_CLASS_OTHER, data);
776 fclose(file);
777 ok = false;
778 break;
779 }
780 size_t got = fread(data, 1, length, file);
781 ok = !ferror(file);
782 fclose(file);
783 data[got] = '\0';
784 char *line = data;
785 if (size > (int64_t)length) {
786 partial = true;
787 char *end = strchr(line, '\n');
788 line = end ? end + 1 : data + got;
789 }
790 while (ok && *line) {
791 char *end = strchr(line, '\n');
792 if (!end) {
793 partial = true; /* Incomplete final write, never import torn JSON. */
794 break;
795 }
796 *end = '\0';
797 yyjson_doc *record = yyjson_read(line, strlen(line), 0);
798 yyjson_val *root = record ? yyjson_doc_get_root(record) : NULL;
799 if (yyjson_is_obj(root) && yyjson_is_str(yyjson_obj_get(root, "received")) &&
800 yyjson_is_str(yyjson_obj_get(root, "page")) &&
801 yyjson_is_str(yyjson_obj_get(root, "session")))

Callers 1

activity_db_lockedFunction · 0.85

Calls 6

cbm_file_sizeFunction · 0.85
cbm_fopenFunction · 0.85
cbm_allocFunction · 0.85
cbm_freeFunction · 0.85
freadFunction · 0.85
cbm_activity_ui_logFunction · 0.85

Tested by

no test coverage detected