From 0328ac04990bf22e635930aa37abb8c2128a17f1 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Fri, 20 Aug 2010 15:00:44 +0200 Subject: [PATCH] Added xattr mime-type detection to sendfile. --- configure.in | 20 ++++++++++++++++++++ src/Makefile.am | 2 +- src/sendfile.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/configure.in b/configure.in index 7166967..2e274a2 100644 --- a/configure.in +++ b/configure.in @@ -12,6 +12,26 @@ dnl AM_PROG_LIBTOOL AC_HEADER_STDC +AH_TEMPLATE(HAVE_XATTR, [define to compile support for filesystem extended attributes]) +AC_ARG_WITH(xattr, [ --with-xattr Enable XATTR support]) +HAS_XATTR="" +if test "$with_xattr" = no; then HAS_XATTR=no; fi +if test -z "$HAS_XATTR"; then + AC_CHECK_LIB(attr, getxattr, [:], [HAS_XATTR=no]) +fi +if test -z "$HAS_XATTR"; then + AC_CHECK_HEADER(attr/xattr.h, [], [HAS_XATTR=no]) +fi +if test "$HAS_XATTR" != no; then HAS_XATTR=yes; fi +if test "$with_xattr" = yes -a "$HAS_XATTR" = no; then + AC_MSG_ERROR([*** cannot find xattr support on this system]) +fi +if test "$HAS_XATTR" = yes; then + XATTR_LIBS=-lattr + AC_DEFINE(HAVE_XATTR) +fi +AC_SUBST(XATTR_LIBS) + AC_OUTPUT([ Makefile src/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index db9f833..3b67b3c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,4 +8,4 @@ debugsink_SOURCES = debugsink.c LDADD = $(top_srcdir)/lib/libht.a AM_CPPFLAGS = -I$(top_srcdir)/lib -sendfile_LDADD = $(LDADD) -lmagic +sendfile_LDADD = $(LDADD) -lmagic @XATTR_LIBS@ diff --git a/src/sendfile.c b/src/sendfile.c index 4ec25a6..601661b 100644 --- a/src/sendfile.c +++ b/src/sendfile.c @@ -34,6 +34,10 @@ #include #include +#ifdef HAVE_XATTR +#include +#endif + static magic_t cookie = NULL; static void passdata(int in, int out) @@ -66,10 +70,40 @@ static int strrcmp(char *str, char *end) return(strcmp(str + strlen(str) - strlen(end), end)); } +static char *attrmimetype(char *file) +{ +#ifdef HAVE_XATTR + static char buf[1024]; + int i; + ssize_t sz; + + if((sz = getxattr(file, "user.ash-mime-type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.mime-type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.mime_type", buf, sizeof(buf) - 1)) > 0) + goto found; + if((sz = getxattr(file, "user.Content-Type", buf, sizeof(buf) - 1)) > 0) + goto found; + return(NULL); +found: + for(i = 0; i < sz; i++) { + if((buf[sz] < 32) || (buf[sz] >= 128)) + return(NULL); + } + buf[sz] = 0; + return(buf); +#else + return(NULL); +#endif +} + static const char *getmimetype(char *file, struct stat *sb) { const char *ret; + if((ret = attrmimetype(file)) != NULL) + return(ret); if(cookie == NULL) { cookie = magic_open(MAGIC_MIME_TYPE); magic_load(cookie, NULL); -- 2.11.0