tool_xattr.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #ifdef HAVE_FSETXATTR
00025 #  include <sys/xattr.h> /* header from libc, not from libattr */
00026 #  define USE_XATTR
00027 #elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
00028 #  include <sys/types.h>
00029 #  include <sys/extattr.h>
00030 #  define USE_XATTR
00031 #endif
00032 
00033 #include "tool_xattr.h"
00034 
00035 #include "memdebug.h" /* keep this as LAST include */
00036 
00037 #ifdef USE_XATTR
00038 
00039 /* mapping table of curl metadata to extended attribute names */
00040 static const struct xattr_mapping {
00041   const char *attr; /* name of the xattr */
00042   CURLINFO info;
00043 } mappings[] = {
00044   /* mappings proposed by
00045    * http://freedesktop.org/wiki/CommonExtendedAttributes
00046    */
00047   { "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
00048   { "user.mime_type",      CURLINFO_CONTENT_TYPE },
00049   { NULL,                  CURLINFO_NONE } /* last element, abort loop here */
00050 };
00051 
00052 /* store metadata from the curl request alongside the downloaded
00053  * file using extended attributes
00054  */
00055 int fwrite_xattr(CURL *curl, int fd)
00056 {
00057   int i = 0;
00058   int err = 0;
00059 
00060   /* loop through all xattr-curlinfo pairs and abort on a set error */
00061   while(err == 0 && mappings[i].attr != NULL) {
00062     char *value = NULL;
00063     CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
00064     if(!result && value) {
00065 #ifdef HAVE_FSETXATTR_6
00066       err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
00067 #elif defined(HAVE_FSETXATTR_5)
00068       err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
00069 #elif defined(__FreeBSD_version)
00070       err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr, value,
00071                            strlen(value));
00072       /* FreeBSD's extattr_set_fd returns the length of the extended attribute
00073        */
00074       err = err < 0 ? err : 0;
00075 #endif
00076     }
00077     i++;
00078   }
00079 
00080   return err;
00081 }
00082 #else
00083 int fwrite_xattr(CURL *curl, int fd)
00084 {
00085   (void)curl;
00086   (void)fd;
00087 
00088   return 0;
00089 }
00090 #endif


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07