-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetxattr.c
More file actions
345 lines (314 loc) · 8.85 KB
/
setxattr.c
File metadata and controls
345 lines (314 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*-
* setxattr.c - Set an extended attribute for a filesystem node.
*
* Copyright (c) 2023 Erik Larsson
*
* 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 2 of the License, 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, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
#include <fcntl.h>
#endif
#include <unistd.h>
#if defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/extattr.h>
#endif
#if defined(__APPLE__) || defined(__DARWIN__) || defined(__linux__)
#include <sys/xattr.h>
#endif
int main(int argc, char **argv)
{
int ret = (EXIT_FAILURE);
int argp = 1;
#if defined(__FreeBSD__) || defined(__NetBSD__)
int namespace = EXTATTR_NAMESPACE_USER;
#endif
int follow_links = 0;
#if defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__)
int create = 0;
int replace = 0;
#endif /* defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__) */
const char *path;
const char *attr_name = NULL;
const char *attr_data = NULL;
#if defined(__APPLE__) || defined(__DARWIN__)
const char *attr_offset_string = NULL;
unsigned long long attr_offset = 0;
#endif
#if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
int nodefd = -1;
int attrdirfd = -1;
int attrfd = -1;
ssize_t attr_bytes_written = 0;
#endif
char *attr_data_alloc = NULL;
size_t attr_data_size = 0;
while(argp < argc) {
if(argv[argp][0] != '-') {
/* Not an option switch. Move on to the mandatory
* arguments. */
break;
}
else if(argv[argp][1] == '-') {
/* Stop parsing options when '--' is encountered. */
++argp;
break;
}
else if(argv[argp][1] == 'L') {
follow_links = 1;
++argp;
}
#if defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__)
else if(argv[argp][1] == 'c' ||
!strcmp(argv[argp], "--create"))
{
create = 1;
++argp;
}
else if(argv[argp][1] == 'r' ||
!strcmp(argv[argp], "--replace"))
{
replace = 1;
++argp;
}
#endif /* defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__) */
#if defined(__FreeBSD__) || defined(__NetBSD__)
#ifdef EXTATTR_NAMESPACE_EMPTY
else if(argv[argp][1] == 'e') {
namespace = EXTATTR_NAMESPACE_EMPTY;
++argp;
}
#endif
else if(argv[argp][1] == 'u') {
namespace = EXTATTR_NAMESPACE_USER;
++argp;
}
else if(argv[argp][1] == 's') {
namespace = EXTATTR_NAMESPACE_SYSTEM;
++argp;
}
#endif /* defined(__FreeBSD__) || defined(__NetBSD__) */
else {
fprintf(stderr, "Error: Unrecognized option '%s'.\n",
argv[argp]);
goto out;
}
}
path = (argp < argc) ? argv[argp++] : NULL;
attr_name = (argp < argc) ? argv[argp++] : NULL;
#if defined(__APPLE__) || defined(__DARWIN__)
attr_offset_string = (argp < argc) ? argv[argp++] : NULL;
#endif
attr_data = (argp < argc) ? argv[argp++] : NULL;
if(!path || !attr_name || argp < argc) {
fprintf(stderr, "usage: setxattr [-L"
#if defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__)
"|-c|-r"
#endif /* defined(__linux__) || defined(__APPLE__) || defined(__DARWIN__) */
#if defined(__FreeBSD__) || defined(__NetBSD__)
#ifdef EXTATTR_NAMESPACE_EMPTY
"|-e"
#endif
"|-u|-s"
#endif /* defined(__FreeBSD__) || defined(__NetBSD__) */
"] <filename> <attribute name> "
#if defined(__APPLE__) || defined(__DARWIN__)
"[<attribute offset>] "
#endif
"[<attribute data>]\n");
goto out;
}
#if defined(__APPLE__) || defined(__DARWIN__)
if(attr_offset_string) {
char *endptr = NULL;
errno = 0;
attr_offset = strtoull(attr_offset_string, &endptr, 0);
if(errno || ((*endptr || attr_offset > SIZE_MAX) &&
(errno = EILSEQ)))
{
fprintf(stderr, "Invalid offset: %s\n",
attr_offset_string);
goto out;
}
}
#endif
if(attr_data) {
attr_data_size = strlen(attr_data);
}
else {
size_t alloc_size = 4096;
attr_data = NULL;
attr_data_size = 0;
while(1) {
char *new_attr_data;
ssize_t bytes_read;
new_attr_data = realloc(attr_data_alloc, alloc_size);
if(!new_attr_data) {
fprintf(stderr, "Error while %sallocating "
"attribute buffer to %zu bytes: %s "
"(errno=%d)\n",
attr_data ? "" : "re", alloc_size,
strerror(errno), errno);
goto out;
}
attr_data_alloc = new_attr_data;
attr_data = attr_data_alloc;
bytes_read = read(STDIN_FILENO,
&attr_data_alloc[attr_data_size],
alloc_size - attr_data_size);
if(bytes_read < 0) {
fprintf(stderr, "Error while reading xattr "
"data from stdin: %s (errno=%d)\n",
strerror(errno), errno);
goto out;
}
if(!bytes_read) {
break;
}
attr_data_size += bytes_read;
if(attr_data_size > alloc_size / 2) {
alloc_size *= 2;
}
}
if(alloc_size != attr_data_size) {
char *new_attr_data;
new_attr_data =
realloc(attr_data_alloc, attr_data_size);
if(!new_attr_data) {
fprintf(stderr, "Error while shrinking "
"attribute buffer from %zu to %zu "
"bytes: %s (errno=%d)\n",
alloc_size, attr_data_size,
strerror(errno), errno);
goto out;
}
attr_data_alloc = new_attr_data;
attr_data = attr_data_alloc;
}
}
#if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
if(attr_name[0] == '/') {
fprintf(stderr, "Invalid attribute name \"%s\" (cannot start "
"with '/').\n", attr_name);
goto out;
}
nodefd = open(
path,
O_RDONLY | (follow_links ? 0 : O_NOFOLLOW));
if(nodefd == -1) {
fprintf(stderr, "Error while opening node \"%s\": %s (%d)\n",
path,
strerror(errno),
errno);
goto out;
}
attrdirfd = openat(nodefd, ".", O_RDONLY | O_XATTR);
if(attrdirfd == -1) {
fprintf(stderr, "Error while opening \"%s\" node's attribute "
"directory: %s (%d)\n",
path,
strerror(errno),
errno);
goto out;
}
#endif
#if defined(__APPLE__) || defined(__DARWIN__)
if(setxattr(
path,
attr_name,
attr_data,
attr_data_size,
attr_offset,
(follow_links ? 0 : XATTR_NOFOLLOW) |
(create ? XATTR_CREATE : 0) |
(replace ? XATTR_REPLACE : 0)))
#elif defined(__linux__)
if((follow_links ? setxattr : lsetxattr)(
path,
attr_name,
attr_data,
attr_data_size,
(create ? XATTR_CREATE : 0) |
(replace ? XATTR_REPLACE : 0)))
#elif defined(__FreeBSD__) || defined(__NetBSD__)
if((follow_links ? extattr_set_file : extattr_set_link)(
path,
namespace,
attr_name,
attr_data,
attr_data_size) < 0)
#elif (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
/* TODO: Solaris allows whole xattr directory hierarchies under a node.
* To support creating attributes in xattr directory hierarchies we must
* split any pathname components and create directories for them if they
* do not exist. At the moment setting such xattrs will fail. */
/* If the attribute existed before, then we should remove it. */
if(unlinkat(attrdirfd, attr_name, 0) && errno != ENOENT) {
fprintf(stderr, "Error while removing existing extended "
"attribute \"%s\" for node \"%s\": %s (%d)\n",
attr_name, path, strerror(errno), errno);
goto out;
}
attrfd = openat(nodefd, attr_name,
O_WRONLY | O_CREAT | O_EXCL | O_XATTR, 0777);
if(attrfd == -1) {
fprintf(stderr, "Error while creating extended attribute "
"\"%s\" for node \"%s\": %s (%d)\n",
attr_name, path, strerror(errno), errno);
goto out;
}
/* Write out the attribute data. */
attr_bytes_written = write(attrfd, attr_data, attr_data_size);
if(attr_bytes_written >= 0 && attr_bytes_written != attr_data_size) {
fprintf(stderr, "Partial write while setting extended "
"attribute \"%s\" for node \"%s\": %zu / %zu bytes "
"written\n",
attr_name, path, attr_bytes_written, attr_data_size);
goto out;
}
else if(attr_bytes_written < 0)
#else
#error "Don't know how to handle extended attributes on this platform."
#endif /* defined(__APPLE__) || defined(__DARWIN__) ... */
{
fprintf(stderr, "Failed to set extended attribute: %s "
"(errno=%d)\n",
strerror(errno), errno);
goto out;
}
ret = (EXIT_SUCCESS);
out:
#if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
if(attrfd != -1) {
close(attrfd);
}
#endif
if(attr_data_alloc) {
free(attr_data_alloc);
}
#if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
if(attrdirfd != -1) {
close(attrdirfd);
}
if(nodefd != -1) {
close(nodefd);
}
#endif
return ret;
}