Skip to content

Commit

Permalink
Merge pull request #374 from openzim/check_magic_file
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Oct 12, 2023
2 parents e4440d1 + 109afe5 commit e08484d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
26 changes: 22 additions & 4 deletions src/zimwriterfs/zimwriterfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ bool verboseFlag = false;
bool withoutFTIndex = false;
bool noUuid = false;
bool dontCheckArgs = false;
bool continue_without_magic = false;

bool thereAreMissingArguments()
{
Expand Down Expand Up @@ -149,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()
{
Expand Down Expand Up @@ -216,6 +224,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;
Expand Down Expand Up @@ -263,6 +272,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'},
Expand All @@ -281,7 +291,7 @@ void parse_args(int argc, char** argv)
tags = optarg;
break;
case 'V':
printVersions();
printZimWriterFsVersions();
exit(0);
break;
case 'h':
Expand Down Expand Up @@ -351,6 +361,9 @@ void parse_args(int argc, char** argv)
case 'B':
dontCheckArgs = true;
break;
case 'M':
continue_without_magic = true;
break;
}
}
} while (c != -1);
Expand Down Expand Up @@ -475,14 +488,19 @@ 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);
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;
if (! continue_without_magic) {
exit(1);
}
}
pthread_mutex_init(&verboseMutex, NULL);

try {
parse_args(argc, argv);

const zim::Metadata metadata = makeMetadata();

if ( !checkMetadata(metadata) ) {
Expand Down

0 comments on commit e08484d

Please sign in to comment.