1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
25 # include <sys/xattr.h> /* header from libc, not from libattr */
27 #elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
28 # include <sys/types.h>
29 # include <sys/extattr.h>
33 #include "tool_xattr.h"
35 #include "memdebug.h" /* keep this as LAST include */
39 /* mapping table of curl metadata to extended attribute names */
40 static const struct xattr_mapping {
41 const char *attr; /* name of the xattr */
44 /* mappings proposed by
45 * http://freedesktop.org/wiki/CommonExtendedAttributes
47 { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
48 { "user.mime_type", CURLINFO_CONTENT_TYPE },
49 { NULL, CURLINFO_NONE } /* last element, abort loop here */
52 /* store metadata from the curl request alongside the downloaded
53 * file using extended attributes
55 int fwrite_xattr(CURL *curl, int fd)
59 /* loop through all xattr-curlinfo pairs and abort on a set error */
60 while(err == 0 && mappings[i].attr != NULL) {
62 CURLcode rc = curl_easy_getinfo(curl, mappings[i].info, &value);
63 if(rc == CURLE_OK && value) {
64 #ifdef HAVE_FSETXATTR_6
65 err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
66 #elif defined(HAVE_FSETXATTR_5)
67 err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
68 #elif defined(__FreeBSD_version)
69 err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr, value,
71 /* FreeBSD's extattr_set_fd returns the length of the extended attribute
73 err = err < 0 ? err : 0;
81 int fwrite_xattr(CURL *curl, int fd)