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