From 95719fbea6acf52d03b17de742f16be8f1f0ce70 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Fri, 5 Nov 2010 14:07:38 +0100
Subject: [PATCH] xattr: add configure check and #ifdefs

setxattr is a glibc call to set extended attributes, so configure now
checks for it and the code is adapted to only build when the
functionality is present.
---
 configure.ac         |    1 +
 m4/curl-functions.m4 |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.c           |   10 ++---
 src/xattr.c          |   42 ++++++++++++++++++++++--
 src/xattr.h          |   25 +++++++++++++++
 5 files changed, 152 insertions(+), 10 deletions(-)

diff --git a/configure.ac b/configure.ac
index 224ca2d..893e53c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2349,6 +2349,7 @@ CURL_CHECK_FUNC_LOCALTIME_R
 CURL_CHECK_FUNC_MEMRCHR
 CURL_CHECK_FUNC_POLL
 CURL_CHECK_FUNC_SETSOCKOPT
+CURL_CHECK_FUNC_SETXATTR
 CURL_CHECK_FUNC_SIGACTION
 CURL_CHECK_FUNC_SIGINTERRUPT
 CURL_CHECK_FUNC_SIGNAL
diff --git a/m4/curl-functions.m4 b/m4/curl-functions.m4
index 36bd31c..3673e53 100644
--- a/m4/curl-functions.m4
+++ b/m4/curl-functions.m4
@@ -5903,3 +5903,87 @@ AC_DEFUN([CURL_CHECK_FUNC_WRITEV], [
     ac_cv_func_writev="no"
   fi
 ])
+
+dnl CURL_CHECK_FUNC_SETXATTR
+dnl -------------------------------------------------
+dnl Verify if setxattr is available, prototyped, and
+dnl can be compiled. If all of these are true, and
+dnl usage has not been previously disallowed with
+dnl shell variable curl_disallow_setxattr, then
+dnl HAVE_SETXATTR will be defined.
+
+AC_DEFUN([CURL_CHECK_FUNC_SETXATTR], [
+  AC_REQUIRE([CURL_INCLUDES_SYS_UIO])dnl
+  #
+  tst_links_setxattr="unknown"
+  tst_proto_setxattr="unknown"
+  tst_compi_setxattr="unknown"
+  tst_allow_setxattr="unknown"
+  #
+  AC_MSG_CHECKING([if setxattr can be linked])
+  AC_LINK_IFELSE([
+    AC_LANG_FUNC_LINK_TRY([setxattr])
+  ],[
+    AC_MSG_RESULT([yes])
+    tst_links_setxattr="yes"
+  ],[
+    AC_MSG_RESULT([no])
+    tst_links_setxattr="no"
+  ])
+  #
+  if test "$tst_links_setxattr" = "yes"; then
+    AC_MSG_CHECKING([if setxattr is prototyped])
+    AC_EGREP_CPP([setxattr],[
+      $curl_includes_sys_uio
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_proto_setxattr="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_proto_setxattr="no"
+    ])
+  fi
+  #
+  if test "$tst_proto_setxattr" = "yes"; then
+    AC_MSG_CHECKING([if setxattr is compilable])
+    AC_COMPILE_IFELSE([
+      AC_LANG_PROGRAM([[
+        $curl_includes_sys_uio
+      ]],[[
+        if(0 != setxattr(0, 0, 0))
+          return 1;
+      ]])
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_compi_setxattr="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_compi_setxattr="no"
+    ])
+  fi
+  #
+  if test "$tst_compi_setxattr" = "yes"; then
+    AC_MSG_CHECKING([if setxattr usage allowed])
+    if test "x$curl_disallow_setxattr" != "xyes"; then
+      AC_MSG_RESULT([yes])
+      tst_allow_setxattr="yes"
+    else
+      AC_MSG_RESULT([no])
+      tst_allow_setxattr="no"
+    fi
+  fi
+  #
+  AC_MSG_CHECKING([if setxattr might be used])
+  if test "$tst_links_setxattr" = "yes" &&
+     test "$tst_proto_setxattr" = "yes" &&
+     test "$tst_compi_setxattr" = "yes" &&
+     test "$tst_allow_setxattr" = "yes"; then
+    AC_MSG_RESULT([yes])
+    AC_DEFINE_UNQUOTED(HAVE_SETXATTR, 1,
+      [Define to 1 if you have the setxattr function.])
+    ac_cv_func_setxattr="yes"
+  else
+    AC_MSG_RESULT([no])
+    ac_cv_func_setxattr="no"
+  fi
+])
diff --git a/src/main.c b/src/main.c
index 5394bac..6998f98 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5648,12 +5648,10 @@ operate(struct Configurable *config, int argc, argv_item_t argv[])
         }
 
         if(config->xattr && outs.filename) {
-          char *url = NULL;
-          curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
-          int err = write_xattr( curl, outs.filename );
-          if (err) {
-            warnf( config, "Error setting extended attributes: %s\n", strerror(errno) );
-          }
+          int err = write_xattr(curl, outs.filename );
+          if(err)
+            warnf( config, "Error setting extended attributes: %s\n",
+                   strerror(errno) );
         }
 
 #ifdef HAVE_UTIME
diff --git a/src/xattr.c b/src/xattr.c
index fbc09c2..1047c8a 100644
--- a/src/xattr.c
+++ b/src/xattr.c
@@ -1,9 +1,35 @@
-#include <sys/types.h>
-#include <sys/xattr.h> /* include header from libc, not from libattr */
-#include <string.h>
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+
+/* client-local setup.h */
+#include "setup.h"
 #include <curl/curl.h>
 #include "xattr.h"
 
+#ifdef HAVE_SETXATTR
+#include <sys/types.h>
+#include <string.h>
+#include <sys/xattr.h> /* include header from libc, not from libattr */
+
 /* mapping table of curl metadata to extended attribute names */
 static struct xattr_mapping {
   char *attr; /* name of the xattr */
@@ -20,7 +46,7 @@ static struct xattr_mapping {
 /* store metadata from the curl request alongside the downloaded
  * file using extended attributes
  */
-int write_xattr( CURL *curl, const char *filename )
+int write_xattr(CURL *curl, const char *filename)
 {
   int i = 0;
   int err = 0;
@@ -35,3 +61,11 @@ int write_xattr( CURL *curl, const char *filename )
   }
   return err;
 }
+#else
+int write_xattr(CURL *curl, const char *filename)
+{
+  (void)curl;
+  (void)filename;
+  return 0;
+}
+#endif
diff --git a/src/xattr.h b/src/xattr.h
index 3067397..df62066 100644
--- a/src/xattr.h
+++ b/src/xattr.h
@@ -1 +1,26 @@
+#ifndef __XATTR_H
+#define __XATTR_H
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
 int write_xattr( CURL *curl, const char *filename );
+
+#endif
-- 
1.7.2.3

