static struct containerMeta* container_meta_duplicate(struct container *c) { struct containerMeta* base = &c->meta; struct containerMeta* dup = (struct containerMeta*) malloc( sizeof(struct containerMeta)); init_container_meta(dup); dup->id = base->id; dup->chunk_num = base->chunk_num; dup->data_size = base->data_size; GList *le = g_list_first (base->chunks); while(le) { struct metaEntry* value = (struct metaEntry*)le->data; struct metaEntry* me = (struct metaEntry*) malloc(sizeof(struct metaEntry)); memcpy(me, value, sizeof(struct metaEntry)); dup->chunks = g_list_append(dup->chunks, me); g_hash_table_insert(dup->map, &me->fp, g_list_last(dup->chunks)); le = g_list_next(le); } // GHashTableIter iter; // gpointer key, value; // g_hash_table_iter_init(&iter, base->map); // while (g_hash_table_iter_next(&iter, &key, &value)) { // struct metaEntry* me = (struct metaEntry*) malloc( // sizeof(struct metaEntry)); // memcpy(me, value, sizeof(struct metaEntry)); // g_hash_table_insert(dup->map, &me->fp, me); // } return dup; }
/* * For backup. */ struct container* create_container() { struct container *c = (struct container*) malloc(sizeof(struct container)); if (destor.simulation_level < SIMULATION_APPEND) c->data = calloc(1, CONTAINER_SIZE); else c->data = 0; init_container_meta(&c->meta); c->meta.id = container_count++; return c; }
struct containerMeta* retrieve_container_meta_by_id(containerid id) { struct containerMeta* cm = NULL; /* First, we find it in the buffer */ cm = sync_queue_find(container_buffer, container_check_id, &id, container_meta_duplicate); if (cm) return cm; cm = (struct containerMeta*) malloc(sizeof(struct containerMeta)); init_container_meta(cm); unsigned char buf[CONTAINER_META_SIZE]; pthread_mutex_lock(&mutex); if (destor.simulation_level >= SIMULATION_APPEND) fseek(fp, id * CONTAINER_META_SIZE + 8, SEEK_SET); else fseek(fp, (id + 1) * CONTAINER_SIZE - CONTAINER_META_SIZE + 8, SEEK_SET); fread(buf, CONTAINER_META_SIZE, 1, fp); pthread_mutex_unlock(&mutex); unser_declare; unser_begin(buf, CONTAINER_META_SIZE); unser_int64(cm->id); unser_int32(cm->chunk_num); unser_int32(cm->data_size); if(cm->id != id){ WARNING("expect %lld, but read %lld", id, cm->id); assert(cm->id == id); } int i; for (i = 0; i < cm->chunk_num; i++) { struct metaEntry* me = (struct metaEntry*) malloc( sizeof(struct metaEntry)); unser_bytes(&me->fp, sizeof(fingerprint)); unser_bytes(&me->len, sizeof(int32_t)); unser_bytes(&me->off, sizeof(int32_t)); g_hash_table_insert(cm->map, &me->fp, me); } return cm; }
static struct containerMeta* container_meta_duplicate(struct container *c) { struct containerMeta* base = &c->meta; struct containerMeta* dup = (struct containerMeta*) malloc( sizeof(struct containerMeta)); init_container_meta(dup); dup->id = base->id; dup->chunk_num = base->chunk_num; dup->data_size = base->data_size; GHashTableIter iter; gpointer key, value; g_hash_table_iter_init(&iter, base->map); while (g_hash_table_iter_next(&iter, &key, &value)) { struct metaEntry* me = (struct metaEntry*) malloc( sizeof(struct metaEntry)); memcpy(me, value, sizeof(struct metaEntry)); g_hash_table_insert(dup->map, &me->fp, me); } return dup; }
struct container* retrieve_container_by_id(containerid id) { struct container *c = (struct container*) malloc(sizeof(struct container)); init_container_meta(&c->meta); unsigned char *cur = 0; if (destor.simulation_level >= SIMULATION_RESTORE) { c->data = malloc(CONTAINER_META_SIZE); pthread_mutex_lock(&mutex); if (destor.simulation_level >= SIMULATION_APPEND) fseek(fp, id * CONTAINER_META_SIZE + 8, SEEK_SET); else fseek(fp, (id + 1) * CONTAINER_SIZE - CONTAINER_META_SIZE + 8, SEEK_SET); fread(c->data, CONTAINER_META_SIZE, 1, fp); pthread_mutex_unlock(&mutex); cur = c->data; } else { c->data = malloc(CONTAINER_SIZE); pthread_mutex_lock(&mutex); fseek(fp, id * CONTAINER_SIZE + 8, SEEK_SET); fread(c->data, CONTAINER_SIZE, 1, fp); pthread_mutex_unlock(&mutex); cur = &c->data[CONTAINER_SIZE - CONTAINER_META_SIZE]; } unser_declare; unser_begin(cur, CONTAINER_META_SIZE); unser_int64(c->meta.id); unser_int32(c->meta.chunk_num); unser_int32(c->meta.data_size); if(c->meta.id != id){ WARNING("expect %lld, but read %lld", id, c->meta.id); assert(c->meta.id == id); } int i; for (i = 0; i < c->meta.chunk_num; i++) { struct metaEntry* me = (struct metaEntry*) malloc( sizeof(struct metaEntry)); unser_bytes(&me->fp, sizeof(fingerprint)); unser_bytes(&me->len, sizeof(int32_t)); unser_bytes(&me->off, sizeof(int32_t)); g_hash_table_insert(c->meta.map, &me->fp, me); } unser_end(cur, CONTAINER_META_SIZE); if (destor.simulation_level >= SIMULATION_RESTORE) { free(c->data); c->data = 0; } return c; }