From 9ea1940c48f96fb923fe654792d5372ab800ef7c Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Wed, 8 May 2013 04:51:48 +0200 Subject: [PATCH] mimeset: WIP to support custom MIME DB directories Add option -m/--mimedb for specifying one or more directories to be used as the MIME DB instead of the system MIME DB. Currently only works with --apps and only non-recursive. When fully implemented the new feature will be used in the build system and on Haiku when building packages to generated the MIME DB entries for applications, so those can be included in the same package. Furthermore it will be possible to use the MIME DB the build system already generates to identify files before packaging them. --- src/bin/Jamfile | 6 +++- src/bin/mimeset.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/bin/Jamfile b/src/bin/Jamfile index ee517b1777..d6f2c6137a 100644 --- a/src/bin/Jamfile +++ b/src/bin/Jamfile @@ -91,7 +91,6 @@ StdBinCommands listattr.cpp listfont.cpp listres.cpp - mimeset.cpp mkindex.cpp message.cpp modifiers.cpp @@ -225,6 +224,11 @@ StdBinCommands watch.c : libncurses.a : $(haiku-utils_rsrc) ; +# mimeset needs libstorage_kit_mime.a +StdBinCommands + mimeset.cpp + : be libstorage_kit_mime.a $(TARGET_LIBSTDC++) : $(haiku-utils_rsrc) ; + # Localization of some applications DoCatalogs dstcheck : x-vnd.Haiku-cmd-dstconfig diff --git a/src/bin/mimeset.cpp b/src/bin/mimeset.cpp index abb54a1de1..45dc1aa517 100644 --- a/src/bin/mimeset.cpp +++ b/src/bin/mimeset.cpp @@ -11,6 +11,14 @@ #include #include +#include + +#include +#include +#include + + +using namespace BPrivate::Storage::Mime; #ifdef HAIKU_HOST_PLATFORM_SUNOS @@ -25,6 +33,8 @@ bool gFiles = true; bool gApps = false; int gForce = B_UPDATE_MIME_INFO_NO_FORCE; +static Database* sDatabase = NULL; + static void usage(int status) @@ -49,6 +59,11 @@ usage(int status) " type of a file.\n" " -h, --help\n" " Display this help information.\n" + " -m, --mimedb \n" + " Instead of the system MIME DB use the given directory\n" + " . The option can occur multiple times to specify a\n" + " list of directories. MIME DB changes are written to the first\n" + " specified directory.\n" "\n" "Obsolete options:\n" " -all (synonymous with --all)\n" @@ -60,6 +75,31 @@ usage(int status) } +static status_t +process_file_with_custom_mime_db(const BEntry& entry) +{ + // TODO: Support recursion! + // TODO: Support MIME update! + AppMetaMimeCreator creator(sDatabase, NULL, gForce); + + entry_ref ref; + status_t error = entry.GetRef(&ref); + bool entryIsDir = false; + if (error == B_OK) + error = creator.Do(ref, &entryIsDir); + if (error != B_OK) { + BPath path; + fprintf(stderr, "%s: \"%s\": %s\n", + sProgramName, + entry.GetPath(&path) == B_OK ? path.Path() : entry.Name(), + strerror(error)); + return error; + } + + return B_OK; +} + + static status_t process_file(const char* path) { @@ -69,6 +109,9 @@ process_file(const char* path) if (!entry.Exists()) status = B_ENTRY_NOT_FOUND; + if (sDatabase != NULL) + return process_file_with_custom_mime_db(entry); + if (gFiles && status == B_OK) status = update_mime_info(path, true, true, gForce); if (gApps && status == B_OK) @@ -98,16 +141,19 @@ main(int argc, const char** argv) argv[i] = "--apps"; } + BStringList databaseDirectories; + for (;;) { static struct option sLongOptions[] = { { "all", no_argument, 0, 'A' }, { "apps", no_argument, 0, 'a' }, { "help", no_argument, 0, 'h' }, + { "mimedb", required_argument, 0, 'm' }, { 0, 0, 0, 0 } }; opterr = 0; // don't print errors - int c = getopt_long(argc, (char**)argv, "aAfFh", sLongOptions, + int c = getopt_long(argc, (char**)argv, "aAfFhm:", sLongOptions, NULL); if (c == -1) break; @@ -130,6 +176,9 @@ main(int argc, const char** argv) case 'h': usage(0); break; + case 'm': + databaseDirectories.Add(optarg); + break; default: usage(1); break; @@ -139,6 +188,35 @@ main(int argc, const char** argv) if (argc - optind < 1) usage(1); + // set up custom MIME DB, if specified + DatabaseLocation databaseLocation; + if (!databaseDirectories.IsEmpty()) { + // the first directory must exist + if (!BEntry(databaseDirectories.StringAt(0)).IsDirectory()) { + fprintf(stderr, "%s: Specified path \"%s\" doesn't exist or isn't " + "a directory.", sProgramName, + databaseDirectories.StringAt(0).String()); + exit(1); + } + + int32 count = databaseDirectories.CountStrings(); + for (int32 i = 0; i < count; i++) + databaseLocation.AddDirectory(databaseDirectories.StringAt(i)); + + sDatabase = new(std::nothrow) Database(&databaseLocation, NULL, NULL); + if (sDatabase == NULL) { + fprintf(stderr, "%s: Out of memory!\n", sProgramName); + exit(1); + } + + status_t error = sDatabase->InitCheck(); + if (error != B_OK) { + fprintf(stderr, "%s: Failed to init MIME DB: %s\n", sProgramName, + strerror(error)); + exit(1); + } + } + // process files BApplication app("application/x-vnd.haiku.mimeset");