? cleanup.patch Index: update-mime-database.c =================================================================== RCS file: /cvs/mime/shared-mime-info/update-mime-database.c,v retrieving revision 1.39 diff -u -p -r1.39 update-mime-database.c --- update-mime-database.c 19 Dec 2005 16:37:47 -0000 1.39 +++ update-mime-database.c 5 Jan 2007 14:51:46 -0000 @@ -482,16 +482,17 @@ static void load_type(Type *type, xmlNod * database. If called more than once, information read in later calls * overrides information read previously. */ -static void load_source_file(const char *filename) +static gboolean load_source_file(const char *filename) { xmlDoc *doc; xmlNode *root, *node; + gboolean retval = FALSE; doc = xmlParseFile(filename); if (!doc) { g_warning(_("Failed to parse '%s'\n"), filename); - return; + return FALSE; } root = xmlDocGetRootElement(doc); @@ -543,11 +544,11 @@ static void load_source_file(const char if (!error) { - g_return_if_fail(type != NULL); + g_return_val_if_fail(type != NULL, FALSE); load_type(type, node, &error); } else - g_return_if_fail(type == NULL); + g_return_val_if_fail(type == NULL, FALSE); if (error) { @@ -559,9 +560,12 @@ static void load_source_file(const char filename, error->message); g_error_free(error); } + else + retval = TRUE; } out: xmlFreeDoc(doc); + return retval; } /* Used as the sort function for sorting GPtrArrays */ @@ -576,7 +580,7 @@ static gint strcmp2(gconstpointer a, gco /* 'path' should be a 'packages' directory. Loads the information from * every file in the directory. */ -static void scan_source_dir(const char *path) +static gboolean scan_source_dir(const char *path) { DIR *dir; struct dirent *ent; @@ -584,6 +588,7 @@ static void scan_source_dir(const char * GPtrArray *files; int i; gboolean have_override = FALSE; + gboolean got_data = FALSE; dir = opendir(path); if (!dir) @@ -618,13 +623,16 @@ static void scan_source_dir(const char * gchar *leaf = (gchar *) files->pdata[i]; filename = g_strconcat(path, "/", leaf, NULL); - load_source_file(filename); + if (load_source_file(filename)) + got_data = TRUE; g_free(filename); } for (i = 0; i < files->len; i++) g_free(files->pdata[i]); g_ptr_array_free(files, TRUE); + + return got_data; } /* Save doc as XML as filename, 0 on success or -1 on failure */ @@ -2646,19 +2654,153 @@ write_cache (FILE *cache) } +static FILE * +open_stream (const char *filename) +{ + FILE *stream = fopen(filename, "wb"); + + if (!stream) + { + g_printerr("Failed to open '%s' for writing\n", + filename); + exit (EXIT_FAILURE); + } + + return stream; +} + + +static void +write_files (const char *mime_dir) +{ + g_hash_table_foreach(types, write_out_type, (gpointer) mime_dir); + + { + FILE *globs; + char *globs_path; + globs_path = g_strconcat(mime_dir, "/globs.new", NULL); + globs = open_stream (globs_path); + fprintf(globs, + "# This file was automatically generated by the\n" + "# update-mime-database command. DO NOT EDIT!\n"); + g_hash_table_foreach(globs_hash, write_out_glob, globs); + fclose(globs); + + atomic_update(globs_path); + g_free(globs_path); + } + + { + FILE *stream; + char *magic_path; + int i; + magic_path = g_strconcat(mime_dir, "/magic.new", NULL); + stream = open_stream (magic_path); + fwrite("MIME-Magic\0\n", 1, 12, stream); + + if (magic_array->len) + g_ptr_array_sort(magic_array, cmp_magic); + for (i = 0; i < magic_array->len; i++) + { + Magic *magic = (Magic *) magic_array->pdata[i]; + + write_magic(stream, magic); + } + fclose(stream); + + atomic_update(magic_path); + g_free(magic_path); + } + + { + FILE *stream; + char *ns_path; + + ns_path = g_strconcat(mime_dir, "/XMLnamespaces.new", NULL); + stream = open_stream (ns_path); + write_namespaces(stream); + + atomic_update(ns_path); + g_free(ns_path); + } + + { + FILE *stream; + char *path; + + path = g_strconcat(mime_dir, "/subclasses.new", NULL); + stream = open_stream (path); + write_subclasses(stream); + + atomic_update(path); + g_free(path); + } + + { + FILE *stream; + char *path; + + path = g_strconcat(mime_dir, "/aliases.new", NULL); + stream = open_stream (path); + write_aliases(stream); + + atomic_update(path); + g_free(path); + } + + { + FILE *stream; + char *path; + + path = g_strconcat(mime_dir, "/mime.cache.new", NULL); + stream = open_stream (path); + write_cache(stream); + + atomic_update(path); + g_free(path); + } +} + + +static void +delete_files (const char *mime_dir) +{ + guint i; + char *path; + const char *names[] = { + "globs", "magic", "XMLnamespaces", "subclasses", + "aliases", "mime.cache" + }; + + remove(mime_dir); + + for (i = 0; i < G_N_ELEMENTS (media_types); ++i) + { + path = g_build_filename(mime_dir, media_types[i], NULL); + remove(path); + g_free (path); + } + + for (i = 0; i < G_N_ELEMENTS (names); ++i) + { + char *path = g_build_filename(mime_dir, names[i], NULL); + unlink(path); + g_free(path); + } +} + + int main(int argc, char **argv) { char *mime_dir = NULL; char *package_dir = NULL; + gboolean has_packages; int opt; while ((opt = getopt(argc, argv, "hv")) != -1) { switch (opt) { - case '?': - usage(argv[0]); - return EXIT_FAILURE; case 'h': usage(argv[0]); return EXIT_SUCCESS; @@ -2668,7 +2810,8 @@ int main(int argc, char **argv) VERSION "\n" COPYING); return EXIT_SUCCESS; default: - abort(); + usage(argv[0]); + return EXIT_FAILURE; } } @@ -2721,120 +2864,19 @@ int main(int argc, char **argv) g_free, free_string_list); alias_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); - - scan_source_dir(package_dir); + + has_packages = scan_source_dir(package_dir); g_free(package_dir); delete_old_types(mime_dir); - g_hash_table_foreach(types, write_out_type, (gpointer) mime_dir); - - { - FILE *globs; - char *globs_path; - globs_path = g_strconcat(mime_dir, "/globs.new", NULL); - globs = fopen(globs_path, "wb"); - if (!globs) - g_error("Failed to open '%s' for writing\n", - globs_path); - fprintf(globs, - "# This file was automatically generated by the\n" - "# update-mime-database command. DO NOT EDIT!\n"); - g_hash_table_foreach(globs_hash, write_out_glob, globs); - fclose(globs); - - atomic_update(globs_path); - g_free(globs_path); - } - - { - FILE *stream; - char *magic_path; - int i; - magic_path = g_strconcat(mime_dir, "/magic.new", NULL); - stream = fopen(magic_path, "wb"); - if (!stream) - g_error("Failed to open '%s' for writing\n", - magic_path); - fwrite("MIME-Magic\0\n", 1, 12, stream); - - if (magic_array->len) - g_ptr_array_sort(magic_array, cmp_magic); - for (i = 0; i < magic_array->len; i++) - { - Magic *magic = (Magic *) magic_array->pdata[i]; - - write_magic(stream, magic); - } - fclose(stream); - - atomic_update(magic_path); - g_free(magic_path); - } - - { - FILE *stream; - char *ns_path; - - ns_path = g_strconcat(mime_dir, "/XMLnamespaces.new", NULL); - stream = fopen(ns_path, "wb"); - if (!stream) - g_error("Failed to open '%s' for writing\n", - ns_path); - - write_namespaces(stream); - - atomic_update(ns_path); - g_free(ns_path); - } - - { - FILE *stream; - char *path; - - path = g_strconcat(mime_dir, "/subclasses.new", NULL); - stream = fopen(path, "wb"); - if (!stream) - g_error("Failed to open '%s' for writing\n", - path); - - write_subclasses(stream); - - atomic_update(path); - g_free(path); - } - - { - FILE *stream; - char *path; - - path = g_strconcat(mime_dir, "/aliases.new", NULL); - stream = fopen(path, "wb"); - if (!stream) - g_error("Failed to open '%s' for writing\n", - path); - - write_aliases(stream); - - atomic_update(path); - g_free(path); - } - - { - FILE *stream; - char *path; - - path = g_strconcat(mime_dir, "/mime.cache.new", NULL); - stream = fopen(path, "wb"); - if (!stream) - g_error("Failed to open '%s' for writing\n", - path); - - write_cache(stream); - - atomic_update(path); - g_free(path); - } + if (has_packages) + { + write_files (mime_dir); + check_in_path_xdg_data(mime_dir); + } + else + delete_files (mime_dir); g_ptr_array_foreach(magic_array, (GFunc)magic_free, NULL); g_ptr_array_free(magic_array, TRUE); @@ -2847,7 +2889,5 @@ int main(int argc, char **argv) g_print("***\n"); - check_in_path_xdg_data(mime_dir); - return EXIT_SUCCESS; }