summaryrefslogtreecommitdiffstats
path: root/src/transaction.c
blob: 0a7b92a5c83f992f151aff0ca71bff00c4f32e05 (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
/*
 * Copyright (C) 2003,2004,2005 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>

static queue_t *queue_init(void);
static void queue_add_install(queue_t *t, pkg_info_t *p);
static void queue_add_upgrade(queue_t *t, pkg_upgrade_t *p);
static void queue_free(queue_t *t);

static void add_suggestion(transaction_t *tran, pkg_info_t *pkg);
static int disk_space(const rc_config *global_config,int space_needed );

void init_transaction(transaction_t *tran){

	tran->install_pkgs = init_pkg_list();
	tran->install_pkgs->free_pkgs = TRUE;

	tran->remove_pkgs = init_pkg_list();
	tran->remove_pkgs->free_pkgs = TRUE;

	tran->exclude_pkgs = init_pkg_list();
	tran->exclude_pkgs->free_pkgs = 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->suggests = slapt_malloc(sizeof *tran->suggests );
	tran->suggests->pkgs = slapt_malloc(sizeof *tran->suggests->pkgs);
	tran->suggests->count = 0;

	tran->queue = queue_init();

}

int handle_transaction(const rc_config *global_config, transaction_t *tran){
	unsigned int i;
	size_t download_size = 0;
	size_t already_download_size = 0;
	size_t uncompressed_size = 0;

	/* show pkgs to exclude */
	if( tran->exclude_pkgs->pkg_count > 0 ){
		unsigned int len = 0;
		printf(_("The following packages have been EXCLUDED:\n"));
		printf("  ");
		for(i = 0; i < tran->exclude_pkgs->pkg_count;i++){
			if( len + strlen(tran->exclude_pkgs->pkgs[i]->name) + 1 < MAX_LINE_LEN ){
				printf("%s ",tran->exclude_pkgs->pkgs[i]->name);
				len += strlen(tran->exclude_pkgs->pkgs[i]->name) + 1;
			}else{
				printf("\n  %s ",tran->exclude_pkgs->pkgs[i]->name);
				len = strlen(tran->exclude_pkgs->pkgs[i]->name) + 3;
			}
		}
		printf("\n");
	}

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

		printf(_("Suggested packages:\n"));
		printf("  ");
		for(i = 0; i < tran->suggests->count; ++i){
			/* don't show suggestion for something we already have in the transaction */
			if( search_transaction(tran,tran->suggests->pkgs[i]) == 1 ) continue;

			if( len + strlen(tran->suggests->pkgs[i]) + 1 < MAX_LINE_LEN ){
				printf("%s ",tran->suggests->pkgs[i]);
				len += strlen(tran->suggests->pkgs[i]) + 1;
			}else{
				printf("\n  %s ",tran->suggests->pkgs[i]);
				len = strlen(tran->suggests->pkgs[i]) + 3;
			}
		}
		printf("\n");

	}

	/* show pkgs to install */
	if( tran->install_pkgs->pkg_count > 0 ){
		unsigned int len = 0;
		printf(_("The following NEW packages will be installed:\n"));
		printf("  ");
		for(i = 0; i < tran->install_pkgs->pkg_count;i++){
			if( len + strlen(tran->install_pkgs->pkgs[i]->name) + 1 < MAX_LINE_LEN ){
				printf("%s ",tran->install_pkgs->pkgs[i]->name);
				len += strlen(tran->install_pkgs->pkgs[i]->name) + 1;
			}else{
				printf("\n  %s ",tran->install_pkgs->pkgs[i]->name);
				len = strlen(tran->install_pkgs->pkgs[i]->name) + 3;
			}
			already_download_size += get_pkg_file_size(
				global_config,tran->install_pkgs->pkgs[i]
			) / 1024;
			download_size += tran->install_pkgs->pkgs[i]->size_c;
			uncompressed_size += tran->install_pkgs->pkgs[i]->size_u;
		}
		printf("\n");
	}

	/* show pkgs to remove */
	if( tran->remove_pkgs->pkg_count > 0 ){
		unsigned int len = 0;
		printf(_("The following packages will be REMOVED:\n"));
		printf("  ");
		for(i = 0; i < tran->remove_pkgs->pkg_count;i++){
			if( len + strlen(tran->remove_pkgs->pkgs[i]->name) + 1 < MAX_LINE_LEN ){
				printf("%s ",tran->remove_pkgs->pkgs[i]->name);
				len += strlen(tran->remove_pkgs->pkgs[i]->name) + 1;
			}else{
				printf("\n  %s ",tran->remove_pkgs->pkgs[i]->name);
				len = strlen(tran->remove_pkgs->pkgs[i]->name) + 3;
			}
			uncompressed_size -= tran->remove_pkgs->pkgs[i]->size_u;
		}
		printf("\n");
	}

	/* show pkgs to upgrade */
	if( tran->upgrade_pkgs->pkg_count > 0 ){
		unsigned int len = 0;
		printf(_("The following packages will be upgraded:\n"));
		printf("  ");
		for(i = 0; i < tran->upgrade_pkgs->pkg_count;i++){
			if(len+strlen(tran->upgrade_pkgs->pkgs[i]->upgrade->name)+1<MAX_LINE_LEN){
				printf("%s ",tran->upgrade_pkgs->pkgs[i]->upgrade->name);
				len += strlen(tran->upgrade_pkgs->pkgs[i]->upgrade->name) + 1;
			}else{
				printf("\n  %s ",tran->upgrade_pkgs->pkgs[i]->upgrade->name);
				len = strlen(tran->upgrade_pkgs->pkgs[i]->upgrade->name) + 3;
			}
			already_download_size += get_pkg_file_size(
				global_config,tran->upgrade_pkgs->pkgs[i]->upgrade
			) / 1024;
			download_size += tran->upgrade_pkgs->pkgs[i]->upgrade->size_c;
			uncompressed_size += tran->upgrade_pkgs->pkgs[i]->upgrade->size_u;
			uncompressed_size -= tran->upgrade_pkgs->pkgs[i]->installed->size_u;
		}
		printf("\n");
	}

	/* print the summary */
	printf(
		_("%d upgraded, %d newly installed, %d to remove and %d not upgraded.\n"),
		tran->upgrade_pkgs->pkg_count,
		tran->install_pkgs->pkg_count,
		tran->remove_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 */
		int 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(disk_space(global_config,need_to_download_size+uncompressed_size) != 0){
			printf(
				_("You don't have enough free space in %s\n"),
				global_config->working_dir
			);
			return 1;
		}

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

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

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

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

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

	/* if simulate is requested, just show what could happen and return */
	if( global_config->simulate == TRUE ){
		for(i = 0; i < tran->remove_pkgs->pkg_count;i++){
			printf(_("%s-%s is to be removed\n"),
				tran->remove_pkgs->pkgs[i]->name,tran->remove_pkgs->pkgs[i]->version
			);
		}
		for(i = 0;i < tran->queue->count; ++i){
			if( tran->queue->pkgs[i]->type == INSTALL ){
				printf(_("%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(_("%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(_("Done\n"));
		return 0;
	}

	/* download pkgs */
	for(i = 0; i < tran->install_pkgs->pkg_count;i++)
		if( download_pkg(global_config,tran->install_pkgs->pkgs[i]) != 0 ) exit(1);
	for(i = 0; i < tran->upgrade_pkgs->pkg_count;i++)
		if( download_pkg(global_config,tran->upgrade_pkgs->pkgs[i]->upgrade) != 0 ) exit(1);

	printf("\n");

	/* run transaction, remove, install, and upgrade */
	if( global_config->download_only == FALSE ){
		for(i = 0; i < tran->remove_pkgs->pkg_count;i++){
			if( remove_pkg(global_config,tran->remove_pkgs->pkgs[i]) == -1 ) exit(1);
		}
		for(i = 0;i < tran->queue->count; ++i){
			if( tran->queue->pkgs[i]->type == INSTALL ){
				if( install_pkg(global_config,tran->queue->pkgs[i]->pkg.i) == -1 ) exit(1);
			}else if( tran->queue->pkgs[i]->type == UPGRADE ){
				if( upgrade_pkg( global_config,
					tran->queue->pkgs[i]->pkg.u->installed,
					tran->queue->pkgs[i]->pkg.u->upgrade
				) == -1 ) exit(1);
			}
		}
	}

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

	return 0;
}

void add_install_to_transaction(transaction_t *tran,pkg_info_t *pkg){
	pkg_info_t **tmp_list;

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

	#if DEBUG == 1
	printf("adding install of %s-%s@%s to transaction\n",
		pkg->name,pkg->version,pkg->location);
	#endif

	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] = 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 add_remove_to_transaction(transaction_t *tran,pkg_info_t *pkg){
	pkg_info_t **tmp_list;

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

	#if DEBUG == 1
	printf("adding remove of %s-%s@%s to transaction\n",
		pkg->name,pkg->version,pkg->location);
	#endif

	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] = copy_pkg(
			tran->remove_pkgs->pkgs[tran->remove_pkgs->pkg_count],
			pkg
		);
		++tran->remove_pkgs->pkg_count;
	}

}

void add_exclude_to_transaction(transaction_t *tran,pkg_info_t *pkg){
	pkg_info_t **tmp_list;

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

	#if DEBUG == 1
	printf("adding exclude of %s-%s@%s to transaction\n",
		pkg->name,pkg->version,pkg->location);
	#endif

	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] = copy_pkg(
			tran->exclude_pkgs->pkgs[tran->exclude_pkgs->pkg_count],
			pkg
		);
		++tran->exclude_pkgs->pkg_count;
	}

}

void add_upgrade_to_transaction(
	transaction_t *tran, pkg_info_t *installed_pkg, pkg_info_t *upgrade_pkg
){
	pkg_upgrade_t **tmp_list;

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

	#if DEBUG == 1
	printf("adding upgrade of %s-%s@%s to transaction\n",
		upgrade_pkg->name,upgrade_pkg->version,upgrade_pkg->location);
	#endif

	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 = copy_pkg(
			tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->installed,
			installed_pkg
		);
		tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade = copy_pkg(
			tran->upgrade_pkgs->pkgs[tran->upgrade_pkgs->pkg_count]->upgrade,
			upgrade_pkg
		);

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

		++tran->upgrade_pkgs->pkg_count;

	}

}

int search_transaction(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;
}

static int search_upgrade_transaction(transaction_t *tran,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 free_transaction(transaction_t *tran){
	unsigned int i;

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

	if( tran->remove_pkgs->free_pkgs == TRUE ){
		for(i = 0;i < tran->remove_pkgs->pkg_count; i++){
			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++){
		free_pkg(tran->upgrade_pkgs->pkgs[i]->upgrade);
		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 == TRUE ){
		for(i = 0; i < tran->exclude_pkgs->pkg_count;i++){
			free_pkg(tran->exclude_pkgs->pkgs[i]);
		}
	}
	free(tran->exclude_pkgs->pkgs);
	free(tran->exclude_pkgs);

	for(i = 0; i < tran->suggests->count; ++i){
		free(tran->suggests->pkgs[i]);
	}
	free(tran->suggests->pkgs);
	free(tran->suggests);

	queue_free(tran->queue);

}

transaction_t *remove_from_transaction(transaction_t *tran,pkg_info_t *pkg){
	unsigned int i;
	transaction_t *new_tran = NULL;

	if( 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 );
	init_transaction(new_tran);

	for(i = 0;i < tran->install_pkgs->pkg_count; i++){
		if(
			strcmp(pkg->name,tran->install_pkgs->pkgs[i]->name) != 0 &&
			strcmp(pkg->version,tran->install_pkgs->pkgs[i]->version) != 0 &&
			strcmp(pkg->location,tran->install_pkgs->pkgs[i]->location) != 0
		) add_install_to_transaction(new_tran,tran->install_pkgs->pkgs[i]);
	}
	for(i = 0;i < tran->remove_pkgs->pkg_count; i++){
		if(
			strcmp(pkg->name,tran->remove_pkgs->pkgs[i]->name) != 0 &&
			strcmp(pkg->version,tran->remove_pkgs->pkgs[i]->version) != 0 &&
			strcmp(pkg->location,tran->remove_pkgs->pkgs[i]->location) != 0
		) add_remove_to_transaction(new_tran,tran->remove_pkgs->pkgs[i]);
	}
	for(i = 0;i < tran->upgrade_pkgs->pkg_count; i++){
		if(
			strcmp(pkg->name,tran->upgrade_pkgs->pkgs[i]->upgrade->name) != 0 &&
			strcmp(pkg->version,tran->upgrade_pkgs->pkgs[i]->upgrade->version) != 0 &&
			strcmp(pkg->location,tran->upgrade_pkgs->pkgs[i]->upgrade->location) != 0
		) add_upgrade_to_transaction(
			new_tran,
			tran->upgrade_pkgs->pkgs[i]->installed,
			tran->upgrade_pkgs->pkgs[i]->upgrade
		);
	}
	for(i = 0; i < tran->exclude_pkgs->pkg_count;i++){
		if(
			strcmp(pkg->name,tran->exclude_pkgs->pkgs[i]->name) != 0 &&
			strcmp(pkg->version,tran->exclude_pkgs->pkgs[i]->version) != 0 &&
			strcmp(pkg->location,tran->exclude_pkgs->pkgs[i]->location) != 0
		) add_exclude_to_transaction(new_tran,tran->exclude_pkgs->pkgs[i]);
	}

	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 add_deps_to_trans(const rc_config *global_config, transaction_t *tran, struct pkg_list *avail_pkgs, struct pkg_list *installed_pkgs, pkg_info_t *pkg){
	unsigned int c;
	int dep_return = -1;
	struct pkg_list *deps;

	if( global_config->disable_dep_check == TRUE ) return 0;
	if( pkg == NULL ) return 0;

	deps = init_pkg_list();

	dep_return = get_pkg_dependencies(global_config,avail_pkgs,installed_pkgs,pkg,deps);

	/* 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 == FALSE) ){
		printf("Excluding %s, use --ignore-dep to override\n",pkg->name);
		add_exclude_to_transaction(tran,pkg);
		free_pkg_list(deps);
		return -1;
	}

	/* loop through the deps */
	for(c = 0; c < deps->pkg_count;c++){
		pkg_info_t *dep_installed;
		pkg_info_t *conflicted_pkg = NULL;

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

		if ( (conflicted_pkg = is_conflicted(tran,avail_pkgs,installed_pkgs,deps->pkgs[c])) != NULL ){
			add_remove_to_transaction(tran,conflicted_pkg);
		}

		if( (dep_installed = get_newest_pkg(installed_pkgs,deps->pkgs[c]->name)) == NULL ){
			add_install_to_transaction(tran,deps->pkgs[c]);
		}else{
			/* add only if its a valid upgrade */
			if(cmp_pkg_versions(dep_installed->version,deps->pkgs[c]->version) < 0 )
				add_upgrade_to_transaction(tran,dep_installed,deps->pkgs[c]);
		}

	}

	free_pkg_list(deps);

	return 0;
}

/* make sure pkg isn't conflicted with what's already in the transaction */
pkg_info_t *is_conflicted(transaction_t *tran, struct pkg_list *avail_pkgs, struct pkg_list *installed_pkgs, pkg_info_t *pkg){
	unsigned int i;
	struct pkg_list *conflicts;

	/* if conflicts exist, check to see if they are installed or in the current transaction */
	conflicts = get_pkg_conflicts(avail_pkgs,installed_pkgs,pkg);
	for(i = 0; i < conflicts->pkg_count; i++){
		if(
			search_upgrade_transaction(tran,conflicts->pkgs[i]) == 1
			|| get_newest_pkg(tran->install_pkgs,conflicts->pkgs[i]->name) != NULL
		){
			pkg_info_t *c = conflicts->pkgs[i];
			printf(_("%s, which is to be installed, conflicts with %s\n"),
				conflicts->pkgs[i]->name,conflicts->pkgs[i]->version, pkg->name,pkg->version
			);
			free_pkg_list(conflicts);
			return c;
		}
		if(get_newest_pkg(installed_pkgs,conflicts->pkgs[i]->name) != NULL) {
			pkg_info_t *c = conflicts->pkgs[i];
			printf(_("Installed %s conflicts with %s\n"),conflicts->pkgs[i]->name,pkg->name);
			free_pkg_list(conflicts);
			return c;
		}
	}

	free_pkg_list(conflicts);

	return NULL;
}

static void add_suggestion(transaction_t *tran, pkg_info_t *pkg){
	unsigned int position = 0,len = 0;

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

	while( position < len ){
		int total_len = 0,rest_len = 0;
		char *p = NULL,*si = NULL,*tmp_suggests = NULL;
		char **tmp_realloc;

		p = pkg->suggests + position;
		if( p == NULL ) break;

		si = strpbrk(p," ,");
		if( si == NULL || strlen(si) <= 2 ){
			total_len = strlen(p);
			rest_len = 0;
			tmp_suggests = strndup(p,strlen(p));
		}else{
			si = si + 1;

			total_len = strlen(p);
			rest_len = strlen(si);

			/* this will always encompass ending space, so we dont + 1 */
			tmp_suggests = slapt_malloc(sizeof *tmp_suggests * (total_len - rest_len) );
			tmp_suggests = strncpy(tmp_suggests,p,(total_len - rest_len));
			tmp_suggests[total_len - rest_len - 1] = '\0';
		}

		/* no need to add it if we already have it */
		if( search_transaction(tran,tmp_suggests) == 1 ){
			free(tmp_suggests);
			position += (total_len - rest_len);
			continue;
		}

		tmp_realloc = realloc(tran->suggests->pkgs,sizeof *tran->suggests->pkgs * (tran->suggests->count + 1));
		if( tmp_realloc != NULL ){
			tran->suggests->pkgs = tmp_realloc;
			tran->suggests->pkgs[tran->suggests->count] = slapt_malloc(
				sizeof *tran->suggests->pkgs[tran->suggests->count]
				* (strlen(tmp_suggests) + 1)
			);
			tran->suggests->pkgs[tran->suggests->count] = strncpy(
				tran->suggests->pkgs[tran->suggests->count],
				tmp_suggests,strlen(tmp_suggests)
			);
			tran->suggests->pkgs[tran->suggests->count][strlen(tmp_suggests)] = '\0';
			++tran->suggests->count;
		}
		free(tmp_suggests);

		position += (total_len - rest_len);
	}

}

static int disk_space(const rc_config *global_config,int space_needed ){
	struct statvfs statvfs_buf;

	space_needed *= 1024;

	if( space_needed < 0 ) return 0;

	if( statvfs(global_config->working_dir,&statvfs_buf) != 0 ){
		if( errno ) perror("statvfs");
    return 1;
	}else{
		if( statvfs_buf.f_bfree < ( space_needed / statvfs_buf.f_bsize) )
			return 1;
	}

	return 0;
}

int search_transaction_by_pkg(transaction_t *tran,pkg_info_t *pkg){
	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 )
		&& (strcmp(pkg->version,tran->install_pkgs->pkgs[i]->version)==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 )
		&& (strcmp(pkg->version,tran->upgrade_pkgs->pkgs[i]->upgrade->version)==0 ))
			return found;
	}
	for(i = 0; i < tran->remove_pkgs->pkg_count;i++){
		if(( strcmp(pkg->name,tran->remove_pkgs->pkgs[i]->name)==0 )
		&& (strcmp(pkg->version,tran->remove_pkgs->pkgs[i]->version)==0 ))
			return found;
	}
	for(i = 0; i < tran->exclude_pkgs->pkg_count;i++){
		if(( strcmp(pkg->name,tran->exclude_pkgs->pkgs[i]->name)==0 )
		&& (strcmp(pkg->version,tran->exclude_pkgs->pkgs[i]->version)==0 ))
			return found;
	}
	return not_found;
}

static queue_t *queue_init(void){
	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(queue_t *t, pkg_info_t *p){
	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(queue_t *t, pkg_upgrade_t *p){
	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(queue_t *t){
	unsigned int i;
	for(i = 0; i < t->count; ++i){
		free(t->pkgs[i]);
	}
	free(t->pkgs);
	free(t);
}

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