tool_xattr.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, 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 https://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 # define USE_XATTR
27 #elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
28 # include <sys/types.h>
29 # include <sys/extattr.h>
30 # define USE_XATTR
31 #endif
32 
33 #include "tool_xattr.h"
34 
35 #include "memdebug.h" /* keep this as LAST include */
36 
37 #ifdef USE_XATTR
38 
39 /* mapping table of curl metadata to extended attribute names */
40 static const struct xattr_mapping {
41  const char *attr; /* name of the xattr */
42  CURLINFO info;
43 } mappings[] = {
44  /* mappings proposed by
45  * https://freedesktop.org/wiki/CommonExtendedAttributes/
46  */
47  { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
48  { "user.mime_type", CURLINFO_CONTENT_TYPE },
49  { NULL, CURLINFO_NONE } /* last element, abort loop here */
50 };
51 
52 /* store metadata from the curl request alongside the downloaded
53  * file using extended attributes
54  */
55 int fwrite_xattr(CURL *curl, int fd)
56 {
57  int i = 0;
58  int err = 0;
59 
60  /* loop through all xattr-curlinfo pairs and abort on a set error */
61  while(err == 0 && mappings[i].attr != NULL) {
62  char *value = NULL;
63  CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
64  if(!result && value) {
65 #ifdef HAVE_FSETXATTR_6
66  err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
67 #elif defined(HAVE_FSETXATTR_5)
68  err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
69 #elif defined(__FreeBSD_version)
70  err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr, value,
71  strlen(value));
72  /* FreeBSD's extattr_set_fd returns the length of the extended attribute
73  */
74  err = err < 0 ? err : 0;
75 #endif
76  }
77  i++;
78  }
79 
80  return err;
81 }
82 #else
83 int fwrite_xattr(CURL *curl, int fd)
84 {
85  (void)curl;
86  (void)fd;
87 
88  return 0;
89 }
90 #endif
CURLcode
Definition: curl.h:454
CURLINFO
Definition: curl.h:2439
UNITTEST_START int result
Definition: unit1304.c:49
unsigned int i
Definition: unit1303.c:79
#define curl_easy_getinfo(handle, info, arg)
void CURL
Definition: curl.h:102
UNITTEST_START int * value
Definition: unit1602.c:51
int fwrite_xattr(CURL *curl, int fd)
Definition: tool_xattr.c:83
static CURL * curl
Definition: sessioninfo.c:35


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16