static int stats_file_history_update(struct stats_file *data_file) { struct stats_file _history_file, *history_file; struct stats_file _temp_file, *temp_file; int err; history_file = &_history_file; temp_file = &_temp_file; bzero(history_file, sizeof(struct stats_file)); bzero(temp_file, sizeof(struct stats_file)); err = stats_open(history_file, data_file->history_name); if (err < 0) return err; stats_file_setup(history_file); err = stats_open_temp(temp_file); if (err < 0) { stats_free(history_file); return err; } stats_file_setup(temp_file); summarize(data_file, history_file, temp_file); err = stats_file_close_swap(history_file, temp_file); return err; }
static void history_file_update(struct stats_file *data_file, const char *history_file_name) { struct stats_file _history_file; struct stats_file tempory_file; struct stats_file *history_file = NULL; if (stats_open(&_history_file, history_file_name) == 0) history_file = &_history_file; if (stats_open(&tempory_file, NULL) < 0) { if (history_file) stats_close(history_file); return; } summarize(data_file, history_file, &tempory_file, 13); swap_and_close_files(history_file, &tempory_file); }
static struct stats *open_stats(const char *name) { struct stats *stats = NULL; int err; err = stats_create(name,&stats); if (err != S_OK) { printf("Failed to create stats: %s\n", error_message(err)); return NULL; } err = stats_open(stats); if (err != S_OK) { printf("Failed to open stats: %s\n", error_message(err)); stats_free(stats); return NULL; } return stats; }
int main(int argc, char *argv[]) { GOptionContext *context; GError *error = NULL; struct stats_file_header *hdr; struct stats_file data, *data_file; struct stats_record *rec; time_t start_ts; int err; rec = NULL; data_file = &data; putenv("TZ=GMT0"); context = g_option_context_new(NULL); g_option_context_add_main_entries(context, options, NULL); if (!g_option_context_parse(context, &argc, &argv, &error)) { if (error) { g_printerr("%s\n", error->message); g_error_free(error); } else g_printerr("An unknown error occurred\n"); exit(1); } g_option_context_free(context); if (argc < 2) { printf("Usage: %s [FILENAME]\n", argv[0]); exit(0); } err = stats_open(data_file, argv[1]); if (err < 0) { fprintf(stderr, "failed open file %s\n", argv[1]); exit(1); } if (option_last_file_name) { struct stats_file last; if (stats_open(&last, option_last_file_name) < 0) { fprintf(stderr, "failed open file %s\n", option_last_file_name); exit(1); } rec = get_end(&last); } if (option_start_ts == -1) start_ts = time(NULL); else start_ts = option_start_ts; if (option_create > 0) stats_create(data_file, option_create, option_interval, start_ts, rec); hdr = get_hdr(data_file); if (hdr->magic != MAGIC) { fprintf(stderr, "header file magic test failed\n"); goto err; } stats_file_update_cache(data_file); stats_hdr_info(data_file); if (option_dump) stats_print_entries(data_file); if (option_summary) stats_print_diff(data_file); if (option_info_file_name) history_file_update(data_file, option_info_file_name); err: stats_close(data_file); return 0; }