1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22#include "tool_setup.h"
23
24#ifdef HAVE_FSETXATTR
25#  include <sys/xattr.h> /* header from libc, not from libattr */
26#endif
27
28#include "tool_xattr.h"
29
30#include "memdebug.h" /* keep this as LAST include */
31
32#ifdef HAVE_FSETXATTR
33
34/* mapping table of curl metadata to extended attribute names */
35static const struct xattr_mapping {
36  const char *attr; /* name of the xattr */
37  CURLINFO info;
38} mappings[] = {
39  /* mappings proposed by
40   * http://freedesktop.org/wiki/CommonExtendedAttributes
41   */
42  { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
43  { "user.mime_type",      CURLINFO_CONTENT_TYPE },
44  { NULL,                  CURLINFO_NONE } /* last element, abort loop here */
45};
46
47/* store metadata from the curl request alongside the downloaded
48 * file using extended attributes
49 */
50int fwrite_xattr(CURL *curl, int fd)
51{
52  int i = 0;
53  int err = 0;
54  /* loop through all xattr-curlinfo pairs and abort on a set error */
55  while(err == 0 && mappings[i].attr != NULL) {
56    char *value = NULL;
57    CURLcode rc = curl_easy_getinfo(curl, mappings[i].info, &value);
58    if(rc == CURLE_OK && value) {
59#ifdef HAVE_FSETXATTR_6
60      err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
61#elif defined(HAVE_FSETXATTR_5)
62      err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
63#endif
64    }
65    i++;
66  }
67  return err;
68}
69#else
70int fwrite_xattr(CURL *curl, int fd)
71{
72  (void)curl;
73  (void)fd;
74  return 0;
75}
76#endif
77