comments on libmagic

Brett Funderburg brettf at deepfile.com
Tue Oct 14 16:28:34 EDT 2003


> On Oct 14, 11:02am, brettf at deepfile.com ("Brett Funderburg") wrote:
> -- Subject: comments on libmagic
>
   -snip-
> |
> | Case 1
> |
> | (pseudo-code)
> |
> | magic_open()
> | result = magic_file("/path/to/some/file")
> | if !result
> |    print magic_error()  <- "No magic files loaded"
>
> Yes, this is because this is an internal magic error.

Right, I purposely used it as an example of "correct" behavior.

>
> | Case 2
> |
> | magic_open()
> | magic_load()
> | result = magic_file("/file/not/found")
> | print magic_error()  <- ""
> | print result         <- "Can't stat file..."
>
> Yes, because this is what file(1) is supposed to return. Actually by posix
> it is supposed to return "not found".

Right, I understand. My point was that file(1) (or any other user of
libmagic) should understand how it should communicate errors to the user.
I see from your response below that you understood my point.

 - snip -

> I see your point. I think that the best solution is to give you access to
> the errno information,

Exactly.

> and change the default behavior via a flag:

Yes, this seems like the best way to preserve backward api compatibility
for now.

>
> 	ms = magic_open(MAGIG_ERROR);
>
> 	if ((result = magic_file(ms, fn)) == NULL) {
> 		int error = magic_errno(ms);
> 		if (error == 0)
> 			// File internal error
> 			fprintf("%s: %s\n", fn, file_error(ms));
> 		else
> 			// Os error
> 			fprintf("%s: %s\n", fn, strerror(error));
> 	}
>
> In fact, I have implemented all this already...
>

In my example, I used a hypothetical new api (magic_errno) to get the
integer value of the error. You're suggesting changing the default
behavior via a flag, yet your code example above also uses magic_errno.
Did you intend for this to be 'magic_error' instead?

Using your example, how would I know whether the call to magic_file
succeeded or not? With the current libmagic, giving a path to a
non-existent file still results in a non-NULL value being assigned to
'result'. This gives the appearance that the call succeeded. I could see
two potential problems:

 * unable to determine whether magic_file succeeded or failed based on
return value alone.
 * unable to determine whether magic_file succeeded by calling magic_error
since there is no 'success' error code. (0 = internal error, non-0 = OS
error)

What am I missing?

brett





More information about the File mailing list