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