opkg: implement opkg_set_option() and opkg_get_option()
[oweals/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg  package management system
2
3    Thomas Wood <thomas@openedhand.com>
4
5    Copyright (C) 2008 OpenMoko Inc
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16  */
17
18 #include <config.h>
19 #include <fnmatch.h>
20
21 #include "opkg.h"
22 #include "opkg_conf.h"
23 #include "args.h"
24
25 #include "opkg_install.h"
26 #include "opkg_configure.h"
27
28 struct _opkg_t
29 {
30   args_t *args;
31   opkg_conf_t *conf;
32   opkg_option_t *options[];
33 };
34
35 /** Private Functions ***/
36
37
38 static int
39 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
40 {
41   pkg_vec_t *all;
42   int i;
43   pkg_t *pkg;
44   int r, err = 0;
45
46   all = pkg_vec_alloc();
47   pkg_hash_fetch_available(&conf->pkg_hash, all);
48
49   for (i = 0; i < all->len; i++)
50   {
51     pkg = all->pkgs[i];
52
53     if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
54       continue;
55
56     if (pkg->state_status == SS_UNPACKED)
57     {
58       r = opkg_configure(conf, pkg);
59       if (r == 0)
60       {
61         pkg->state_status = SS_INSTALLED;
62         pkg->parent->state_status = SS_INSTALLED;
63         pkg->state_flag &= ~SF_PREFER;
64       }
65       else
66       {
67         if (!err)
68           err = r;
69       }
70     }
71   }
72
73   pkg_vec_free(all);
74   return err;
75 }
76
77
78
79 /*** Public API ***/
80
81 opkg_t *
82 opkg_new ()
83 {
84   opkg_t *opkg;
85   opkg = malloc (sizeof (opkg_t));
86   args_init (opkg->args);
87   opkg_conf_init (opkg->conf, opkg->args);
88   opkg_init_options_array (opkg->conf, opkg->options);
89   return opkg;
90 }
91
92 void
93 opkg_free (opkg_t *opkg)
94 {
95   opkg_conf_deinit (opkg->conf);
96   args_deinit (opkg->args);
97 }
98
99 void
100 opkg_get_option (opkg_t *opkg, char *option, void **value)
101 {
102   int i = 0;
103   opkg_option_t **options = opkg->options;
104
105   /* can't store a value in a NULL pointer! */
106   if (!value)
107     return;
108
109   /* look up the option
110    * TODO: this would be much better as a hash table
111    */
112   while (options[i]->name)
113   {
114     if (strcmp(options[i]->name, option) != 0)
115     {
116       i++;
117       continue;
118     }
119   }
120
121   /* get the option */
122   switch (options[i]->type)
123   {
124     case OPKG_OPT_TYPE_BOOL:
125       *((int *) value) = *((int *) options[i]->value);
126       return;
127
128     case OPKG_OPT_TYPE_INT:
129       *((int *) value) = *((int *) options[i]->value);
130       return;
131
132     case OPKG_OPT_TYPE_STRING:
133       *((char **)value) = strdup (options[i]->value);
134       return;
135    }
136
137 }
138
139 void
140 opkg_set_option (opkg_t *opkg, char *option, void *value)
141 {
142   int i = 0;
143   opkg_option_t **options = opkg->options;
144
145   /* NULL values are not defined */
146   if (!value)
147     return;
148
149   /* look up the option
150    * TODO: this would be much better as a hash table
151    */
152   while (options[i]->name)
153   {
154     if (strcmp(options[i]->name, option) != 0)
155     {
156       i++;
157       continue;
158     }
159   }
160
161   /* set the option */
162   switch (options[i]->type)
163   {
164     case OPKG_OPT_TYPE_BOOL:
165       if (*((int *) value) == 0)
166         *((int *)options[i]->value) = 0;
167       else
168         *((int *)options[i]->value) = 1;
169       return;
170
171     case OPKG_OPT_TYPE_INT:
172       *((int *) options[i]->value) = *((int *) value);
173       return;
174
175     case OPKG_OPT_TYPE_STRING:
176       *((char **)options[i]->value) = strdup(value);
177       return;
178    }
179
180 }
181
182 int
183 opkg_install_package (opkg_t *opkg, char *package_name)
184 {
185   int err;
186
187   pkg_info_preinstall_check(opkg->conf);
188
189   if (opkg->conf->multiple_providers)
190   {
191     err = opkg_install_multi_by_name (opkg->conf, package_name);
192   }
193   else
194   {
195     err = opkg_install_by_name (opkg->conf, package_name);
196   }
197
198   err = opkg_configure_packages (opkg->conf, NULL);
199
200   if (opkg->conf->noaction)
201     return err;
202
203   opkg_conf_write_status_files (opkg->conf);
204   pkg_write_changed_filelists (opkg->conf);
205
206   return err;
207 }
208
209 int
210 opkg_remove_package (opkg_t *opkg, char *package_name)
211 {
212   return 1;
213 }
214
215 int
216 opkg_upgrade_package (opkg_t *opkg, char *package_name)
217 {
218   return 1;
219 }
220
221 int
222 opkg_upgrade_all (opkg_t *opkg)
223 {
224   return 1;
225 }
226
227 int
228 opkg_update_package_lists (opkg_t *opkg)
229 {
230   return 1;
231 }