-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathcasync.c
4533 lines (3438 loc) · 128 KB
/
casync.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
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <fcntl.h>
#include <poll.h>
#include <sys/stat.h>
#include "cacache.h"
#include "cachunk.h"
#include "cachunker.h"
#include "cadecoder.h"
#include "caencoder.h"
#include "caformat-util.h"
#include "caformat.h"
#include "caindex.h"
#include "caprotocol.h"
#include "caremote.h"
#include "caseed.h"
#include "castore.h"
#include "casync.h"
#include "def.h"
#include "realloc-buffer.h"
#include "time-util.h"
#include "util.h"
/* #undef EINVAL */
/* #define EINVAL __LINE__ */
/* #undef ENXIO */
/* #define ENXIO __LINE__ */
/* #undef EUNATCH */
/* #define EUNATCH __LINE__ */
typedef enum CaDirection {
CA_SYNC_ENCODE,
CA_SYNC_DECODE,
} CaDirection;
/* In which state regarding cache check and validation are we? */
typedef enum CaCacheState {
CA_SYNC_CACHE_OFF, /* Possibly write to the cache, but don't consult it */
CA_SYNC_CACHE_CHECK, /* We are at a position where it might make sense to check the cache */
CA_SYNC_CACHE_VERIFY, /* We found a chunk in the cache, and are validating if it still matches disk */
CA_SYNC_CACHE_FAILED, /* We found a chunk in the cache, but it turned out not to match disk, now seek back where the cache experiment started */
CA_SYNC_CACHE_SUCCEEDED, /* We found a chunk in the cache, and it checked out */
CA_SYNC_CACHE_IDLE, /* We are not at a position where it might make sense to check the cache */
} CaCacheState;
struct CaSync {
CaDirection direction;
uint64_t start_nsec;
CaEncoder *encoder;
CaDecoder *decoder;
CaChunker chunker;
CaChunker original_chunker; /* A copy of the full chunker state from the beginning, which we can use when we need to reset things */
CaIndex *index;
CaRemote *remote_index;
CaRemote *remote_archive;
CaChunkID next_chunk;
uint64_t next_chunk_size;
bool next_chunk_valid;
CaStore *wstore;
CaStore **rstores;
size_t n_rstores;
CaStore *cache_store;
CaRemote *remote_wstore;
CaRemote **remote_rstores;
size_t n_remote_rstores;
size_t current_remote;
CaSeed **seeds;
size_t n_seeds;
size_t current_seed; /* The seed we are currently indexing */
bool index_flags_propagated;
CaCache *cache;
CaCacheState cache_state;
CaChunkID current_cache_chunk_id;
uint64_t current_cache_chunk_size;
CaOrigin *current_cache_origin;
CaLocation *current_cache_start_location;
int base_fd;
int boundary_fd;
int archive_fd;
char *base_path, *temporary_base_path;
char *boundary_path;
char *archive_path, *temporary_archive_path;
mode_t base_mode;
mode_t make_mode;
ReallocBuffer buffer;
ReallocBuffer index_buffer;
ReallocBuffer archive_buffer;
ReallocBuffer compress_buffer;
CaOrigin *buffer_origin;
CaDigest *chunk_digest;
bool archive_eof;
bool remote_index_eof;
int log_level;
size_t rate_limit_bps;
uint64_t feature_flags;
uint64_t feature_flags_mask;
uint64_t n_written_chunks;
uint64_t n_reused_chunks;
uint64_t n_prefetched_chunks;
uint64_t n_cache_hits;
uint64_t n_cache_misses;
uint64_t n_cache_invalidated;
uint64_t n_cache_added;
uint64_t archive_size;
uint64_t chunk_skip;
bool punch_holes:1;
bool reflink:1;
bool hardlink:1;
bool delete:1;
bool payload:1;
bool undo_immutable:1;
bool archive_digest:1;
bool hardlink_digest:1;
bool payload_digest:1;
CaFileRoot *archive_root;
uid_t uid_shift;
uid_t uid_range; /* uid_range == 0 means "full range" */
uint64_t chunk_size_min;
uint64_t chunk_size_avg;
uint64_t chunk_size_max;
CaCompressionType compression_type;
uint64_t first_chunk_request_nsec;
uint64_t last_chunk_request_nsec;
};
#define CA_SYNC_IS_STARTED(s) ((s)->start_nsec != 0)
static CaSync *ca_sync_new(void) {
CaSync *s;
s = new0(CaSync, 1);
if (!s)
return NULL;
s->base_fd = s->boundary_fd = s->archive_fd = -1;
s->base_mode = (mode_t) -1;
s->make_mode = (mode_t) -1;
s->chunker = (CaChunker) CA_CHUNKER_INIT;
s->log_level = -1;
s->archive_size = UINT64_MAX;
s->punch_holes = true;
s->reflink = true;
s->delete = true;
s->payload = true;
s->feature_flags = s->feature_flags_mask = UINT64_MAX;
s->compression_type = CA_COMPRESSION_DEFAULT;
return s;
}
CaSync *ca_sync_new_encode(void) {
CaSync *s;
s = ca_sync_new();
if (!s)
return NULL;
s->direction = CA_SYNC_ENCODE;
s->feature_flags = CA_FORMAT_DEFAULT & SUPPORTED_FEATURE_MASK;
return s;
}
CaSync *ca_sync_new_decode(void) {
CaSync *s;
s = ca_sync_new();
if (!s)
return NULL;
s->direction = CA_SYNC_DECODE;
return s;
}
int ca_sync_set_chunk_size_min(CaSync *s, uint64_t v) {
int r;
if (!s)
return -EINVAL;
r = ca_chunker_set_size(&s->chunker, v, s->chunk_size_avg, s->chunk_size_max);
if (r < 0)
return r;
s->chunk_size_min = v;
return 0;
}
int ca_sync_set_chunk_size_avg(CaSync *s, uint64_t v) {
int r;
if (!s)
return -EINVAL;
r = ca_chunker_set_size(&s->chunker, s->chunk_size_min, v, s->chunk_size_max);
if (r < 0)
return r;
s->chunk_size_avg = v;
return 0;
}
int ca_sync_set_chunk_size_max(CaSync *s, uint64_t v) {
int r;
if (!s)
return -EINVAL;
r = ca_chunker_set_size(&s->chunker, s->chunk_size_min, s->chunk_size_avg, v);
if (r < 0)
return r;
s->chunk_size_max = v;
return 0;
}
int ca_sync_get_chunk_size_avg(CaSync *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (!ret)
return -EINVAL;
*ret = s->chunker.chunk_size_avg;
return 0;
}
int ca_sync_get_chunk_size_min(CaSync *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (!ret)
return -EINVAL;
*ret = s->chunker.chunk_size_min;
return 0;
}
int ca_sync_get_chunk_size_max(CaSync *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (!ret)
return -EINVAL;
*ret = s->chunker.chunk_size_max;
return 0;
}
int ca_sync_set_punch_holes(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_set_punch_holes(s->decoder, enabled);
if (r < 0)
return r;
}
s->punch_holes = enabled;
return 0;
}
int ca_sync_set_reflink(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_set_reflink(s->decoder, enabled);
if (r < 0)
return r;
}
s->reflink = enabled;
return 0;
}
int ca_sync_set_hardlink(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_enable_hardlink_digest(s->decoder, s->hardlink_digest || enabled);
if (r < 0)
return r;
r = ca_decoder_set_hardlink(s->decoder, enabled);
if (r < 0)
return r;
}
s->hardlink = enabled;
return 0;
}
int ca_sync_set_delete(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_set_delete(s->decoder, enabled);
if (r < 0)
return r;
}
s->delete = enabled;
return 0;
}
int ca_sync_set_payload(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_set_payload(s->decoder, enabled || s->remote_archive);
if (r < 0)
return r;
}
s->payload = enabled;
return 0;
}
int ca_sync_set_undo_immutable(CaSync *s, bool enabled) {
int r;
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder) {
r = ca_decoder_set_undo_immutable(s->decoder, enabled);
if (r < 0)
return r;
}
s->undo_immutable = enabled;
return 0;
}
int ca_sync_set_uid_shift(CaSync *s, uid_t u) {
int r;
if (!s)
return -EINVAL;
if (s->decoder) {
r = ca_decoder_set_uid_shift(s->decoder, u);
if (r < 0)
return r;
}
if (s->encoder) {
r = ca_encoder_set_uid_shift(s->encoder, u);
if (r < 0)
return r;
}
s->uid_shift = u;
return 0;
}
int ca_sync_set_uid_range(CaSync *s, uid_t u) {
int r;
if (!s)
return -EINVAL;
if (s->decoder) {
r = ca_decoder_set_uid_range(s->decoder, u);
if (r < 0)
return r;
}
if (s->encoder) {
r = ca_encoder_set_uid_range(s->encoder, u);
if (r < 0)
return r;
}
s->uid_range = u;
return 0;
}
static void ca_sync_reset_cache_data(CaSync *s) {
assert(s);
/* Resets the cache data, i.e. the cached item we are currently processing. */
s->current_cache_start_location = ca_location_unref(s->current_cache_start_location);
s->current_cache_origin = ca_origin_unref(s->current_cache_origin);
}
CaSync *ca_sync_unref(CaSync *s) {
size_t i;
if (!s)
return NULL;
ca_encoder_unref(s->encoder);
ca_decoder_unref(s->decoder);
ca_store_unref(s->wstore);
for (i = 0; i < s->n_rstores; i++)
ca_store_unref(s->rstores[i]);
free(s->rstores);
ca_store_unref(s->cache_store);
ca_remote_unref(s->remote_wstore);
for (i = 0; i < s->n_remote_rstores; i++)
ca_remote_unref(s->remote_rstores[i]);
free(s->remote_rstores);
for (i = 0; i < s->n_seeds; i++)
ca_seed_unref(s->seeds[i]);
free(s->seeds);
ca_sync_reset_cache_data(s);
ca_cache_unref(s->cache);
safe_close(s->base_fd);
safe_close(s->boundary_fd);
safe_close(s->archive_fd);
free(s->base_path);
free(s->archive_path);
free(s->boundary_path);
if (s->temporary_base_path) {
(void) unlink(s->temporary_base_path);
free(s->temporary_base_path);
}
if (s->temporary_archive_path) {
(void) unlink(s->temporary_archive_path);
free(s->temporary_archive_path);
}
ca_index_unref(s->index);
ca_remote_unref(s->remote_index);
ca_remote_unref(s->remote_archive);
realloc_buffer_free(&s->buffer);
realloc_buffer_free(&s->index_buffer);
realloc_buffer_free(&s->archive_buffer);
realloc_buffer_free(&s->compress_buffer);
ca_origin_unref(s->buffer_origin);
ca_file_root_unref(s->archive_root);
ca_digest_free(s->chunk_digest);
return mfree(s);
}
int ca_sync_set_log_level(CaSync *s, int log_level) {
if (!s)
return -EINVAL;
s->log_level = log_level;
return 0;
}
int ca_sync_set_rate_limit_bps(CaSync *s, uint64_t rate_limit_bps) {
if (!s)
return -EINVAL;
s->rate_limit_bps = rate_limit_bps;
return 0;
}
int ca_sync_set_feature_flags(CaSync *s, uint64_t flags) {
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_ENCODE)
return -ENOTTY;
if (s->encoder)
return -EBUSY;
return ca_feature_flags_normalize(flags, &s->feature_flags);
}
int ca_sync_get_feature_flags(CaSync *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (s->direction == CA_SYNC_ENCODE)
*ret = s->feature_flags;
else {
if (!s->decoder)
return -ENODATA;
return ca_decoder_get_feature_flags(s->decoder, ret);
}
return 0;
}
int ca_sync_get_covering_feature_flags(CaSync *s, uint64_t *ret) {
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_ENCODE)
return -ENOTTY;
if (!s->encoder)
return -ENODATA;
return ca_encoder_get_covering_feature_flags(s->encoder, ret);
}
int ca_sync_set_feature_flags_mask(CaSync *s, uint64_t mask) {
if (!s)
return -EINVAL;
if (s->direction != CA_SYNC_DECODE)
return -ENOTTY;
if (s->decoder)
return -EBUSY;
return ca_feature_flags_normalize_mask(mask, &s->feature_flags_mask);
}
static int ca_sync_allocate_index(CaSync *s) {
assert(s);
if (s->index)
return -EBUSY;
if (s->remote_index)
return -EBUSY;
if (s->direction == CA_SYNC_ENCODE)
s->index = ca_index_new_write();
else if (s->direction == CA_SYNC_DECODE)
s->index = ca_index_new_read();
else
assert(false);
if (!s->index)
return -ENOMEM;
return 0;
}
int ca_sync_set_index_fd(CaSync *s, int fd) {
int r;
if (!s)
return -EINVAL;
if (fd < 0)
return -EINVAL;
r = ca_sync_allocate_index(s);
if (r < 0)
return r;
r = ca_index_set_fd(s->index, fd);
if (r < 0) {
s->index = ca_index_unref(s->index);
return r;
}
return 0;
}
int ca_sync_set_index_path(CaSync *s, const char *path) {
int r;
if (!s)
return -EINVAL;
if (!path)
return -EINVAL;
r = ca_sync_allocate_index(s);
if (r < 0)
return r;
r = ca_index_set_path(s->index, path);
if (r < 0) {
s->index = ca_index_unref(s->index);
return r;
}
return 0;
}
int ca_sync_set_index_remote(CaSync *s, const char *url) {
uint64_t flags;
int r;
if (!s)
return -EINVAL;
if (!url)
return -EINVAL;
if (s->index)
return -EBUSY;
if (s->remote_index)
return -EBUSY;
flags = s->direction == CA_SYNC_ENCODE ? CA_PROTOCOL_PUSH_INDEX : CA_PROTOCOL_PULL_INDEX;
if (s->remote_wstore) {
/* Try to reuse the main store remote for the index too, if it matches the same server */
r = ca_remote_set_index_url(s->remote_wstore, url);
if (r >= 0) {
r = ca_remote_add_local_feature_flags(s->remote_wstore, flags);
if (r < 0)
return r;
s->remote_index = ca_remote_ref(s->remote_wstore);
return 0;
}
if (r != -EBUSY) /* Fail, except when the reason is that it matches the same server. */
return r;
}
s->remote_index = ca_remote_new();
if (!s->remote_index)
return -ENOMEM;
if (s->log_level != -1) {
r = ca_remote_set_log_level(s->remote_index, s->log_level);
if (r < 0)
return r;
}
if (s->rate_limit_bps > 0) {
r = ca_remote_set_rate_limit_bps(s->remote_index, s->rate_limit_bps);
if (r < 0)
return r;
}
r = ca_remote_set_index_url(s->remote_index, url);
if (r < 0)
return r;
r = ca_remote_set_local_feature_flags(s->remote_index, flags);
if (r < 0)
return r;
return 0;
}
int ca_sync_set_index_auto(CaSync *s, const char *locator) {
CaLocatorClass c;
if (!s)
return -EINVAL;
if (!locator)
return -EINVAL;
c = ca_classify_locator(locator);
if (c < 0)
return -EINVAL;
if (c == CA_LOCATOR_PATH)
return ca_sync_set_index_path(s, locator);
return ca_sync_set_index_remote(s, locator);
}
int ca_sync_set_base_fd(CaSync *s, int fd) {
if (!s)
return -EINVAL;
if (fd < 0)
return -EINVAL;
if (s->base_fd >= 0)
return -EBUSY;
if (s->base_mode != (mode_t) -1)
return -EBUSY;
if (s->base_path)
return -EBUSY;
if (s->boundary_fd >= 0)
return -EBUSY;
if (s->boundary_path)
return -EBUSY;
s->base_fd = fd;
return 0;
}
int ca_sync_set_base_path(CaSync *s, const char *path) {
if (!s)
return -EINVAL;
if (!path)
return -EINVAL;
if (s->base_fd >= 0)
return -EBUSY;
if (s->base_path)
return -EBUSY;
if (s->boundary_fd >= 0)
return -EBUSY;
if (s->boundary_path)
return -EBUSY;
if (s->base_mode == (mode_t) -1 || S_ISDIR(s->base_mode)) {
s->base_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
if (s->base_fd >= 0) /* Base exists already and is a directory */
return 0;
if (s->direction == CA_SYNC_ENCODE && errno != ENOTDIR)
return -errno;
}
if (s->direction == CA_SYNC_ENCODE) {
s->base_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (s->base_fd < 0)
return -errno;
return 0;
}
assert(s->direction == CA_SYNC_DECODE);
s->base_path = strdup(path);
if (!s->base_path)
return -ENOMEM;
return 0;
}
int ca_sync_set_make_mode(CaSync *s, mode_t m) {
if (!s)
return -EINVAL;
if (m & ~0666)
return -EINVAL;
if (s->direction != CA_SYNC_ENCODE)
return -ENOTTY;
if (s->make_mode != (mode_t) -1)
return -EBUSY;
s->make_mode = m;
return 0;
}
int ca_sync_set_base_mode(CaSync *s, mode_t m) {
if (!s)
return -EINVAL;
if (m & ~(07777 | S_IFMT))
return -EINVAL;
if (!S_ISREG(m) && !S_ISDIR(m) && !S_ISBLK(m))
return -ENOTTY;
if (s->direction == CA_SYNC_ENCODE)
return -ENOTTY;
if (s->base_fd >= 0)
return -EBUSY;
if (s->base_mode != (mode_t) -1)
return -EBUSY;
if (s->boundary_fd >= 0)
return -EBUSY;
if (s->boundary_path)
return -EBUSY;
s->base_mode = m;
return 0;
}
int ca_sync_set_boundary_fd(CaSync *s, int fd) {
if (!s)
return -EINVAL;
if (fd < 0)
return -EINVAL;
if (s->base_fd >= 0)
return -EBUSY;
if (s->base_mode != (mode_t) -1)
return -EBUSY;
if (s->base_path)
return -EBUSY;
if (s->boundary_fd >= 0)
return -EBUSY;
if (s->boundary_path)
return -EBUSY;
if (s->direction == CA_SYNC_ENCODE)
return -ENOTTY;
s->boundary_fd = fd;
return 0;
}
int ca_sync_set_boundary_path(CaSync *s, const char *p) {
if (!s)
return -EINVAL;
if (!p)
return -EINVAL;
if (s->base_fd >= 0)
return -EBUSY;
if (s->base_mode != (mode_t) -1)
return -EBUSY;
if (s->base_path)
return -EBUSY;
if (s->boundary_fd >= 0)
return -EBUSY;
if (s->boundary_path)
return -EBUSY;
if (s->direction == CA_SYNC_ENCODE)
return -ENOTTY;
s->boundary_fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
if (s->boundary_fd >= 0) /* Base exists already is a directory, good */
return 0;
if (errno != ENOENT)
return -errno;
s->boundary_path = strdup(p);
if (!s->boundary_path)
return -ENOMEM;
return 0;
}
int ca_sync_set_archive_fd(CaSync *s, int fd) {
if (!s)
return -EINVAL;
if (fd < 0)
return -EINVAL;
if (s->archive_fd >= 0)
return -EBUSY;
if (s->archive_path)
return -EBUSY;
if (s->remote_archive)
return -EBUSY;
s->archive_fd = fd;
return 0;
}
int ca_sync_set_archive_path(CaSync *s, const char *path) {
if (!s)
return -EINVAL;
if (!path)
return -EINVAL;
if (s->archive_fd >= 0)
return -EBUSY;
if (s->archive_path)
return -EBUSY;
if (s->remote_archive)
return -EBUSY;
if (s->direction == CA_SYNC_ENCODE) {
s->archive_path = strdup(path);
if (!s->archive_path)
return -ENOMEM;
return 0;
}
assert(s->direction == CA_SYNC_DECODE);
s->archive_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (s->archive_fd < 0)
return -errno;
return 0;
}
int ca_sync_set_archive_remote(CaSync *s, const char *url) {
int r;
if (!s)
return -EINVAL;
if (!url)
return -EINVAL;
if (s->archive_fd >= 0)
return -EBUSY;
if (s->archive_path)
return -EBUSY;
if (s->remote_archive)
return -EBUSY;
s->remote_archive = ca_remote_new();
if (!s->remote_archive)
return -ENOMEM;
r = ca_remote_set_archive_url(s->remote_archive, url);
if (r < 0)
return r;
r = ca_remote_set_local_feature_flags(s->remote_archive,
s->direction == CA_SYNC_ENCODE ? CA_PROTOCOL_PUSH_ARCHIVE : CA_PROTOCOL_PULL_ARCHIVE);
if (r < 0)
return r;
return 0;
}
int ca_sync_set_archive_auto(CaSync *s, const char *locator) {
CaLocatorClass c;
if (!s)
return -EINVAL;
if (!locator)
return -EINVAL;
c = ca_classify_locator(locator);
if (c < 0)
return -EINVAL;
if (c == CA_LOCATOR_PATH)
return ca_sync_set_archive_path(s, locator);
return ca_sync_set_archive_remote(s, locator);
}
int ca_sync_set_store_path(CaSync *s, const char *path) {
int r;
if (!s)
return -EINVAL;
if (!path)
return -EINVAL;
if (s->wstore)
return -EBUSY;
if (s->remote_wstore)
return -EBUSY;
s->wstore = ca_store_new();
if (!s->wstore)
return -ENOMEM;
r = ca_store_set_path(s->wstore, path);
if (r < 0) {