From 0bae5bf6945e4e8834d1e2f3aaa129fc8adc70b2 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 10 Oct 2023 11:57:27 +0200 Subject: [PATCH 1/5] Exit zimwriterfs with a clear message if magic database cannot be loaded. --- src/zimwriterfs/zimwriterfs.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/zimwriterfs/zimwriterfs.cpp b/src/zimwriterfs/zimwriterfs.cpp index d8375180..7c5ca9a3 100644 --- a/src/zimwriterfs/zimwriterfs.cpp +++ b/src/zimwriterfs/zimwriterfs.cpp @@ -477,7 +477,10 @@ int main(int argc, char** argv) { /* Init */ magic = magic_open(MAGIC_MIME); - magic_load(magic, NULL); + if (magic_load(magic, NULL) != 0) { + std::cerr << "Impossible to load magic file. Set `MAGIC` environment variable to a `magic` (or `magic.mgc`) file." << std::endl; + exit(1); + } pthread_mutex_init(&verboseMutex, NULL); try { From bab3b331a29a2438c8d93abf47cb2c0e613dacfd Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 10 Oct 2023 12:02:40 +0200 Subject: [PATCH 2/5] Add an option to accept running even if magic db cannot be loaded. --- src/zimwriterfs/zimwriterfs.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/zimwriterfs/zimwriterfs.cpp b/src/zimwriterfs/zimwriterfs.cpp index 7c5ca9a3..4e2a9ce4 100644 --- a/src/zimwriterfs/zimwriterfs.cpp +++ b/src/zimwriterfs/zimwriterfs.cpp @@ -65,6 +65,7 @@ bool verboseFlag = false; bool withoutFTIndex = false; bool noUuid = false; bool dontCheckArgs = false; +bool continue_without_magic = false; bool thereAreMissingArguments() { @@ -216,6 +217,7 @@ void usage() << std::endl; std::cout << "\t-s, --scraper\t\tname & version of tool used to produce HTML content" << std::endl; + std::cout << "\t--skip-libmagic-check\tAccept to run even if magic file cannot be loaded (mimetypes in the zim file may be wrong)." << std::endl; // --no-uuid and --dont-check-arguments are dev options, let's keep them secret // std::cout << "\t-U, --no-uuid\t\tdon't generate a random UUID" << std::endl; // std::cout << "\t-B, --dont-check-arguments\t\tdon't check arguments (and possibly produce a broken ZIM file)" << std::endl; @@ -263,6 +265,7 @@ void parse_args(int argc, char** argv) {"threads", required_argument, 0, 'J'}, {"no-uuid", no_argument, 0, 'U'}, {"dont-check-arguments", no_argument, 0, 'B'}, + {"skip-libmagic-check", no_argument, 0, 'M'}, // Only for backward compatibility {"withFullTextIndex", no_argument, 0, 'i'}, @@ -351,6 +354,9 @@ void parse_args(int argc, char** argv) case 'B': dontCheckArgs = true; break; + case 'M': + continue_without_magic = true; + break; } } } while (c != -1); @@ -479,7 +485,9 @@ int main(int argc, char** argv) magic = magic_open(MAGIC_MIME); if (magic_load(magic, NULL) != 0) { std::cerr << "Impossible to load magic file. Set `MAGIC` environment variable to a `magic` (or `magic.mgc`) file." << std::endl; - exit(1); + if (! continue_without_magic) { + exit(1); + } } pthread_mutex_init(&verboseMutex, NULL); From 3b5f0cf6d0033b8741760f28254d8515871000f3 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 10 Oct 2023 12:09:05 +0200 Subject: [PATCH 3/5] Add magic in the versions output. --- src/zimwriterfs/zimwriterfs.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/zimwriterfs/zimwriterfs.cpp b/src/zimwriterfs/zimwriterfs.cpp index 4e2a9ce4..6027872d 100644 --- a/src/zimwriterfs/zimwriterfs.cpp +++ b/src/zimwriterfs/zimwriterfs.cpp @@ -150,6 +150,13 @@ bool isVerbose() return retVal; } +void printZimWriterFsVersions(std::ostream& out = std::cout) { + out << "zim-tools " << VERSION << std::endl; + out << "+ libmagic " << magic_version() << std::endl; + out << std::endl; + zim::printVersions(out); +} + /* Print correct console usage options */ void usage() { @@ -284,7 +291,7 @@ void parse_args(int argc, char** argv) tags = optarg; break; case 'V': - printVersions(); + printZimWriterFsVersions(); exit(0); break; case 'h': From 714c88b855d1be79c626f6df37b6fd8f064dd579 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 10 Oct 2023 12:14:45 +0200 Subject: [PATCH 4/5] Update README with information about zimwriterfs and magic. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 8ec4b738..93841fb5 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,17 @@ distribution. Try then with a source tarball distributed by the problematic upstream project or even directly from the source code repository. +### Magic File + +`zimwriterfs` use libmagic to detect the mimetype of files to add. +It needs to load the magic database. Most of the time, this file is already present +in your system and everything works. +But sometime, this file may be not present or cannot be found. +You can set the `MAGIC` environment variable to point to this file. + +`zimwriterfs` will refuse to run if this file is not found. You can force +it to run passing the option `--skip-libmagic-check`. + License ------- From 109afe5d319d4dca2d96bb8fcd50f00663d8193b Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 10 Oct 2023 12:19:01 +0200 Subject: [PATCH 5/5] Give the user a change to get help before failing because of missing magic. --- src/zimwriterfs/zimwriterfs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zimwriterfs/zimwriterfs.cpp b/src/zimwriterfs/zimwriterfs.cpp index 6027872d..81ff9d20 100644 --- a/src/zimwriterfs/zimwriterfs.cpp +++ b/src/zimwriterfs/zimwriterfs.cpp @@ -488,6 +488,8 @@ void create_zim(const zim::Metadata& metadata) /* Main program entry point */ int main(int argc, char** argv) { + parse_args(argc, argv); + /* Init */ magic = magic_open(MAGIC_MIME); if (magic_load(magic, NULL) != 0) { @@ -499,8 +501,6 @@ int main(int argc, char** argv) pthread_mutex_init(&verboseMutex, NULL); try { - parse_args(argc, argv); - const zim::Metadata metadata = makeMetadata(); if ( !checkMetadata(metadata) ) {