Reputation: 21
i try use this code:
#include <stdio.h>
#include <magic.h>
int main(void)
{
char *actual_file = "/file/you/want.yay";
const char *magic_full;
magic_t magic_cookie;
magic_cookie = magic_open(MAGIC_MIME);
if (magic_cookie == NULL) {
printf("unable to initialize magic library\n");
return 1;
}
printf("Loading default magic database\n");
if (magic_load(magic_cookie, NULL) != 0) {
printf("cannot load magic database - %s\n", magic_error(magic_cookie));
magic_close(magic_cookie);
return 1;
}
magic_full = magic_file(magic_cookie, actual_file);
printf("%s\n", magic_full);
magic_close(magic_cookie);
return 0;
}
when execute this code it appears the messagge: “cannot load magic database”. why? I don’t understand what are the causes….
To compile I use visual studio 2010, there aren't any build errors.
Upvotes: 2
Views: 2864
Reputation: 2195
Your code is correct (apart from not checking magic_full
for NULL
at the end. It actually works on my machine.
You have a problem with the magic library - perhaps you don't have the proper magic signatures file, or you don't have access to it, or even file is corrupted! Please check that if you have the MAGIC
env var set it is pointing to the right file!
Also try to determine the default file for magic_load like this:
$ strace ./magic 2>&1 | grep open
open("/etc/ld.so.cache", O_RDONLY) = 3
open("/usr/lib64/libmagic.so.1", O_RDONLY) = 3
open("/lib64/libc.so.6", O_RDONLY) = 3
open("/lib64/libz.so.1", O_RDONLY) = 3
open("/usr/share/file/magic.mime.mgc", O_RDONLY) = 3
$
This: "/usr/share/file/magic.mime.mgc"
is what you're looking for.
Then, do file
on the same file.yay
with strace again (this will confirm is if the *mgc
file was good):
$ strace file --mime `/path/to/file.yay` 2>&1 | grep open
...
open("/usr/share/file/magic.mime.mgc", O_RDONLY) = 3
...
$
Good luck!
Upvotes: 1
Reputation: 3975
The man-page for libmagic has this.
magic_load(magic_t cookie, const char *filename);
You are passing NULL
for the filename
parameter so it will attempt to load the default database file. Which appears to be failing. Change that to actual_file
perhaps and try again.
Upvotes: 1
Reputation: 400129
Probably the default magic database (what you get when passing NULL
as the second argumen to magic_load()
is not installed, or it's not found under Windows. Try being explicit, i.e. give it the actual absolute filename.
The docs say:
The magic_load() function must be used to load the the colon separated list of database files passed in as filename, or NULL for the default database file before any magic queries can performed.
Upvotes: 2