-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-mirrorer.c
8083 lines (7702 loc) · 262 KB
/
git-mirrorer.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
/*
git-mirrorer, to mirror, archive and checkout git repos even across submodules
Copyright (C) 2023-present Guoxin "7Ji" Pu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
This file is written in A SINGLE FILE, ON PURPOSE.
It could be easily splitted up, but I kept it as a single file to ensure most
cross-module calling could be inlined.
Yeah, split, incremental, then LTO, and similar performance could be achieved,
but that comes with great compilation time penalty.
*/
#define _GNU_SOURCE
/* C */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
/* POSIX */
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <dirent.h>
/* LINUX */
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <linux/limits.h>
/* EXTERNAL */
#include <xxh3.h>
#include <git2.h>
#include <yaml.h>
/* No operation */
#define __noop
#define __maybe__unused __attribute__((unused))
/* Print formatters */
#define pr_with_prefix_and_source(prefix, format, arg...) \
printf("["prefix"] %s:%d: "format, __FUNCTION__, __LINE__, ##arg)
#define pr_with_prefix(prefix, format, arg...) \
printf("["prefix"] "format, ##arg)
#ifdef DEBUGGING
#define pr_debug(format, arg...) \
pr_with_prefix_and_source("DEBUG", format, ##arg)
#else /* no-op debugging print */
#define pr_debug(format, arg...)
#endif
#define pr_info(format, arg...) pr_with_prefix("INFO", format, ##arg)
#define pr_warn(format, arg...) pr_with_prefix("WARN", format, ##arg)
#define pr_error(format, arg...) \
pr_with_prefix_and_source("ERROR", format, ##arg)
#define pr_error_with_errno(format, arg...) \
pr_error(format", errno: %d, error: %s\n", ##arg, errno, strerror(errno))
#define pr_error_with_libgit_error(format, arg...) \
pr_error(format", libgit return %d (%d: %s)\n", ##arg, \
r, git_error_last()->klass, git_error_last()->message)
#define pr_error_with_pthread_error(format, arg...) \
pr_error(format", pthread error: (%d: %s)\n", ##arg, pr, strerror(pr))
#define fpr_with_prefix_and_source(file, prefix, format, arg...) \
fprintf(file, "["prefix"] %s:%d: "format, __FUNCTION__, __LINE__, ##arg)
#define fpr_with_prefix(file, prefix, format, arg...) \
fprintf(file, "["prefix"] "format, ##arg)
#define fpr_info(file, format, arg...) \
fpr_with_prefix("INFO", format, ##arg)
#define fpr_warn(file, format, arg...) \
fpr_with_prefix("WARN", format, ##arg)
#define fpr_error(file, format, arg...) \
fpr_with_prefix_and_source(file, "ERROR", format, ##arg)
#define fpr_error_with_errno(file, format, arg...) \
fpr_error(file, format", errno: %d, error: %s\n", \
##arg, errno, strerror(errno))
/* String */
/*
Packed data structure so we don't need to have A LOT of
different pointers for all of the strings we need that
each need to be free'd seperately
The other struct that allocates string inside this buffer
shouldn't need to have a sentry value to mark invalid
offset, as a length of 0 is enough even if the offset
is at its init value 0
*/
struct string_buffer {
char *buffer;
unsigned int used, size;
};
#define buffer_get_string(string_buffer, name) \
string_buffer->buffer + name##_offset
#define get_string_from(parent, name) \
parent->string_buffer.buffer + name##_offset
#define STRING_DECLARE(NAME) \
unsigned int NAME##_offset; \
unsigned short len_##NAME
#define LAZY_ALLOC_STRING_STACK_SIZE NAME_MAX + 1
struct lazy_alloc_string {
char stack[LAZY_ALLOC_STRING_STACK_SIZE];
char *heap;
char *string;
size_t len; // The length without terminating NULL
size_t alloc;
};
/* Dynamic array */
#define DYNAMIC_ARRAY_DECLARE_RAW(POINTER, NAME) \
POINTER; \
unsigned long NAME##s_count, \
NAME##s_allocated; \
#define DYNAMIC_ARRAY_DECLARE(TYPE, NAME) \
DYNAMIC_ARRAY_DECLARE_RAW(TYPE *NAME##s, NAME)
#define DYNAMIC_ARRAY_DECLARE_SAME(NAME) \
DYNAMIC_ARRAY_DECLARE(struct NAME, NAME)
#define ALLOC_BASE 10
#define ALLOC_MULTIPLIER 2
#ifdef CHUNK_SIZE
#define CHUNK_SIZE PAGE_SIZE
#else
#define CHUNK_SIZE 4096
#endif
#define get_last(x) x + x##_count - 1
#define free_if_allocated(name) if (name) free(name)
#define free_if_allocated_to_null(name) if (name) { free(name); name = NULL; }
#define dynamic_array_free(name) \
free_if_allocated_to_null(name); \
name##_count = 0; \
name##_allocated = 0;
/* Commit */
#define COMMIT_ID_DECLARE { \
git_oid oid; \
unsigned int oid_hex_offset; \
}
struct commit_id COMMIT_ID_DECLARE;
#define COMMIT_ID_UNION_DECLARE \
union { \
struct commit_id id; \
struct COMMIT_ID_DECLARE; \
}
struct submodule {
COMMIT_ID_UNION_DECLARE;
STRING_DECLARE(path);
STRING_DECLARE(url);
XXH64_hash_t hash_url;
unsigned long target_repo_id,
target_commit_id;
};
struct submodule const SUBMODULE_INIT = {
.target_repo_id = (unsigned long) -1,
.target_commit_id = (unsigned long) -1};
struct commit {
COMMIT_ID_UNION_DECLARE;
git_commit *git_commit;
DYNAMIC_ARRAY_DECLARE_SAME(submodule);
bool /*submodules_parsed,*/
archive,
checkout;
};
struct commit const COMMIT_INIT = {0};
#define git_commit_free_if_allocated(name) if (name) git_commit_free(name)
#define git_commit_free_if_allocated_to_null(name) \
if (name) { git_commit_free(name); name = NULL; }
#define git_commit_free_to_null(name) git_commit_free(name); name = NULL;
/* Wanted objects */
enum wanted_type {
WANTED_TYPE_UNKNOWN,
WANTED_TYPE_ALL_BRANCHES,
WANTED_TYPE_ALL_TAGS,
WANTED_TYPE_REFERENCE,
WANTED_TYPE_BRANCH,
WANTED_TYPE_TAG,
WANTED_TYPE_HEAD,
WANTED_TYPE_COMMIT,
};
#define WANTED_TYPE_MAX WANTED_TYPE_HEAD
char const *wanted_type_strings[] = {
"unknown",
"all_branches",
"all_tags",
"reference",
"branch",
"tag",
"head",
"commit",
};
#define WANTED_BASE_DECLARE {\
enum wanted_type type;\
STRING_DECLARE(name); \
bool archive;\
bool checkout;\
}
struct wanted_base WANTED_BASE_DECLARE;
struct wanted_base const WANTED_BASE_INIT = {0};
struct wanted_base const WANTED_BASE_HEAD_INIT = {
.type = WANTED_TYPE_HEAD};
struct wanted_base const WANTED_BASE_ALL_BRANCHES_INIT = {
.type = WANTED_TYPE_ALL_BRANCHES };
struct wanted_base const WANTED_BASE_ALL_TAGS_INIT = {
.type = WANTED_TYPE_ALL_TAGS };
#define WANTED_COMMIT_DECLARE { \
union { \
struct wanted_base base; \
struct WANTED_BASE_DECLARE; \
}; \
COMMIT_ID_UNION_DECLARE; \
unsigned long parsed_commit_id; \
}
struct wanted_commit WANTED_COMMIT_DECLARE;
struct wanted_commit const WANTED_COMMIT_INIT = {
.base.type = WANTED_TYPE_COMMIT, .parsed_commit_id = (unsigned long) -1};
#define WANTED_REFERENCE_DECLARE { \
union { \
struct wanted_commit commit; \
struct WANTED_COMMIT_DECLARE; \
}; \
bool commit_parsed; \
}
struct wanted_reference WANTED_REFERENCE_DECLARE;
struct wanted_object {
union {
struct wanted_reference reference;
struct WANTED_REFERENCE_DECLARE;
};
};
struct wanted_object const WANTED_OBJECT_INIT = {
.type = WANTED_TYPE_UNKNOWN, .parsed_commit_id = (unsigned long) -1};
struct wanted_reference const WANTED_REFERENCE_INIT = {
.type = WANTED_TYPE_REFERENCE,
.parsed_commit_id = (unsigned long) -1};
struct wanted_reference const WANTED_BRANCH_INIT = {
.type = WANTED_TYPE_BRANCH,
.parsed_commit_id = (unsigned long) -1};
struct wanted_reference const WANTED_TAG_INIT = {
.type = WANTED_TYPE_TAG,
.parsed_commit_id = (unsigned long) -1};
struct wanted_reference const WANTED_HEAD_INIT = {
.type = WANTED_TYPE_HEAD,
.parsed_commit_id = (unsigned long) -1};
/* Hash */
#define hash_type XXH64_hash_t
#define hash_calculate(data, size) XXH3_64bits(data, size)
#define HASH_NAME "64bit xxh3 hash"
#define HASH_FORMAT "%016lx"
#define HASH_STRING_LEN 16
/* Repo */
#define REPO_COMMON_DECLARE { \
STRING_DECLARE(url); \
STRING_DECLARE(long_name); \
STRING_DECLARE(short_name); \
hash_type hash_url, \
hash_long_name, \
hash_short_name, \
hash_domain; \
unsigned short depth_long_name; \
char hash_url_string[HASH_STRING_LEN + 1]; \
}
struct repo_common REPO_COMMON_DECLARE;
struct repo_config {
union {
struct repo_common common;
struct REPO_COMMON_DECLARE;
};
DYNAMIC_ARRAY_DECLARE(struct wanted_base, wanted_object);
};
struct repo_work {
union {
struct repo_common common;
struct REPO_COMMON_DECLARE;
};
DYNAMIC_ARRAY_DECLARE_SAME(wanted_object);
DYNAMIC_ARRAY_DECLARE_SAME(commit);
unsigned long wanted_objects_count_original;
git_repository *git_repository;
bool from_config, wanted_dynamic, need_update, updated;
};
#define git_repository_free_if_allocated(name) \
if (name) git_repository_free(name)
#define git_repository_free_if_allocated_to_null(name) \
if (name) { git_repository_free(name); name = NULL; }
struct repo_domain_group {
hash_type domain;
DYNAMIC_ARRAY_DECLARE(struct repo_work *, repo);
};
struct repo_domain_map {
DYNAMIC_ARRAY_DECLARE(struct repo_domain_group, group);
};
struct repo_commit_pair {
struct repo_work *repo;
struct commit *commit;
};
/* Config */
struct archive_pipe_arg {
unsigned int offset;
unsigned short len;
};
#define ARCHIVE_PIPE_ARGS_MAX_COUNT 64
#define CONFIG_STATIC_DECLARE { \
STRING_DECLARE(proxy_url); \
STRING_DECLARE(dir_repos); \
STRING_DECLARE(dir_archives); \
STRING_DECLARE(dir_checkouts); \
STRING_DECLARE(archive_suffix); \
unsigned int daemon_interval; \
int timeout_connect; \
unsigned short proxy_after, \
connections_per_server, \
export_threads, \
clean_links_pass; \
bool archive_gh_prefix, \
clean_repos, \
clean_archives, \
clean_checkouts, \
daemon; \
}
struct config_static CONFIG_STATIC_DECLARE;
struct config {
struct string_buffer string_buffer;
DYNAMIC_ARRAY_DECLARE(struct repo_config, repo);
DYNAMIC_ARRAY_DECLARE(struct wanted_base, empty_wanted_object);
DYNAMIC_ARRAY_DECLARE(struct wanted_base, always_wanted_object);
DYNAMIC_ARRAY_DECLARE_SAME(archive_pipe_arg); \
union {
struct config_static _static;
struct CONFIG_STATIC_DECLARE;
};
};
#define config_get_string(name) \
get_string_from(config, name)
#define DIR_DATA "data"
#define DIR_LINKS "links"
#define DIR_REPOS_DEFAULT "repos"
#define DIR_ARCHIVES_DEFAULT "archives"
#define DIR_CHECKOUTS_DEFAULT "checkouts"
#define ARCHIVE_SUFFIX_DEFAULT ".tar"
#define CONNECTIONS_PER_SERVER_DEFAULT 10
#define EXPORT_THREADS_DEFAULT 10
#define CLEAN_LINKS_PASS_DEFAULT 1
#define DAEMON_INTERVAL_DEFAULT 60
struct config const CONFIG_INIT = {
.connections_per_server = CONNECTIONS_PER_SERVER_DEFAULT,
.export_threads = EXPORT_THREADS_DEFAULT,
.clean_links_pass = CLEAN_LINKS_PASS_DEFAULT,
.daemon_interval = DAEMON_INTERVAL_DEFAULT,
.string_buffer = {
.used = 1,
},
};
/* Work */
// struct work_keep {
// unsigned int offset;
// unsigned short len;
// };
struct work_directory {
STRING_DECLARE(path);
int datafd;
int linkfd;
DYNAMIC_ARRAY_DECLARE(char const*, keep);
};
#define WORK_DIRECTORY_INIT_ASSIGN .datafd = -1, .linkfd = -1
struct work_directory const WORK_DIRECTORY_INIT = {
WORK_DIRECTORY_INIT_ASSIGN};
struct work_handle {
struct string_buffer string_buffer;
DYNAMIC_ARRAY_DECLARE(struct repo_work, repo);
DYNAMIC_ARRAY_DECLARE_SAME(archive_pipe_arg); \
struct work_directory dir_repos,
dir_archives,
dir_checkouts;
git_transport_message_cb cb_sideband;
git_indexer_progress_cb cb_fetch;
int cwd; // File decriptor of the current work directory, to quick return
union {
struct config_static _static;
struct CONFIG_STATIC_DECLARE;
};
};
#define work_handle_get_string(name) \
get_string_from(work_handle, name)
struct work_handle const WORK_HANDLE_INIT = {
.dir_repos = {WORK_DIRECTORY_INIT_ASSIGN},
.dir_archives = {WORK_DIRECTORY_INIT_ASSIGN},
.dir_checkouts = {WORK_DIRECTORY_INIT_ASSIGN}
};
/* IO */
#define BUFFER_READ_CHUNK CHUNK_SIZE * 64
/* TAR */
#define TAR_HEADER_MTIME_LEN 12
struct tar_header {/* byte offset */\
char name[100]; /* 0 */\
char mode[8]; /* 100 octal mode string %07o */\
char uid[8]; /* 108 octal uid string %07o */\
char gid[8]; /* 116 octal gid string %07o */\
char size[12]; /* 124 octal size %011o */\
char mtime[12]; /* 136 octal mtime string %011o */\
char chksum[8]; /* 148 octal checksum string %06o + space */\
char typeflag; /* 156 either TAR_{REG,LINK,DIR}TYPE */\
char linkname[100]; /* 157 symlink target */\
char magic[6]; /* 257 ustar\0 */\
char version[2]; /* 263 \0 0*/\
char uname[32]; /* 265 uname + padding \0 */\
char gname[32]; /* 297 gname + padding \0 */\
char devmajor[8]; /* 329 all 0 */\
char devminor[8]; /* 337 all 0 */\
char prefix[155]; /* 345 all 0 */\
/* 500 */\
};
#define TAR_POSIX_MAGIC "ustar" /* ustar and a null */
#define TAR_POSIX_VERSION "00" /* 00 and no null */
#define TAR_GNU_MAGIC "ustar "
#define TAR_GNU_VERSION " "
#define TAR_MAGIC TAR_GNU_MAGIC
#define TAR_VERSION TAR_GNU_VERSION
// Basically, posix is "ustar\000", gnu is "ustar \0"
/* Values used in typeflag field. */
#define TAR_REGTYPE '0' /* regular file */
#define TAR_LNKTYPE '1' /* link */
#define TAR_SYMTYPE '2'
#define TAR_DIRTYPE '5' /* directory */
#define PAX_TAR_GLOBAL_HEADER_TYPE 'g'
#define GNUTAR_LONGLINK 'K'
#define GNUTAR_LONGNAME 'L'
#define TAR_LONGLINK_TYPE GNUTAR_LONGLINK
#define TAR_LONGNAME_TYPE GNUTAR_LONGNAME
#define TAR_GLOBAL_HEADER_TYPE PAX_TAR_GLOBAL_HEADER_TYPE
#define GNUTAR_LONGLINK_NAME "././@LongLink"
#define PAXTAR_GLOBAL_HEADER_NAME "pax_global_header"
#define TAR_MODE(X) "0000" #X
#define TAR_MODE_644 TAR_MODE(644)
#define TAR_MODE_755 TAR_MODE(755)
#define TAR_MODE_777 TAR_MODE(777)
#define TAR_HEADER_7_BYTE_0 "0000000"
#define TAR_UID_ROOT TAR_HEADER_7_BYTE_0
#define TAR_GID_ROOT TAR_HEADER_7_BYTE_0
#define TAR_HEADER_11_BYTE_0 "00000000000"
#define TAR_SIZE_0 TAR_HEADER_11_BYTE_0
#define TAR_MTIME_0 TAR_HEADER_11_BYTE_0
#define TAR_CHECKSUM_BLANK " "
#define TAR_DEVMAJOR TAR_HEADER_7_BYTE_0
#define TAR_DEVMINOR TAR_HEADER_7_BYTE_0
#define TAR_INIT(NAME, MODE, TYPEFLAG) {\
.name = NAME, \
.mode = TAR_MODE(MODE), \
.uid = TAR_UID_ROOT, \
.gid = TAR_GID_ROOT, \
.size = TAR_SIZE_0, \
.mtime = TAR_MTIME_0, \
.chksum = TAR_CHECKSUM_BLANK, \
.typeflag = TYPEFLAG, \
.linkname = "", \
.magic = TAR_MAGIC, \
.version = TAR_VERSION, \
.uname = "root", \
.gname = "root", \
.devmajor = "", \
.devminor = "", \
.prefix = "" \
}
#define TAR_POSIX_INIT(MODE, TYPEFLAG) TAR_INIT("", MODE, TYPEFLAG)
struct tar_header const TAR_HEADER_FILE_REG_INIT =
TAR_POSIX_INIT(644, TAR_REGTYPE);
struct tar_header const TAR_HEADER_FILE_EXE_INIT =
TAR_POSIX_INIT(755, TAR_REGTYPE);
struct tar_header const TAR_HEADER_SYMLINK_INIT =
TAR_POSIX_INIT(777, TAR_SYMTYPE);
struct tar_header const TAR_HEADER_FOLDER_INIT =
TAR_POSIX_INIT(755, TAR_DIRTYPE);
struct tar_header const TAR_HEADER_GNU_LONGLINK_INIT =
TAR_INIT(GNUTAR_LONGLINK_NAME, 644, TAR_LONGLINK_TYPE);
struct tar_header const TAR_HEADER_GNU_LONGNAME_INIT =
TAR_INIT(GNUTAR_LONGLINK_NAME, 644, TAR_LONGNAME_TYPE);
struct tar_header const TAR_HEADER_PAX_GLOBAL_HEADER_INIT =
TAR_INIT(PAXTAR_GLOBAL_HEADER_NAME, 666, TAR_GLOBAL_HEADER_TYPE);
/* To help padding */
unsigned char const EMPTY_512_BLOCK[512] = {0};
/* YAML Config */
#define yamlconf_add_string(config, event) \
string_buffer_add(&config->string_buffer, \
(char const *)event->data.scalar.value, event->data.scalar.length)
char const *yamlconf_event_type_strings[] = {
"no",
"stream start",
"stream end",
"document start",
"document end",
"alias",
"scalar",
"sequence start",
"sequence end",
"mapping start",
"mapping end",
};
enum yamlconf_wanted_type {
YAMLCONF_WANTED_UNKNOWN,
YAMLCONF_WANTED_GLOBAL_EMPTY,
YAMLCONF_WANTED_GLOBAL_ALWAYS,
YAMLCONF_WANTED_REPO,
};
char const *yamlconf_wanted_type_strings[] = {
"unknown",
"global, when repo empty",
"global, always to repo",
"repo",
};
enum yamlconf_parsing_status {
YAMLCONF_PARSING_STATUS_NONE,
YAMLCONF_PARSING_STATUS_STREAM,
YAMLCONF_PARSING_STATUS_DOCUMENT,
YAMLCONF_PARSING_STATUS_SECTION,
YAMLCONF_PARSING_STATUS_DAEMON,
YAMLCONF_PARSING_STATUS_DAEMON_INTERVAL,
YAMLCONF_PARSING_STATUS_PROXY,
YAMLCONF_PARSING_STATUS_PROXY_AFTER,
YAMLCONF_PARSING_STATUS_CONNECT_TIMEOUT,
YAMLCONF_PARSING_STATUS_DIR_REPOS,
YAMLCONF_PARSING_STATUS_DIR_ARCHIVES,
YAMLCONF_PARSING_STATUS_DIR_CHECKOUTS,
YAMLCONF_PARSING_STATUS_ARCHIVE,
YAMLCONF_PARSING_STATUS_ARCHIVE_SECTION,
YAMLCONF_PARSING_STATUS_ARCHIVE_GHPREFIX,
YAMLCONF_PARSING_STATUS_ARCHIVE_SUFFIX,
YAMLCONF_PARSING_STATUS_ARCHIVE_PIPE,
YAMLCONF_PARSING_STATUS_ARCHIVE_PIPE_LIST,
YAMLCONF_PARSING_STATUS_CLEAN,
YAMLCONF_PARSING_STATUS_CLEAN_SECTION,
YAMLCONF_PARSING_STATUS_CLEAN_REPOS,
YAMLCONF_PARSING_STATUS_CLEAN_ARCHIVES,
YAMLCONF_PARSING_STATUS_CLEAN_CHECKOUTS,
YAMLCONF_PARSING_STATUS_CLEAN_LINKS_PASS,
YAMLCONF_PARSING_STATUS_EXPORT_THREADS,
YAMLCONF_PARSING_STATUS_CONNECTIONS_PER_SERVER,
YAMLCONF_PARSING_STATUS_WANTED,
YAMLCONF_PARSING_STATUS_WANTED_SECTION,
YAMLCONF_PARSING_STATUS_WANTED_SECTION_START,
YAMLCONF_PARSING_STATUS_WANTED_LIST,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT_START,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT_SECTION,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT_TYPE,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT_ARCHIVE,
YAMLCONF_PARSING_STATUS_WANTED_OBJECT_CHECKOUT,
YAMLCONF_PARSING_STATUS_REPOS,
YAMLCONF_PARSING_STATUS_REPOS_LIST,
YAMLCONF_PARSING_STATUS_REPO_URL,
YAMLCONF_PARSING_STATUS_REPO_AFTER_URL,
YAMLCONF_PARSING_STATUS_REPO_SECTION,
};
char const *yamlconf_parsing_status_strings[] = {
"none",
"stream",
"document",
"section",
"daemon",
"daemon interval",
"proxy",
"proxy after",
"connect timeout",
"dir repos",
"dir archives",
"dir checkouts",
"archive",
"archive section",
"archive github-like prefix",
"archive suffix",
"archive pipe",
"archive pipe list",
"clean",
"clean section",
"clean repos",
"clean archives",
"clean checkouts",
"clean links pass",
"export threads",
"connections per server",
"wanted",
"wanted section",
"wanted section start",
"wanted list",
"wanted object",
"wanted object start",
"wanted object section",
"wanted object type",
"wanted object archive",
"wanted object checkout",
"repos",
"repos list",
"repo url",
"repo after url",
"repo section",
};
struct yamlconf_parsing_handle {
enum yamlconf_parsing_status status;
enum yamlconf_wanted_type wanted_type;
};
/* Git mirror */
#define GMR_REMOTE "origin"
#define GMR_FETCHSPEC "+refs/*:refs/*"
#define GMR_CONFIG "remote."GMR_REMOTE".mirror"
char const *gmr_refspecs_strings[] = {
GMR_FETCHSPEC,
NULL
};
static git_strarray const gmr_refspecs = {
.count = 1,
.strings = (char **)gmr_refspecs_strings
};
struct gmr_payload {
char const *const restrict url;
time_t first_transfer;
time_t *last_transfer;
};
/* Console lock */
pthread_mutex_t console_mutex = PTHREAD_MUTEX_INITIALIZER;
#define console_with_trylock(job) \
console_locked = console_trylock(); \
job; \
if (console_locked) console_unlock() \
#define console_with_lock(job) \
console_locked = console_lock(); \
if (console_locked) { \
job; \
console_unlock(); \
}
/* Functions */
// Dumb help message
static inline void help() {
fputs(
"git-mirrorer\n"
" --config/-c\t[path to .yaml config file]"
" or a single - for reading from stdin; if not set, read from stdin\n"
" --help/-h\tprint this message\n"
" --version/-v\tprint the version\n",
stderr
);
}
static inline void version() {
fputs(
"git-mirrorer version "
#ifdef VERSION
VERSION
#else
"UNKNOWN"
#endif
" by Guoxin \"7Ji\" Pu, "
"licensed under GNU Affero General Public License v3 or later\n",
stderr);
}
int string_buffer_add(
struct string_buffer *const restrict sbuffer,
char const *const restrict string,
unsigned short const len
) {
unsigned int used_new;
char *buffer_new;
if (!sbuffer->buffer) {
if (!(sbuffer->buffer = malloc(sbuffer->size = CHUNK_SIZE))) {
pr_error_with_errno(
"Failed to allocate memory for string buffer");
return -1;
}
}
if ((used_new = sbuffer->used + len + 1) > sbuffer->size) {
while (used_new > sbuffer->size) {
if (sbuffer->size == UINT_MAX) {
pr_error("Impossible to allocate more memory, "
"wanted size at UINT_MAX\n");
return -1;
} else if (sbuffer->size >= UINT_MAX / ALLOC_MULTIPLIER) {
sbuffer->size = UINT_MAX;
} else {
sbuffer->size *= ALLOC_MULTIPLIER;
}
}
if (!(buffer_new = realloc(sbuffer->buffer, sbuffer->size))) {
pr_error_with_errno("Failed to allocate more memory");
return -1;
}
sbuffer->buffer = buffer_new;
}
memcpy(sbuffer->buffer + sbuffer->used, string, len);
*(sbuffer->buffer + sbuffer->used + len) = '\0';
sbuffer->used = used_new;
return 0;
}
int string_buffer_free(
struct string_buffer *const restrict sbuffer
) {
if (!sbuffer) {
pr_error("Internal: called passed NULL pointer to us\n");
return -1;
}
free_if_allocated_to_null(sbuffer->buffer);
sbuffer->size = 0;
sbuffer->used = 0;
return 0;
}
int string_buffer_partial_free(
struct string_buffer *const restrict sbuffer
) {
char *buffer_new;
if (!sbuffer) {
pr_error("Internal: called passed NULL pointer to us\n");
return -1;
}
if (!sbuffer->used) {
free_if_allocated_to_null(sbuffer->buffer);
sbuffer->size = 0;
return 0;
}
if (!sbuffer->buffer) {
pr_error("String buffer not allocated but it's marked as used\n");
return -1;
}
if (sbuffer->size == sbuffer->used) return 0;
if (sbuffer->used > sbuffer->size) {
pr_error("Used buffer larger than size, impossible\n");
return -1;
}
if (!(buffer_new = realloc(sbuffer->buffer, sbuffer->used))) {
pr_error_with_errno("Failed to re-allocate memory for buffer");
return -1;
}
sbuffer->buffer = buffer_new;
sbuffer->size = sbuffer->used;
return 0;
}
int string_buffer_clone(
struct string_buffer *const restrict target,
struct string_buffer const *const restrict source
) {
if (target && source);
else {
pr_error("Internal: called passed NULL pointer to us\n");
return -1;
}
target->size = (source->used + CHUNK_SIZE - 1) / CHUNK_SIZE * CHUNK_SIZE;
if (!(target->buffer = malloc(target->size))) {
pr_error_with_errno("Failed to allocate memory for new string buffer");
return -1;
}
if (!(target->used = source->used)) return 0;
memcpy(target->buffer, source->buffer, target->used);
return 0;
}
static inline
void lazy_alloc_string_init(
struct lazy_alloc_string *const restrict string
) {
string->stack[0] = '\0';
string->heap = NULL;
string->string = string->stack;
string->len = 0;
string->alloc = 0;
}
static inline
void lazy_alloc_string_init_full_stack(
struct lazy_alloc_string *const restrict string
) {
string->stack[sizeof string->stack - 1] = '\0';
string->heap = NULL;
string->string = string->stack;
string->len = sizeof string->stack - 1;
string->alloc = 0;
}
static inline
int lazy_alloc_string_init_len(
struct lazy_alloc_string *const restrict string,
size_t const len
) {
if (len >= LAZY_ALLOC_STRING_STACK_SIZE) {
if (!(string->heap = malloc((
string->alloc = (len + 1) / 0x1000 * 0x1000))))
{
pr_error_with_errno("Failed to allocate memory for string");
return -1;
}
string->string = string->heap;
} else {
string->heap = NULL;
string->string = string->stack;
string->alloc = 0;
}
string->string[len] = '\0';
string->len = len;
return 0;
}
static inline
int lazy_alloc_string_init_with(
struct lazy_alloc_string *const restrict string,
void const *const restrict content,
size_t const len
) {
if (lazy_alloc_string_init_len(string, len)) return -1;
memcpy(string->string, content, len);
return 0;
}
static inline
int lazy_alloc_string_alloc(
struct lazy_alloc_string *const restrict string,
size_t const len
) {
if (len >= LAZY_ALLOC_STRING_STACK_SIZE) {
if (len >= string->alloc) {
free_if_allocated(string->heap);
if (!(string->heap = malloc((
string->alloc = (len + 1) / 0x1000 * 0x1000))))
{
pr_error_with_errno("Failed to allocate memory for string");
return -1;
}
}
string->string = string->heap;
} else {
string->string = string->stack;
}
return 0;
}
static inline
int lazy_alloc_string_setlen_discard(
struct lazy_alloc_string *const restrict string,
size_t const len
) {
if (lazy_alloc_string_alloc(string, len)) return -1;
string->string[(string->len = len)] = '\0';
return 0;
}
__maybe__unused
static inline
int lazy_alloc_string_setlen_keep(
struct lazy_alloc_string *const restrict string,
size_t const len
) {
char const *const restrict old_string = string->string;
if (lazy_alloc_string_alloc(string, len)) return -1;
if (old_string != string->string) {
if (string->len) memcpy(string->string, old_string, string->len);
}
string->string[(string->len = len)] = '\0';
return 0;
}
static inline
int lazy_alloc_string_replace(
struct lazy_alloc_string *const restrict string,
void const *const restrict content,
size_t const len
) {
if (lazy_alloc_string_setlen_discard(string, len)) {
pr_error("Failed to set length for lazy alloc string\n");
return -1;
}
memcpy(string->string, content, len);