Debian Codegraph @

src/xattr.c

raw
1 /* xattr.h -- POSIX Extended Attribute support.
2
3 Copyright (C) 2016, 2018-2024 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
17
18 #include "wget.h"
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "log.h"
24 #include "utils.h"
25 #include "xattr.h"
26
27 #ifdef USE_XATTR
28
29 static int
30 write_xattr_metadata (const char *name, const char *value, FILE *fp)
31 {
32 int retval = -1;
33
34 if (name && value && fp)
35 {
36 retval = fsetxattr (fileno (fp), name, value, strlen (value), 0);
37 /* FreeBSD's extattr_set_fd returns the length of the extended attribute. */
38 retval = (retval < 0) ? retval : 0;
39 if (retval)
40 DEBUGP (("Failed to set xattr %s.\n", quote(name)));
41 }
42
43 return retval;
44 }
45
46 #else /* USE_XATTR */
47
48 static int
49 write_xattr_metadata (const char *name, const char *value, FILE *fp)
50 {
51 (void)name;
52 (void)value;
53 (void)fp;
54
55 return 0;
56 }
57
58 #endif /* USE_XATTR */
59
60 int
61 set_file_metadata (const struct url *origin_url, const struct url *referrer_url, FILE *fp)
62 {
63 /* Save metadata about where the file came from (requested, final URLs) to
64 * user POSIX Extended Attributes of retrieved file.
65 *
66 * For more details about the user namespace see
67 * [http://freedesktop.org/wiki/CommonExtendedAttributes] and
68 * [http://0pointer.de/lennart/projects/mod_mime_xattr/].
69 */
70 int retval = -1;
71 char *value;
72
73 if (!origin_url || !fp)
74 return retval;
75
76 value = url_string (origin_url, URL_AUTH_HIDE);
77 retval = write_xattr_metadata ("user.xdg.origin.url", escnonprint_uri (value), fp);
78 xfree (value);
79
80 if (!retval && referrer_url)
81 {
82 struct url u;
83
84 memset(&u, 0, sizeof(u));
85 u.scheme = referrer_url->scheme;
86 u.host = referrer_url->host;
87 u.port = referrer_url->port;
88
89 value = url_string (&u, 0);
90 retval = write_xattr_metadata ("user.xdg.referrer.url", escnonprint_uri (value), fp);
91 xfree (value);
92 }
93
94 return retval;
95 }