=== modified file 'myloader.c' --- myloader.c 2015-08-24 18:35:43 +0000 +++ myloader.c 2016-05-08 08:11:51 +0000 @@ -193,6 +193,49 @@ return errors ? EXIT_FAILURE : EXIT_SUCCESS; } +typedef struct { + GString *filename; + off_t size; +} TableEntry; + +static TableEntry *table_entry_new(const gchar *filename) +{ + GString *path = g_string_new(directory); + + path = g_string_append(path, "/"); + path = g_string_append(path, filename); + + GStatBuf statistics; + int ret = g_stat(path->str, &statistics); + + g_string_free(path, TRUE); + + if(ret != 0) + g_critical("couldn't stat file: %s\n", g_strerror(errno)); + + TableEntry *entry = malloc(sizeof(TableEntry)); + entry->filename = g_string_new(filename); + + if(ret == 0) + entry->size = statistics.st_size; + else + entry->size = 0; + + return entry; +} + +static void table_entry_free(gpointer entry) +{ + g_string_free(((TableEntry *) entry)->filename, TRUE); + free(entry); +} + +static gint table_entry_compare(gconstpointer first, gconstpointer second) +{ + return ((const TableEntry *) first)->size == ((const TableEntry *) second)->size ? 0 : + (((const TableEntry *) first)->size < ((const TableEntry *) second)->size ? 1 : -1); +} + void restore_databases(struct configuration *conf, MYSQL *conn) { GError *error= NULL; GDir* dir= g_dir_open(directory, 0, &error); @@ -214,6 +257,7 @@ } g_dir_rewind(dir); + GList *table_entries = NULL; while((filename= g_dir_read_name(dir))) { if (!source_db || g_str_has_prefix(filename, g_strdup_printf("%s.", source_db))){ @@ -223,12 +267,19 @@ && !g_strrstr(filename, "-schema-post.sql") && !g_strrstr(filename, "-schema-create.sql") && g_strrstr(filename, ".sql")) { - add_table(filename, conf); + table_entries = g_list_append(table_entries, table_entry_new(filename)); } } } + table_entries = g_list_sort(table_entries, table_entry_compare); + + const GList *entry = NULL; + for(entry = table_entries; entry; entry = entry->next) + add_table(((const TableEntry *) entry->data)->filename->str, conf); + g_dir_close(dir); + g_list_free_full(table_entries, table_entry_free); }