-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathcreaterepo_c.c
2503 lines (2213 loc) · 103 KB
/
createrepo_c.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
/* createrepo_c - Library of routines for manipulation with repodata
* Copyright (C) 2012 Tomas Mlcoch
*
* 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 <glib.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include "cmd_parser.h"
#include "compression_wrapper.h"
#include "createrepo_shared.h"
#include "deltarpms.h"
#include "dumper_thread.h"
#include "checksum.h"
#include "cleanup.h"
#include "error.h"
#include "helpers.h"
#include "load_metadata.h"
#include "metadata_internal.h"
#include "locate_metadata.h"
#include "misc.h"
#include "parsepkg.h"
#include "repomd.h"
#include "repomd_internal.h"
#include "sqlite.h"
#include "threads.h"
#include "version.h"
#include "xml_dump.h"
#include "xml_file.h"
#ifdef WITH_LIBMODULEMD
#include <modulemd.h>
#endif /* WITH_LIBMODULEMD */
#define OUTDELTADIR "drpms/"
#define DEFAULT_DATABASE_VERSION 10
/*
* Starting with glib 2.70.0, g_pattern_spec_match() replaces
* g_pattern_match().
*/
#if GLIB_CHECK_VERSION(2, 70, 0)
#define PATTERN_MATCH g_pattern_spec_match
#else
#define PATTERN_MATCH g_pattern_match
#endif
/** Check if the filename is excluded by any exclude mask.
* @param filename Filename (basename).
* @param exclude_masks List of exclude masks
* @return TRUE if file should be included, FALSE otherwise
*/
static gboolean
allowed_file(const gchar *filename, GSList *exclude_masks)
{
// Check file against exclude glob masks
if (exclude_masks) {
int str_len = strlen(filename);
gchar *reversed_filename = g_utf8_strreverse(filename, str_len);
GSList *element = exclude_masks;
for (; element; element=g_slist_next(element)) {
if (PATTERN_MATCH((GPatternSpec *) element->data,
str_len, filename, reversed_filename))
{
g_free(reversed_filename);
g_debug("Exclude masks hit - skipping: %s", filename);
return FALSE;
}
}
g_free(reversed_filename);
}
return TRUE;
}
static gboolean
allowed_modulemd_module_metadata_file(const gchar *filename)
{
if (g_strrstr(filename, "modules.yaml"))
return TRUE;
if (g_strrstr(filename, ".modulemd.yaml"))
return TRUE;
if (g_strrstr(filename, ".modulemd-defaults.yaml"))
return TRUE;
return FALSE;
}
/** Function used to sort pool tasks.
* This function is responsible for order of packages in metadata.
*
* @param a_p Pointer to first struct PoolTask
* @param b_p Pointer to second struct PoolTask
*/
static int
task_cmp(gconstpointer a_p, gconstpointer b_p)
{
int ret;
const struct PoolTask *a = *(struct PoolTask **) a_p;
const struct PoolTask *b = *(struct PoolTask **) b_p;
ret = g_strcmp0(a->filename, b->filename);
if (ret) return ret;
return g_strcmp0(a->path, b->path);
}
/** Recursively walkt throught the input directory and add push the found
* rpms to the thread pool (create a PoolTask and push it to the pool).
* If the filelists is supplied then no recursive walk is done and only
* files from filelists are pushed into the pool.
* This function also filters out files that shouldn't be processed
* (e.g. directories with .rpm suffix, files that match one of
* the exclude masks, etc.).
*
* @param pool GThreadPool pool
* @param in_dir Directory to scan
* @param cmd_options Options specified on command line
* @param current_pkglist Pointer to a list where basenames of files that
* will be processed will be appended to.
* @return Number of packages that are going to be processed
*/
static long
fill_pool(GThreadPool *pool,
gchar *in_dir,
struct CmdOptions *cmd_options,
GSList **current_pkglist,
long *task_count,
int media_id)
{
GArray *package_tasks = g_array_new(FALSE, FALSE, sizeof(struct PoolTask *));
struct PoolTask *task;
if ( ! cmd_options->split ) {
media_id = 0;
}
if ((cmd_options->pkglist || cmd_options->recycle_pkglist) && !cmd_options->include_pkgs) {
g_warning("Used pkglist doesn't contain any useful items");
} else if (!(cmd_options->include_pkgs)) {
// --pkglist (or --includepkg, or --recycle-pkglist) is not supplied
// --> do dir walk
g_message("Directory walk started");
size_t in_dir_len = strlen(in_dir);
GStringChunk *sub_dirs_chunk = g_string_chunk_new(1024);
GQueue *sub_dirs = g_queue_new();
gchar *input_dir_stripped;
input_dir_stripped = g_string_chunk_insert_len(sub_dirs_chunk,
in_dir,
in_dir_len-1);
g_queue_push_head(sub_dirs, input_dir_stripped);
char *dirname;
while ((dirname = g_queue_pop_head(sub_dirs))) {
// Open dir
GDir *dirp;
dirp = g_dir_open (dirname, 0, NULL);
if (!dirp) {
g_warning("Cannot open directory: %s", dirname);
continue;
}
const gchar *filename;
while ((filename = g_dir_read_name(dirp))) {
if (!allowed_file(filename, cmd_options->exclude_masks)) {
continue;
}
gchar *full_path = g_strconcat(dirname, "/", filename, NULL);
if (!g_file_test(full_path, G_FILE_TEST_IS_REGULAR)) {
if (g_file_test(full_path, G_FILE_TEST_IS_DIR)) {
// Directory
gchar *sub_dir_in_chunk;
sub_dir_in_chunk = g_string_chunk_insert(sub_dirs_chunk,
full_path);
g_queue_push_head(sub_dirs, sub_dir_in_chunk);
g_debug("Dir to scan: %s", sub_dir_in_chunk);
}
g_free(full_path);
continue;
}
// Skip symbolic links if --skip-symlinks arg is used
if (cmd_options->skip_symlinks
&& g_file_test(full_path, G_FILE_TEST_IS_SYMLINK))
{
g_debug("Skipped symlink: %s", full_path);
g_free(full_path);
continue;
}
if (allowed_modulemd_module_metadata_file(full_path)) {
#ifdef WITH_LIBMODULEMD
cmd_options->modulemd_metadata = g_slist_prepend(
cmd_options->modulemd_metadata,
(gpointer) full_path);
#else
g_warning("createrepo_c not compiled with libmodulemd support, "
"ignoring found module metadata: %s", full_path);
g_free(full_path);
#endif /* WITH_LIBMODULEMD */
continue;
}
// Non .rpm files are ignored
if (!g_str_has_suffix (filename, ".rpm")) {
g_free(full_path);
continue;
}
// Check filename against exclude glob masks
const gchar *repo_relative_path = filename;
if (in_dir_len < strlen(full_path))
// This probably should be always true
repo_relative_path = full_path + in_dir_len;
if (allowed_file(repo_relative_path, cmd_options->exclude_masks)) {
// FINALLY! Add file into pool
g_debug("Adding pkg: %s", full_path);
task = g_malloc(sizeof(struct PoolTask));
task->full_path = full_path;
task->filename = g_strdup(filename);
task->path = g_strdup(dirname);
*current_pkglist = g_slist_prepend(*current_pkglist, task->filename);
// TODO: One common path for all tasks with the same path?
g_array_append_val(package_tasks, task);
} else {
g_free(full_path);
}
}
// Cleanup
g_dir_close (dirp);
}
g_string_chunk_free (sub_dirs_chunk);
g_queue_free(sub_dirs);
} else {
// pkglist is supplied - use only files in pkglist
g_debug("Skipping dir walk - using pkglist");
GSList *element = cmd_options->include_pkgs;
for (; element; element=g_slist_next(element)) {
gchar *relative_path = (gchar *) element->data;
// ^^^ path from pkglist e.g. packages/i386/foobar.rpm
if (allowed_modulemd_module_metadata_file(relative_path)) {
#ifdef WITH_LIBMODULEMD
cmd_options->modulemd_metadata = g_slist_prepend(
cmd_options->modulemd_metadata,
(gpointer) g_strdup(relative_path));
#else
g_warning("createrepo_c not compiled with libmodulemd support, "
"ignoring found module metadata: %s", relative_path);
#endif /* WITH_LIBMODULEMD */
continue;
}
gchar *filename; // foobar.rpm
// Get index of last '/'
int x = strlen(relative_path);
for (; x > 0 && relative_path[x] != '/'; x--)
;
if (!x) // There was no '/' in path
filename = relative_path;
else // Use only a last part of the path
filename = relative_path + x + 1;
if (allowed_file(relative_path, cmd_options->exclude_masks)) {
// Check filename against exclude glob masks
gchar *full_path = g_strconcat(in_dir, relative_path, NULL);
// ^^^ /path/to/in_repo/packages/i386/foobar.rpm
g_debug("Adding pkg: %s", full_path);
task = g_malloc(sizeof(struct PoolTask));
task->full_path = full_path;
task->filename = g_strdup(filename); // foobar.rpm
task->path = strndup(relative_path, x); // packages/i386/
*current_pkglist = g_slist_prepend(*current_pkglist, task->filename);
g_array_append_val(package_tasks, task);
}
}
}
g_array_sort(package_tasks, task_cmp);
// Push sorted tasks into the thread pool
for (int i=0; i<package_tasks->len; i++) {
task = g_array_index(package_tasks, struct PoolTask *, i);
task->id = *task_count;
task->media_id = media_id;
g_thread_pool_push(pool, task, NULL);
++*task_count;
}
g_array_free(package_tasks, TRUE);
return *task_count;
}
/** Prepare cache dir for checksums.
* Called only if --cachedir options is used.
* It tries to create cache directory if it doesn't exist yet.
* It also fill checksum_cachedir option in cmd_options structure.
*
* @param cmd_options Commandline options
* @param out_dir Repo output directory
* @param err GError **
* @return FALSE if err is set, TRUE otherwise
*/
static gboolean
prepare_cache_dir(struct CmdOptions *cmd_options,
const gchar *out_dir,
GError **err)
{
if (!cmd_options->cachedir)
return TRUE;
if (g_str_has_prefix(cmd_options->cachedir, "/")) {
// Absolute local path
cmd_options->checksum_cachedir = cr_normalize_dir_path(
cmd_options->cachedir);
} else {
// Relative path (from intput_dir)
gchar *tmp = g_strconcat(out_dir, cmd_options->cachedir, NULL);
cmd_options->checksum_cachedir = cr_normalize_dir_path(tmp);
g_free(tmp);
}
// Create the cache directory
if (g_mkdir(cmd_options->checksum_cachedir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH)) {
if (errno == EEXIST) {
if (!g_file_test(cmd_options->checksum_cachedir,
G_FILE_TEST_IS_DIR))
{
g_set_error(err, CREATEREPO_C_ERROR, CRE_BADARG,
"The %s already exists and it is not a directory!",
cmd_options->checksum_cachedir);
return FALSE;
}
} else {
g_set_error(err, CREATEREPO_C_ERROR, CRE_BADARG,
"cannot use cachedir %s: %s",
cmd_options->checksum_cachedir, g_strerror(errno));
return FALSE;
}
}
g_debug("Cachedir for checksums is %s", cmd_options->checksum_cachedir);
return TRUE;
}
/** Creates list of cr_RepomdRecords from list
* of additional metadata (cr_Metadatum)
*
* @param additional_metadata List of cr_Metadatum
* @param repomd_checksum_type
*
* @return New GSList of cr_RepomdRecords
*/
static GSList*
cr_create_repomd_records_for_additional_metadata(GSList *additional_metadata,
cr_ChecksumType repomd_checksum_type)
{
GError *tmp_err = NULL;
GSList *additional_metadata_rec = NULL;
GSList *element = additional_metadata;
for (; element; element=g_slist_next(element)) {
additional_metadata_rec = g_slist_prepend(additional_metadata_rec,
cr_repomd_record_new(
((cr_Metadatum *) element->data)->type,
((cr_Metadatum *) element->data)->name
));
cr_repomd_record_fill(additional_metadata_rec->data,
repomd_checksum_type,
&tmp_err);
if (tmp_err) {
g_critical("Cannot process %s %s: %s",
((cr_Metadatum *) element->data)->type,
((cr_Metadatum *) element->data)->name,
tmp_err->message);
g_clear_error(&tmp_err);
exit(EXIT_FAILURE);
}
}
return additional_metadata_rec;
}
/** Check if task finished without error, if yes
* use content stats of the new file
*
* @param task Rewrite pkg count task
* @param filename Name of file with wrong package count
* @param exit_val If errors occured set createrepo_c exit value
* @param content_stat Content stats for filename
*
*/
static void
error_check_and_set_content_stat(cr_CompressionTask *task, char *filename, int *exit_val, cr_ContentStat **content_stat){
if (task->err) {
g_critical("Cannot rewrite pkg count in %s: %s",
filename, task->err->message);
*exit_val = 2;
}else{
cr_contentstat_free(*content_stat, NULL);
*content_stat = task->stat;
task->stat = NULL;
}
}
static void
load_old_metadata(cr_Metadata **md,
struct cr_MetadataLocation **md_location,
GSList *current_pkglist,
struct CmdOptions *cmd_options,
gchar *dir,
GThreadPool *pool,
GError *tmp_err)
{
*md_location = cr_locate_metadata(dir, TRUE, &tmp_err);
if (tmp_err) {
if (tmp_err->domain == CRE_MODULEMD) {
g_thread_pool_free(pool, FALSE, FALSE);
g_clear_pointer(md_location, cr_metadatalocation_free);
g_critical("%s\n",tmp_err->message);
exit(tmp_err->code);
} else {
g_debug("Old metadata from default outputdir not found: %s",tmp_err->message);
g_clear_error(&tmp_err);
}
}
*md = cr_metadata_new(CR_HT_KEY_HREF, 1, current_pkglist);
cr_metadata_set_dupaction(*md, CR_HT_DUPACT_REMOVEALL);
int ret;
if (*md_location) {
ret = cr_metadata_load_xml(*md, *md_location, &tmp_err);
assert(ret == CRE_OK || tmp_err);
if (ret == CRE_OK) {
g_debug("Old metadata from: %s - loaded",
(*md_location)->original_url);
} else {
g_debug("Old metadata from %s - loading failed: %s",
(*md_location)->original_url, tmp_err->message);
g_clear_error(&tmp_err);
}
}
// Load repodata from --update-md-path
GSList *element = cmd_options->l_update_md_paths;
for (; element; element = g_slist_next(element)) {
char *path = (char *) element->data;
g_message("Loading metadata from md-path: %s", path);
ret = cr_metadata_locate_and_load_xml(*md, path, &tmp_err);
assert(ret == CRE_OK || tmp_err);
if (ret == CRE_OK) {
g_debug("Metadata from md-path %s - loaded", path);
} else {
g_warning("Metadata from md-path %s - loading failed: %s",
path, tmp_err->message);
g_clear_error(&tmp_err);
}
}
g_message("Loaded information about %d packages",
g_hash_table_size(cr_metadata_hashtable(*md)));
}
// Sorting function for location_href strings, by length.
// Compatible with g_array_sort()
static int strlensort(gconstpointer a, gconstpointer b)
{
// Function is supposed to take a double-pointer so unfortunately you cannot pass a
// string-comparison function directly.
gchar **a_ptr = (gchar **)a;
gchar **b_ptr = (gchar **)b;
int a_len = strnlen(*a_ptr, 4096);
int b_len = strnlen(*b_ptr, 4096);
if (a_len > b_len)
{
return 1;
}
else if (b_len > a_len)
{
return -1;
}
else
{
return 0;
}
}
// Sorting function for DuplicateLocation pointers, by pkg build time.
// Compatible with g_array_sort()
static int buildtimesort(gconstpointer a, gconstpointer b)
{
// Function is supposed to take a double-pointer so unfortunately you cannot pass a
// string-comparison function directly.
struct DuplicateLocation *a_loc = (struct DuplicateLocation *)a;
struct DuplicateLocation *b_loc = (struct DuplicateLocation *)b;
assert(a_loc->pkg->time_build != 0);
assert(b_loc->pkg->time_build != 0);
// order by build time first
int64_t result = a_loc->pkg->time_build - b_loc->pkg->time_build;
if (result)
return result;
// and then alphabetically by the rpm location
return g_strcmp0(a_loc->location, b_loc->location);
}
static int
handle_nevra_duplicates(GArray *locations, CmdDupNevra option)
{
int skipped = 0;
for (size_t i=0; i<locations->len; i++) {
struct DuplicateLocation location = g_array_index(
locations, struct DuplicateLocation, i);
if (option == CR_ARG_DUP_NEVRA_KEEP_LAST) {
if (i < locations->len - 1) {
location.pkg->skip_dump = TRUE;
skipped += 1;
}
}
}
return skipped;
}
static void
duplicates_warning(const char *nevra, GArray *locations, CmdDupNevra option)
{
g_warning("Package '%s' has duplicate metadata entries, only one should exist", nevra);
char *skip_reason= "";
if (option == CR_ARG_DUP_NEVRA_KEEP_LAST) {
skip_reason = " (not dumped, 'keep-last')";
}
for (size_t i=0; i<locations->len; i++) {
struct DuplicateLocation location = g_array_index(locations, struct
DuplicateLocation, i);
g_warning(" Sourced from location: \'%s\', build timestamp: %jd%s",
location.location,
(intmax_t) location.pkg->time_build,
location.pkg->skip_dump ? skip_reason : "");
}
}
int
main(int argc, char **argv)
{
struct CmdOptions *cmd_options;
gboolean ret;
GError *tmp_err = NULL;
int exit_val = EXIT_SUCCESS;
// Arguments parsing
cmd_options = parse_arguments(&argc, &argv, &tmp_err);
if (!cmd_options) {
g_printerr("Argument parsing failed: %s\n", tmp_err->message);
g_error_free(tmp_err);
exit(EXIT_FAILURE);
}
// Arguments pre-check
if (cmd_options->version) {
// Just print version
printf("Version: %s\n", cr_version_string_with_features());
free_options(cmd_options);
exit(EXIT_SUCCESS);
}
if ( cmd_options->split ) {
if (argc < 2) {
g_printerr("Must specify at least one directory to index.\n");
g_printerr("Usage: %s [options] <directory_to_index> [directory_to_index] ...\n\n",
cr_get_filename(argv[0]));
free_options(cmd_options);
exit(EXIT_FAILURE);
}
} else {
if (argc != 2) {
// No mandatory arguments
g_printerr("Must specify exactly one directory to index.\n");
g_printerr("Usage: %s [options] <directory_to_index>\n\n",
cr_get_filename(argv[0]));
free_options(cmd_options);
exit(EXIT_FAILURE);
}
}
// Dirs
gchar *in_dir = NULL; // path/to/repo/
gchar *in_repo = NULL; // path/to/repo/repodata/
gchar *out_dir = NULL; // path/to/out_repo/
gchar *out_repo = NULL; // path/to/out_repo/repodata/
gchar *tmp_out_repo = NULL; // usually path/to/out_repo/.repodata/
gchar *lock_dir = NULL; // path/to/out_repo/.repodata/
if (cmd_options->basedir && !g_str_has_prefix(argv[1], "/")) {
gchar *tmp = cr_normalize_dir_path(argv[1]);
in_dir = g_build_filename(cmd_options->basedir, tmp, NULL);
g_free(tmp);
} else {
in_dir = cr_normalize_dir_path(argv[1]);
}
// Check if inputdir exists
if (!g_file_test(in_dir, G_FILE_TEST_IS_DIR)) {
g_printerr("Directory %s must exist\n", in_dir);
g_free(in_dir);
free_options(cmd_options);
exit(EXIT_FAILURE);
}
// Check parsed arguments
if (!check_arguments(cmd_options, in_dir, &tmp_err)) {
g_printerr("%s\n", tmp_err->message);
g_error_free(tmp_err);
g_free(in_dir);
free_options(cmd_options);
exit(EXIT_FAILURE);
}
// Set logging stuff
cr_setup_logging(cmd_options->quiet, cmd_options->verbose);
// Emit debug message with version
g_debug("Version: %s", cr_version_string_with_features());
// Set paths of input and output repos
in_repo = g_strconcat(in_dir, "repodata/", NULL);
if (cmd_options->outputdir) {
out_dir = cr_normalize_dir_path(cmd_options->outputdir);
out_repo = g_strconcat(out_dir, "repodata/", NULL);
} else {
out_dir = g_strdup(in_dir);
out_repo = g_strdup(in_repo);
}
// Prepare cachedir for checksum if --cachedir is used
if (!prepare_cache_dir(cmd_options, out_dir, &tmp_err)) {
g_printerr("%s\n", tmp_err->message);
g_error_free(tmp_err);
g_free(in_dir);
g_free(in_repo);
g_free(out_dir);
g_free(out_repo);
free_options(cmd_options);
exit(EXIT_FAILURE);
}
// Block signals that terminates the process
if (!cr_block_terminating_signals(&tmp_err)) {
g_printerr("%s\n", tmp_err->message);
exit(EXIT_FAILURE);
}
// Check if lock exists & Create lock dir
if (!cr_lock_repo(out_dir, cmd_options->ignore_lock, &lock_dir, &tmp_out_repo, &tmp_err)) {
g_printerr("%s\n", tmp_err->message);
exit(EXIT_FAILURE);
}
// Setup cleanup handlers
if (!cr_set_cleanup_handler(lock_dir, tmp_out_repo, &tmp_err)) {
g_printerr("%s\n", tmp_err->message);
exit(EXIT_FAILURE);
}
// Unblock the blocked signals
if (!cr_unblock_terminating_signals(&tmp_err)) {
g_printerr("%s\n", tmp_err->message);
exit(EXIT_FAILURE);
}
// Open package list
FILE *output_pkg_list = NULL;
if (cmd_options->read_pkgs_list) {
output_pkg_list = fopen(cmd_options->read_pkgs_list, "w");
if (!output_pkg_list) {
g_critical("Cannot open \"%s\" for writing: %s",
cmd_options->read_pkgs_list, g_strerror(errno));
exit(EXIT_FAILURE);
}
}
// Init package parser
cr_package_parser_init();
cr_xml_dump_init();
cr_xml_dump_set_parameter(CR_XML_DUMP_DO_PRETTY_PRINT, cmd_options->pretty);
// Thread pool - Creation
struct UserData user_data = {0};
GThreadPool *pool = g_thread_pool_new(cr_dumper_thread,
&user_data,
0,
TRUE,
NULL);
g_debug("Thread pool ready");
long task_count = 0;
long package_count_in_headers = 0;
GSList *current_pkglist = NULL;
/* ^^^ List with basenames of files which will be processed */
// Load old metadata if --update
struct cr_MetadataLocation *old_metadata_location = NULL;
cr_Metadata *old_metadata = NULL;
gchar *old_metadata_dir = cmd_options->outputdir ? out_dir : in_dir;
if (cmd_options->recycle_pkglist) {
// load the old metadata early, so we can read the list of RPMs
load_old_metadata(&old_metadata,
&old_metadata_location,
NULL /* no filter wanted in this case */,
cmd_options,
old_metadata_dir,
pool,
tmp_err);
GHashTableIter iter;
g_hash_table_iter_init(&iter, cr_metadata_hashtable(old_metadata));
gpointer pkg_pointer;
while (g_hash_table_iter_next(&iter, NULL, &pkg_pointer)) {
cr_Package *pkg = (cr_Package *)pkg_pointer;
cmd_options->include_pkgs = g_slist_prepend(
cmd_options->include_pkgs,
(gpointer) g_strdup(pkg->location_href));
}
}
for (int media_id = 1; media_id < argc; media_id++ ) {
gchar *tmp_in_dir = cr_normalize_dir_path(argv[media_id]);
// Thread pool - Fill with tasks
fill_pool(pool,
tmp_in_dir,
cmd_options,
¤t_pkglist,
&task_count,
media_id);
g_free(tmp_in_dir);
}
g_debug("Package count: %ld", task_count);
g_message("Directory walk done - %ld packages", task_count);
if (cmd_options->nevra_duplicates)
// we need to construct the large table of cr_Packages to analyse
// all the NEVRAs together.
cmd_options->delayed_dump = TRUE;
user_data.task_count = task_count;
if (cmd_options->delayed_dump)
// call this when we know the expected task_count
cr_delayed_dump_set(&user_data);
if (cmd_options->update) {
if (old_metadata)
g_debug("Old metadata already loaded.");
else if (!task_count)
g_debug("No packages found - skipping metadata loading");
else
load_old_metadata(&old_metadata,
&old_metadata_location,
current_pkglist,
cmd_options,
old_metadata_dir,
pool,
tmp_err);
}
g_slist_free(current_pkglist);
current_pkglist = NULL;
GSList *additional_metadata = NULL;
// Setup compression types
const char *xml_compression_suffix = NULL;
const char *sqlite_compression_suffix = NULL;
const char *compression_suffix = NULL;
cr_CompressionType xml_compression = CR_CW_ZSTD_COMPRESSION;
cr_CompressionType sqlite_compression = CR_CW_BZ2_COMPRESSION;
cr_CompressionType compression = CR_CW_ZSTD_COMPRESSION;
if (cmd_options->compatibility) {
xml_compression = CR_CW_GZ_COMPRESSION;
compression = CR_CW_GZ_COMPRESSION;
}
if (cmd_options->compression_type != CR_CW_UNKNOWN_COMPRESSION) {
sqlite_compression = cmd_options->compression_type;
compression = cmd_options->compression_type;
}
if (cmd_options->general_compression_type != CR_CW_UNKNOWN_COMPRESSION) {
xml_compression = cmd_options->general_compression_type;
sqlite_compression = cmd_options->general_compression_type;
compression = cmd_options->general_compression_type;
}
xml_compression_suffix = cr_compression_suffix(xml_compression);
sqlite_compression_suffix = cr_compression_suffix(sqlite_compression);
compression_suffix = cr_compression_suffix(compression);
// Groupfile specified as argument
if (cmd_options->groupfile_fullpath) {
cr_CompressionType group_compression = compression;
// Skip compressing the group metadata when using --compatibility flag
if (cmd_options->compatibility) {
group_compression = CR_CW_NO_COMPRESSION;
}
gchar *compressed_path = cr_compress_groupfile(cmd_options->groupfile_fullpath, tmp_out_repo, group_compression);
cr_Metadatum *new_groupfile_metadatum = g_malloc0(sizeof(cr_Metadatum));
new_groupfile_metadatum->name = compressed_path;
new_groupfile_metadatum->type = g_strdup("group");
additional_metadata = g_slist_prepend(additional_metadata, new_groupfile_metadatum);
//remove old groupfile(s) (every [compressed] variant)
if (old_metadata_location){
GSList *node_iter = old_metadata_location->additional_metadata;
while (node_iter != NULL){
cr_Metadatum *m = node_iter->data;
GSList *next = g_slist_next(node_iter);
if(g_str_has_prefix(m->type, "group")){
old_metadata_location->additional_metadata = g_slist_delete_link(old_metadata_location->additional_metadata,
node_iter);
cr_metadatum_free(m);
}
node_iter = next;
}
}
}
#ifdef WITH_LIBMODULEMD
// module metadata found in repo
if (cmd_options->modulemd_metadata) {
gboolean merger_is_empty = TRUE;
ModulemdModuleIndexMerger *merger = modulemd_module_index_merger_new();
if (!merger) {
g_critical("Could not allocate module merger");
exit(EXIT_FAILURE);
}
if (cmd_options->update && old_metadata_location && old_metadata_location->additional_metadata){
//associate old metadata into the merger if we want to keep them (--keep-all-metadata)
if (cr_metadata_modulemd(old_metadata) && cmd_options->keep_all_metadata){
modulemd_module_index_merger_associate_index(merger, cr_metadata_modulemd(old_metadata), 0);
merger_is_empty = FALSE;
if (tmp_err) {
g_critical("%s: Cannot merge old module index with new: %s", __func__, tmp_err->message);
g_clear_error(&tmp_err);
g_clear_pointer(&merger, g_object_unref);
exit(EXIT_FAILURE);
}
}
//remove old modules (every [compressed] variant)
GSList *node_iter = old_metadata_location->additional_metadata;
while (node_iter != NULL){
GSList *next = g_slist_next(node_iter);
cr_Metadatum *m = node_iter->data;
/* If we are updating some existing repodata that have modular metadata
* remove those from found cmd_options->modulemd_metadata.
* If --keel-all-metadata is not specified we don't want them and if it is they
* were already added from old_metadata module index above.
*/
GSList *element_iter = cmd_options->modulemd_metadata;
while (element_iter != NULL){
GSList *next_inner = g_slist_next(element_iter);
gchar *path_to_found_md = (gchar *) element_iter->data;
if (!g_strcmp0(path_to_found_md, m->name)) {
g_free(path_to_found_md);
cmd_options->modulemd_metadata = g_slist_delete_link(
cmd_options->modulemd_metadata, element_iter);
}
element_iter = next_inner;
}
if(g_str_has_prefix(m->type, "modules")){
old_metadata_location->additional_metadata = g_slist_delete_link(
old_metadata_location->additional_metadata, node_iter);
cr_metadatum_free(m);
}
node_iter = next;
}
}
ModulemdModuleIndex *moduleindex;
//load all found module metatada and associate it with merger
GSList *element = cmd_options->modulemd_metadata;
for (; element; element=g_slist_next(element)) {
int result = cr_metadata_load_modulemd(&moduleindex, (char *) element->data, &tmp_err);
if (result != CRE_OK) {
g_critical("Could not load module index file %s: %s", (char *) element->data,
(tmp_err ? tmp_err->message : "Unknown error"));
g_clear_error(&tmp_err);
g_clear_pointer(&moduleindex, g_object_unref);
g_clear_pointer(&merger, g_object_unref);
exit(EXIT_FAILURE);
}
modulemd_module_index_merger_associate_index(merger, moduleindex, 0);
merger_is_empty = FALSE;
g_clear_pointer(&moduleindex, g_object_unref);
}
if (!merger_is_empty) {
//merge module metadata and dump it to string
moduleindex = modulemd_module_index_merger_resolve (merger, &tmp_err);
char *moduleindex_str = modulemd_module_index_dump_to_string (moduleindex, &tmp_err);
g_clear_pointer(&moduleindex, g_object_unref);
if (tmp_err) {
g_critical("%s: Cannot dump module index: %s", __func__, tmp_err->message);
free(moduleindex_str);
g_clear_error(&tmp_err);
g_clear_pointer(&merger, g_object_unref);
exit(EXIT_FAILURE);
}
//compress new module metadata string to a file in temporary .repodata
gchar *modules_metadata_path = g_strconcat(tmp_out_repo, "modules.yaml", compression_suffix, NULL);
CR_FILE *modules_file = NULL;
modules_file = cr_open(modules_metadata_path, CR_CW_MODE_WRITE, compression, &tmp_err);
if (modules_file == NULL) {
g_critical("%s: Cannot open source file %s: %s", __func__, modules_metadata_path,
(tmp_err ? tmp_err->message : "Unknown error"));
g_clear_error(&tmp_err);
free(moduleindex_str);
g_free(modules_metadata_path);
g_clear_pointer(&merger, g_object_unref);
exit(EXIT_FAILURE);
}
cr_puts(modules_file, moduleindex_str, &tmp_err);
free(moduleindex_str);
cr_close(modules_file, &tmp_err);
if (tmp_err) {
g_critical("%s: Error while closing: : %s", __func__, tmp_err->message);
g_clear_error(&tmp_err);
g_free(modules_metadata_path);
g_clear_pointer(&merger, g_object_unref);
exit(EXIT_FAILURE);
}
//create additional metadatum for new module metadata file
cr_Metadatum *new_modules_metadatum = g_malloc0(sizeof(cr_Metadatum));
new_modules_metadatum->name = modules_metadata_path;
new_modules_metadatum->type = g_strdup("modules");
additional_metadata = g_slist_prepend(additional_metadata, new_modules_metadatum);
}