summaryrefslogtreecommitdiffstats
path: root/libopkg/pkg.c
blob: e36a38133c688165f71a2b19d9045637c46c24e6 (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
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
/* pkg.c - the opkg package management system

   Carl D. Worth

   Copyright (C) 2001 University of Southern California

   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, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include "includes.h"
#include <ctype.h>
#include <alloca.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>

#include "pkg.h"

#include "pkg_parse.h"
#include "pkg_extract.h"
#include "opkg_message.h"
#include "opkg_utils.h"

#include "sprintf_alloc.h"
#include "file_util.h"
#include "str_util.h"
#include "xsystem.h"
#include "opkg_conf.h"

typedef struct enum_map enum_map_t;
struct enum_map
{
     int value;
     char *str;
};

static const enum_map_t pkg_state_want_map[] = {
     { SW_UNKNOWN, "unknown"},
     { SW_INSTALL, "install"},
     { SW_DEINSTALL, "deinstall"},
     { SW_PURGE, "purge"}
};

static const enum_map_t pkg_state_flag_map[] = {
     { SF_OK, "ok"},
     { SF_REINSTREQ, "reinstreq"},
     { SF_HOLD, "hold"},
     { SF_REPLACE, "replace"},
     { SF_NOPRUNE, "noprune"},
     { SF_PREFER, "prefer"},
     { SF_OBSOLETE, "obsolete"},
     { SF_USER, "user"},
};

static const enum_map_t pkg_state_status_map[] = {
     { SS_NOT_INSTALLED, "not-installed" },
     { SS_UNPACKED, "unpacked" },
     { SS_HALF_CONFIGURED, "half-configured" },
     { SS_INSTALLED, "installed" },
     { SS_HALF_INSTALLED, "half-installed" },
     { SS_CONFIG_FILES, "config-files" },
     { SS_POST_INST_FAILED, "post-inst-failed" },
     { SS_REMOVAL_FAILED, "removal-failed" }
};

static int verrevcmp(const char *val, const char *ref);


pkg_t *pkg_new(void)
{
     pkg_t *pkg;

     pkg = calloc(1, sizeof(pkg_t));
     if (pkg == NULL) {
	  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
	  return NULL;
     }

     pkg_init(pkg);

     return pkg;
}

int pkg_init(pkg_t *pkg)
{
     pkg->name = NULL;
     pkg->epoch = 0;
     pkg->version = NULL;
     pkg->revision = NULL;
     pkg->dest = NULL;
     pkg->src = NULL;
     pkg->architecture = NULL;
     pkg->maintainer = NULL;
     pkg->section = NULL;
     pkg->description = NULL;
     pkg->state_want = SW_UNKNOWN;
     pkg->state_flag = SF_OK;
     pkg->state_status = SS_NOT_INSTALLED;
     pkg->depends_str = NULL;
     pkg->provides_str = NULL;
     pkg->depends_count = 0;
     pkg->depends = NULL;
     pkg->suggests_str = NULL;
     pkg->recommends_str = NULL;
     pkg->suggests_count = 0;
     pkg->recommends_count = 0;
     
     active_list_init(&pkg->list);

     /* Abhaya: added init for conflicts fields */
     pkg->conflicts = NULL;
     pkg->conflicts_count = 0;

     /* added for replaces.  Jamey 7/23/2002 */
     pkg->replaces = NULL;
     pkg->replaces_count = 0;
    
     pkg->pre_depends_count = 0;
     pkg->pre_depends_str = NULL;
     pkg->provides_count = 0;
     pkg->provides = NULL;
     pkg->filename = NULL;
     pkg->local_filename = NULL;
     pkg->tmp_unpack_dir = NULL;
     pkg->md5sum = NULL;
#if defined HAVE_SHA256
     pkg->sha256sum = NULL;
#endif
     pkg->size = NULL;
     pkg->installed_size = NULL;
     pkg->priority = NULL;
     pkg->source = NULL;
     conffile_list_init(&pkg->conffiles);
     pkg->installed_files = NULL;
     pkg->installed_files_ref_cnt = 0;
     pkg->essential = 0;
     pkg->provided_by_hand = 0;

     return 0;
}

void compound_depend_deinit (compound_depend_t *depends)
{
    int i;
    for (i = 0; i < depends->possibility_count; i++)
    {
        depend_t *d;
        d = depends->possibilities[i];
        free (d->version);
        free (d);
    }
    free (depends->possibilities);
}

void pkg_deinit(pkg_t *pkg)
{
     int i;

     free(pkg->name);
     pkg->name = NULL;
     pkg->epoch = 0;
     free(pkg->version);
     pkg->version = NULL;
     /* revision shares storage with version, so
	don't free */
     pkg->revision = NULL;
     /* owned by opkg_conf_t */
     pkg->dest = NULL;
     /* owned by opkg_conf_t */
     pkg->src = NULL;
     free(pkg->architecture);
     pkg->architecture = NULL;
     free(pkg->maintainer);
     pkg->maintainer = NULL;
     free(pkg->section);
     pkg->section = NULL;
     free(pkg->description);
     pkg->description = NULL;
     pkg->state_want = SW_UNKNOWN;
     pkg->state_flag = SF_OK;
     pkg->state_status = SS_NOT_INSTALLED;

     active_list_clear(&pkg->list);

     free (pkg->replaces);
     pkg->replaces = NULL;

     for (i = 0; i < pkg->depends_count; i++)
       free (pkg->depends_str[i]);
     free(pkg->depends_str);
     pkg->depends_str = NULL;

     for (i = 0; i < pkg->provides_count; i++)
       free (pkg->provides_str[i]);
     free(pkg->provides_str);
     pkg->provides_str = NULL;

     for (i = 0; i < pkg->conflicts_count; i++)
       free (pkg->conflicts_str[i]);
     free(pkg->conflicts_str);
     pkg->conflicts_str = NULL;

     for (i = 0; i < pkg->replaces_count; i++)
       free (pkg->replaces_str[i]);
     free(pkg->replaces_str);
     pkg->replaces_str = NULL;

     for (i = 0; i < pkg->recommends_count; i++)
       free (pkg->recommends_str[i]);
     free(pkg->recommends_str);
     pkg->recommends_str = NULL;

     for (i = 0; i < pkg->suggests_count; i++)
       free (pkg->suggests_str[i]);
     free(pkg->suggests_str);
     pkg->suggests_str = NULL;

     if (pkg->depends)
     {
       int count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
       int x;

       for (x = 0; x < count; x++)
	 compound_depend_deinit (&pkg->depends[x]);
       free (pkg->depends);
     }

     if (pkg->conflicts)
     {
       int x;
       for (x = 0; x < pkg->conflicts_count; x++)
         compound_depend_deinit (&pkg->conflicts[x]);
       free (pkg->conflicts);
     }

     free (pkg->provides);

     pkg->pre_depends_count = 0;
     free(pkg->pre_depends_str);
     pkg->pre_depends_str = NULL;
     pkg->provides_count = 0;
     free(pkg->filename);
     pkg->filename = NULL;
     free(pkg->local_filename);
     pkg->local_filename = NULL;
     /* CLEANUP: It'd be nice to pullin the cleanup function from
	opkg_install.c here. See comment in
	opkg_install.c:cleanup_temporary_files */
     free(pkg->tmp_unpack_dir);
     pkg->tmp_unpack_dir = NULL;
     free(pkg->md5sum);
     pkg->md5sum = NULL;
#if defined HAVE_SHA256
     free(pkg->sha256sum);
     pkg->sha256sum = NULL;
#endif
     free(pkg->size);
     pkg->size = NULL;
     free(pkg->installed_size);
     pkg->installed_size = NULL;
     free(pkg->priority);
     pkg->priority = NULL;
     free(pkg->source);
     pkg->source = NULL;
     conffile_list_deinit(&pkg->conffiles);
     /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
	since if they are calling deinit, they should know. Maybe do an
	assertion here instead? */
     pkg->installed_files_ref_cnt = 1;
     pkg_free_installed_files(pkg);
     pkg->essential = 0;
     free (pkg->tags);
     pkg->tags = NULL;
}

int pkg_init_from_file(pkg_t *pkg, const char *filename)
{
     int err;
     char **raw, **raw_start;
     FILE *control_file;

     err = pkg_init(pkg);
     if (err) { return err; }

     pkg->local_filename = strdup(filename);
    
     control_file = tmpfile();
     err = pkg_extract_control_file_to_stream(pkg, control_file);
     if (err) { return err; }

     rewind(control_file);
     raw = raw_start = read_raw_pkgs_from_stream(control_file);
     pkg_parse_raw(pkg, &raw, NULL, NULL);

     fclose(control_file);

     raw = raw_start;
     while (*raw) {
	  free(*raw++);
     }
     free(raw_start);

     return 0;
}

/* Merge any new information in newpkg into oldpkg */
/* XXX: CLEANUP: This function shouldn't actually modify anything in
   newpkg, but should leave it usable. This rework is so that
   pkg_hash_insert doesn't clobber the pkg that you pass into it. */
/* 
 * uh, i thought that i had originally written this so that it took 
 * two pkgs and returned a new one?  we can do that again... -sma
 */
int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
{
     if (oldpkg == newpkg) {
	  return 0;
     }

     if (!oldpkg->src)
	  oldpkg->src = newpkg->src;
     if (!oldpkg->dest)
	  oldpkg->dest = newpkg->dest;
     if (!oldpkg->architecture)
	  oldpkg->architecture = str_dup_safe(newpkg->architecture);
     if (!oldpkg->arch_priority)
	  oldpkg->arch_priority = newpkg->arch_priority;
     if (!oldpkg->section)
	  oldpkg->section = str_dup_safe(newpkg->section);
     if(!oldpkg->maintainer)
	  oldpkg->maintainer = str_dup_safe(newpkg->maintainer);
     if(!oldpkg->description)
	  oldpkg->description = str_dup_safe(newpkg->description);
     if (set_status) {
	  /* merge the state_flags from the new package */
	  oldpkg->state_want = newpkg->state_want;
	  oldpkg->state_status = newpkg->state_status;
	  oldpkg->state_flag = newpkg->state_flag;
     } else {
	  if (oldpkg->state_want == SW_UNKNOWN)
	       oldpkg->state_want = newpkg->state_want;
	  if (oldpkg->state_status == SS_NOT_INSTALLED)
	       oldpkg->state_status = newpkg->state_status;
	  oldpkg->state_flag |= newpkg->state_flag;
     }

     if (!oldpkg->depends_str && !oldpkg->pre_depends_str && !oldpkg->recommends_str && !oldpkg->suggests_str) {
	  oldpkg->depends_str = newpkg->depends_str;
	  newpkg->depends_str = NULL;
	  oldpkg->depends_count = newpkg->depends_count;
	  newpkg->depends_count = 0;

	  oldpkg->depends = newpkg->depends;
	  newpkg->depends = NULL;

	  oldpkg->pre_depends_str = newpkg->pre_depends_str;
	  newpkg->pre_depends_str = NULL;
	  oldpkg->pre_depends_count = newpkg->pre_depends_count;
	  newpkg->pre_depends_count = 0;

	  oldpkg->recommends_str = newpkg->recommends_str;
	  newpkg->recommends_str = NULL;
	  oldpkg->recommends_count = newpkg->recommends_count;
	  newpkg->recommends_count = 0;

	  oldpkg->suggests_str = newpkg->suggests_str;
	  newpkg->suggests_str = NULL;
	  oldpkg->suggests_count = newpkg->suggests_count;
	  newpkg->suggests_count = 0;
     }

     if (!oldpkg->provides_str) {
	  oldpkg->provides_str = newpkg->provides_str;
	  newpkg->provides_str = NULL;
	  oldpkg->provides_count = newpkg->provides_count;
	  newpkg->provides_count = 0;

	  oldpkg->provides = newpkg->provides;
	  newpkg->provides = NULL;
     }

     if (!oldpkg->conflicts_str) {
	  oldpkg->conflicts_str = newpkg->conflicts_str;
	  newpkg->conflicts_str = NULL;
	  oldpkg->conflicts_count = newpkg->conflicts_count;
	  newpkg->conflicts_count = 0;

	  oldpkg->conflicts = newpkg->conflicts;
	  newpkg->conflicts = NULL;
     }

     if (!oldpkg->replaces_str) {
	  oldpkg->replaces_str = newpkg->replaces_str;
	  newpkg->replaces_str = NULL;
	  oldpkg->replaces_count = newpkg->replaces_count;
	  newpkg->replaces_count = 0;

	  oldpkg->replaces = newpkg->replaces;
	  newpkg->replaces = NULL;
     }

     if (!oldpkg->filename)
	  oldpkg->filename = str_dup_safe(newpkg->filename);
     if (0)
     fprintf(stdout, "pkg=%s old local_filename=%s new local_filename=%s\n", 
	     oldpkg->name, oldpkg->local_filename, newpkg->local_filename);
     if (!oldpkg->local_filename)
	  oldpkg->local_filename = str_dup_safe(newpkg->local_filename);
     if (!oldpkg->tmp_unpack_dir)
	  oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
     if (!oldpkg->md5sum)
	  oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
#if defined HAVE_SHA256
     if (!oldpkg->sha256sum)
	  oldpkg->sha256sum = str_dup_safe(newpkg->sha256sum);
#endif
     if (!oldpkg->size)
	  oldpkg->size = str_dup_safe(newpkg->size);
     if (!oldpkg->installed_size)
	  oldpkg->installed_size = str_dup_safe(newpkg->installed_size);
     if (!oldpkg->priority)
	  oldpkg->priority = str_dup_safe(newpkg->priority);
     if (!oldpkg->source)
	  oldpkg->source = str_dup_safe(newpkg->source);
     if (nv_pair_list_empty(&oldpkg->conffiles)){
	  list_splice_init(&newpkg->conffiles.head, &oldpkg->conffiles.head);
	  conffile_list_init(&newpkg->conffiles);
     }
     if (!oldpkg->installed_files){
	  oldpkg->installed_files = newpkg->installed_files;
	  oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt;
	  newpkg->installed_files = NULL;
     }
     if (!oldpkg->essential)
	  oldpkg->essential = newpkg->essential;

     return 0;
}

abstract_pkg_t *abstract_pkg_new(void)
{
     abstract_pkg_t * ab_pkg;

     ab_pkg = calloc(1, sizeof(abstract_pkg_t));

     if (ab_pkg == NULL) {
	  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
	  return NULL;
     }

     if ( abstract_pkg_init(ab_pkg) < 0 ) 
        return NULL;

     return ab_pkg;
}

int abstract_pkg_init(abstract_pkg_t *ab_pkg)
{
     ab_pkg->provided_by = abstract_pkg_vec_alloc();
     if (ab_pkg->provided_by==NULL){
        return -1;
     }
     ab_pkg->dependencies_checked = 0;
     ab_pkg->state_status = SS_NOT_INSTALLED;

     return 0;
}

void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
     char * temp_str;
     char **raw =NULL;
     char **raw_start=NULL; 

     size_t str_size = strlen(pkg->dest->info_dir)+strlen(pkg->name)+12;
     temp_str = (char *) alloca (str_size);
     memset(temp_str, 0 , str_size);
     
     if (temp_str == NULL ){
        opkg_message(conf, OPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
        return;
     }
     sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
   
     raw = raw_start = read_raw_pkgs_from_file(temp_str);
     if (raw == NULL ){
        opkg_message(conf, OPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
        return;
     }

     while(*raw){
        if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
            opkg_message(conf, OPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
        }
     }
     raw = raw_start;
     while (*raw) {
        if (raw!=NULL)
          free(*raw++);
     }

     free(raw_start); 

     return ;

}

void pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field)
{
     int i;
     int flag_provide_false = 0;

     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
	  goto UNKNOWN_FMT_FIELD;
     }

     switch (field[0])
     {
     case 'a':
     case 'A':
	  if (strcasecmp(field, "Architecture") == 0) {
	       if (pkg->architecture) {
                   fprintf(fp, "Architecture: %s\n", pkg->architecture);
	       }
	  } else if (strcasecmp(field, "Auto-Installed") == 0) {
		if (pkg->auto_installed)
		    fprintf(fp, "Auto-Installed: yes\n");
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 'c':
     case 'C':
	  if (strcasecmp(field, "Conffiles") == 0) {
	       conffile_list_elt_t *iter;

	       if (nv_pair_list_empty(&pkg->conffiles))
		    return;

               fprintf(fp, "Conffiles:\n");
	       for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
		    if (((conffile_t *)iter->data)->name && ((conffile_t *)iter->data)->value) {
                         fprintf(fp, "%s %s\n", 
                                 ((conffile_t *)iter->data)->name, 
                                 ((conffile_t *)iter->data)->value);
		    }
	       }
	  } else if (strcasecmp(field, "Conflicts") == 0) {
	       if (pkg->conflicts_count) {
                    fprintf(fp, "Conflicts:");
		    for(i = 0; i < pkg->conflicts_count; i++) {
                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
                    }
                    fprintf(fp, "\n");
	       }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 'd':
     case 'D':
	  if (strcasecmp(field, "Depends") == 0) {
	       if (pkg->depends_count) {
                    fprintf(fp, "Depends:");
		    for(i = 0; i < pkg->depends_count; i++) {
                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
                    }
		    fprintf(fp, "\n");
	       }
	  } else if (strcasecmp(field, "Description") == 0) {
	       if (pkg->description) {
                   fprintf(fp, "Description: %s\n", pkg->description);
	       }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
          break;
     case 'e':
     case 'E':
	  if (pkg->essential) {
              fprintf(fp, "Essential: yes\n");
	  }
	  break;
     case 'f':
     case 'F':
	  if (pkg->filename) {
              fprintf(fp, "Filename: %s\n", pkg->filename);
	  }
	  break;
     case 'i':
     case 'I':
	  if (strcasecmp(field, "Installed-Size") == 0) {
               fprintf(fp, "Installed-Size: %s\n", pkg->installed_size);
	  } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
               fprintf(fp, "Installed-Time: %lu\n", pkg->installed_time);
	  }
	  break;
     case 'm':
     case 'M':
	  if (strcasecmp(field, "Maintainer") == 0) {
	       if (pkg->maintainer) {
                   fprintf(fp, "maintainer: %s\n", pkg->maintainer);
	       }
	  } else if (strcasecmp(field, "MD5sum") == 0) {
	       if (pkg->md5sum) {
                   fprintf(fp, "MD5Sum: %s\n", pkg->md5sum);
	       }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 'p':
     case 'P':
	  if (strcasecmp(field, "Package") == 0) {
               fprintf(fp, "Package: %s\n", pkg->name);
	  } else if (strcasecmp(field, "Priority") == 0) {
               fprintf(fp, "Priority: %s\n", pkg->priority);
	  } else if (strcasecmp(field, "Provides") == 0) {
	       if (pkg->provides_count) {
               /* Here we check if the opkg_internal_use_only is used, and we discard it.*/
                  for ( i=0; i < pkg->provides_count; i++ ){
	              if (strstr(pkg->provides_str[i],"opkg_internal_use_only")!=NULL) {
                         memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
                         flag_provide_false = 1;
                      }
                  }
                  if ( !flag_provide_false ||                                             /* Pigi there is not my trick flag */
                     ((flag_provide_false) &&  (pkg->provides_count > 1))){             /* Pigi There is, but we also have others Provides */
                     fprintf(fp, "Provides:");
		     for(i = 0; i < pkg->provides_count; i++) {
                         if (strlen(pkg->provides_str[i])>0) {
                            fprintf(fp, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
                         }
                     }
                     fprintf(fp, "\n");
                  }
               }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 'r':
     case 'R':
	  if (strcasecmp (field, "Replaces") == 0) {
	       if (pkg->replaces_count) {
                    fprintf(fp, "Replaces:");
		    for (i = 0; i < pkg->replaces_count; i++) {
                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
                    }
                    fprintf(fp, "\n");
	       }
	  } else if (strcasecmp (field, "Recommends") == 0) {
	       if (pkg->recommends_count) {
                    fprintf(fp, "Recommends:");
		    for(i = 0; i < pkg->recommends_count; i++) {
                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
                    }
                    fprintf(fp, "\n");
	       }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 's':
     case 'S':
	  if (strcasecmp(field, "Section") == 0) {
	       if (pkg->section) {
                   fprintf(fp, "Section: %s\n", pkg->section);
	       }
#if defined HAVE_SHA256
	  } else if (strcasecmp(field, "SHA256sum") == 0) {
	       if (pkg->sha256sum) {
                   fprintf(fp, "SHA256sum: %s\n", pkg->sha256sum);
	       }
#endif
	  } else if (strcasecmp(field, "Size") == 0) {
	       if (pkg->size) {
                   fprintf(fp, "Size: %s\n", pkg->size);
	       }
	  } else if (strcasecmp(field, "Source") == 0) {
	       if (pkg->source) {
                   fprintf(fp, "Source: %s\n", pkg->source);
               }
	  } else if (strcasecmp(field, "Status") == 0) {
               char *pflag = pkg_state_flag_to_str(pkg->state_flag);
               char *pstat = pkg_state_status_to_str(pkg->state_status);
               char *pwant = pkg_state_want_to_str(pkg->state_want);

	       if (pflag == NULL || pstat == NULL || pwant == NULL)
		       return;

               fprintf(fp, "Status: %s %s %s\n", pwant, pflag, pstat);

               free(pflag);
               free(pwant);
               free(pstat);
	  } else if (strcasecmp(field, "Suggests") == 0) {
	       if (pkg->suggests_count) {
                    fprintf(fp, "Suggests:");
		    for(i = 0; i < pkg->suggests_count; i++) {
                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
                    }
                    fprintf(fp, "\n");
	       }
	  } else {
	       goto UNKNOWN_FMT_FIELD;
	  }
	  break;
     case 't':
     case 'T':
	  if (strcasecmp(field, "Tags") == 0) {
	       if (pkg->tags) {
                   fprintf(fp, "Tags: %s\n", pkg->tags);
	       }
	  }
	  break;
     case 'v':
     case 'V':
	  {
	       char *version = pkg_version_str_alloc(pkg);
	       if (version == NULL)
	            return;
               fprintf(fp, "Version: %s\n", version);
	       free(version);
          }
	  break;
     default:
	  goto UNKNOWN_FMT_FIELD;
     }

     return;

UNKNOWN_FMT_FIELD:
     fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
}

void pkg_formatted_info(FILE *fp, pkg_t *pkg)
{
	pkg_formatted_field(fp, pkg, "Package");
	pkg_formatted_field(fp, pkg, "Version");
	pkg_formatted_field(fp, pkg, "Depends");
	pkg_formatted_field(fp, pkg, "Recommends");
	pkg_formatted_field(fp, pkg, "Suggests");
	pkg_formatted_field(fp, pkg, "Provides");
	pkg_formatted_field(fp, pkg, "Replaces");
	pkg_formatted_field(fp, pkg, "Conflicts");
	pkg_formatted_field(fp, pkg, "Status");
	pkg_formatted_field(fp, pkg, "Section");
	pkg_formatted_field(fp, pkg, "Essential");
	pkg_formatted_field(fp, pkg, "Architecture");
	pkg_formatted_field(fp, pkg, "Maintainer");
	pkg_formatted_field(fp, pkg, "MD5sum");
	pkg_formatted_field(fp, pkg, "Size");
	pkg_formatted_field(fp, pkg, "Filename");
	pkg_formatted_field(fp, pkg, "Conffiles");
	pkg_formatted_field(fp, pkg, "Source");
	pkg_formatted_field(fp, pkg, "Description");
	pkg_formatted_field(fp, pkg, "Installed-Time");
	pkg_formatted_field(fp, pkg, "Tags");
	fputs("\n", fp);
}

void pkg_print_status(pkg_t * pkg, FILE * file)
{
     if (pkg == NULL) {
	  return;
     }

     /* XXX: QUESTION: Do we actually want more fields here? The
	original idea was to save space by installing only what was
	needed for actual computation, (package, version, status,
	essential, conffiles). The assumption is that all other fields
	can be found in th available file.

	But, someone proposed the idea to make it possible to
	reconstruct a .opk from an installed package, (ie. for beaming
	from one handheld to another). So, maybe we actually want a few
	more fields here, (depends, suggests, etc.), so that that would
	be guaranteed to work even in the absence of more information
	from the available file.

	28-MAR-03: kergoth and I discussed this yesterday.  We think
	the essential info needs to be here for all installed packages
	because they may not appear in the Packages files on various
	feeds.  Furthermore, one should be able to install from URL or
	local storage without requiring a Packages file from any feed.
	-Jamey
     */
     pkg_formatted_field(file, pkg, "Package");
     pkg_formatted_field(file, pkg, "Version");
     pkg_formatted_field(file, pkg, "Depends");
     pkg_formatted_field(file, pkg, "Recommends");
     pkg_formatted_field(file, pkg, "Suggests");
     pkg_formatted_field(file, pkg, "Provides");
     pkg_formatted_field(file, pkg, "Replaces");
     pkg_formatted_field(file, pkg, "Conflicts");
     pkg_formatted_field(file, pkg, "Status");
     pkg_formatted_field(file, pkg, "Essential");
     pkg_formatted_field(file, pkg, "Architecture");
     pkg_formatted_field(file, pkg, "Conffiles");
     pkg_formatted_field(file, pkg, "Installed-Time");
     pkg_formatted_field(file, pkg, "Auto-Installed");
     fputs("\n", file);
}

/*
 * libdpkg - Debian packaging suite library routines
 * vercmp.c - comparison of version numbers
 *
 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
 */
int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg)
{
     int r;

     if (pkg->epoch > ref_pkg->epoch) {
	  return 1;
     }

     if (pkg->epoch < ref_pkg->epoch) {
	  return -1;
     }

     r = verrevcmp(pkg->version, ref_pkg->version);
     if (r) {
	  return r;
     }

     r = verrevcmp(pkg->revision, ref_pkg->revision);
     if (r) {
	  return r;
     }

     return r;
}

/* assume ascii; warning: evaluates x multiple times! */
#define order(x) ((x) == '~' ? -1 \
		: isdigit((x)) ? 0 \
		: !(x) ? 0 \
		: isalpha((x)) ? (x) \
		: (x) + 256)

static int verrevcmp(const char *val, const char *ref) {
  if (!val) val= "";
  if (!ref) ref= "";

  while (*val || *ref) {
    int first_diff= 0;

    while ( (*val && !isdigit(*val)) || (*ref && !isdigit(*ref)) ) {
      int vc= order(*val), rc= order(*ref);
      if (vc != rc) return vc - rc;
      val++; ref++;
    }

    while ( *val == '0' ) val++;
    while ( *ref == '0' ) ref++;
    while (isdigit(*val) && isdigit(*ref)) {
      if (!first_diff) first_diff= *val - *ref;
      val++; ref++;
    }
    if (isdigit(*val)) return 1;
    if (isdigit(*ref)) return -1;
    if (first_diff) return first_diff;
  }
  return 0;
}

int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
{
     int r;

     r = pkg_compare_versions(it, ref);

     if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
	  return r <= 0;
     }

     if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
	  return r >= 0;
     }

     if (strcmp(op, "<<") == 0) {
	  return r < 0;
     }

     if (strcmp(op, ">>") == 0) {
	  return r > 0;
     }

     if (strcmp(op, "=") == 0) {
	  return r == 0;
     }

     fprintf(stderr, "unknown operator: %s", op);
     return 0;
}

int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
{
     const pkg_t *a = *(const pkg_t**) p1;
     const pkg_t *b = *(const pkg_t**) p2;
     int namecmp;
     int vercmp;
     if (!a->name || !b->name) {
       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->name=%p b=%p b->name=%p\n",
	       a, a->name, b, b->name);
       return 0;
     }
       
     namecmp = strcmp(a->name, b->name);
     if (namecmp)
	  return namecmp;
     vercmp = pkg_compare_versions(a, b);
     if (vercmp)
	  return vercmp;
     if (!a->arch_priority || !b->arch_priority) {
       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->arch_priority=%i b=%p b->arch_priority=%i\n",
	       a, a->arch_priority, b, b->arch_priority);
       return 0;
     }
     if (a->arch_priority > b->arch_priority)
	  return 1;
     if (a->arch_priority < b->arch_priority)
	  return -1;
     return 0;
}

int abstract_pkg_name_compare(const void *p1, const void *p2)
{
     const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
     const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
     if (!a->name || !b->name) {
       fprintf(stderr, "abstract_pkg_name_compare: a=%p a->name=%p b=%p b->name=%p\n",
	       a, a->name, b, b->name);
       return 0;
     }
     return strcmp(a->name, b->name);
}


char *pkg_version_str_alloc(pkg_t *pkg)
{
     char *complete_version;
     char *epoch_str;
     char *revision_str;

     if (pkg->epoch) {
	  sprintf_alloc(&epoch_str, "%d:", pkg->epoch);
     } else {
	  epoch_str = strdup("");
     }

     if (pkg->revision && strlen(pkg->revision)) {
	  sprintf_alloc(&revision_str, "-%s", pkg->revision);
     } else {
	  revision_str = strdup("");
     }


     sprintf_alloc(&complete_version, "%s%s%s",
		   epoch_str, pkg->version, revision_str);

     free(epoch_str);
     free(revision_str);

     return complete_version;
}

str_list_t *pkg_get_installed_files(pkg_t *pkg)
{
     int err;
     char *list_file_name = NULL;
     FILE *list_file = NULL;
     char *line;
     char *installed_file_name;
     int rootdirlen;

     pkg->installed_files_ref_cnt++;

     if (pkg->installed_files) {
	  return pkg->installed_files;
     }

     pkg->installed_files = str_list_alloc();
     if (pkg->installed_files == NULL) {
	  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
	  return NULL;
     }

     /* For uninstalled packages, get the file list directly from the package.
	For installed packages, look at the package.list file in the database.
     */
     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
	  if (pkg->local_filename == NULL) {
	       return pkg->installed_files;
	  }
	  /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
	     file. In other words, change deb_extract so that it can
	     simply return the file list as a char *[] rather than
	     insisting on writing in to a FILE * as it does now. */
	  list_file = tmpfile();
	  err = pkg_extract_data_file_names_to_stream(pkg, list_file);
	  if (err) {
	       fclose(list_file);
	       fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
		       __FUNCTION__, pkg->local_filename, strerror(err));
	       return pkg->installed_files;
	  }
	  rewind(list_file);
     } else {
	  sprintf_alloc(&list_file_name, "%s/%s.list",
			pkg->dest->info_dir, pkg->name);
	  if (! file_exists(list_file_name)) {
	       free(list_file_name);
	       return pkg->installed_files;
	  }

	  list_file = fopen(list_file_name, "r");
	  if (list_file == NULL) {
	       fprintf(stderr, "WARNING: Cannot open %s: %s\n",
		       list_file_name, strerror(errno));
	       free(list_file_name);
	       return pkg->installed_files;
	  }
	  free(list_file_name);
     }

     rootdirlen = strlen( pkg->dest->root_dir );
     while (1) {
	  char *file_name;
	
	  line = file_read_line_alloc(list_file);
	  if (line == NULL) {
	       break;
	  }
	  str_chomp(line);
	  file_name = line;

	  /* Take pains to avoid uglies like "/./" in the middle of file_name. */
	  if( strncmp( pkg->dest->root_dir, 
		       file_name, 
		       rootdirlen ) ) {
	       if (*file_name == '.') {
		    file_name++;
	       }
	       if (*file_name == '/') {
		    file_name++;
	       }

	       /* Freed in pkg_free_installed_files */
	       sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
	  } else {
	       // already contains root_dir as header -> ABSOLUTE
	       sprintf_alloc(&installed_file_name, "%s", file_name);
	  }
	  str_list_append(pkg->installed_files, installed_file_name);
          free(installed_file_name);
	  free(line);
     }

     fclose(list_file);

     return pkg->installed_files;
}

/* XXX: CLEANUP: This function and it's counterpart,
   (pkg_get_installed_files), do not match our init/deinit naming
   convention. Nor the alloc/free convention. But, then again, neither
   of these conventions currrently fit the way these two functions
   work. */
int pkg_free_installed_files(pkg_t *pkg)
{
     pkg->installed_files_ref_cnt--;

     if (pkg->installed_files_ref_cnt > 0)
	  return 0;

     if (pkg->installed_files) {
         str_list_purge(pkg->installed_files);
     }

     pkg->installed_files = NULL;

     return 0;
}

int pkg_remove_installed_files_list(opkg_conf_t *conf, pkg_t *pkg)
{
     int err;
     char *list_file_name;

     //I don't think pkg_free_installed_files should be called here. Jamey
     //pkg_free_installed_files(pkg);

     sprintf_alloc(&list_file_name, "%s/%s.list",
		   pkg->dest->info_dir, pkg->name);
     if (!conf->noaction) {
	  err = unlink(list_file_name);
	  free(list_file_name);

	  if (err) {
	       return errno;
	  }
     }
     return 0;
}

conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name)
{
     conffile_list_elt_t *iter;
     conffile_t *conffile;

     if (pkg == NULL) {
	  return NULL;
     }

     for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
	  conffile = (conffile_t *)iter->data;

	  if (strcmp(conffile->name, file_name) == 0) {
	       return conffile;
	  }
     }

     return NULL;
}

int pkg_run_script(opkg_conf_t *conf, pkg_t *pkg,
		   const char *script, const char *args)
{
     int err;
     char *path;
     char *cmd;

     /* XXX: FEATURE: When conf->offline_root is set, we should run the
	maintainer script within a chroot environment. */

     /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
	have scripts in pkg->tmp_unpack_dir. */
     if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
	  if (pkg->dest == NULL) {
	       fprintf(stderr, "%s: ERROR: installed package %s has a NULL dest\n",
		       __FUNCTION__, pkg->name);
	       return EINVAL;
	  }
	  sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script);
     } else {
	  if (pkg->tmp_unpack_dir == NULL) {
	       fprintf(stderr, "%s: ERROR: uninstalled package %s has a NULL tmp_unpack_dir\n",
		       __FUNCTION__, pkg->name);
	       return EINVAL;
	  }
	  sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
     }

     opkg_message(conf, OPKG_INFO, "Running script %s\n", path);
     if (conf->noaction) return 0;

     /* XXX: CLEANUP: There must be a better way to handle maintainer
	scripts when running with offline_root mode and/or a dest other
	than '/'. I've been playing around with some clever chroot
	tricks and I might come up with something workable. */
     /*
      * Attempt to provide a restricted environment for offline operation
      * Need the following set as a minimum:
      * OPKG_OFFLINE_ROOT = absolute path to root dir
      * D                 = absolute path to root dir (for OE generated postinst)
      * PATH              = something safe (a restricted set of utilities)
      */

     bool AllowOfflineMode = false;
     if (conf->offline_root) {
	  setenv("OPKG_OFFLINE_ROOT", conf->offline_root, 1);
	  setenv("D", conf->offline_root, 1);
          if (NULL == conf->offline_root_path || '\0' == conf->offline_root_path[0]) {
            setenv("PATH", "/dev/null", 1);
          } else {
            setenv("PATH", conf->offline_root_path, 1);
            AllowOfflineMode = true;
          }
     }

     setenv("PKG_ROOT",
	    pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1);

     if (! file_exists(path)) {
	  free(path);
	  return 0;
     }

     if (conf->offline_root && !AllowOfflineMode) {
	  fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
	  free(path);
	  return 0;
     }

     sprintf_alloc(&cmd, "%s %s", path, args);
     free(path);

     err = xsystem(cmd);
     free(cmd);

     if (err) {
	  fprintf(stderr, "%s script returned status %d\n", script, err);
	  return err;
     }

     return 0;
}

char *pkg_state_want_to_str(pkg_state_want_t sw)
{
     int i;

     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
	  if (pkg_state_want_map[i].value == sw) {
	       return strdup(pkg_state_want_map[i].str);
	  }
     }

     fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n",
	     __FUNCTION__, sw);
     return strdup("<STATE_WANT_UNKNOWN>");
}

pkg_state_want_t pkg_state_want_from_str(char *str)
{
     int i;

     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
	  if (strcmp(str, pkg_state_want_map[i].str) == 0) {
	       return pkg_state_want_map[i].value;
	  }
     }

     fprintf(stderr, "%s: ERROR: Illegal value for state_want string: %s\n",
	     __FUNCTION__, str);
     return SW_UNKNOWN;
}

char *pkg_state_flag_to_str(pkg_state_flag_t sf)
{
     int i;
     int len = 3; /* ok\000 is minimum */
     char *str = NULL;

     /* clear the temporary flags before converting to string */
     sf &= SF_NONVOLATILE_FLAGS;

     if (sf == 0) {
	  return strdup("ok");
     } else {

	  for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
	       if (sf & pkg_state_flag_map[i].value) {
		    len += strlen(pkg_state_flag_map[i].str) + 1;
	       }
	  }
	  str = malloc(len);
          if ( str == NULL ) {
	      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
              return NULL;
          }
	  str[0] = 0;
	  for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
	       if (sf & pkg_state_flag_map[i].value) {
		    strcat(str, pkg_state_flag_map[i].str);
		    strcat(str, ",");
	       }
	  }
	  len = strlen(str);
	  str[len-1] = 0; /* squash last comma */
	  return str;
     }
}

pkg_state_flag_t pkg_state_flag_from_str(const char *str)
{
     int i;
     int sf = SF_OK;

     if (strcmp(str, "ok") == 0) {
	  return SF_OK;
     }
     for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
	  const char *sfname = pkg_state_flag_map[i].str;
	  int sfname_len = strlen(sfname);
	  if (strncmp(str, sfname, sfname_len) == 0) {
	       sf |= pkg_state_flag_map[i].value;
	       str += sfname_len;
	       if (str[0] == ',') {
		    str++;
	       } else {
		    break;
	       }
	  }
     }

     return sf;
}

char *pkg_state_status_to_str(pkg_state_status_t ss)
{
     int i;

     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
	  if (pkg_state_status_map[i].value == ss) {
	       return strdup(pkg_state_status_map[i].str);
	  }
     }

     fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n",
	     __FUNCTION__, ss);
     return strdup("<STATE_STATUS_UNKNOWN>");
}

pkg_state_status_t pkg_state_status_from_str(const char *str)
{
     int i;

     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
	  if (strcmp(str, pkg_state_status_map[i].str) == 0) {
	       return pkg_state_status_map[i].value;
	  }
     }

     fprintf(stderr, "%s: ERROR: Illegal value for state_status string: %s\n",
	     __FUNCTION__, str);
     return SS_NOT_INSTALLED;
}

int pkg_arch_supported(opkg_conf_t *conf, pkg_t *pkg)
{
     nv_pair_list_elt_t *l;

     if (!pkg->architecture)
	  return 1;

     list_for_each_entry(l , &conf->arch_list.head, node) {
	  nv_pair_t *nv = (nv_pair_t *)l->data;
	  if (strcmp(nv->name, pkg->architecture) == 0) {
	       opkg_message(conf, OPKG_DEBUG, "arch %s (priority %s) supported for pkg %s\n", nv->name, nv->value, pkg->name);
	       return 1;
	  }
     }

     opkg_message(conf, OPKG_DEBUG, "arch %s unsupported for pkg %s\n", pkg->architecture, pkg->name);
     return 0;
}

int pkg_get_arch_priority(opkg_conf_t *conf, const char *archname)
{
     nv_pair_list_elt_t *l;

     list_for_each_entry(l , &conf->arch_list.head, node) {
	  nv_pair_t *nv = (nv_pair_t *)l->data;
	  if (strcmp(nv->name, archname) == 0) {
	       int priority = strtol(nv->value, NULL, 0);
	       return priority;
	  }
     }
     return 0;
}

int pkg_info_preinstall_check(opkg_conf_t *conf)
{
     int i;
     hash_table_t *pkg_hash = &conf->pkg_hash;
     pkg_vec_t *available_pkgs = pkg_vec_alloc();
     pkg_vec_t *installed_pkgs = pkg_vec_alloc();

     opkg_message(conf, OPKG_INFO, "pkg_info_preinstall_check: updating arch priority for each package\n");
     pkg_hash_fetch_available(pkg_hash, available_pkgs);
     /* update arch_priority for each package */
     for (i = 0; i < available_pkgs->len; i++) {
	  pkg_t *pkg = available_pkgs->pkgs[i];
	  int arch_priority = 1;
	  if (!pkg)
	       continue;
	  // opkg_message(conf, OPKG_DEBUG2, " package %s version=%s arch=%p:", pkg->name, pkg->version, pkg->architecture);
	  if (pkg->architecture) 
	       arch_priority = pkg_get_arch_priority(conf, pkg->architecture);
	  else 
	       opkg_message(conf, OPKG_ERROR, "pkg_info_preinstall_check: no architecture for package %s\n", pkg->name);
	  // opkg_message(conf, OPKG_DEBUG2, "%s arch_priority=%d\n", pkg->architecture, arch_priority);
	  pkg->arch_priority = arch_priority;
     }

     for (i = 0; i < available_pkgs->len; i++) {
	  pkg_t *pkg = available_pkgs->pkgs[i];
	  if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
	       /* clear flags and want for any uninstallable package */
	       opkg_message(conf, OPKG_DEBUG, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
			    pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
	       pkg->state_want = SW_UNKNOWN;
	       pkg->state_flag = 0;
	  }
     }
     pkg_vec_free(available_pkgs);

     /* update the file owner data structure */
     opkg_message(conf, OPKG_INFO, "pkg_info_preinstall_check: update file owner list\n");
     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
     for (i = 0; i < installed_pkgs->len; i++) {
	  pkg_t *pkg = installed_pkgs->pkgs[i];
	  str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
	  str_list_elt_t *iter, *niter;
	  if (installed_files == NULL) {
	       opkg_message(conf, OPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
	       break;
	  }
	  for (iter = str_list_first(installed_files), niter = str_list_next(installed_files, iter); 
                  iter; 
                  iter = niter, niter = str_list_next(installed_files, iter)) {
	       char *installed_file = (char *) iter->data;
	       // opkg_message(conf, OPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
	       file_hash_set_file_owner(conf, installed_file, pkg);
	  }
	  pkg_free_installed_files(pkg);
     }
     pkg_vec_free(installed_pkgs);

     return 0;
}

struct pkg_write_filelist_data {
     opkg_conf_t *conf;
     pkg_t *pkg;
     FILE *stream;
};

void pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
{
     struct pkg_write_filelist_data *data = data_;
     pkg_t *entry = entry_;
     if (entry == data->pkg) {
	  fprintf(data->stream, "%s\n", key);
     }
}

int pkg_write_filelist(opkg_conf_t *conf, pkg_t *pkg)
{
     struct pkg_write_filelist_data data;
     char *list_file_name = NULL;
     int err = 0;

     if (!pkg) {
	  opkg_message(conf, OPKG_ERROR, "Null pkg\n");
	  return -EINVAL;
     }
     opkg_message(conf, OPKG_INFO,
		  "    creating %s.list file\n", pkg->name);
     sprintf_alloc(&list_file_name, "%s/%s.list", pkg->dest->info_dir, pkg->name);
     if (!list_file_name) {
	  opkg_message(conf, OPKG_ERROR, "Failed to alloc list_file_name\n");
	  return -ENOMEM;
     }
     opkg_message(conf, OPKG_INFO,
		  "    creating %s file for pkg %s\n", list_file_name, pkg->name);
     data.stream = fopen(list_file_name, "w");
     if (!data.stream) {
	  opkg_message(conf, OPKG_ERROR, "Could not open %s for writing: %s\n",
		       list_file_name, strerror(errno));
		       return errno;
     }
     data.pkg = pkg;
     data.conf = conf;
     hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
     fclose(data.stream);
     free(list_file_name);

     pkg->state_flag &= ~SF_FILELIST_CHANGED;

     return err;
}

int pkg_write_changed_filelists(opkg_conf_t *conf)
{
     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
     hash_table_t *pkg_hash = &conf->pkg_hash;
     int i;
     int err;
     if (conf->noaction)
	  return 0;

     opkg_message(conf, OPKG_INFO, "%s: saving changed filelists\n", __FUNCTION__);
     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
     for (i = 0; i < installed_pkgs->len; i++) {
	  pkg_t *pkg = installed_pkgs->pkgs[i];
	  if (pkg->state_flag & SF_FILELIST_CHANGED) {
               opkg_message(conf, OPKG_DEBUG, "Calling pkg_write_filelist for pkg=%s from %s\n", pkg->name, __FUNCTION__);
	       err = pkg_write_filelist(conf, pkg);
	       if (err)
		    opkg_message(conf, OPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
	  }
     }
     pkg_vec_free (installed_pkgs);
     return 0;
}