forked from kristapsdz/openrsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.c
434 lines (351 loc) · 9.32 KB
/
platform.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Platform hooks to more cleanly provide some functionality not otherwise
* applicable to openrsync at-large.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include "extern.h"
#ifdef __APPLE__
#include <assert.h>
#include <copyfile.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <paths.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define FLSTAT_PLATFORM_XATTR FLSTAT_PLATFORM_BIT1
#define FLSTAT_PLATFORM_CLEAR_XATTR FLSTAT_PLATFORM_BIT2
#define FLSTAT_PLATFORM_UNLINKED FLSTAT_PLATFORM_BIT3
static int
apple_open_xattrs(const struct sess *sess, const struct flist *f, int oflags)
{
char *path, tmpfile[PATH_MAX];
size_t idx;
int copyflags, fd, error, serrno;
assert(strstr(f->path, "._") != NULL);
copyflags = COPYFILE_PACK | COPYFILE_ACL | COPYFILE_XATTR;
if (sess->opts->preserve_links)
copyflags |= COPYFILE_NOFOLLOW;
/* Chop off the ._ */
if (asprintf(&path, "%s/%s", dirname(f->path),
basename(f->path) + 2) == -1) {
ERR("asprintf");
return -1;
}
idx = snprintf(tmpfile, sizeof(tmpfile), "%s.%d.", _PATH_TMP, getpid());
/* We need to synthesize the xattrs. */
for (const char *c = f->wpath; *c != '\0' && idx < sizeof(tmpfile) - 1;
c++) {
if (*c == '/')
tmpfile[idx++] = '_';
else
tmpfile[idx++] = *c;
}
tmpfile[idx++] = '\0';
error = copyfile(path, tmpfile, NULL, copyflags);
serrno = errno;
free(path);
if (error != 0) {
errno = serrno;
ERR("copyfile");
return -1;
}
fd = open(tmpfile, oflags);
serrno = errno;
unlink(tmpfile);
if (fd == -1)
errno = serrno;
return fd;
}
static int
apple_flist_sent(struct sess *sess, int fdout, const struct flist *f)
{
char send;
assert(sess->opts->extended_attributes);
if ((f->st.flags & FLSTAT_PLATFORM_XATTR) != 0)
send = 1;
else
send = 0;
if (!io_write_byte(sess, fdout, send)) {
ERRX("io_write_byte");
return 0;
}
return 1;
}
#define HAVE_PLATFORM_FLIST_MODIFY 1
int
platform_flist_modify(const struct sess *sess, struct fl *fl)
{
struct flist *f;
size_t insz;
int copyflags;
if (!sess->opts->extended_attributes)
return 1;
copyflags = COPYFILE_CHECK | COPYFILE_ACL | COPYFILE_XATTR;
if (sess->opts->preserve_links)
copyflags |= COPYFILE_NOFOLLOW;
insz = fl->sz;
for (size_t i = 0; i < insz; i++) {
struct flist *packed;
const char *base;
char *ppath;
ptrdiff_t stripdir;
f = &fl->flp[i];
base = strrchr(f->path, '/');
if (base == NULL)
base = f->path;
else
base++;
if (strncmp(base, "._", 2) == 0)
goto hooksent;
if (copyfile(f->path, NULL, 0, copyflags) == 0)
goto hooksent;
stripdir = f->wpath - f->path;
/* Setup the different bits */
if (asprintf(&ppath, "%.*s._%s",
(int)(base - f->path), f->path, basename(f->path)) == -1) {
ERR("asprintf --extended-attributes path");
return 0;
}
packed = fl_new(fl);
memcpy(packed, f, sizeof(*f));
packed->path = ppath;
packed->wpath = ppath + stripdir;
packed->link = NULL;
packed->open = apple_open_xattrs;
packed->sent = apple_flist_sent;
f->st.flags |= FLSTAT_PLATFORM_XATTR;
hooksent:
f->sent = apple_flist_sent;
}
return 1;
}
#define HAVE_PLATFORM_FLIST_RECEIVED 1
void
platform_flist_received(struct sess *sess, struct flist *fl, size_t flsz)
{
struct flist temp;
if (!sess->opts->extended_attributes)
return;
/*
* With extended attributes, we synthesize an AppleDouble file (._foo)
* and transmit that alongside the base file that it applies to. In
* order for this to work out cleanly, we need to be sure that we're
* requesting the base file before the AppleDouble file so that we can
* just pack the AppleDouble file back in when it's done. If we don't
* do it here, it requires searching both when a file we know has
* some extended attributes finishes, and also when the AppleDouble file
* finishes in case some other reordering has occurred.
*/
for (size_t i = 0; i < flsz - 1; i++) {
struct flist *search;
const char *search_base;
size_t search_prefix;
search = &fl[i];
if (!S_ISREG(search->st.mode))
continue;
search_base = strrchr(search->path, '/');
if (search_base == NULL)
search_base = search->path;
else
search_base++;
if (strncmp(search_base, "._", 2) != 0)
continue;
/*
* Track the parts we need to match; the leading directory bits,
* and the trailing filename.
*/
search_prefix = search_base - search->path;
search_base += 2;
for (size_t j = i + 1; j < flsz; j++) {
struct flist *next;
next = &fl[j];
/*
* It's already sorted in such a way that we shouldn't
* have to worry about missing anything if we stop
* searching as soon as we move out of the search
* prefix.
*/
if (strncmp(next->path, search->path, search_prefix) != 0)
break;
if (strcmp(next->path + search_prefix, search_base) != 0)
continue;
/* Swap */
temp = *next;
*next = *search;
*search = temp;
break;
}
}
}
#define HAVE_PLATFORM_FLIST_ENTRY_RECEIVED 1
int
platform_flist_entry_received(struct sess *sess, int fdin, struct flist *f)
{
uint8_t xattr;
if (!sess->opts->extended_attributes)
return 1;
if (!io_read_byte(sess, fdin, &xattr)) {
ERRX1("io_read_byte");
return 0;
}
if (xattr)
f->st.flags |= FLSTAT_PLATFORM_XATTR;
else
f->st.flags |= FLSTAT_PLATFORM_CLEAR_XATTR;
return 1;
}
static int
apple_merge_appledouble(const struct sess *sess, struct flist *f,
int fromfd, const char *fname, int tofd, const char *toname)
{
char newname[PATH_MAX];
const char *base;
int error, ffd, tfd, serrno;
/*
* We'll redo our base name calculation just to preserve the parts of
* toname so that we can more easily reconstruct it into `newname`.
*/
base = strrchr(toname, '/');
if (base == NULL)
base = toname;
else
base++;
/* Chop off the leading ._ to get the unpacked name */
if (snprintf(newname, sizeof(newname), "%.*s%s", (int)(base - toname),
toname, base + 2) == -1) {
ERR("snprintf");
return 0;
}
ffd = tfd = -1;
ffd = openat(fromfd, fname, O_RDONLY);
if (ffd == -1) {
ERR("%s: openat", fname);
return 0;
}
tfd = openat(tofd, newname,
O_WRONLY | (sess->opts->preserve_links ? O_NOFOLLOW : 0));
if (tfd == -1) {
ERR("%s: openat", newname);
close(ffd);
return 0;
}
error = fcopyfile(ffd, tfd, NULL,
COPYFILE_UNPACK | COPYFILE_ACL | COPYFILE_XATTR);
serrno = errno;
close(tfd);
close(ffd);
/*
* We won't make failing to unlink the AppleDouble file fatal to the
* operation if the unpack succeeded.
*/
if (error != 0) {
ERRX1("%s: copyfile extended attributes from %s: %s",
newname, fname, strerror(serrno));
return 0;
}
if (unlinkat(fromfd, fname, 0) != 0)
ERRX1("%s: unlink: %s", fname, strerror(errno));
f->st.flags |= FLSTAT_PLATFORM_UNLINKED;
return 1;
}
#define HAVE_PLATFORM_MOVE_FILE 1
int
platform_move_file(const struct sess *sess, struct flist *fl,
int fromfd, const char *fname, int tofd, const char *toname, int final)
{
if (final && sess->opts->extended_attributes) {
const char *base;
base = basename((void *)toname);
if (strncmp(base, "._", 2) == 0) {
/* We won't move this, we'll just unpack it. */
return apple_merge_appledouble(sess, fl, fromfd, fname,
tofd, toname);
}
}
if (move_file(fromfd, fname, tofd, toname, final) != 0) {
ERR("%s: move_file: %s", fname, toname);
return 0;
}
return 1;
}
#define HAVE_PLATFORM_FINISH_TRANSFER 1
int
platform_finish_transfer(const struct sess *sess, struct flist *fl,
int rootfd, const char *name)
{
const char *base;
/*
* Here we'll handle the case of an --inplace transfer being finished.
*/
if (!sess->opts->extended_attributes || sess->opts->dlupdates)
return 1;
if ((fl->st.flags & FLSTAT_PLATFORM_UNLINKED) != 0)
return 1;
base = basename((void *)name);
if (strncmp(base, "._", 2) != 0) {
#ifdef NOTYET
/*
* Not yet clear: optimal approach to clearing extended
* attributes on a file. copyfile() from /dev/null -> path
* works to clear them, but trying to plumb the root path
* through adds some complexity. There is no such thing as
* copyfileat() that takes dirfd / path pairs, and we can't take
* the approach we do to merge the AppleDouble file in because
* fcopyfile() is implemented in userspace and won't accept
* /dev/null at the moment.
*/
if ((fl->st.flags & FLSTAT_PLATFORM_CLEAR_XATTR) != 0) {
/* XXX Clear xattrs / acls */
}
#endif
return 1;
}
/* Merge xattrs in. */
return apple_merge_appledouble(sess, fl, rootfd, name, rootfd, name);
}
#endif /* __APPLE__ */
#if !HAVE_PLATFORM_FLIST_MODIFY
int
platform_flist_modify(const struct sess *sess, struct fl *fl)
{
return 1;
}
#endif
#if !HAVE_PLATFORM_FLIST_RECEIVED
void
platform_flist_received(struct sess *sess, struct flist *fl, size_t flsz)
{
}
#endif
#if !HAVE_PLATFORM_FLIST_ENTRY_RECEIVED
int
platform_flist_entry_received(struct sess *sess, int fdin, struct flist *f)
{
return 1;
}
#endif
#if !HAVE_PLATFORM_MOVE_FILE
int
platform_move_file(const struct sess *sess, struct flist *fl,
int fromfd, const char *fname, int tofd, const char *toname, int final)
{
if (move_file(fromfd, fname, tofd, toname, final) != 0) {
ERR("%s: move_file: %s", fname, toname);
return 0;
}
return 1;
}
#endif
#if !HAVE_PLATFORM_FINISH_TRANSFER
int
platform_finish_transfer(const struct sess *sess, struct flist *fl,
int rootfd, const char *name)
{
return 1;
}
#endif