int fs_metadata_set_int(fs_metadata *m, const char *key, long long int val) { for (int e=0; e < m->length; e++) { if (!strcmp(m->entries[e].key, key)) { g_free(m->entries[e].val); m->entries[e].val = g_strdup_printf("%lld", val); return 0; } } return fs_metadata_add_int(m, key, val); }
void setup_metadata(kbconfig *config) { LOG(1, "Writing metadata.\n"); fs_metadata *md = fs_metadata_open(config->name); fs_metadata_clear(md); fs_metadata_set(md, FS_MD_NAME, config->name); fs_metadata_set(md, FS_MD_HASHFUNC, FS_HASH); fs_metadata_set(md, FS_MD_STORE, "native"); fs_metadata_set(md, FS_MD_MODEL_DATA, "true"); if (config->model_files) { fs_metadata_set(md, FS_MD_MODEL_FILES, "true"); } else { fs_metadata_set(md, FS_MD_MODEL_FILES, "false"); } fs_metadata_set(md, FS_MD_CODE_VERSION, GIT_REV); for (int seg = 0; seg < config->segments; seg++) { if (primary_segment(config, seg)) fs_metadata_add_int(md, FS_MD_SEGMENT_P, seg); if (mirror_segment(config, seg)) fs_metadata_add_int(md, FS_MD_SEGMENT_M, seg); } /* Generate store UUID for skolemisation */ #if defined(USE_LINUX_UUID) uuid_t uu; uuid_string_t uus; uuid_generate(uu); uuid_unparse(uu, uus); #elif defined(USE_BSD_UUID) uuid_t uu; char *uus = NULL; int status = -1; uuid_create(&uu, &status); if (status) { fs_error(LOG_ERR, "bad return from uuid_create"); exit(1); } uuid_to_string(&uu, &uus, &status); if (status || uus == NULL) { fs_error(LOG_ERR, "bad return from uuid_to_string"); exit(1); } #elif defined(USE_OSSP_UUID) uuid_t *uu = NULL; char *uus = NULL; if (uuid_create(&uu)) { fs_error(LOG_ERR, "bad return from uuid_create"); exit(1); } if (uuid_make(uu, UUID_MAKE_V1)) { fs_error(LOG_ERR, "bad return from uuid_make"); exit(1); } if (uuid_export(uu, UUID_FMT_STR, &uus, NULL) || uus == NULL) { fs_error(LOG_ERR, "bad return from uuid_export"); exit(1); } #endif fs_metadata_add(md, FS_MD_UUID, uus); #if defined(USE_OSSP_UUID) uuid_destroy(uu); #endif unsigned char stage1[20], stage2[16]; char hash[33] = "none"; int now = 0; if (config->password) { md5_state_t md5; char *pw = g_strdup_printf("%s:%s", config->name, config->password); /* stage1 will contain the 4 byte Unix time_t value as a salt ... */ now = time(NULL); memcpy(stage1, &now, sizeof(now)); /* ... followed by the on-wire 16 byte MD5 auth string */ md5_init(&md5); md5_append(&md5, (md5_byte_t *) pw, strlen(pw)); md5_finish(&md5, stage1 + 4); /* now use MD5 on all 20 bytes and store both the salt and the hash */ md5_init(&md5); md5_append(&md5, stage1, sizeof(stage1)); md5_finish(&md5, stage2); sprintf(hash, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", stage2[0], stage2[1], stage2[2], stage2[3], stage2[4], stage2[5], stage2[6], stage2[7], stage2[8], stage2[9], stage2[10], stage2[11], stage2[12], stage2[13], stage2[14], stage2[15]); g_free(pw); } fs_metadata_add_int(md, FS_MD_VERSION, FS_CURRENT_TABLE_VERSION); fs_metadata_add_int(md, FS_MD_SEGMENTS, config->segments); fs_metadata_add_int(md, FS_MD_SALT, now); fs_metadata_add_int(md, FS_MD_BNODE, 1); fs_metadata_add(md, FS_MD_HASH, hash); fs_metadata_flush(md); fs_metadata_close(md); fs_error(LOG_INFO, "created RDF metadata for KB %s", config->name); }