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.
This commit is contained in:
Ingo Weinhold 2013-05-08 04:51:48 +02:00
parent d0815ca03b
commit 9ea1940c48
2 changed files with 84 additions and 2 deletions

View File

@ -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

View File

@ -11,6 +11,14 @@
#include <Application.h>
#include <Mime.h>
#include <Path.h>
#include <mime/AppMetaMimeCreator.h>
#include <mime/Database.h>
#include <mime/DatabaseLocation.h>
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 <directory>\n"
" Instead of the system MIME DB use the given directory\n"
" <directory>. 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");