opkg: fix nullpointer dereference
[oweals/opkg-lede.git] / libopkg / opkg_state.c
1 /* opkg_state.c - the opkg package management system
2
3    Thomas Wood <thomas@openedhand.com>
4
5    Copyright (C) 2008 by 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 "opkg_state.h"
19
20
21 static char *state_strings[] =
22 {
23   "None",
24   "Downloading Package",
25   "Installing Package",
26   "Configuring Package",
27   "Upgrading Package",
28   "Removing Package",
29   "Downloading Repository",
30   "Verifying Repository Signature"
31 };
32
33
34
35 opkg_state_changed_callback opkg_cb_state_changed = NULL;
36
37 static opkg_state_t opkg_state = 0;
38 static char *opkg_state_data = NULL;
39
40 void
41 opkg_set_current_state (opkg_conf_t *conf, opkg_state_t state, const char *data)
42 {
43   if (opkg_state_data)
44     free (opkg_state_data);
45   if (data)
46   {
47     opkg_state_data = strdup (data);
48   }
49   else
50   {
51     opkg_state_data = NULL;
52   }
53
54   opkg_state = state;
55
56   if (opkg_cb_state_changed)
57   {
58     opkg_cb_state_changed (opkg_state, opkg_state_data);
59   }
60
61
62   if (data == NULL)
63     opkg_message (conf, OPKG_INFO, "opkg state set to %s\n", state_strings[state]);
64   else
65     opkg_message (conf, OPKG_INFO, "opkg state set to %s: %s\n", state_strings[state], data);
66 }
67
68 void
69 opkg_get_current_state (opkg_state_t *state, const char **data)
70 {
71   *state = opkg_state;
72   *data = opkg_state_data;
73 }