comments on libmagic

Brett Funderburg brettf at deepfile.com
Tue Oct 14 12:02:49 EDT 2003


I've been playing around with libmagic over the past few days and I'd like
to offer the following comments for feedback.

I've noticed some inconsistencies in how the api works. For example,

Case 1

(pseudo-code)

magic_open()
result = magic_file("/path/to/some/file")
if !result
   print magic_error()  <- "No magic files loaded"


Case 2

magic_open()
magic_load()
result = magic_file("/file/not/found")
print magic_error()  <- ""
print result         <- "Can't stat file..."


Case 1 works as I would expect it to. I think it's preferable in Case 2 to
have it work like the following:

magic_open()
magic_load()
result = magic_file("/file/not/found")
if !result
   print magic_error()  <- "Can't stat file"

Indeed, I think this is how the man page describes how it should work.
However, this points out an additional limitation. Relying on simple 0,
-1, NULL return values from library functions does not provide enough
information to the caller about what has gone wrong. Textual error
descriptions provided by magic_error are fine but aren't suitable for
error handling. As a consumer of the libmagic api, this is what I would
like to do:

my_app_get_file_type(char* file_path) {

... set up libmagic stuff ...

result = magic_file(file_path);
if(result != NULL)
    return result;    <- "ELF executable, etc."

  error = magic_errno()
  switch(error) {
  case STAT_ERROR:
    ... handle this case ...
    print magic_error()  <- "Doesn't exist..."

  case OPEN_ERROR:
   ... handle this other case ...
    print magic_error()  <- "Can't open..."
  }
}

Comments?

brett




More information about the File mailing list