我尝试使用以下代码:
#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;
}当执行这段代码时,它会出现消息:“无法加载魔术数据库”。为什么?我不明白是什么导致了…。
编译时我使用的是visual studio 2010,没有任何构建错误。
发布于 2013-02-07 21:53:59
可能没有安装默认的魔术数据库(将NULL作为第二个参数传递给magic_load()时获得的数据库),或者在Windows下找不到它。尝试显式,也就是给它一个实际的绝对文件名。
医生说:
在执行任何魔术查询之前,必须使用magic_load()函数加载以冒号分隔的数据库文件列表,将其作为filename传入;对于默认数据库文件,则为NULL。
发布于 2013-02-07 21:50:34
libmagic的手册页上有这样的内容。
magic_load(magic_t cookie, const char *filename);
您正在为filename参数传递NULL,因此它将尝试加载默认的数据库文件。这似乎是失败的。也许可以将其更改为actual_file,然后重试。
发布于 2013-02-07 22:26:56
你的代码是正确的(除了在最后没有检查magic_full中的NULL。它实际上可以在我的机器上工作。
你的魔术库有问题--可能你没有正确的魔术签名文件,或者你没有访问它的权限,或者甚至文件被破坏了!如果您设置了MAGIC环境变量,请检查它是否指向正确的文件!
还要尝试确定magic_load的默认文件,如下所示:
$ 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
$这个:"/usr/share/file/magic.mime.mgc"就是你要找的。然后,使用strace在同一file.yay上再次执行file (这将确认*mgc文件是否正确):
$ strace file --mime `/path/to/file.yay` 2>&1 | grep open
...
open("/usr/share/file/magic.mime.mgc", O_RDONLY) = 3
...
$祝好运!
https://stackoverflow.com/questions/14752656
复制相似问题