forked from rpm-software-management/libdnf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnf-repo.cpp
2345 lines (2122 loc) · 70.1 KB
/
dnf-repo.cpp
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2013-2015 Richard Hughes <richard@hughsie.com>
*
* Most of this code was taken from Zif, libzif/zif-repos.c
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or(at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:dnf-repo
* @short_description: Object representing a remote repo.
* @include: libdnf.h
* @stability: Unstable
*
* Sources are remote repositories of packages.
*
* See also: #DnfRepo
*/
#include "conf/OptionBool.hpp"
#include "dnf-context.hpp"
#include "hy-repo-private.hpp"
#include "hy-iutil-private.hpp"
#include <strings.h>
#include <fcntl.h>
#include <glib/gstdio.h>
#include "hy-util.h"
#include <librepo/librepo.h>
#include <rpm/rpmts.h>
#include <librepo/yum.h>
#include "dnf-keyring.h"
#include "dnf-package.h"
#include "dnf-repo.hpp"
#include "dnf-types.h"
#include "dnf-utils.h"
#include "utils/File.hpp"
#include <set>
#include <string>
#include <vector>
typedef struct
{
DnfRepoEnabled enabled;
gchar **gpgkeys;
gchar **exclude_packages;
gchar *filename; /* /etc/yum.repos.d/updates.repo */
gchar *location; /* /var/cache/PackageKit/metadata/fedora */
gchar *location_tmp; /* /var/cache/PackageKit/metadata/fedora.tmp */
gchar *packages; /* /var/cache/PackageKit/metadata/fedora/packages */
gchar *packages_tmp; /* /var/cache/PackageKit/metadata/fedora.tmp/packages */
gchar *keyring; /* /var/cache/PackageKit/metadata/fedora/gpgdir */
gchar *keyring_tmp; /* /var/cache/PackageKit/metadata/fedora.tmp/gpgdir */
gint64 timestamp_generated; /* µs */
gint64 timestamp_modified; /* µs */
GError *last_check_error;
GKeyFile *keyfile;
DnfContext *context; /* weak reference */
DnfRepoKind kind;
libdnf::Repo *repo;
LrHandle *repo_handle;
LrResult *repo_result;
LrUrlVars *urlvars;
} DnfRepoPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(DnfRepo, dnf_repo, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (static_cast<DnfRepoPrivate *>(dnf_repo_get_instance_private (o)))
/**
* dnf_repo_finalize:
**/
static void
dnf_repo_finalize(GObject *object)
{
DnfRepo *repo = DNF_REPO(object);
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->filename);
g_strfreev(priv->gpgkeys);
g_strfreev(priv->exclude_packages);
g_free(priv->location_tmp);
g_free(priv->location);
g_free(priv->packages);
g_free(priv->packages_tmp);
g_free(priv->keyring);
g_free(priv->keyring_tmp);
g_clear_error(&priv->last_check_error);
if (priv->repo_result != NULL)
lr_result_free(priv->repo_result);
if (priv->repo_handle != NULL)
lr_handle_free(priv->repo_handle);
if (priv->repo != NULL)
hy_repo_free(priv->repo);
if (priv->keyfile != NULL)
g_key_file_unref(priv->keyfile);
if (priv->context != NULL)
g_object_remove_weak_pointer(G_OBJECT(priv->context),
(void **) &priv->context);
G_OBJECT_CLASS(dnf_repo_parent_class)->finalize(object);
}
/**
* dnf_repo_init:
**/
static void
dnf_repo_init(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo = hy_repo_create("<preinit>");
priv->repo_handle = lr_handle_init();
priv->repo_result = lr_result_init();
}
/**
* dnf_repo_class_init:
**/
static void
dnf_repo_class_init(DnfRepoClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = dnf_repo_finalize;
}
/**
* dnf_repo_get_id:
* @repo: a #DnfRepo instance.
*
* Gets the repo ID.
*
* Returns: the repo ID, e.g. "fedora-updates"
*
* Since: 0.1.0
**/
const gchar *
dnf_repo_get_id(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return libdnf::repoGetImpl(priv->repo)->id.c_str();
}
/**
* dnf_repo_get_location:
* @repo: a #DnfRepo instance.
*
* Gets the repo location.
*
* Returns: the repo location, e.g. "/var/cache/PackageKit/metadata/fedora"
*
* Since: 0.1.0
**/
const gchar *
dnf_repo_get_location(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->location;
}
/**
* dnf_repo_get_filename:
* @repo: a #DnfRepo instance.
*
* Gets the repo filename.
*
* Returns: the repo filename, e.g. "/etc/yum.repos.d/updates.repo"
*
* Since: 0.1.0
**/
const gchar *
dnf_repo_get_filename(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->filename;
}
/**
* dnf_repo_get_packages:
* @repo: a #DnfRepo instance.
*
* Gets the repo packages location.
*
* Returns: the repo packages location, e.g. "/var/cache/PackageKit/metadata/fedora/packages"
*
* Since: 0.1.0
**/
const gchar *
dnf_repo_get_packages(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->packages;
}
/**
* dnf_repo_get_public_keys:
* @repo: a #DnfRepo instance.
*
* Gets the public key location.
*
* Returns: (transfer full)(array zero-terminated=1): The repo public key location, e.g. "/var/cache/PackageKit/metadata/fedora/repomd.pub"
*
* Since: 0.8.2
**/
gchar **
dnf_repo_get_public_keys(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_autoptr(GPtrArray) ret = g_ptr_array_new();
for (char **iter = priv->gpgkeys; iter && *iter; iter++) {
const char *key = *iter;
g_autofree gchar *key_bn = g_path_get_basename(key);
g_ptr_array_add(ret, g_build_filename(priv->location, key_bn, NULL));
}
g_ptr_array_add(ret, NULL);
return (gchar**)g_ptr_array_free(static_cast<GPtrArray *>(g_steal_pointer(&ret)), FALSE);
}
/**
* dnf_repo_substitute:
*/
static gchar *
dnf_repo_substitute(DnfRepo *repo, const gchar *url)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
char *tmp;
gchar *substituted;
/* do a little dance so we can use g_free() rather than lr_free() */
tmp = lr_url_substitute(url, priv->urlvars);
substituted = g_strdup(tmp);
lr_free(tmp);
return substituted;
}
/**
* dnf_repo_get_description:
* @repo: a #DnfRepo instance.
*
* Gets the repo description.
*
* Returns: the repo description, e.g. "Fedora 20 Updates"
*
* Since: 0.1.0
**/
gchar *
dnf_repo_get_description(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_autofree gchar *tmp = NULL;
/* is DVD */
if (priv->kind == DNF_REPO_KIND_MEDIA) {
tmp = g_key_file_get_string(priv->keyfile, "general", "name", NULL);
if (tmp == NULL)
return NULL;
} else {
tmp = g_key_file_get_string(priv->keyfile,
dnf_repo_get_id(repo),
"name",
NULL);
if (tmp == NULL)
return NULL;
}
/* have to substitute things like $releasever and $basearch */
return dnf_repo_substitute(repo, tmp);
}
/**
* dnf_repo_get_timestamp_generated:
* @repo: a #DnfRepo instance.
*
* Returns: Time in seconds since the epoch (UTC)
**/
guint64
dnf_repo_get_timestamp_generated (DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->timestamp_generated;
}
/**
* dnf_repo_get_n_solvables:
* @repo: a #DnfRepo instance.
*
* Returns: Number of packages in the repo
**/
guint
dnf_repo_get_n_solvables (DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return libdnf::repoGetImpl(priv->repo)->libsolvRepo->nsolvables;
}
/**
* dnf_repo_get_enabled:
* @repo: a #DnfRepo instance.
*
* Gets if the repo is enabled.
*
* Returns: %DNF_REPO_ENABLED_PACKAGES if enabled
*
* Since: 0.1.0
**/
DnfRepoEnabled
dnf_repo_get_enabled(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->enabled;
}
/**
* dnf_repo_get_required:
* @repo: a #DnfRepo instance.
*
* Returns: %TRUE if failure fetching this repo is fatal
*
* Since: 0.2.1
**/
gboolean
dnf_repo_get_required(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return !priv->repo->getConfig()->skip_if_unavailable().getValue();
}
/**
* dnf_repo_get_cost:
* @repo: a #DnfRepo instance.
*
* Gets the repo cost.
*
* Returns: the repo cost, where 1000 is default
*
* Since: 0.1.0
**/
guint
dnf_repo_get_cost(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return repoGetImpl(priv->repo)->conf->cost().getValue();
}
/**
* dnf_repo_get_module_hotfixes:
* @repo: a #DnfRepo instance.
*
* Gets the repo hotfixes flag for module RPMs filtering.
*
* Returns: the repo module_hotfixes flag, where false is default
*
* Since: 0.16.1
**/
gboolean
dnf_repo_get_module_hotfixes(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->repo->getConfig()->module_hotfixes().getValue();
}
/**
* dnf_repo_get_kind:
* @repo: a #DnfRepo instance.
*
* Gets the repo kind.
*
* Returns: the repo kind, e.g. %DNF_REPO_KIND_MEDIA
*
* Since: 0.1.0
**/
DnfRepoKind
dnf_repo_get_kind(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->kind;
}
/**
* dnf_repo_get_exclude_packages:
* @repo: a #DnfRepo instance.
*
* Returns:(transfer none)(array zero-terminated=1): Packages which should be excluded
*
* Since: 0.1.9
**/
gchar **
dnf_repo_get_exclude_packages(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->exclude_packages;
}
/**
* dnf_repo_get_gpgcheck:
* @repo: a #DnfRepo instance.
*
* Gets if the packages should be signed.
*
* Returns: %TRUE if packages should be signed
*
* Since: 0.1.0
**/
gboolean
dnf_repo_get_gpgcheck(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->repo->getConfig()->gpgcheck().getValue();
}
/**
* dnf_repo_get_gpgcheck_md:
* @repo: a #DnfRepo instance.
*
* Gets if the metadata should be signed.
*
* Returns: %TRUE if metadata should be signed
*
* Since: 0.1.7
**/
gboolean
dnf_repo_get_gpgcheck_md(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->repo->getConfig()->repo_gpgcheck().getValue();
}
/**
* dnf_repo_get_repo:
* @repo: a #DnfRepo instance.
*
* Gets the #HyRepo backing this repo.
*
* Returns: the #HyRepo
*
* Since: 0.1.0
**/
HyRepo
dnf_repo_get_repo(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->repo;
}
/**
* dnf_repo_get_metadata_expire:
* @repo: a #DnfRepo instance.
*
* Gets the metadata_expire time for the repo
*
* Returns: the metadata_expire time in seconds
*/
guint
dnf_repo_get_metadata_expire(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
return priv->repo->getConfig()->metadata_expire().getValue();
}
/**
* dnf_repo_is_devel:
* @repo: a #DnfRepo instance.
*
* Returns: %TRUE if the repo is a development repo
*
* Since: 0.1.0
**/
gboolean
dnf_repo_is_devel(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
auto repoId = priv->repo->getId().c_str();
if (g_str_has_suffix(repoId, "-debuginfo"))
return TRUE;
if (g_str_has_suffix(repoId, "-debug"))
return TRUE;
if (g_str_has_suffix(repoId, "-development"))
return TRUE;
return FALSE;
}
/**
* dnf_repo_is_local:
* @repo: a #DnfRepo instance.
*
* Returns: %TRUE if the repo is a repo on local or media filesystem
*
* Since: 0.1.6
**/
gboolean
dnf_repo_is_local(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
/* media or local */
if (priv->kind == DNF_REPO_KIND_MEDIA ||
priv->kind == DNF_REPO_KIND_LOCAL)
return TRUE;
return FALSE;
}
/**
* dnf_repo_is_source:
* @repo: a #DnfRepo instance.
*
* Returns: %TRUE if the repo is a source repo
*
* Since: 0.1.0
**/
gboolean
dnf_repo_is_source(DnfRepo *repo)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
if (g_str_has_suffix(priv->repo->getId().c_str(), "-source"))
return TRUE;
return FALSE;
}
/**
* dnf_repo_set_id:
* @repo: a #DnfRepo instance.
* @id: the ID, e.g. "fedora-updates"
*
* Sets the repo ID.
*
* Since: 0.1.0
**/
void
dnf_repo_set_id(DnfRepo *repo, const gchar *id)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
libdnf::repoGetImpl(priv->repo)->id = id;
libdnf::repoGetImpl(priv->repo)->conf->name().set(libdnf::Option::Priority::RUNTIME, id);
}
/**
* dnf_repo_set_location:
* @repo: a #DnfRepo instance.
* @location: the path
*
* Sets the repo location and the default packages path.
*
* Since: 0.1.0
**/
void
dnf_repo_set_location(DnfRepo *repo, const gchar *location)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->location);
g_free(priv->packages);
g_free(priv->keyring);
priv->location = dnf_repo_substitute(repo, location);
priv->packages = g_build_filename(location, "packages", NULL);
priv->keyring = g_build_filename(location, "gpgdir", NULL);
}
/**
* dnf_repo_set_location_tmp:
* @repo: a #DnfRepo instance.
* @location_tmp: the temp path
*
* Sets the repo location for temporary files and the default packages path.
*
* Since: 0.1.0
**/
void
dnf_repo_set_location_tmp(DnfRepo *repo, const gchar *location_tmp)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->location_tmp);
g_free(priv->packages_tmp);
g_free(priv->keyring_tmp);
priv->location_tmp = dnf_repo_substitute(repo, location_tmp);
priv->packages_tmp = g_build_filename(location_tmp, "packages", NULL);
priv->keyring_tmp = g_build_filename(location_tmp, "gpgdir", NULL);
}
/**
* dnf_repo_set_filename:
* @repo: a #DnfRepo instance.
* @filename: the filename path
*
* Sets the repo backing filename.
*
* Since: 0.1.0
**/
void
dnf_repo_set_filename(DnfRepo *repo, const gchar *filename)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->filename);
priv->filename = g_strdup(filename);
}
/**
* dnf_repo_set_packages:
* @repo: a #DnfRepo instance.
* @packages: the path to the package files
*
* Sets the repo package location, typically ending in "Packages" or "packages".
*
* Since: 0.1.0
**/
void
dnf_repo_set_packages(DnfRepo *repo, const gchar *packages)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->packages);
priv->packages = g_strdup(packages);
}
/**
* dnf_repo_set_packages_tmp:
* @repo: a #DnfRepo instance.
* @packages_tmp: the path to the temporary package files
*
* Sets the repo package location for temporary files.
*
* Since: 0.1.0
**/
void
dnf_repo_set_packages_tmp(DnfRepo *repo, const gchar *packages_tmp)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
g_free(priv->packages_tmp);
priv->packages_tmp = g_strdup(packages_tmp);
}
/**
* dnf_repo_set_enabled:
* @repo: a #DnfRepo instance.
* @enabled: if the repo is enabled
*
* Sets the repo enabled state.
*
* Since: 0.1.0
**/
void
dnf_repo_set_enabled(DnfRepo *repo, DnfRepoEnabled enabled)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->enabled = enabled;
/* packages implies metadata */
if (priv->enabled & DNF_REPO_ENABLED_PACKAGES)
priv->enabled |= DNF_REPO_ENABLED_METADATA;
}
/**
* dnf_repo_set_required:
* @repo: a #DnfRepo instance.
* @required: if the repo is required
*
* Sets whether failure to retrieve the repo is fatal; by default it
* is not.
*
* Since: 0.2.1
**/
void
dnf_repo_set_required(DnfRepo *repo, gboolean required)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo->getConfig()->skip_if_unavailable().set(libdnf::Option::Priority::RUNTIME, !required);
}
/**
* dnf_repo_set_cost:
* @repo: a #DnfRepo instance.
* @cost: the repo cost
*
* Sets the repo cost, where 1000 is the default.
*
* Since: 0.1.0
**/
void
dnf_repo_set_cost(DnfRepo *repo, guint cost)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
repoGetImpl(priv->repo)->conf->cost().set(libdnf::Option::Priority::RUNTIME, cost);
}
/**
* dnf_repo_set_module_hotfixes:
* @repo: a #DnfRepo instance.
* @module_hotfixes: hotfixes flag for module RPMs filtering
*
* Sets the repo hotfixes flag for module RPMs filtering.
*
* Since: 0.16.1
**/
void
dnf_repo_set_module_hotfixes(DnfRepo *repo, gboolean module_hotfixes)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo->getConfig()->module_hotfixes().set(libdnf::Option::Priority::RUNTIME, module_hotfixes);
}
/**
* dnf_repo_set_kind:
* @repo: a #DnfRepo instance.
* @kind: the #DnfRepoKind
*
* Sets the repo kind.
*
* Since: 0.1.0
**/
void
dnf_repo_set_kind(DnfRepo *repo, DnfRepoKind kind)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->kind = kind;
}
/**
* dnf_repo_set_gpgcheck:
* @repo: a #DnfRepo instance.
* @gpgcheck_pkgs: if the repo packages should be signed
*
* Sets the repo signed status.
*
* Since: 0.1.0
**/
void
dnf_repo_set_gpgcheck(DnfRepo *repo, gboolean gpgcheck_pkgs)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo->getConfig()->gpgcheck().set(libdnf::Option::Priority::RUNTIME, gpgcheck_pkgs);
}
/**
* dnf_repo_set_gpgcheck_md:
* @repo: a #DnfRepo instance.
* @gpgcheck_md: if the repo metadata should be signed
*
* Sets the metadata signed status.
*
* Since: 0.1.7
**/
void
dnf_repo_set_gpgcheck_md(DnfRepo *repo, gboolean gpgcheck_md)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo->getConfig()->repo_gpgcheck().set(libdnf::Option::Priority::RUNTIME, gpgcheck_md);
}
/**
* dnf_repo_set_keyfile:
* @repo: a #DnfRepo instance.
* @keyfile: the #GKeyFile
*
* Sets the repo keyfile used to create the repo.
*
* Since: 0.1.0
**/
void
dnf_repo_set_keyfile(DnfRepo *repo, GKeyFile *keyfile)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
if (priv->keyfile != NULL)
g_key_file_unref(priv->keyfile);
priv->keyfile = g_key_file_ref(keyfile);
}
/**
* dnf_repo_set_metadata_expire:
* @repo: a #DnfRepo instance.
* @metadata_expire: the expected expiry time for metadata
*
* Sets the metadata_expire time, which defaults to 48h.
**/
void
dnf_repo_set_metadata_expire(DnfRepo *repo, guint metadata_expire)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
priv->repo->getConfig()->metadata_expire().set(libdnf::Option::Priority::RUNTIME, metadata_expire);
}
/**
* dnf_repo_parse_time_from_str
* @expression: a expression to be parsed
* @out_parsed_time: (out): return location for parsed time
* @error: error item
*
* Parse String into an integer value of seconds, or a human
* readable variation specifying days, hours, minutes or seconds
* until something happens. Note that due to historical president
* -1 means "never", so this accepts that and allows
* the word never, too.
*
* Valid inputs: 100, 1.5m, 90s, 1.2d, 1d, 0xF, 0.1, -1, never.
* Invalid inputs: -10, -0.1, 45.6Z, 1d6h, 1day, 1y.
* Returns: integer value in seconds
**/
static gboolean
dnf_repo_parse_time_from_str(const gchar *expression, guint *out_parsed_time, GError **error)
{
gint multiplier;
gdouble parsed_time;
gchar *endptr = NULL;
if (!g_strcmp0(expression, "")) {
g_set_error_literal(error,
DNF_ERROR,
DNF_ERROR_FILE_INVALID,
"no metadata value specified");
return FALSE;
}
if (g_strcmp0(expression, "-1") == 0 ||
g_strcmp0(expression,"never") == 0) {
*out_parsed_time = G_MAXUINT;
return TRUE; /* Note early return */
}
gchar last_char = expression[ strlen(expression) - 1 ];
/* check if the input ends with h, m ,d ,s as units */
if (g_ascii_isalpha(last_char)) {
if (last_char == 'h')
multiplier = 60 * 60;
else if (last_char == 's')
multiplier = 1;
else if (last_char == 'm')
multiplier = 60;
else if (last_char == 'd')
multiplier = 60 * 60 * 24;
else {
g_set_error(error, DNF_ERROR, DNF_ERROR_FILE_INVALID,
"unknown unit %c", last_char);
return FALSE;
}
}
else
multiplier = 1;
/* convert expression into a double*/
parsed_time = g_ascii_strtod(expression, &endptr);
/* failed to parse */
if (expression == endptr) {
g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
"failed to parse time: %s", expression);
return FALSE;
}
/* time can not be below zero */
if (parsed_time < 0) {
g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
"seconds value must not be negative %s",expression );
return FALSE;
}
/* time too large */
if (parsed_time > G_MAXDOUBLE || (parsed_time * multiplier) > G_MAXUINT){
g_set_error(error, DNF_ERROR, DNF_ERROR_INTERNAL_ERROR,
"time too large");
return FALSE;
}
*out_parsed_time = (guint) (parsed_time * multiplier);
return TRUE;
}
/**
* dnf_repo_get_username_password_string:
*/
static gchar *
dnf_repo_get_username_password_string(const gchar *user, const gchar *pass)
{
if (user == NULL && pass == NULL)
return NULL;
if (user != NULL && pass == NULL)
return g_strdup(user);
if (user == NULL && pass != NULL)
return g_strdup_printf(":%s", pass);
return g_strdup_printf("%s:%s", user, pass);
}
static gboolean
dnf_repo_get_boolean(GKeyFile *keyfile,
const gchar *group_name,
const gchar *key)
{
g_autofree gchar *str = NULL;
str = g_key_file_get_value(keyfile, group_name, key, NULL);
if (str) {
try {
return libdnf::OptionBool(false).fromString(str);
} catch (const std::exception & ex) {
g_warning("Config error in section \"%s\" key \"%s\": %s", group_name, key, ex.what());
}
}
return false;
}
/* Initialize (or potentially reset) repo & LrHandle from keyfile values. */
static gboolean
dnf_repo_set_keyfile_data(DnfRepo *repo, GError **error)
{
DnfRepoPrivate *priv = GET_PRIVATE(repo);
guint cost;
gboolean module_hotfixes = false;
g_autofree gchar *metadata_expire_str = NULL;
g_autofree gchar *mirrorlist = NULL;
g_autofree gchar *mirrorlisturl = NULL;
g_autofree gchar *metalinkurl = NULL;
g_autofree gchar *proxy = NULL;
g_autofree gchar *tmp_strval = NULL;
g_autofree gchar *pwd = NULL;
g_autofree gchar *usr = NULL;
g_autofree gchar *usr_pwd = NULL;
g_autofree gchar *usr_pwd_proxy = NULL;
g_auto(GStrv) baseurls;
auto repoId = priv->repo->getId().c_str();
g_debug("setting keyfile data for %s", repoId);
/* skip_if_unavailable is optional */
if (g_key_file_has_key(priv->keyfile, repoId, "skip_if_unavailable", NULL)) {
bool skip = dnf_repo_get_boolean(priv->keyfile, repoId, "skip_if_unavailable");
priv->repo->getConfig()->skip_if_unavailable().set(libdnf::Option::Priority::REPOCONFIG, skip);
}
/* cost is optional */
cost = g_key_file_get_integer(priv->keyfile, repoId, "cost", NULL);
if (cost != 0)
dnf_repo_set_cost(repo, cost);
module_hotfixes = g_key_file_get_boolean(priv->keyfile, repoId, "module_hotfixes", NULL);
dnf_repo_set_module_hotfixes(repo, module_hotfixes);
/* baseurl is optional; if missing, unset it */
baseurls = g_key_file_get_string_list(priv->keyfile, repoId, "baseurl", NULL, NULL);
if (!lr_handle_setopt(priv->repo_handle, error, LRO_URLS, baseurls))
return FALSE;
/* metadata_expire is optional, if shown, we parse the string to add the time */
metadata_expire_str = g_key_file_get_string(priv->keyfile, repoId, "metadata_expire", NULL);
if (metadata_expire_str) {
guint metadata_expire;
if (!dnf_repo_parse_time_from_str(metadata_expire_str, &metadata_expire, error))
return FALSE;
dnf_repo_set_metadata_expire(repo, metadata_expire);
} else {
/* default to 48h; this is in line with dnf's default */
dnf_repo_set_metadata_expire(repo, 60 * 60 * 48);
}
/* the "mirrorlist" entry could be either a real mirrorlist, or a metalink entry */
mirrorlist = g_key_file_get_string(priv->keyfile, repoId, "mirrorlist", NULL);
if (mirrorlist) {
if (strstr(mirrorlist, "metalink"))
metalinkurl = static_cast<gchar *>(g_steal_pointer(&mirrorlist));
else /* it really is a mirrorlist */
mirrorlisturl = static_cast<gchar *>(g_steal_pointer(&mirrorlist));
}
/* let "metalink" entry override metalink-as-mirrorlist entry */
if (g_key_file_has_key(priv->keyfile, repoId, "metalink", NULL)) {
g_free(metalinkurl);
metalinkurl = g_key_file_get_string(priv->keyfile, repoId, "metalink", NULL);
}
/* now set the final values (or unset them) */
if (!lr_handle_setopt(priv->repo_handle, error, LRO_MIRRORLISTURL, mirrorlisturl))
return FALSE;
if (!lr_handle_setopt(priv->repo_handle, error, LRO_METALINKURL, metalinkurl))
return FALSE;
/* needed in order for addCountmeFlag() to use the same persistdir as DNF
* would */
if (metalinkurl)
priv->repo->getConfig()->metalink().set(libdnf::Option::Priority::REPOCONFIG, metalinkurl);
if (mirrorlisturl)
priv->repo->getConfig()->mirrorlist().set(libdnf::Option::Priority::REPOCONFIG, mirrorlisturl);
if (g_key_file_has_key(priv->keyfile, repoId, "countme", NULL)) {