2010-11-05 09:07:38 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-12-25 06:23:44 -05:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-11-05 09:07:38 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2010-11-05 07:39:46 -04:00
|
|
|
|
2010-11-07 10:54:49 -05:00
|
|
|
#ifdef HAVE_FSETXATTR
|
2011-10-06 11:39:00 -04:00
|
|
|
# include <sys/xattr.h> /* header from libc, not from libattr */
|
2013-10-01 15:57:14 -04:00
|
|
|
# define USE_XATTR
|
|
|
|
#elif defined(__FreeBSD_version) && (__FreeBSD_version > 500000)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/extattr.h>
|
|
|
|
# define USE_XATTR
|
2011-09-14 05:27:12 -04:00
|
|
|
#endif
|
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_xattr.h"
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2013-10-01 15:57:14 -04:00
|
|
|
#ifdef USE_XATTR
|
2010-11-05 09:07:38 -04:00
|
|
|
|
2010-11-05 07:39:46 -04:00
|
|
|
/* mapping table of curl metadata to extended attribute names */
|
2010-11-09 21:47:16 -05:00
|
|
|
static const struct xattr_mapping {
|
2010-11-08 03:10:33 -05:00
|
|
|
const char *attr; /* name of the xattr */
|
2010-11-05 07:39:46 -04:00
|
|
|
CURLINFO info;
|
|
|
|
} mappings[] = {
|
|
|
|
/* mappings proposed by
|
|
|
|
* http://freedesktop.org/wiki/CommonExtendedAttributes
|
|
|
|
*/
|
|
|
|
{ "user.xdg.origin.url", CURLINFO_EFFECTIVE_URL },
|
2011-10-06 11:39:00 -04:00
|
|
|
{ "user.mime_type", CURLINFO_CONTENT_TYPE },
|
|
|
|
{ NULL, CURLINFO_NONE } /* last element, abort loop here */
|
2010-11-05 07:39:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* store metadata from the curl request alongside the downloaded
|
|
|
|
* file using extended attributes
|
|
|
|
*/
|
2010-11-07 10:54:49 -05:00
|
|
|
int fwrite_xattr(CURL *curl, int fd)
|
2010-11-05 07:39:46 -04:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int err = 0;
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-09 21:47:16 -05:00
|
|
|
/* loop through all xattr-curlinfo pairs and abort on a set error */
|
2011-04-25 16:44:39 -04:00
|
|
|
while(err == 0 && mappings[i].attr != NULL) {
|
2010-11-05 07:39:46 -04:00
|
|
|
char *value = NULL;
|
2014-12-25 06:23:44 -05:00
|
|
|
CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
|
|
|
|
if(!result && value) {
|
2010-11-10 12:39:44 -05:00
|
|
|
#ifdef HAVE_FSETXATTR_6
|
2011-10-06 11:39:00 -04:00
|
|
|
err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
|
2010-11-10 12:39:44 -05:00
|
|
|
#elif defined(HAVE_FSETXATTR_5)
|
2011-10-06 11:39:00 -04:00
|
|
|
err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
|
2013-10-01 15:57:14 -04:00
|
|
|
#elif defined(__FreeBSD_version)
|
|
|
|
err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr, value,
|
|
|
|
strlen(value));
|
|
|
|
/* FreeBSD's extattr_set_fd returns the length of the extended attribute
|
|
|
|
*/
|
|
|
|
err = err < 0 ? err : 0;
|
2010-11-10 12:39:44 -05:00
|
|
|
#endif
|
2010-11-05 07:39:46 -04:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-05 07:39:46 -04:00
|
|
|
return err;
|
|
|
|
}
|
2010-11-05 09:07:38 -04:00
|
|
|
#else
|
2010-11-07 10:54:49 -05:00
|
|
|
int fwrite_xattr(CURL *curl, int fd)
|
2010-11-05 09:07:38 -04:00
|
|
|
{
|
|
|
|
(void)curl;
|
2010-11-07 10:54:49 -05:00
|
|
|
(void)fd;
|
2014-12-25 06:23:44 -05:00
|
|
|
|
2010-11-05 09:07:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|