static gboolean pdbtool_merge_dir(const gchar *dir, gboolean recursive, GString *merged) { GDir *pdb_dir; gboolean ok = TRUE; GError *error = NULL; const gchar *filename; if ((pdb_dir = g_dir_open(dir, 0, &error)) == NULL) { fprintf(stderr, "Error opening directory %s, error='%s'\n", merge_dir, error ? error->message : "Unknown error"); g_clear_error(&error); return FALSE; } while ((filename = g_dir_read_name(pdb_dir)) != NULL && ok) { gchar *full_name = g_build_filename(dir, filename, NULL); if (recursive && is_file_directory(full_name)) { ok = pdbtool_merge_dir(full_name, recursive, merged); } else if (is_file_regular(full_name) && (!merge_glob || g_pattern_match_simple(merge_glob, filename))) { ok = pdbtool_merge_file(full_name, merged); } g_free(full_name); } g_dir_close(pdb_dir); return TRUE; }
static gint pdbtool_merge(int argc, char *argv[]) { GDate date; GError *error = NULL; GString *merged = NULL; gchar *buff; gboolean ok; if (!merge_dir) { fprintf(stderr, "No directory is specified to merge from\n"); return 1; } if (!patterndb_file) { fprintf(stderr, "No patterndb file is specified to merge to\n"); return 1; } merged = g_string_sized_new(4096); g_date_clear(&date, 1); g_date_set_time_t(&date, time (NULL)); buff = g_markup_printf_escaped("<?xml version='1.0' encoding='UTF-8'?>\n<patterndb version='4' pub_date='%04d-%02d-%02d'>", g_date_get_year(&date), g_date_get_month(&date), g_date_get_day(&date)); g_string_append(merged, buff); g_free(buff); ok = pdbtool_merge_dir(merge_dir, merge_recursive, merged); g_string_append(merged, "</patterndb>\n"); if (ok && !g_file_set_contents(patterndb_file, merged->str, merged->len, &error)) { fprintf(stderr, "Error storing patterndb; filename='%s', errror='%s'\n", patterndb_file, error ? error->message : "Unknown error"); ok = FALSE; } g_string_free(merged, TRUE); return ok ? 0 : 1; }