Hi!
> How do i get the mime-type of a file? Is there an egg for this? The
> magic.egg doesn't provide this, does it?
Oh yes it does!
However, I noticed the magic egg is currently broken. It lacked -X
easyffi in the .setup file. I attached a patch for this. It also adds a
function to sanitize the mime-type returned by the magic-file function. It
also adds an example on how to get the mime-type to the magic.html file. I
also attached the code of this example to this message for your convenience.
Groetjes,
Peter.
diff -uNr orig/f new/f
--- orig/f 1970-01-01 01:00:00.000000000 +0100
+++ new/f 2006-10-25 11:09:34.000000000 +0200
@@ -0,0 +1 @@
+csc -o magic.so -X easyffi -shared magic.scm -d0 -O2 -L -lmagic
Binary files orig/magic.egg and new/magic.egg differ
diff -uNr orig/magic.html new/magic.html
--- orig/magic.html 2006-08-19 23:29:50.000000000 +0200
+++ new/magic.html 2006-10-25 11:32:09.000000000 +0200
@@ -187,6 +187,10 @@
<div class="section">
<h3>Examples</h3>
<div id="examples">
+<p>
+An example which does what the UNIX file(1) command does:
+</p>
+<br>
<pre>(display "load extension") (newline)
(require-extension magic)
@@ -202,6 +206,37 @@
(display "close") (newline)
(magic-close cookie)</pre></div></div>
<div class="section">
+<p>
+An example which prints the mime-type and the sanitized mime-type of a file:
+</p>
+<br>
+<div id="examples">
+<pre>
+#!/usr/bin/csi -script
+
+(require-extension posix easyffi magic)
+
+(define (main args)
+ (if (not (null? args))
+ (begin
+ (define cookie (magic-open (+ MAGIC_MIME MAGIC_SYMLINK)))
+ (magic-load cookie #f)
+
+ (for-each (lambda (path)
+ (let ((mime-type (magic-file cookie path)))
+ (print path ": raw mime-type: " mime-type)
+ (print path ": sanitized mime-type: "
+ (magic-sanitize-mime-type mime-type path))))
+ args)
+
+ (magic-close cookie))))
+
+(main (command-line-arguments))
+(exit 0)
+</pre>
+</div>
+</div>
+<div class="section">
<h3>License</h3>
<pre id="license">
<pre>Copyright (c) 2005, Peter Busser
@@ -228,4 +263,4 @@
POSSIBILITY OF SUCH DAMAGE.</pre></pre></div></div>
<div id="footer">
<hr /><a href="index.html">< Egg index</a>
-<div id="revision-history">$Revision$ $Date$</div> </div></body></html>
\ No newline at end of file
+<div id="revision-history">$Revision$ $Date$</div> </div></body></html>
diff -uNr orig/magic.scm new/magic.scm
--- orig/magic.scm 2006-08-19 23:28:49.000000000 +0200
+++ new/magic.scm 2006-10-25 11:10:58.000000000 +0200
@@ -1,9 +1,10 @@
; csc -s magic.scm -L -lmagic
+; Copyright (c) 2005,2006 by Peter Busser
+; This file has been released under the BSD licence. See magic.html for the
+; licence text.
(declare
- (usual-integrations)
-
(export
magic-open
@@ -13,6 +14,7 @@
magic-close
magic-setflags
magic-errno
+ magic-sanitize-mime-type
MAGIC_NONE
MAGIC_DEBUG
@@ -25,18 +27,14 @@
MAGIC_PRESERVE_ATIME
MAGIC_RAW
MAGIC_ERROR
- )
-
-(use easyffi)
+ ))
- (foreign-declare #<<EOF
+#>
#include <magic.h>
+<#
-EOF
-))
-
-(foreign-parse #<<EOF
+#>!
___declare(export_constants, yes)
#define MAGIC_NONE 0x000 /* No flags */
#define MAGIC_DEBUG 0x001 /* Turn on debugging */
@@ -49,8 +47,7 @@
#define MAGIC_PRESERVE_ATIME 0x080 /* Restore access time on exit
*/
#define MAGIC_RAW 0x100 /* Don't translate unprintable
chars */
#define MAGIC_ERROR 0x200 /* Handle ENOENT etc as real
errors */
-EOF
-)
+<#
(define-foreign-type magic-t (pointer (struct "magic_set")))
@@ -75,3 +72,21 @@
(define magic-errno
(foreign-lambda int "magic_errno" magic-t))
+;
+; Sanitizes the mime-type provided by magic-file (with the MAGIC_MIME and
+; MAGIC_SYMLINK flags set in magic-open). The mime-types stored in the
+; file database sometimes do not contain useful information in which case
+; this function provides an alternative. Or it contains additional garbage,
+; in which case it strips the garbage.
+;
+(define (magic-sanitize-mime-type raw-mime-type path)
+ (if raw-mime-type
+ (if (string=? raw-mime-type "")
+ "unknown"
+ (if (directory? path)
+ "directory"
+ (car (string-split raw-mime-type ";,"))))
+ (if (symbolic-link? path)
+ "symlink"
+ "unknown")))
+
diff -uNr orig/magic.setup new/magic.setup
--- orig/magic.setup 2005-10-24 17:39:08.000000000 +0200
+++ new/magic.setup 2006-10-25 11:01:40.000000000 +0200
@@ -1,2 +1,2 @@
-(run (csc -o "magic.so" -shared "magic.scm" -d0 -O2 -L -lmagic))
+(run (csc -o "magic.so" -X easyffi -shared "magic.scm" -d0 -O2 -L -lmagic))
(install-extension 'magic '("magic.so" "magic.html") '((documentation
"magic.html")))
#!/usr/bin/csi -script
(require-extension posix easyffi magic)
(define (main args)
(if (not (null? args))
(begin
(define cookie (magic-open (+ MAGIC_MIME MAGIC_SYMLINK)))
(magic-load cookie #f)
(for-each (lambda (path)
(let ((mime-type (magic-file cookie path)))
(print path ": raw mime-type: " mime-type)
(print path ": sanitized mime-type: "
(magic-sanitize-mime-type mime-type path))))
args)
(magic-close cookie))))
(main (command-line-arguments))
(exit 0)
_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users