2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
user2050969
  • 21
  • 1
  • 2

3 Answers3

2

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I find the problem. You are right. I don't find where program search default magic database, so I insert another database that I find. – user2050969 Feb 08 '13 at 10:23
  • excuse me, but how did you write it exactly? I tried magic_load(mhandle_magic,"C:\\magic.mgc"); and magic_load(mhandle_magic,"C:\\magic"), but it still fails (Did you take file with database from Linux machine? or how? ) – Tebe Feb 09 '13 at 01:39
  • 1
    hah, it has turned out that there are different versions of magic.mgc file, it's not the same for every version of libmagic, so one need check at first whether proper version of magic.mgc file is used, so be carefully! It was my problem with which I had stuck. Perhaps, it helps someone. – Tebe Feb 11 '13 at 00:57
1

The man-page for libmagic has this.

magic_load(magic_t cookie, const char *filename);

You are passing NULLfor 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.

jcopenha
  • 3,935
  • 1
  • 17
  • 15
1

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!

RumburaK
  • 1,985
  • 1
  • 21
  • 30