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