Line | Count | Source |
1 | | /* xattr.h -- POSIX Extended Attribute support. |
2 | | |
3 | | Copyright (C) 2016, 2018-2024, 2026 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 | 0 | { |
32 | 0 | int retval = -1; |
33 | |
|
34 | 0 | if (name && value && fp) |
35 | 0 | { |
36 | 0 | retval = fsetxattr (fileno (fp), name, value, strlen (value), 0); |
37 | | /* FreeBSD's extattr_set_fd returns the length of the extended attribute. */ |
38 | 0 | retval = (retval < 0) ? retval : 0; |
39 | 0 | if (retval) |
40 | 0 | DEBUGP (("Failed to set xattr %s.\n", quote(name))); |
41 | 0 | } |
42 | |
|
43 | 0 | return retval; |
44 | 0 | } |
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 | 0 | { |
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 | 0 | int retval = -1; |
71 | 0 | char *value; |
72 | |
|
73 | 0 | if (!origin_url || !fp) |
74 | 0 | return retval; |
75 | | |
76 | 0 | value = url_string (origin_url, URL_AUTH_HIDE); |
77 | 0 | retval = write_xattr_metadata ("user.xdg.origin.url", escnonprint_uri (value), fp); |
78 | 0 | xfree (value); |
79 | |
|
80 | 0 | if (!retval && referrer_url) |
81 | 0 | { |
82 | 0 | struct url u; |
83 | |
|
84 | 0 | memset(&u, 0, sizeof(u)); |
85 | 0 | u.scheme = referrer_url->scheme; |
86 | 0 | u.host = referrer_url->host; |
87 | 0 | u.port = referrer_url->port; |
88 | |
|
89 | 0 | value = url_string (&u, 0); |
90 | 0 | retval = write_xattr_metadata ("user.xdg.referrer.url", escnonprint_uri (value), fp); |
91 | 0 | xfree (value); |
92 | 0 | } |
93 | |
|
94 | 0 | return retval; |
95 | 0 | } |