Read and validate the two generation keys. Extra store_meta keys belong to * other store features and are deliberately ignored. */
| 2665 | /* Read and validate the two generation keys. Extra store_meta keys belong to |
| 2666 | * other store features and are deliberately ignored. */ |
| 2667 | static int generation_metadata_read(cbm_store_t *s, bool *absent, char uid[17], char counter[21], |
| 2668 | uint64_t *counter_value) { |
| 2669 | *absent = false; |
| 2670 | sqlite3_stmt *object = NULL; |
| 2671 | int rc = sqlite3_prepare_v2(s->db, |
| 2672 | "SELECT type FROM sqlite_schema WHERE name='store_meta' " |
| 2673 | "AND type IN ('table','view') LIMIT 1;", |
| 2674 | CBM_NOT_FOUND, &object, NULL); |
| 2675 | if (rc != SQLITE_OK) { |
| 2676 | store_set_error_sqlite(s, "store generation schema probe prepare"); |
| 2677 | return CBM_STORE_ERR; |
| 2678 | } |
| 2679 | rc = sqlite3_step(object); |
| 2680 | if (rc == SQLITE_DONE) { |
| 2681 | sqlite3_finalize(object); |
| 2682 | *absent = true; |
| 2683 | return CBM_STORE_OK; |
| 2684 | } |
| 2685 | if (rc != SQLITE_ROW) { |
| 2686 | store_set_error_sqlite(s, "store generation schema probe"); |
| 2687 | sqlite3_finalize(object); |
| 2688 | return CBM_STORE_ERR; |
| 2689 | } |
| 2690 | const char *object_type = (const char *)sqlite3_column_text(object, 0); |
| 2691 | bool is_table = object_type && strcmp(object_type, "table") == 0; |
| 2692 | sqlite3_finalize(object); |
| 2693 | if (!is_table) { |
| 2694 | store_set_error(s, "store generation metadata is not a table"); |
| 2695 | return CBM_STORE_ERR; |
| 2696 | } |
| 2697 | |
| 2698 | sqlite3_stmt *stmt = NULL; |
| 2699 | rc = sqlite3_prepare_v2(s->db, |
| 2700 | "SELECT k, v, typeof(v) FROM store_meta " |
| 2701 | "WHERE k IN ('db_uid','mutation_gen') ORDER BY k;", |
| 2702 | CBM_NOT_FOUND, &stmt, NULL); |
| 2703 | if (rc != SQLITE_OK) { |
| 2704 | store_set_error_sqlite(s, "store generation metadata prepare"); |
| 2705 | return CBM_STORE_ERR; |
| 2706 | } |
| 2707 | bool have_uid = false; |
| 2708 | bool have_counter = false; |
| 2709 | while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { |
| 2710 | const char *key = (const char *)sqlite3_column_text(stmt, 0); |
| 2711 | const char *value = (const char *)sqlite3_column_text(stmt, 1); |
| 2712 | const char *value_type = (const char *)sqlite3_column_text(stmt, 2); |
| 2713 | if (!key || !value || !value_type || strcmp(value_type, "text") != 0) { |
| 2714 | store_set_error(s, "store generation metadata has a non-text value"); |
| 2715 | sqlite3_finalize(stmt); |
| 2716 | return CBM_STORE_ERR; |
| 2717 | } |
| 2718 | if ((size_t)sqlite3_column_bytes(stmt, 0) != strlen(key) || |
| 2719 | (size_t)sqlite3_column_bytes(stmt, 1) != strlen(value) || |
| 2720 | (size_t)sqlite3_column_bytes(stmt, 2) != strlen(value_type)) { |
| 2721 | store_set_error(s, "store generation metadata contains an embedded NUL"); |
| 2722 | sqlite3_finalize(stmt); |
| 2723 | return CBM_STORE_ERR; |
| 2724 | } |
no test coverage detected