2016-07-21 12:15:49 +08:00
|
|
|
/* xattr.h -- POSIX Extended Attribute support.
|
|
|
|
|
2019-02-10 18:29:48 +08:00
|
|
|
Copyright (C) 2016, 2018-2019 Free Software Foundation, Inc.
|
2016-07-21 12:15:49 +08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include "wget.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2018-12-26 21:38:18 +08:00
|
|
|
#include "utils.h"
|
2016-07-21 12:15:49 +08:00
|
|
|
#include "xattr.h"
|
|
|
|
|
|
|
|
#ifdef USE_XATTR
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_xattr_metadata (const char *name, const char *value, FILE *fp)
|
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
|
|
|
|
if (name && value && fp)
|
|
|
|
{
|
2016-07-27 07:30:41 +08:00
|
|
|
retval = fsetxattr (fileno (fp), name, value, strlen (value), 0);
|
2016-07-21 12:15:49 +08:00
|
|
|
/* FreeBSD's extattr_set_fd returns the length of the extended attribute. */
|
|
|
|
retval = (retval < 0) ? retval : 0;
|
2016-07-27 07:30:41 +08:00
|
|
|
if (retval)
|
|
|
|
DEBUGP (("Failed to set xattr %s.\n", quote(name)));
|
2016-07-21 12:15:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_XATTR */
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_xattr_metadata (const char *name, const char *value, FILE *fp)
|
|
|
|
{
|
|
|
|
(void)name;
|
|
|
|
(void)value;
|
|
|
|
(void)fp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_XATTR */
|
|
|
|
|
|
|
|
int
|
2018-12-26 21:38:18 +08:00
|
|
|
set_file_metadata (const struct url *origin_url, const struct url *referrer_url, FILE *fp)
|
2016-07-21 12:15:49 +08:00
|
|
|
{
|
|
|
|
/* Save metadata about where the file came from (requested, final URLs) to
|
|
|
|
* user POSIX Extended Attributes of retrieved file.
|
|
|
|
*
|
|
|
|
* For more details about the user namespace see
|
|
|
|
* [http://freedesktop.org/wiki/CommonExtendedAttributes] and
|
|
|
|
* [http://0pointer.de/lennart/projects/mod_mime_xattr/].
|
|
|
|
*/
|
|
|
|
int retval = -1;
|
2018-12-26 21:38:18 +08:00
|
|
|
char *value;
|
2016-07-21 12:15:49 +08:00
|
|
|
|
|
|
|
if (!origin_url || !fp)
|
|
|
|
return retval;
|
|
|
|
|
2018-12-26 21:38:18 +08:00
|
|
|
value = url_string (origin_url, URL_AUTH_HIDE);
|
|
|
|
retval = write_xattr_metadata ("user.xdg.origin.url", escnonprint_uri (value), fp);
|
|
|
|
xfree (value);
|
|
|
|
|
|
|
|
if (!retval && referrer_url)
|
|
|
|
{
|
|
|
|
struct url u;
|
|
|
|
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
u.scheme = referrer_url->scheme;
|
|
|
|
u.host = referrer_url->host;
|
|
|
|
u.port = referrer_url->port;
|
|
|
|
|
|
|
|
value = url_string (&u, 0);
|
|
|
|
retval = write_xattr_metadata ("user.xdg.referrer.url", escnonprint_uri (value), fp);
|
|
|
|
xfree (value);
|
|
|
|
}
|
2016-07-21 12:15:49 +08:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|