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