summaryrefslogtreecommitdiffstats
path: root/src/transaction.c
blob: 0d4241cd1d14bd0e918df0594948db5ee824c79b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
/*
 * Copyright (C) 2003-2012 Jason Woodward <woodwardj at jaos dot org>
 *
 * 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
 * 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main.h"

#define SLAPT_PKG_DL_NOTE_LEN 16

static slapt_queue_t *slapt_queue_init(void);
static void queue_add_install(slapt_queue_t *t, slapt_pkg_info_t *p);
static void queue_add_upgrade(slapt_queue_t *t, slapt_pkg_upgrade_t *p);
static void queue_free(slapt_queue_t *t);

static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg);

static void _slapt_add_upgrade_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *installed_pkg,
                                      slapt_pkg_info_t *slapt_upgrade_pkg,
                                      SLAPT_BOOL_T reinstall
);

slapt_transaction_t *slapt_init_transaction(void)
{
  slapt_transaction_t *tran = slapt_malloc(sizeof *tran);

  tran->install_pkgs = slapt_init_pkg_list();
  tran->install_pkgs->free_pkgs = SLAPT_TRUE;

  tran->remove_pkgs = slapt_init_pkg_list();
  tran->remove_pkgs->free_pkgs = SLAPT_TRUE;

  tran->exclude_pkgs = slapt_init_pkg_list();
  tran->exclude_pkgs->free_pkgs = SLAPT_TRUE;

  tran->upgrade_pkgs = slapt_malloc(sizeof *tran->upgrade_pkgs);
  tran->upgrade_pkgs->pkgs = slapt_malloc(sizeof *tran->upgrade_pkgs->pkgs);
  tran->upgrade_pkgs->pkg_count = 0;
  tran->upgrade_pkgs->reinstall_count = 0;


  tran->suggests = slapt_init_list();

  tran->queue = slapt_queue_init();

  tran->conflict_err = slapt_init_pkg_err_list();
  tran->missing_err = slapt_init_pkg_err_list();

  return tran;
}

int slapt_handle_transaction (const slapt_rc_config *global_config,
                              slapt_transaction_t *tran)
{
  unsigned int i, pkg_dl_count = 0, dl_counter = 0;
  double download_size = 0;
  double already_download_size = 0;
  double uncompressed_size = 0;
  char dl_note[SLAPT_PKG_DL_NOTE_LEN];

  /* show unmet dependencies */
  if (tran->missing_err->err_count > 0) {
    fprintf(stderr,gettext("The following packages have unmet dependencies:\n"));
    for (i=0; i < tran->missing_err->err_count; ++i) {
      fprintf(stderr,gettext("  %s: Depends: %s\n"),
        tran->missing_err->errs[i]->pkg,tran->missing_err->errs[i]->error);
    }
  }

  /* show conflicts */
  if (tran->conflict_err->err_count > 0) {
    for (i=0; i < tran->conflict_err->err_count; ++i) {
      fprintf(stderr,gettext("%s, which is required by %s, is excluded\n"),
        tran->conflict_err->errs[i]->error,tran->conflict_err->errs[i]->pkg);
    }
  }

  /* show pkgs to exclude */
  if (tran->exclude_pkgs->pkg_count > 0) {
    unsigned int len = 0;
    printf(gettext("The following packages have been EXCLUDED:\n"));
    printf("  ");

    for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
      const slapt_pkg_info_t *e = tran->exclude_pkgs->pkgs[i];

      if (len + strlen(e->name) + 1 < MAX_LINE_LEN) {
        printf("%s ",e->name);
        len += strlen(e->name) + 1;
      } else {
        printf("\n  %s ",e->name);
        len = strlen(e->name) + 3;
      }

    }

    printf("\n");
  }

  /* show suggested pkgs */
  slapt_generate_suggestions(tran);
  if (tran->suggests->count > 0) {
    unsigned int len = 0;

    printf(gettext("Suggested packages:\n"));
    printf("  ");

    for (i = 0; i < tran->suggests->count; ++i) {
      char *s = tran->suggests->items[i];

      /* don't show suggestion for something we already have
         in the transaction */
      if (slapt_search_transaction(tran,s) == 1)
        continue;

      if (len + strlen(s) + 1 < MAX_LINE_LEN) {
        printf("%s ",s);
        len += strlen(s) + 1;
      } else {
        printf("\n  %s ",s);
        len = strlen(s) + 3;
      }

    }
    printf("\n");

  }

  /* show pkgs to install */
  if (tran->install_pkgs->pkg_count > 0) {
    unsigned int len = 0;
    printf(gettext("The following NEW packages will be installed:\n"));
    printf("  ");

    for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
      slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];
      size_t existing_file_size = 0;

      if (len + strlen(p->name) + 1 < MAX_LINE_LEN) {
        printf("%s ",p->name);
        len += strlen(p->name) + 1;
      } else {
        printf("\n  %s ",p->name);
        len = strlen(p->name) + 3;
      }

      existing_file_size = slapt_get_pkg_file_size(global_config,p) / 1024;

      download_size += p->size_c;

      if (existing_file_size <= p->size_c)
        already_download_size += existing_file_size;

      uncompressed_size += p->size_u;
    }
    printf("\n");
  }

  /* show pkgs to remove */
  if (tran->remove_pkgs->pkg_count > 0) {
    unsigned int len = 0;
    printf(gettext("The following packages will be REMOVED:\n"));
    printf("  ");

    for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
      const slapt_pkg_info_t *r = tran->remove_pkgs->pkgs[i];

      if (len + strlen(r->name) + 1 < MAX_LINE_LEN) {
        printf("%s ",r->name);
        len += strlen(r->name) + 1;
      } else {
        printf("\n  %s ",r->name);
        len = strlen(r->name) + 3;
      }

      uncompressed_size -= r->size_u;

    }

    printf("\n");
  }

  /* show pkgs to upgrade */
  if (tran->upgrade_pkgs->pkg_count > 0) {
    unsigned int len = 0;

    if ((tran->upgrade_pkgs->pkg_count - tran->upgrade_pkgs->reinstall_count) > 0) {
      printf(gettext("The following packages will be upgraded:\n"));
      printf("  ");
    }

    for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
      slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
      slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->installed;

      size_t existing_file_size = 0;
      int line_len = len + strlen(u->name) + 1;

      existing_file_size = slapt_get_pkg_file_size(
        global_config,u) / 1024;

      download_size += u->size_c;

      if (existing_file_size <= u->size_c)
          already_download_size += existing_file_size;

      uncompressed_size += u->size_u;
      uncompressed_size -= p->size_u;

      if (tran->upgrade_pkgs->pkgs[i]->reinstall == SLAPT_TRUE)
        continue;

      if (line_len < MAX_LINE_LEN) {
        printf("%s ",u->name);
        len += strlen(u->name) + 1;
      } else {
        printf("\n  %s ",u->name);
        len = strlen(u->name) + 3;
      }

    }

    if ((tran->upgrade_pkgs->pkg_count - tran->upgrade_pkgs->reinstall_count) > 0)
      printf("\n");

    if (tran->upgrade_pkgs->reinstall_count > 0) {
      unsigned int len = 0;
      printf(gettext("The following packages will be reinstalled:\n"));
      printf("  ");

      for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
        slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
        int line_len = len + strlen(u->name) + 1;

        if (tran->upgrade_pkgs->pkgs[i]->reinstall == SLAPT_FALSE)
          continue;

        if (line_len < MAX_LINE_LEN) {
          printf("%s ",u->name);
          len += strlen(u->name) + 1;
        } else {
          printf("\n  %s ",u->name);
          len = strlen(u->name) + 3;
        }
      }
      printf("\n");
    }
  }

  /* print the summary */
  printf(ngettext("%d upgraded, ", "%d upgraded, ",
    tran->upgrade_pkgs->pkg_count - tran->upgrade_pkgs->reinstall_count),
    tran->upgrade_pkgs->pkg_count - tran->upgrade_pkgs->reinstall_count);
  printf(ngettext("%d reinstalled, ", "%d reinstalled, ",
    tran->upgrade_pkgs->reinstall_count), tran->upgrade_pkgs->reinstall_count);
  printf(ngettext("%d newly installed, ", "%d newly installed, ",
    tran->install_pkgs->pkg_count), tran->install_pkgs->pkg_count);
  printf(ngettext("%d to remove, ", "%d to remove, ",
    tran->remove_pkgs->pkg_count), tran->remove_pkgs->pkg_count);
  printf(ngettext("%d not upgraded.\n", "%d not upgraded.\n",
    tran->exclude_pkgs->pkg_count), tran->exclude_pkgs->pkg_count);

  /* only show this if we are going to do download something */
  if (tran->upgrade_pkgs->pkg_count > 0 || tran->install_pkgs->pkg_count > 0) {

    /* how much we need to download */
    double need_to_download_size = download_size - already_download_size;

    /* make sure it's not negative due to changing pkg sizes on upgrades */
    if (need_to_download_size < 0)
      need_to_download_size = 0;

    if (already_download_size > 0) {
      printf(gettext("Need to get %.1f%s/%.1f%s of archives.\n"),
        (need_to_download_size > 1024) ? need_to_download_size / (double)1024
          : need_to_download_size,
        (need_to_download_size > 1024) ? "MB" : "kB",
        (download_size > 1024) ? download_size / (double)1024 : download_size,
        (download_size > 1024) ? "MB" : "kB"
     );
    } else {
      printf(gettext("Need to get %.1f%s of archives.\n"),
        (download_size > 1024) ? download_size / (double)1024 : download_size,
        (download_size > 1024) ? "MB" : "kB"
     );
    }

    /* check we have enough space to download to our working dir */
    if (slapt_disk_space_check (global_config->working_dir,need_to_download_size) == SLAPT_FALSE) {
      printf(
        gettext("You don't have enough free space in %s\n"),
        global_config->working_dir
     );
      exit(EXIT_FAILURE);
    }
    /* check that we have enough space in the root filesystem */
    if (global_config->download_only == SLAPT_FALSE) {
      if (slapt_disk_space_check ("/", uncompressed_size) == SLAPT_FALSE) {
        printf(gettext("You don't have enough free space in %s\n"),"/");
        exit(EXIT_FAILURE);
      }
    }

  }

  if (tran->upgrade_pkgs->pkg_count > 0 || tran->remove_pkgs->pkg_count > 0 ||
  tran->install_pkgs->pkg_count > 0) {

    if (global_config->download_only == SLAPT_FALSE) {
      if ((int)uncompressed_size < 0) {
        uncompressed_size *= -1;
        printf(gettext("After unpacking %.1f%s disk space will be freed.\n"),
          (uncompressed_size > 1024) ? uncompressed_size / (double)1024
            : uncompressed_size,
          (uncompressed_size > 1024) ? "MB" : "kB"
       );
      } else {
        printf(
          gettext("After unpacking %.1f%s of additional disk space will be used.\n"),
          (uncompressed_size > 1024) ? uncompressed_size / (double)1024
            : uncompressed_size,
          (uncompressed_size > 1024) ? "MB" : "kB"
       );
      }
    }
  }

  /* prompt */
  if ((global_config->prompt == SLAPT_TRUE) ||
      ((tran->upgrade_pkgs->pkg_count > 0 || tran->remove_pkgs->pkg_count > 0 ||
      (tran->install_pkgs->pkg_count > 0 &&
       global_config->dist_upgrade == SLAPT_TRUE)) &&
      (global_config->no_prompt == SLAPT_FALSE &&
       global_config->download_only == SLAPT_FALSE &&
       global_config->simulate == SLAPT_FALSE &&
       global_config->print_uris == SLAPT_FALSE))
  ) {
    if (slapt_ask_yes_no(gettext("Do you want to continue? [y/N] ")) != 1) {
      printf(gettext("Abort.\n"));
      return 1;
    }
  }

  if (global_config->print_uris == SLAPT_TRUE) {
    for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
      const slapt_pkg_info_t *info = tran->install_pkgs->pkgs[i];
      const char *location = info->location + strspn(info->location, "./");
      printf("%s%s/%s-%s%s\n",
             info->mirror, location, info->name,
             info->version, info->file_ext);
    }
    for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
      const slapt_pkg_info_t *info = tran->upgrade_pkgs->pkgs[i]->upgrade;
      const char *location = info->location + strspn(info->location, "./");
      printf("%s%s/%s-%s%s\n",
             info->mirror, location, info->name,
             info->version, info->file_ext);
    }
    return 0;
  }

  /* if simulate is requested, just show what could happen and return */
  if (global_config->simulate == SLAPT_TRUE) {

    for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
      const slapt_pkg_info_t *r = tran->remove_pkgs->pkgs[i];
      printf(gettext("%s-%s is to be removed\n"), r->name, r->version);
    }

    for (i = 0;i < tran->queue->count; ++i) {

      if (tran->queue->pkgs[i]->type == INSTALL) {
        printf(gettext("%s-%s is to be installed\n"),
          tran->queue->pkgs[i]->pkg.i->name,
          tran->queue->pkgs[i]->pkg.i->version
       );
      } else if (tran->queue->pkgs[i]->type == UPGRADE) {
        printf(gettext("%s-%s is to be upgraded to version %s\n"),
          tran->queue->pkgs[i]->pkg.u->upgrade->name,
          tran->queue->pkgs[i]->pkg.u->installed->version,
          tran->queue->pkgs[i]->pkg.u->upgrade->version
       );
      }

    }

    printf(gettext("Done\n"));
    return 0;
  }

  pkg_dl_count = tran->install_pkgs->pkg_count + tran->upgrade_pkgs->pkg_count;

  /* download pkgs */
  for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
    unsigned int retry_count, failed = 1;

    ++dl_counter;
    snprintf(dl_note, SLAPT_PKG_DL_NOTE_LEN, "%d/%d", dl_counter, pkg_dl_count);

    for (retry_count = 0; retry_count < global_config->retry; ++retry_count) {
      const char *err = slapt_download_pkg(global_config,tran->install_pkgs->pkgs[i], dl_note);
      if (err) {
        fprintf(stderr,gettext("Failed to download: %s\n"),err);
        failed = 1;
      } else {
        failed = 0;
        break;
      }
    }

    if (failed == 1)
      exit(EXIT_FAILURE);
  }

  for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
    unsigned int retry_count, failed = 1;

    ++dl_counter;
    snprintf(dl_note, SLAPT_PKG_DL_NOTE_LEN, "%d/%d", dl_counter, pkg_dl_count);

    for (retry_count = 0; retry_count < global_config->retry; ++retry_count) {
      const char *err = slapt_download_pkg(global_config,tran->upgrade_pkgs->pkgs[i]->upgrade, dl_note);
      if (err) {
        fprintf(stderr,gettext("Failed to download: %s\n"),err);
        failed = 1;
      } else {
        failed = 0;
        break;
      }
    }
    if (failed == 1)
      exit(EXIT_FAILURE);
  }

  printf("\n");

  /* run transaction, remove, install, and upgrade */
  if (global_config->download_only == SLAPT_FALSE) {

    for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
      if (slapt_remove_pkg(global_config,tran->remove_pkgs->pkgs[i]) == -1) {
        exit(EXIT_FAILURE);
      }
    }

    for (i = 0;i < tran->queue->count; ++i) {

      if (tran->queue->pkgs[i]->type == INSTALL) {
        printf(gettext("Preparing to install %s-%s\n"),
               tran->queue->pkgs[i]->pkg.i->name,
               tran->queue->pkgs[i]->pkg.i->version);
        if (slapt_install_pkg(global_config,
            tran->queue->pkgs[i]->pkg.i) == -1) {
          exit(EXIT_FAILURE);
        }
      } else if (tran->queue->pkgs[i]->type == UPGRADE) {
        printf(gettext("Preparing to replace %s-%s with %s-%s\n"),
               tran->queue->pkgs[i]->pkg.u->upgrade->name,
               tran->queue->pkgs[i]->pkg.u->installed->version,
               tran->queue->pkgs[i]->pkg.u->upgrade->name,
               tran->queue->pkgs[i]->pkg.u->upgrade->version);
        if (slapt_upgrade_pkg(global_config,
            tran->queue->pkgs[i]->pkg.u->upgrade) == -1) {
          exit(EXIT_FAILURE);
        }
      }

    }

  }

  printf(gettext("Done\n"));

  return 0;
}

void slapt_add_install_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *pkg)
{
  slapt_pkg_info_t **tmp_list;

  /* don't add if already present in the transaction */
  if (slapt_search_transaction_by_pkg(tran,pkg) == 1)
    return;

  tmp_list = realloc(
    tran->install_pkgs->pkgs,
    sizeof *tran->install_pkgs->pkgs * (tran->install_pkgs->pkg_count + 1)
  );

  if (tmp_list != NULL) {
    tran->install_pkgs->pkgs = tmp_list;

    tran->install_pkgs->pkgs[tran->install_pkgs->pkg_count] = slapt_malloc(
      sizeof *tran->install_pkgs->pkgs[tran->install_pkgs->pkg_count]
   );
    tran->install_pkgs->pkgs[tran->install_pkgs->pkg_count] = slapt_copy_pkg(
      tran->install_pkgs->pkgs[tran->install_pkgs->pkg_count],
      pkg
   );
    queue_add_install(
      tran->queue,
      tran->install_pkgs->pkgs[tran->install_pkgs->pkg_count]
   );

    ++tran->install_pkgs->pkg_count;
  }

}

void slapt_add_remove_to_transaction(slapt_transaction_t *tran, 
                                     slapt_pkg_info_t *pkg)
{
  slapt_pkg_info_t **tmp_list;

  /* don't add if already present in the transaction */
  if (slapt_search_transaction_by_pkg(tran,pkg) == 1)
    return;

  tmp_list = realloc(
    tran->remove_pkgs->pkgs,
    sizeof *tran->remove_pkgs->pkgs * (tran->remove_pkgs->pkg_count + 1)
  );
  if (tmp_list != NULL) {
    tran->remove_pkgs->pkgs = tmp_list;

    tran->remove_pkgs->pkgs[tran->remove_pkgs->pkg_count] = slapt_malloc(
      sizeof *tran->remove_pkgs->pkgs[tran->remove_pkgs->pkg_count]
   );
    tran->remove_pkgs->pkgs[tran->remove_pkgs->pkg_count] = slapt_copy_pkg(
      tran->remove_pkgs->pkgs[tran->remove_pkgs->pkg_count],
      pkg
   );
    ++tran->remove_pkgs->pkg_count;
  }

}

void slapt_add_exclude_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *pkg)
{
  slapt_pkg_info_t **tmp_list;

  /* don't add if already present in the transaction */
  if (slapt_search_transaction_by_pkg(tran,pkg) == 1)
    return;

  tmp_list = realloc(
    tran->exclude_pkgs->pkgs,
    sizeof *tran->exclude_pkgs->pkgs * (tran->exclude_pkgs->pkg_count + 1)
 );
  if (tmp_list != NULL) {
    tran->exclude_pkgs->pkgs = tmp_list;

    tran->exclude_pkgs->pkgs[tran->exclude_pkgs->pkg_count] = slapt_malloc(
      sizeof *tran->exclude_pkgs->pkgs[tran->exclude_pkgs->pkg_count]
   );
    tran->exclude_pkgs->pkgs[tran->exclude_pkgs->pkg_count] = slapt_copy_pkg(
      tran->exclude_pkgs->pkgs[tran->exclude_pkgs->pkg_count],
      pkg
   );
    ++tran->exclude_pkgs->pkg_count;
  }

}

void slapt_add_reinstall_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *installed_pkg,
                                      slapt_pkg_info_t *slapt_upgrade_pkg
)
{
  _slapt_add_upgrade_to_transaction(tran, installed_pkg, slapt_upgrade_pkg, SLAPT_TRUE);
}

void slapt_add_upgrade_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *installed_pkg,
                                      slapt_pkg_info_t *slapt_upgrade_pkg
)
{
  _slapt_add_upgrade_to_transaction(tran, installed_pkg, slapt_upgrade_pkg, SLAPT_FALSE);
}


static void _slapt_add_upgrade_to_transaction(slapt_transaction_t *tran,
                                      slapt_pkg_info_t *installed_pkg,
                                      slapt_pkg_info_t *slapt_upgrade_pkg,
                                      SLAPT_BOOL_T reinstall
)
{
  slapt_pkg_upgrade_t **tmp_list;

  /* don't add if already present in the transaction */
  if (slapt_search_transaction_by_pkg(tran,slapt_upgrade_pkg) == 1)
    return;

  tmp_list = realloc(
    tran->upgrade_pkgs->pkgs,
    sizeof *tran->upgrade_pkgs->pkgs * (tran->upgrade_pkgs->pkg_count + 1)
  );

  if (tmp_list != NULL) {
    tran->upgrade_pkgs->pkgs = tmp_list;

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count] = slapt_malloc(
      sizeof *tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]);

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->installed =
      slapt_malloc(
        sizeof *tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->installed
     );

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade =
      slapt_malloc(
       sizeof *tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade
     );

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->installed = slapt_copy_pkg(
      tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->installed,
      installed_pkg);

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade = slapt_copy_pkg(
      tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade,
      slapt_upgrade_pkg);

    tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->reinstall = reinstall;

    queue_add_upgrade(tran->queue,
      tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]);

    ++tran->upgrade_pkgs->pkg_count;

    if (reinstall == SLAPT_TRUE)
      ++tran->upgrade_pkgs->reinstall_count;

  }

}

int slapt_search_transaction(slapt_transaction_t *tran,char *pkg_name)
{
  unsigned int i,found = 1, not_found = 0;

  for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {

    if (strcmp(pkg_name,tran->install_pkgs->pkgs[i]->name) == 0)
      return found;

  }

  for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {

    if (strcmp(pkg_name,tran->upgrade_pkgs->pkgs[i]->upgrade->name) == 0)
      return found;

  }

  for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {

    if (strcmp(pkg_name,tran->remove_pkgs->pkgs[i]->name) == 0)
      return found;

  }

  for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {

    if (strcmp(pkg_name,tran->exclude_pkgs->pkgs[i]->name) == 0)
      return found;

  }

  return not_found;
}

int slapt_search_upgrade_transaction(slapt_transaction_t *tran,
                                     slapt_pkg_info_t *pkg)
{
  unsigned int i,
               found = 1,
               not_found = 0;

  for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {

    if (strcmp(pkg->name,tran->upgrade_pkgs->pkgs[i]->upgrade->name) == 0)
      return found;

  }

  return not_found;
}

void slapt_free_transaction(slapt_transaction_t *tran)
{
  unsigned int i;

  if (tran->install_pkgs->free_pkgs == SLAPT_TRUE) {
    for (i = 0;i < tran->install_pkgs->pkg_count; ++i) {
      slapt_free_pkg(tran->install_pkgs->pkgs[i]);
    }
  }
  free(tran->install_pkgs->pkgs);
  free(tran->install_pkgs);

  if (tran->remove_pkgs->free_pkgs == SLAPT_TRUE) {
    for (i = 0;i < tran->remove_pkgs->pkg_count; ++i) {
      slapt_free_pkg(tran->remove_pkgs->pkgs[i]);
    }
  }
  free(tran->remove_pkgs->pkgs);
  free(tran->remove_pkgs);

  for (i = 0;i < tran->upgrade_pkgs->pkg_count; ++i) {
    slapt_free_pkg(tran->upgrade_pkgs->pkgs[i]->upgrade);
    slapt_free_pkg(tran->upgrade_pkgs->pkgs[i]->installed);
    free(tran->upgrade_pkgs->pkgs[i]);
  }
  free(tran->upgrade_pkgs->pkgs);
  free(tran->upgrade_pkgs);

  if (tran->exclude_pkgs->free_pkgs == SLAPT_TRUE) {
    for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
      slapt_free_pkg(tran->exclude_pkgs->pkgs[i]);
    }
  }
  free(tran->exclude_pkgs->pkgs);
  free(tran->exclude_pkgs);

  slapt_free_list(tran->suggests);

  queue_free(tran->queue);

  slapt_free_pkg_err_list(tran->conflict_err);
  slapt_free_pkg_err_list(tran->missing_err);

  free(tran);
}

slapt_transaction_t *slapt_remove_from_transaction(slapt_transaction_t *tran,
                                                   slapt_pkg_info_t *pkg)
{
  unsigned int i;
  slapt_transaction_t *new_tran = NULL;

  if (slapt_search_transaction_by_pkg(tran,pkg) == 0)
    return tran;

  /* since this is a pointer, slapt_malloc before calling init */
  new_tran = slapt_malloc(sizeof *new_tran);
  new_tran->install_pkgs = slapt_malloc(sizeof *new_tran->install_pkgs);
  new_tran->remove_pkgs = slapt_malloc(sizeof *new_tran->remove_pkgs);
  new_tran->upgrade_pkgs = slapt_malloc(sizeof *new_tran->upgrade_pkgs);
  new_tran->exclude_pkgs = slapt_malloc(sizeof *new_tran->exclude_pkgs);
  new_tran = slapt_init_transaction();

  for (i = 0;i < tran->install_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];

    if (
      strcmp(pkg->name,p->name) == 0 &&
      strcmp(pkg->version,p->version) == 0 &&
      strcmp(pkg->location,p->location) == 0
   ) {
      continue;
    }

    slapt_add_install_to_transaction(new_tran,p);
  }

  for (i = 0;i < tran->remove_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->remove_pkgs->pkgs[i];

    if (
      strcmp(pkg->name,p->name) == 0 &&
      strcmp(pkg->version,p->version) == 0 &&
      strcmp(pkg->location,p->location) == 0
   ) {
      continue;
    }

    slapt_add_remove_to_transaction(new_tran,tran->remove_pkgs->pkgs[i]);
  }

  for (i = 0;i < tran->upgrade_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
    slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->installed;

    if (
      strcmp(pkg->name,u->name) == 0 &&
      strcmp(pkg->version,u->version) == 0 &&
      strcmp(pkg->location,u->location) == 0
   ) {
      continue;
    }

    slapt_add_upgrade_to_transaction(new_tran, p, u);
  }

  for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->exclude_pkgs->pkgs[i];

    if (
      strcmp(pkg->name,p->name) == 0 &&
      strcmp(pkg->version,p->version) == 0 &&
      strcmp(pkg->location,p->location) == 0
   ) {
      continue;
    }

    slapt_add_exclude_to_transaction(new_tran,p);
  }

  return new_tran;
}

/* parse the dependencies for a package, and add them to the transaction as */
/* needed check to see if a package is conflicted */
int slapt_add_deps_to_trans(const slapt_rc_config *global_config,
                            slapt_transaction_t *tran,
                            slapt_pkg_list_t *avail_pkgs,
                            slapt_pkg_list_t *installed_pkgs,
                            slapt_pkg_info_t *pkg)
{
  unsigned int c;
  int dep_return = -1;
  slapt_pkg_list_t *deps = NULL;

  if (global_config->disable_dep_check == SLAPT_TRUE)
    return 0;

  if (pkg == NULL)
    return 0;

  deps = slapt_init_pkg_list();

  dep_return = slapt_get_pkg_dependencies(
    global_config,avail_pkgs,installed_pkgs,pkg,
    deps,tran->conflict_err,tran->missing_err
 );

  /* check to see if there where issues with dep checking */
  /* exclude the package if dep check barfed */
  if ((dep_return == -1) && (global_config->ignore_dep == SLAPT_FALSE) &&
      (slapt_get_exact_pkg(tran->exclude_pkgs,pkg->name,pkg->version) == NULL)
 ) {
    slapt_free_pkg_list(deps);
    return -1;
  }

  /* loop through the deps */
  for (c = 0; c < deps->pkg_count; ++c) {
    unsigned int cindex = 0;
    slapt_pkg_info_t *dep = deps->pkgs[c];
    slapt_pkg_info_t *dep_installed  = NULL;
    slapt_pkg_list_t *conflicts = NULL;

    /*
     * the dep wouldn't get this far if it where excluded,
     * so we don't check for that here
     */

    conflicts =
      slapt_is_conflicted(tran,avail_pkgs,installed_pkgs,dep);

    for (cindex = 0; cindex < conflicts->pkg_count;cindex++) {
      slapt_add_remove_to_transaction(tran,conflicts->pkgs[cindex]);
    }

    slapt_free_pkg_list(conflicts);

    dep_installed = slapt_get_newest_pkg(installed_pkgs,dep->name);
    if (dep_installed == NULL) {
      slapt_add_install_to_transaction(tran,dep);
    } else {
      /* add only if its a valid upgrade */
      if (slapt_cmp_pkgs(dep_installed,dep) < 0)
        slapt_add_upgrade_to_transaction(tran,dep_installed,dep);
    }

  }

  slapt_free_pkg_list(deps);

  return 0;
}

/* make sure pkg isn't conflicted with what's already in the transaction */
slapt_pkg_list_t *slapt_is_conflicted(slapt_transaction_t *tran,
                                      slapt_pkg_list_t *avail_pkgs,
                                      slapt_pkg_list_t *installed_pkgs,
                                      slapt_pkg_info_t *pkg)
{
  unsigned int i;
  slapt_pkg_list_t *conflicts = NULL;
  slapt_pkg_list_t *conflicts_in_transaction = slapt_init_pkg_list();

  /* if conflicts exist, check to see if they are installed
     or in the current transaction
  */
  conflicts = slapt_get_pkg_conflicts(avail_pkgs,installed_pkgs,pkg);
  for (i = 0; i < conflicts->pkg_count; ++i) {
    slapt_pkg_info_t *p = conflicts->pkgs[i];

    if ( slapt_search_upgrade_transaction(tran,p) == 1
        || slapt_get_newest_pkg(tran->install_pkgs,p->name) != NULL
    ) {
      printf(gettext("%s, which is to be installed, conflicts with %s\n"), p->name,pkg->name);

      slapt_add_pkg_to_pkg_list(conflicts_in_transaction, p);
    }
    if (slapt_get_newest_pkg(installed_pkgs,p->name) != NULL) {
      printf(gettext("Installed %s conflicts with %s\n"), p->name,pkg->name);

      slapt_add_pkg_to_pkg_list(conflicts_in_transaction, p);
    }
  }

  slapt_free_pkg_list(conflicts);

  return conflicts_in_transaction;
}

static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg)
{
  slapt_list_t *suggests = NULL;
  unsigned int i = 0;

  if (pkg->suggests == NULL || strlen(pkg->suggests) == 0) {
    return;
  }

  suggests = slapt_parse_delimited_list(pkg->suggests, ',');

  for (i = 0; i < suggests->count; i++) {

    /* no need to add it if we already have it */
    if (slapt_search_transaction(tran,suggests->items[i]) == 1)
      continue;

    slapt_add_list_item(tran->suggests, suggests->items[i]);

  }

  slapt_free_list(suggests);
}

int slapt_search_transaction_by_pkg(slapt_transaction_t *tran,
                                    slapt_pkg_info_t *pkg)
{
  unsigned int i,found = 1, not_found = 0;

  for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];

    if ((strcmp(pkg->name,p->name) == 0)
    && (strcmp(pkg->version,p->version) == 0))
      return found;

  }

  for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->upgrade;

    if ((strcmp(pkg->name,p->name) == 0)
    && (strcmp(pkg->version,p->version) == 0))
      return found;

  }

  for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->remove_pkgs->pkgs[i];

    if ((strcmp(pkg->name,p->name) == 0)
    && (strcmp(pkg->version,p->version) == 0))
      return found;

  }

  for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
    slapt_pkg_info_t *p = tran->exclude_pkgs->pkgs[i];

    if ((strcmp(pkg->name,p->name) == 0)
    && (strcmp(pkg->version,p->version) == 0))
      return found;

  }

  return not_found;
}

static slapt_queue_t *slapt_queue_init(void)
{
  slapt_queue_t *t = NULL;
  t = slapt_malloc(sizeof *t);
  t->pkgs = slapt_malloc(sizeof *t->pkgs);
  t->count = 0;

  return t;
}

static void queue_add_install(slapt_queue_t *t, slapt_pkg_info_t *p)
{
  slapt_queue_i **tmp;
  tmp = realloc(t->pkgs, sizeof *t->pkgs * (t->count + 1));

  if (!tmp)
    return;

  t->pkgs = tmp;
  t->pkgs[t->count] = slapt_malloc(sizeof *t->pkgs[t->count]);
  t->pkgs[t->count]->pkg.i = p;
  t->pkgs[t->count]->type = INSTALL;
  ++t->count;
}

static void queue_add_upgrade(slapt_queue_t *t, slapt_pkg_upgrade_t *p)
{
  slapt_queue_i **tmp;
  tmp = realloc(t->pkgs, sizeof *t->pkgs * (t->count + 1));

  if (!tmp)
    return;

  t->pkgs = tmp;
  t->pkgs[t->count] = slapt_malloc(sizeof *t->pkgs[t->count]);
  t->pkgs[t->count]->pkg.u = p;
  t->pkgs[t->count]->type = UPGRADE;
  ++t->count;
}

static void queue_free(slapt_queue_t *t)
{
  unsigned int i;
  for (i = 0; i < t->count; ++i) {
    free(t->pkgs[i]);
  }
  free(t->pkgs);
  free(t);
}

void slapt_generate_suggestions(slapt_transaction_t *tran)
{
  unsigned int i;
  for (i = 0;i < tran->install_pkgs->pkg_count; ++i) {
    add_suggestion(tran,tran->install_pkgs->pkgs[i]);
  }
}