pkg: store size, installed size and installed time info in blob buffer
[project/opkg-lede.git] / tests / libopkg_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <libgen.h>
5
6 #include <opkg.h>
7
8 int opkg_state_changed;
9 pkg_t *find_pkg = NULL;
10
11 #define TEST_PACKAGE "aspell"
12
13 void progress_callback(const opkg_progress_data_t * progress, void *data)
14 {
15 printf("\r%s %3d%%\n", (char *)data, progress->percentage);
16 fflush(stdout);
17 }
18
19 static void list_pkg(pkg_t * pkg)
20 {
21 char *v = pkg_version_str_alloc(pkg);
22 printf("%s - %s\n", pkg->name, v);
23 free(v);
24 }
25
26 void package_list_installed_callback(pkg_t * pkg, void *data)
27 {
28 if (pkg->state_status == SS_INSTALLED)
29 list_pkg(pkg);
30 }
31
32 void package_list_callback(pkg_t * pkg, void *data)
33 {
34 static int install_count = 0;
35 static int total_count = 0;
36
37 if (pkg->state_status == SS_INSTALLED)
38 install_count++;
39
40 total_count++;
41
42 printf("\rPackage count: %d Installed, %d Total Available",
43 install_count, total_count);
44 fflush(stdout);
45
46 if (!find_pkg) {
47 /* store the first package to print out later */
48 find_pkg = pkg;
49 }
50 }
51
52 void package_list_upgradable_callback(pkg_t * pkg, void *data)
53 {
54 list_pkg(pkg);
55 }
56
57 void print_package(pkg_t * pkg)
58 {
59 char *v = pkg_version_str_alloc(pkg);
60 const char *tags = pkg_get_string(pkg, PKG_TAGS);
61
62 printf("Name: %s\n"
63 "Version: %s\n"
64 "Repository: %s\n"
65 "Architecture: %s\n"
66 "Description: %s\n"
67 "Tags: %s\n"
68 "Size: %lu\n"
69 "Status: %d\n",
70 pkg->name,
71 v,
72 pkg->src->name,
73 pkg_get_string(pkg, PKG_ARCHITECTURE),
74 pkg_get_string(pkg, PKG_DESCRIPTION),
75 tags ? tags : "",
76 (unsigned long) pkg_get_int(pkg, PKG_SIZE), pkg->state_status);
77 free(v);
78 }
79
80 void opkg_test(void)
81 {
82 int err;
83 pkg_t *pkg;
84
85 err = opkg_update_package_lists(progress_callback, "Updating...");
86 printf("\nopkg_update_package_lists returned %d\n", err);
87
88 opkg_list_packages(package_list_callback, NULL);
89 printf("\n");
90
91 if (find_pkg) {
92 printf("Finding package \"%s\"\n", find_pkg->name);
93 pkg =
94 opkg_find_package(find_pkg->name,
95 pkg_get_string(find_pkg, PKG_VERSION),
96 pkg_get_string(find_pkg, PKG_ARCHITECTURE),
97 find_pkg->src->name);
98 if (pkg) {
99 print_package(pkg);
100 } else
101 printf("Package \"%s\" not found!\n", find_pkg->name);
102 } else
103 printf("No package available to test find_package.\n");
104
105 err =
106 opkg_install_package(TEST_PACKAGE, progress_callback,
107 "Installing...");
108 printf("\nopkg_install_package returned %d\n", err);
109
110 err =
111 opkg_upgrade_package(TEST_PACKAGE, progress_callback,
112 "Upgrading...");
113 printf("\nopkg_upgrade_package returned %d\n", err);
114
115 err =
116 opkg_remove_package(TEST_PACKAGE, progress_callback, "Removing...");
117 printf("\nopkg_remove_package returned %d\n", err);
118
119 printf("Listing upgradable packages...\n");
120 opkg_list_upgradable_packages(package_list_upgradable_callback, NULL);
121
122 err = opkg_upgrade_all(progress_callback, "Upgrading all...");
123 printf("\nopkg_upgrade_all returned %d\n", err);
124
125 }
126
127 int main(int argc, char **argv)
128 {
129 pkg_t *pkg;
130 int err;
131
132 if (argc < 2) {
133 printf("Usage: %s command\n"
134 "\nCommands:\n"
135 "\tupdate - Update package lists\n"
136 "\tfind [package] - Print details of the specified package\n"
137 "\tinstall [package] - Install the specified package\n"
138 "\tupgrade [package] - Upgrade the specified package\n"
139 "\tlist upgrades - List the available upgrades\n"
140 "\tlist all - List all available packages\n"
141 "\tlist installed - List all the installed packages\n"
142 "\tremove [package] - Remove the specified package\n"
143 "\trping - Reposiroties ping, check the accessibility of repositories\n"
144 "\ttest - Run test script\n", basename(argv[0]));
145 exit(0);
146 }
147
148 setenv("OFFLINE_ROOT", "/tmp", 0);
149
150 if (opkg_new()) {
151 printf("opkg_new() failed. This sucks.\n");
152 print_error_list();
153 return 1;
154 }
155
156 switch (argv[1][0]) {
157 case 'f':
158 pkg = opkg_find_package(argv[2], NULL, NULL, NULL);
159 if (pkg) {
160 print_package(pkg);
161 } else
162 printf("Package \"%s\" not found!\n", find_pkg->name);
163 break;
164 case 'i':
165 err =
166 opkg_install_package(argv[2], progress_callback,
167 "Installing...");
168 printf("\nopkg_install_package returned %d\n", err);
169 break;
170
171 case 'u':
172 if (argv[1][2] == 'd') {
173 err =
174 opkg_update_package_lists(progress_callback,
175 "Updating...");
176 printf("\nopkg_update_package_lists returned %d\n",
177 err);
178 break;
179 } else {
180 if (argc < 3) {
181 err =
182 opkg_upgrade_all(progress_callback,
183 "Upgrading all...");
184 printf("\nopkg_upgrade_all returned %d\n", err);
185 } else {
186 err =
187 opkg_upgrade_package(argv[2],
188 progress_callback,
189 "Upgrading...");
190 printf("\nopkg_upgrade_package returned %d\n",
191 err);
192 }
193 }
194 break;
195
196 case 'l':
197 if (argc < 3) {
198 printf
199 ("Please specify one either all, installed or upgrades\n");
200 } else {
201 switch (argv[2][0]) {
202 case 'u':
203 printf("Listing upgradable packages...\n");
204 opkg_list_upgradable_packages
205 (package_list_upgradable_callback, NULL);
206 break;
207 case 'a':
208 printf("Listing all packages...\n");
209 opkg_list_packages(package_list_callback, NULL);
210 printf("\n");
211 break;
212 case 'i':
213 printf("Listing installed packages...\n");
214 opkg_list_packages
215 (package_list_installed_callback, NULL);
216 break;
217 default:
218 printf("Unknown list option \"%s\"\n", argv[2]);
219 }
220 }
221 break;
222
223 case 'r':
224 if (argv[1][1] == 'e') {
225 err =
226 opkg_remove_package(argv[2], progress_callback,
227 "Removing...");
228 printf("\nopkg_remove_package returned %d\n", err);
229 break;
230 } else if (argv[1][1] == 'p') {
231 err = opkg_repository_accessibility_check();
232 printf
233 ("\nopkg_repository_accessibility_check returned (%d)\n",
234 err);
235 break;
236 }
237
238 default:
239 printf("Unknown command \"%s\"\n", argv[1]);
240 }
241
242 opkg_free();
243
244 return 0;
245 }