static int editor_db_read (void) { char buffer[1024]; int size = 0; editor_db_section *section; int error = 0; int signature; int res; res = roadmap_file_read (EditorDataFile, &signature, sizeof (int)); if (res != sizeof (int) || signature != DB_SIGNATURE) return -1; while (1) { char *head = buffer; int count; int item_id; int align; res = roadmap_file_read (EditorDataFile, buffer + size, sizeof(buffer) - size); if (res <= 0) return error; size += res; while ((count = editor_db_read_items(&head, size - (head - buffer), §ion, &item_id, &error)) >= 0) { int i; for (i=0; i<count; i++) { if (item_id != -1) { /* Update */ void *data = editor_db_get_record(section, item_id); assert(data); if (section->flag_committed) { editor_db_update_generation (section, ((editor_record_header *)data)->generation); } memcpy(data, head, section->record_size); } else { /* Add */ if (editor_db_add_record(section, head, head + section->item_offset, 0) == -1) return -1; if (section->flag_committed) { editor_db_update_generation (section, ((editor_record_header *)head)->generation); } } head += section->record_size; } align = (count * section->record_size) % EDITOR_DB_ALIGN; if (align) head += EDITOR_DB_ALIGN - align; } if (error) break; size -= (head - buffer); if (size > 0) memmove(buffer, head, size); } return error; }
static int load_index(int fips, void **base, size_t *size) { const char *map_path = roadmap_db_map_path (); char name[30]; char filename[512]; const char *suffix = "index"; snprintf (name, sizeof (name), "%05d_%s%s", fips, suffix, ROADMAP_DATA_TYPE); roadmap_path_format (filename, sizeof (filename), map_path, name); roadmap_log(ROADMAP_DEBUG,"index filename: %s", filename); RoadMapFile file = roadmap_file_open (filename, "r"); if (!ROADMAP_FILE_IS_VALID(file)) { return -1; } *size = favail(file); *base = malloc (*size); int res = roadmap_file_read (file, *base, *size); roadmap_file_close (file); if (res != (int)*size) { free (*base); return -1; } return 0; }
int roadmap_io_read (RoadMapIO *io, void *data, int size) { switch (io->subsystem) { case ROADMAP_IO_FILE: return roadmap_file_read (io->os.file, data, size); case ROADMAP_IO_NET: return roadmap_net_receive (io->os.socket, data, size); case ROADMAP_IO_PIPE: return roadmap_spawn_read_pipe (io->os.pipe, data, size); case ROADMAP_IO_NULL: return 0; /* Cannot receive anything from there. */ } return -1; }