-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync-fetch.c
2801 lines (2409 loc) · 74 KB
/
rsync-fetch.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pythread.h"
#include <errno.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <time.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/uio.h>
#include <avl.h>
#define orz(x) (sizeof (x) / sizeof *(x))
static inline void ignore_result(ssize_t r) {
if(r == -1)
return;
}
#define NANOSECONDS_ARE_SIGNED 1
#if NANOSECONDS_ARE_SIGNED
typedef int64_t nanosecond_t;
#define NANOSECOND_C(x) INT64_C(x)
#else
typedef uint64_t nanosecond_t;
#define NANOSECOND_C(x) UINT64_C(x)
#endif
#define NANOSECONDS NANOSECOND_C(1000000000)
static inline nanosecond_t timespec2nanoseconds(struct timespec *ts) {
return (nanosecond_t)ts->tv_sec * NANOSECONDS
+ (nanosecond_t)ts->tv_nsec;
}
static inline nanosecond_t nanosecond_truncate(nanosecond_t time) {
#if NANOSECONDS_ARE_SIGNED
return time - (time % NANOSECONDS + NANOSECONDS) % NANOSECONDS;
#else
return time - time % NANOSECONDS;
#endif
}
__attribute__((unused))
static inline struct timespec nanoseconds2timespec(nanosecond_t ns) {
#if NANOSECONDS_ARE_SIGNED
// signed nanosecond case
nanosecond_t tv_nsec = (ns % NANOSECONDS + NANOSECONDS) % NANOSECONDS;
struct timespec ts = {
(ns - tv_nsec) / NANOSECONDS,
tv_nsec,
};
#else
// unsigned nanosecond case
struct timespec ts = {
(time_t)(ns / NANOSECONDS),
(long)(ns % NANOSECONDS),
};
#endif
return ts;
}
static nanosecond_t nanosecond_get_clock(void) {
struct timespec ts;
static bool cg_b0rked = false, gtod_b0rked = false;
if(cg_b0rked || clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
cg_b0rked = true;
struct timeval tv;
if(gtod_b0rked || gettimeofday(&tv, NULL) == -1) {
gtod_b0rked = true;
return (nanosecond_t)time(NULL) * NANOSECONDS;
} else {
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * (suseconds_t)1000;
}
}
return timespec2nanoseconds(&ts);
}
#if 0
#include <execinfo.h>
__attribute__((unused))
static void cluck(const char *fmt, ...) {
void *trace[128];
char boem[128];
int r;
va_list ap;
ignore_result(write(STDERR_FILENO, "\n", 1));
if(fmt) {
va_start(ap, fmt);
r = vsnprintf(boem, sizeof boem, fmt, ap);
va_end(ap);
if(r > 0) {
size_t l = (size_t)r;
if(l > sizeof boem - 1)
l = sizeof boem - 1;
if(boem[l - 1] != '\n')
boem[l++] = '\n';
ignore_result(write(STDERR_FILENO, boem, l));
}
}
backtrace_symbols_fd(trace, backtrace(trace, orz(trace)), STDERR_FILENO);
ignore_result(write(STDERR_FILENO, "\n", 1));
}
#endif
#ifdef WORDS_BIGENDIAN
#ifdef HAVE_BUILTIN_BSWAP16
#define le16(x) ((uint16_t)__builtin_bswap16(x))
#else
__attribute__((const,optimize(3)))
static inline uint16_t le16(uint16_t x) {
return (x << 8) | (x >> 8);
}
#endif
#ifdef HAVE_BUILTIN_BSWAP32
#define le32(x) ((uint32_t)__builtin_bswap32(x))
#else
__attribute__((const,optimize(3)))
static inline uint32_t le32(uint32_t x) {
x = ((x & UINT32_C(0x00FF00FF)) << 8) | ((x & UINT32_C(0xFF00FF00)) >> 8);
return (x << 16) | (x >> 16);
}
#endif
#ifdef HAVE_BUILTIN_BSWAP64
#define le64(x) ((uint64_t)__builtin_bswap64(x))
#else
__attribute__((const,optimize(3)))
static inline uint64_t le64(uint64_t x) {
x = ((x & UINT64_C(0x00FF00FF00FF00FF)) << 8) | ((x & UINT64_C(0xFF00FF00FF00FF00)) >> 8);
x = ((x & UINT64_C(0x0000FFFF0000FFFF)) << 16) | ((x & UINT64_C(0xFFFF0000FFFF0000)) >> 16);
return (x << 32) | (x >> 32);
}
#endif
#else
#define le16(x) (x)
#define le32(x) (x)
#define le64(x) (x)
#endif
typedef struct rf_pipestream {
char *buf;
size_t size; // size of the memory allocation
size_t offset; // offset of start of data
size_t fill; // how much space in use by data
int fd;
} rf_pipestream_t;
#define PIPESTREAM_INITIALIZER {.fd = -1}
__attribute__((unused))
static const rf_pipestream_t rf_pipestream_0 = PIPESTREAM_INITIALIZER;
struct rf_refstring_header {
size_t len;
size_t refcount;
};
typedef char *rf_refstring_t;
#define RF_STREAM_IN_BUFSIZE 65536
#define RF_STREAM_OUT_BUFSIZE 65536
#define RF_STREAM_ERR_BUFSIZE 4096
#define RF_BUFNUM_ADJUSTMENT (3)
#define RF_BUFSIZE_ADJUSTMENT (RF_BUFNUM_ADJUSTMENT * sizeof(void *))
#define RF_KEEPALIVE_INTERVAL (NANOSECOND_C(10) * NANOSECONDS)
#define MAX_BLOCK_SIZE 131072
#define MPLEX_BASE 7
typedef int32_t ndx_t;
#define NDX_DONE INT32_C(1)
#define NDX_FLIST_EOF INT32_C(-2)
#define NDX_FLIST_OFFSET INT32_C(-101)
#define XMIT_TOP_DIR (UINT16_C(1) << 0)
#define XMIT_SAME_MODE (UINT16_C(1) << 1)
#define XMIT_EXTENDED_FLAGS (UINT16_C(1) << 2)
#define XMIT_SAME_UID (UINT16_C(1) << 3)
#define XMIT_SAME_GID (UINT16_C(1) << 4)
#define XMIT_SAME_NAME (UINT16_C(1) << 5)
#define XMIT_LONG_NAME (UINT16_C(1) << 6)
#define XMIT_SAME_TIME (UINT16_C(1) << 7)
#define XMIT_SAME_RDEV_MAJOR (UINT16_C(1) << 8)
#define XMIT_NO_CONTENT_DIR (UINT16_C(1) << 8)
#define XMIT_HLINKED (UINT16_C(1) << 9)
#define XMIT_USER_NAME_FOLLOWS (UINT16_C(1) << 10)
#define XMIT_GROUP_NAME_FOLLOWS (UINT16_C(1) << 11)
#define XMIT_HLINK_FIRST (UINT16_C(1) << 12)
#define XMIT_IO_ERROR_ENDLIST (UINT16_C(1) << 12)
#define XMIT_MOD_NSEC (UINT16_C(1) << 13)
#define ITEM_REPORT_CHANGE (UINT16_C(1) << 1)
#define ITEM_REPORT_SIZE (UINT16_C(1) << 2)
#define ITEM_REPORT_TIMEFAIL (UINT16_C(1) << 2)
#define ITEM_REPORT_TIME (UINT16_C(1) << 3)
#define ITEM_REPORT_PERMS (UINT16_C(1) << 4)
#define ITEM_REPORT_OWNER (UINT16_C(1) << 5)
#define ITEM_REPORT_GROUP (UINT16_C(1) << 6)
#define ITEM_REPORT_ACL (UINT16_C(1) << 7)
#define ITEM_REPORT_XATTR (UINT16_C(1) << 8)
#define ITEM_BASIS_TYPE_FOLLOWS (UINT16_C(1) << 11)
#define ITEM_XNAME_FOLLOWS (UINT16_C(1) << 12)
#define ITEM_IS_NEW (UINT16_C(1) << 13)
#define ITEM_LOCAL_CHANGE (UINT16_C(1) << 14)
#define ITEM_TRANSFER (UINT16_C(1) << 15)
enum {
RF_STREAM_IN,
RF_STREAM_OUT,
RF_STREAM_ERR,
RF_STREAM_NUM
};
enum message_id {
MSG_DATA = 0,
MSG_ERROR_XFER = 1,
MSG_INFO = 2,
MSG_ERROR = 3,
MSG_WARNING = 4,
MSG_LOG = 6,
MSG_NOOP = 42,
MSG_DELETED = 101,
// integer type:
MSG_IO_ERROR = 22,
MSG_ERROR_EXIT = 86,
MSG_SUCCESS = 100,
MSG_NO_SEND = 102,
// rsync internal only (we should never see these):
MSG_ERROR_SOCKET = 5,
MSG_CLIENT = 7,
MSG_ERROR_UTF8 = 8,
MSG_REDO = 9,
MSG_FLIST = 20,
MSG_FLIST_EOF = 21,
};
typedef enum {
RF_STATUS_OK,
RF_STATUS_ERRNO,
RF_STATUS_PYTHON,
RF_STATUS_TIMEOUT,
RF_STATUS_HANGUP,
RF_STATUS_ASSERT,
RF_STATUS_PROTO,
RF_STATUS_EXIT,
} rf_status_t;
typedef struct rf_flist_entry {
rf_refstring_t name;
rf_refstring_t user;
rf_refstring_t group;
rf_refstring_t symlink;
rf_refstring_t hardlink;
PyObject *data_callback;
nanosecond_t mtime;
uint64_t size;
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t major;
uint32_t minor;
bool is_hardlink_target;
} rf_flist_entry_t;
static const rf_flist_entry_t rf_flist_entry_0;
typedef struct rf_flist {
avl_node_t node;
ndx_t size;
ndx_t num;
ndx_t offset;
rf_flist_entry_t **entries;
} rf_flist_t;
static const rf_flist_t rf_flist_0 = { .node = AVL_NODE_INITIALIZER(NULL) };
typedef struct rf_hardlinks {
rf_refstring_t name[2];
ndx_t ndx[2];
} rf_hardlinks_t;
#define RF_HARDLINKS_BUFSIZE (65536 - RF_BUFSIZE_ADJUSTMENT)
#define RF_HARDLINKS_SIZE ((RF_HARDLINKS_BUFSIZE - sizeof(avl_node_t)) / sizeof(rf_hardlinks_t))
#if 1
#define RF_PROPAGATE_STATUS(x) return (x)
#define RF_PROPAGATE_ERROR(x) do { rf_status_t __rf_propagate_error = (x); if(__rf_propagate_error != RF_STATUS_OK) return __rf_propagate_error; } while(false)
#define RF_RETURN_STATUS(x) return (x)
#else
static const char * const rf_status_names[] = {
"RF_STATUS_OK",
"RF_STATUS_ERRNO",
"RF_STATUS_PYTHON",
"RF_STATUS_TIMEOUT",
"RF_STATUS_HANGUP",
"RF_STATUS_ASSERT",
"RF_STATUS_PROTO",
"RF_STATUS_EXIT",
};
#define RF_PROPAGATE_STATUS(x) do { rf_status_t __rf_propagate_status = (x); if(__rf_propagate_status != RF_STATUS_OK) fprintf(stderr, "%s:%d: %s propagated by %s()\n", __FILE__, __LINE__, rf_status_names[__rf_propagate_status], __func__); return __rf_propagate_status; } while(false)
#define RF_PROPAGATE_ERROR(x) do { rf_status_t __rf_propagate_error = (x); if(__rf_propagate_error != RF_STATUS_OK) { fprintf(stderr, "%s:%d: %s propagated by %s()\n", __FILE__, __LINE__, rf_status_names[__rf_propagate_error], __func__); return __rf_propagate_error; } } while(false)
#define RF_RETURN_STATUS(x) do { rf_status_t __rf_return_status = (x); if(__rf_return_status != RF_STATUS_OK) fprintf(stderr, "%s:%d: %s returned by %s()\n", __FILE__, __LINE__, rf_status_names[__rf_return_status], __func__); return __rf_return_status; } while(false)
#endif
#define RSYNCFETCH_MAGIC UINT64_C(0x6FB32179D3F495D0)
static int rf_flist_cmp(const void *a, const void *b, void *userdata) {
ndx_t offset_a = ((rf_flist_t const *)a)->offset;
ndx_t offset_b = ((rf_flist_t const *)b)->offset;
return AVL_CMP(offset_a, offset_b);
}
static int rf_hardlinks_cmp(const void *a, const void *b, void *userdata) {
ndx_t ndx_a = ((rf_hardlinks_t const *)a)->ndx[0];
ndx_t ndx_b = ((rf_hardlinks_t const *)b)->ndx[0];
return AVL_CMP(ndx_a, ndx_b);
}
typedef struct RsyncFetch {
uint64_t magic;
#ifdef WITH_THREAD
PyThreadState *py_thread_state;
PyThread_type_lock lock;
#else
bool py_thread_state;
#endif
PyObject *entry_callback;
PyObject *error_callback;
PyObject *chunk_bytes;
char *chunk_buffer;
char **command;
char **environ;
char **filters;
avl_tree_t flists;
avl_tree_t hardlinks;
rf_pipestream_t in_stream;
rf_pipestream_t out_stream;
rf_pipestream_t err_stream;
rf_flist_entry_t last;
nanosecond_t timeout;
nanosecond_t keepalive_deadline;
size_t hardlinks_num;
size_t filters_num;
size_t multiplex_in_remaining;
size_t multiplex_out_remaining;
size_t chunk_size;
pid_t pid;
uint32_t protocol;
ndx_t ndx;
ndx_t prev_negative_ndx_in;
ndx_t prev_positive_ndx_in;
ndx_t prev_negative_ndx_out;
ndx_t prev_positive_ndx_out;
bool multiplex;
bool failed;
bool closed;
} RsyncFetch_t;
static const RsyncFetch_t RsyncFetch_0 = {
.magic = RSYNCFETCH_MAGIC,
.in_stream = PIPESTREAM_INITIALIZER,
.out_stream = PIPESTREAM_INITIALIZER,
.err_stream = PIPESTREAM_INITIALIZER,
.flists = AVL_TREE_INITIALIZER(rf_flist_cmp, NULL),
.hardlinks = AVL_TREE_INITIALIZER(rf_hardlinks_cmp, NULL),
.hardlinks_num = RF_HARDLINKS_SIZE,
.ndx = 1,
.prev_negative_ndx_in = 1,
.prev_positive_ndx_in = -1,
.prev_negative_ndx_out = 1,
.prev_positive_ndx_out = -1,
.chunk_size = 32768,
.timeout = UINT64_C(30000000000),
};
struct RsyncFetchObject {
PyObject_HEAD
RsyncFetch_t rf;
};
static struct PyModuleDef rsync_fetch_module;
static PyTypeObject RsyncFetch_type;
static inline RsyncFetch_t *RsyncFetch_Check(PyObject *v, bool check_failed) {
if(v && PyObject_TypeCheck(v, &RsyncFetch_type)
&& ((struct RsyncFetchObject *)v)->rf.magic == RSYNCFETCH_MAGIC) {
RsyncFetch_t *rf = &((struct RsyncFetchObject *)v)->rf;
if(check_failed && rf->failed) {
PyErr_Format(PyExc_RuntimeError, "RsyncFetch object is in failed state");
return NULL;
} else {
return rf;
}
} else {
PyErr_Format(PyExc_TypeError, "not a valid RsyncFetch object");
return NULL;
}
}
#ifdef WITH_THREAD
static inline bool rf_unblock_threads(RsyncFetch_t *rf) {
if(rf->py_thread_state) {
return false;
} else {
rf->py_thread_state = PyEval_SaveThread();
return true;
}
}
static inline bool rf_block_threads(RsyncFetch_t *rf) {
PyThreadState *py_thread_state = rf->py_thread_state;
if(py_thread_state) {
rf->py_thread_state = NULL;
PyEval_RestoreThread(py_thread_state);
return true;
} else {
return false;
}
}
static bool rf_acquire_lock(RsyncFetch_t *rf) {
PyThread_type_lock lock = rf->lock;
PyLockStatus have_lock;
Py_BEGIN_ALLOW_THREADS
have_lock = PyThread_acquire_lock(lock, WAIT_LOCK);
Py_END_ALLOW_THREADS
if(have_lock != PY_LOCK_ACQUIRED) {
PyErr_Format(PyExc_RuntimeError, "unable to acquire lock");
return false;
}
return true;
}
static inline void rf_release_lock(RsyncFetch_t *rf) {
PyThread_release_lock(rf->lock);
}
#else
static inline bool rf_block_threads(RsyncFetch_t *rf) {
if(rf->py_thread_state) {
return false;
} else {
rf->py_thread_state = true;
return true;
}
}
static inline bool rf_unblock_threads(RsyncFetch_t *rf) {
if(rf->py_thread_state) {
rf->py_thread_state = false;
return true;
} else {
return false;
}
}
static inline bool rf_acquire_lock(RsyncFetch_t *rf) {
return true;
}
static inline void rf_release_lock(RsyncFetch_t *rf) {}
#endif
static inline size_t rf_refstring_len(const rf_refstring_t str) {
return str ? ((struct rf_refstring_header *)str)[-1].len : 0;
}
static void rf_refstring_free(RsyncFetch_t *rf, rf_refstring_t *strp) {
if(strp) {
char *str = *strp;
if(str) {
*strp = NULL;
struct rf_refstring_header *h = (struct rf_refstring_header *)str - 1;
size_t refcount = h->refcount;
if(refcount == 1)
free(h);
else
h->refcount = refcount - 1;
}
}
}
static rf_status_t rf_refstring_newlen(RsyncFetch_t *rf, const char *src, size_t len, rf_refstring_t *strp) {
struct rf_refstring_header *h = malloc(sizeof *h + len + 1);
if(!h)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
h->len = len;
h->refcount = 1;
rf_refstring_t str = (rf_refstring_t)(h + 1);
if(src)
memcpy(str, src, len);
str[len] = '\0';
rf_refstring_free(rf, strp);
return *strp = str, RF_STATUS_OK;
}
__attribute__((unused))
static rf_status_t rf_refstring_new(RsyncFetch_t *rf, const char *src, rf_refstring_t *strp) {
if(src)
return rf_refstring_newlen(rf, src, strlen(src), strp);
else
return *strp = NULL, RF_STATUS_OK;
}
static void rf_refstring_dup(RsyncFetch_t *rf, rf_refstring_t str, rf_refstring_t *strp) {
rf_refstring_free(rf, strp);
if(str) {
struct rf_refstring_header *h = (struct rf_refstring_header *)str - 1;
h->refcount++;
if(strp)
*strp = str;
}
}
static rf_status_t rf_ensure_bytes(RsyncFetch_t *rf, PyObject *obj, PyObject **bytesp) {
rf_block_threads(rf);
if(PyUnicode_Check(obj)) {
PyObject *bytes = PyUnicode_AsEncodedString(obj, "UTF-8", "surrogateescape");
if(!bytes)
RF_RETURN_STATUS(RF_STATUS_PYTHON);
*bytesp = bytes;
} else if(PyBytes_Check(obj)) {
Py_IncRef(obj);
*bytesp = obj;
} else {
PyObject *bytes = PyBytes_FromObject(obj);
if(!bytes)
RF_RETURN_STATUS(RF_STATUS_PYTHON);
*bytesp = bytes;
}
RF_RETURN_STATUS(RF_STATUS_OK);
}
static rf_status_t rf_iterate(RsyncFetch_t *rf, PyObject *iterable, char ***listp, size_t *countp) {
rf_block_threads(rf);
PyObject *iterator = PyObject_GetIter(iterable);
if(!iterator)
RF_RETURN_STATUS(RF_STATUS_PYTHON);
rf_status_t s = RF_STATUS_OK;
size_t fill = 0;
size_t size = 32;
while(size < RF_BUFNUM_ADJUSTMENT)
size <<= 1;
size -= RF_BUFNUM_ADJUSTMENT;
void **list = malloc(size * sizeof *list);
if(list) {
for(;;) {
PyObject *item = PyIter_Next(iterator);
if(!item) {
if(PyErr_Occurred())
s = RF_STATUS_PYTHON;
break;
}
PyObject *bytes;
s = rf_ensure_bytes(rf, item, &bytes);
Py_DecRef(item);
if(s != RF_STATUS_OK)
break;
if(fill == size) {
size = ((size + RF_BUFNUM_ADJUSTMENT) << 1) - RF_BUFNUM_ADJUSTMENT;
void **newlist = realloc(list, size * sizeof *list);
if(!newlist) {
s = RF_STATUS_ERRNO;
break;
}
list = newlist;
}
list[fill++] = bytes;
}
// OK, we now have a list with bytes items (we hope).
// The idea is now to convert it to a single memory
// allocation that starts with the actual list of string
// pointers, followed by the strings themselves.
size_t total = 0;
if(s == RF_STATUS_OK) {
for(size_t i = 0; i < fill; i++) {
Py_ssize_t len = PyBytes_Size(list[i]);
if(len == -1) {
s = RF_STATUS_PYTHON;
break;
}
total += (size_t)len;
}
}
size_t converted = 0;
if(s == RF_STATUS_OK) {
// tack on an extra '+ fill' to accomodate the \0 for each string
void **newlist = realloc(list, (fill + 1) * sizeof *list + total + fill);
if(newlist) {
list = newlist;
char *string_location = (char *)(list + fill + 1);
for(size_t i = 0; i < fill; i++) {
PyObject *bytes = list[i];
char *buf;
Py_ssize_t len;
if(PyBytes_AsStringAndSize(bytes, &buf, &len) == -1) {
s = RF_STATUS_PYTHON;
break;
}
len++; // include the trailing \0
memcpy(string_location, buf, (size_t)len);
Py_DecRef(bytes);
list[i] = string_location;
string_location += len;
converted++;
}
list[fill] = string_location;
} else {
s = RF_STATUS_ERRNO;
}
}
if(s == RF_STATUS_OK) {
*listp = (char **)list;
if(countp)
*countp = fill;
else
list[fill] = NULL;
} else {
for(size_t i = converted; i < fill; i++)
Py_DecRef(list[i]);
free(list);
}
} else {
s = RF_STATUS_ERRNO;
}
Py_DecRef(iterator);
RF_RETURN_STATUS(s);
}
static rf_flist_entry_t *rf_flist_get_entry(RsyncFetch_t *rf, rf_flist_t *flist, ndx_t ndx) {
ndx_t offset = flist->offset;
if(ndx >= offset) {
ndx -= offset;
ndx_t size = flist->size;
if(ndx < size)
return flist->entries[ndx];
}
return NULL;
}
static rf_flist_entry_t *rf_find_ndx(RsyncFetch_t *rf, ndx_t ndx) {
rf_flist_t dummy = rf_flist_0;
dummy.offset = ndx;
avl_node_t *node = avl_search_right(&rf->flists, &dummy, NULL);
if(!node)
return NULL;
return rf_flist_get_entry(rf, node->item, ndx);
}
#ifndef HAVE_PIPE2
static int pipe2(int *fds, int flags) {
if(pipe(fds) == -1)
return -1;
if(fcntl(fds[0], F_SETFD, flags) == -1 || fcntl(fds[1], F_SETFD, flags) == -1) {
int *errno_pointer = &errno;
int saved_errno = *errno_pointer;
close(fds[0]);
close(fds[1]);
*errno_pointer = saved_errno;
return -1;
}
return 0;
}
#endif
static int create_pipe(int *fds) {
if(pipe2(fds, O_CLOEXEC|O_NONBLOCK) == -1)
return -1;
if(fds[0] < 3 || fds[1] < 3) {
close(fds[0]);
close(fds[1]);
errno = EBADFD;
return -1;
}
return 0;
}
static rf_status_t rf_flush_output(RsyncFetch_t *rf) {
size_t multiplex_out_remaining = rf->multiplex_out_remaining;
if(multiplex_out_remaining) {
rf_pipestream_t *stream = &rf->out_stream;
size_t start = stream->offset + stream->fill - multiplex_out_remaining - 4;
size_t size = stream->size;
char *buf = stream->buf;
if(start >= size) {
start -= size;
buf[start] = (char)multiplex_out_remaining;
buf[start + 1] = (char)(multiplex_out_remaining >> 8);
buf[start + 2] = (char)(multiplex_out_remaining >> 16);
} else {
switch(size - start) {
case 1:
buf[start] = (char)multiplex_out_remaining;
buf[0] = (char)(multiplex_out_remaining >> 8);
buf[1] = (char)(multiplex_out_remaining >> 16);
break;
case 2:
buf[start] = (char)multiplex_out_remaining;
buf[start + 1] = (char)(multiplex_out_remaining >> 8);
buf[0] = (char)(multiplex_out_remaining >> 16);
break;
default:
buf[start] = (char)multiplex_out_remaining;
buf[start + 1] = (char)(multiplex_out_remaining >> 8);
buf[start + 2] = (char)(multiplex_out_remaining >> 16);
}
}
rf->multiplex_out_remaining = 0;
}
RF_RETURN_STATUS(RF_STATUS_OK);
}
__attribute__((hot))
static rf_status_t rf_send_bytes_raw(RsyncFetch_t *rf, const char *src, size_t len) {
rf_pipestream_t *stream = &rf->out_stream;
size_t fill = stream->fill;
size_t size = stream->size;
size_t offset = stream->offset;
char *buf = stream->buf;
if(buf) {
if(fill + len > size) {
size_t newsize = (size + RF_BUFSIZE_ADJUSTMENT) << 1;
if(newsize < RF_STREAM_OUT_BUFSIZE)
newsize = RF_STREAM_OUT_BUFSIZE;
while(fill + len > newsize - RF_BUFSIZE_ADJUSTMENT)
newsize <<= 1;
newsize -= RF_BUFSIZE_ADJUSTMENT;
if(offset) {
char *newbuf = malloc(newsize);
if(!newbuf)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
if(offset + fill > size) {
size_t amount = size - offset;
memcpy(newbuf, buf + offset, amount);
memcpy(newbuf + amount, buf, fill - amount);
} else {
memcpy(newbuf, buf + offset, size);
}
stream->offset = offset = 0;
free(buf);
buf = newbuf;
} else {
buf = realloc(buf, newsize);
if(!buf)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
}
stream->buf = buf;
stream->size = size = newsize;
}
} else {
size += RF_BUFSIZE_ADJUSTMENT;
if(size < RF_STREAM_OUT_BUFSIZE)
size = RF_STREAM_OUT_BUFSIZE;
while(len > size - RF_BUFSIZE_ADJUSTMENT)
size <<= 1;
size -= RF_BUFSIZE_ADJUSTMENT;
buf = malloc(size);
if(!buf)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
stream->buf = buf;
stream->size = size;
}
size_t start = offset + fill;
if(start > size)
start -= size;
if(len == 1) {
buf[start] = *src;
} if(start + len > size) {
size_t amount = size - start;
memcpy(buf + start, src, amount);
memcpy(buf, src + amount, len - amount);
} else {
memcpy(buf + start, src, len);
}
stream->fill = fill + len;
RF_RETURN_STATUS(RF_STATUS_OK);
}
__attribute__((hot))
static rf_status_t rf_send_bytes(RsyncFetch_t *rf, const char *buf, size_t len) {
if(!rf->multiplex) {
RF_PROPAGATE_ERROR(rf_flush_output(rf));
RF_PROPAGATE_STATUS(rf_send_bytes_raw(rf, buf, len));
}
size_t multiplex_out_remaining = rf->multiplex_out_remaining;
if(multiplex_out_remaining + len >= 0xFFFFFF) {
size_t chunk = 0xFFFFFF - multiplex_out_remaining;
RF_PROPAGATE_ERROR(rf_send_bytes_raw(rf, buf, chunk));
rf->multiplex_out_remaining = 0xFFFFFF;
RF_PROPAGATE_ERROR(rf_flush_output(rf));
buf += chunk;
len -= chunk;
while(len >= 0xFFFFFF) {
static const uint8_t mplex[] = { 0xFF, 0xFF, 0xFF, MSG_DATA + MPLEX_BASE };
RF_PROPAGATE_ERROR(rf_send_bytes_raw(rf, (const char *)mplex, sizeof mplex));
RF_PROPAGATE_ERROR(rf_send_bytes_raw(rf, buf, 0xFFFFFF));
len -= 0xFFFFFF;
buf += 0xFFFFFF;
}
multiplex_out_remaining = 0;
}
if(!len)
RF_RETURN_STATUS(RF_STATUS_OK);
if(!multiplex_out_remaining) {
static const uint8_t mplex[] = { 0, 0, 0, MSG_DATA + MPLEX_BASE };
RF_PROPAGATE_ERROR(rf_send_bytes_raw(rf, (const char *)mplex, sizeof mplex));
}
RF_PROPAGATE_ERROR(rf_send_bytes_raw(rf, buf, len));
rf->multiplex_out_remaining = multiplex_out_remaining + len;
RF_RETURN_STATUS(RF_STATUS_OK);
}
__attribute__((unused))
static inline rf_status_t rf_send_int8(RsyncFetch_t *rf, int8_t d) {
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&d, sizeof d));
}
static inline rf_status_t rf_send_uint8(RsyncFetch_t *rf, uint8_t d) {
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&d, sizeof d));
}
__attribute__((unused))
static inline rf_status_t rf_send_int16(RsyncFetch_t *rf, int16_t d) {
int16_t le = le16(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
static inline rf_status_t rf_send_uint16(RsyncFetch_t *rf, uint16_t d) {
uint16_t le = le16(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
__attribute__((unused))
static inline rf_status_t rf_send_int32(RsyncFetch_t *rf, int32_t d) {
int32_t le = le32(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
static inline rf_status_t rf_send_uint32(RsyncFetch_t *rf, uint32_t d) {
uint32_t le = le32(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
__attribute__((unused))
static inline rf_status_t rf_send_int64(RsyncFetch_t *rf, int64_t d) {
int64_t le = le64(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
__attribute__((unused))
static inline rf_status_t rf_send_uint64(RsyncFetch_t *rf, uint64_t d) {
uint64_t le = le64(d);
RF_PROPAGATE_STATUS(rf_send_bytes(rf, (char *)&le, sizeof le));
}
static rf_status_t rf_write_out_stream(RsyncFetch_t *rf) {
rf_pipestream_t *stream = &rf->out_stream;
size_t fill = stream->fill;
size_t size = stream->size;
size_t offset = stream->offset;
char *buf = stream->buf;
size_t multiplex_out_remaining = rf->multiplex_out_remaining;
if(multiplex_out_remaining)
RF_PROPAGATE_ERROR(rf_flush_output(rf));
rf_unblock_threads(rf);
ssize_t r;
if(offset + fill > size) {
size_t amount = size - offset;
struct iovec iov[2] = {
{ buf + offset, amount },
{ buf, fill - amount },
};
r = writev(stream->fd, iov, orz(iov));
} else {
r = write(stream->fd, buf + offset, fill);
}
if(r == -1)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
if(r > 0)
rf->keepalive_deadline = nanosecond_get_clock() + RF_KEEPALIVE_INTERVAL;
fill -= (size_t)r;
if(fill) {
stream->fill = fill;
offset += (size_t)r;
stream->offset = offset < size ? offset : offset - size;
if(multiplex_out_remaining && fill >= multiplex_out_remaining + 4)
rf->multiplex_out_remaining = multiplex_out_remaining;
} else {
stream->fill = 0;
stream->offset = 0;
}
RF_RETURN_STATUS(RF_STATUS_OK);
}
static rf_status_t rf_read_error_stream(RsyncFetch_t *rf) {
rf_pipestream_t *stream = &rf->err_stream;
size_t fill = stream->fill;
size_t size = stream->size;
char *buf = stream->buf;
if(!buf) {
size = RF_STREAM_ERR_BUFSIZE - RF_BUFSIZE_ADJUSTMENT;
buf = malloc(size);
if(!buf)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
stream->buf = buf;
stream->size = size;
}
rf_unblock_threads(rf);
char *old_buf_end = buf + fill;
ssize_t r = read(stream->fd, old_buf_end, size - fill);
if(r == -1)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
char *new_buf_end = old_buf_end + r;
char *todo = buf;
PyObject *error_callback = rf->error_callback;
for(char *eol = memchr(old_buf_end, '\n', (size_t)r); eol++; eol = memchr(eol, '\n', (size_t)(new_buf_end - eol))) {
if(error_callback) {
rf_block_threads(rf);
PyObject *result = PyObject_CallFunction(error_callback, "y#", todo, (Py_ssize_t)(eol - todo));
if(!result)
RF_RETURN_STATUS(RF_STATUS_PYTHON);
Py_DecRef(result);
} else {
rf_unblock_threads(rf);
if(write(STDERR_FILENO, todo, (size_t)(eol - todo)) == -1)
RF_RETURN_STATUS(RF_STATUS_ERRNO);
}