opkg: (leak fixing, day 2) lots and lots of memory leaks fixed
[project/opkg-lede.git] / tests / libopkg_test.c
1 #include <opkg.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 opkg_package_t *find_pkg = NULL;
6
7 void
8 progress_callback (opkg_t *opkg, const opkg_progress_data_t *progress, void *data)
9 {
10 printf ("\r%s %3d%%", (char*) data, progress->percentage);
11 fflush (stdout);
12 }
13
14 void
15 package_list_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
16 {
17 static install_count = 0;
18 static total_count = 0;
19
20 if (pkg->installed)
21 install_count++;
22
23 total_count++;
24
25 printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
26 fflush (stdout);
27
28 if (!find_pkg)
29 {
30 /* store the first package to print out later */
31 find_pkg = pkg;
32 }
33 else
34 opkg_package_free (pkg);
35 }
36
37 void
38 package_list_upgradable_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
39 {
40 printf ("%s - %s\n", pkg->name, pkg->version);
41 opkg_package_free (pkg);
42 }
43
44 void
45 print_package (opkg_package_t *pkg)
46 {
47 printf (
48 "Name: %s\n"
49 "Version: %s\n"
50 "Repository: %s\n"
51 "Architecture: %s\n"
52 "Description: %s\n"
53 "Tags: %s\n"
54 "URL: %s\n"
55 "Size: %d\n"
56 "Installed: %s\n",
57 pkg->name,
58 pkg->version,
59 pkg->repository,
60 pkg->architecture,
61 pkg->description,
62 pkg->tags,
63 pkg->url,
64 pkg->size,
65 (pkg->installed ? "True" : "False")
66 );
67 }
68
69 int
70 main (int argc, char **argv)
71 {
72 opkg_t *opkg;
73 opkg_package_t *pkg;
74 int err;
75
76 opkg = opkg_new ();
77
78 opkg_set_option (opkg, "offline_root", "/tmp/");
79
80 opkg_re_read_config_files (opkg);
81
82 err = opkg_update_package_lists (opkg, progress_callback, "Updating...");
83 printf ("\nopkg_update_package_lists returned %d\n", err);
84
85 opkg_list_packages (opkg, package_list_callback, NULL);
86 printf ("\n");
87
88 if (find_pkg)
89 {
90 printf ("Finding package \"%s\"\n", find_pkg->name);
91 pkg = opkg_find_package (opkg, find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->repository);
92 if (pkg)
93 {
94 print_package (pkg);
95 opkg_package_free (pkg);
96 }
97 else
98 printf ("Package \"%s\" not found!\n", find_pkg->name);
99 opkg_package_free (find_pkg);
100 }
101 else
102 printf ("No package available to test find_package.\n");
103
104 err = opkg_install_package (opkg, "aspell", progress_callback, "Installing...");
105 printf ("\nopkg_install_package returned %d\n", err);
106
107 err = opkg_upgrade_package (opkg, "aspell", progress_callback, "Upgrading...");
108 printf ("\nopkg_upgrade_package returned %d\n", err);
109
110 err = opkg_remove_package (opkg, "aspell", progress_callback, "Removing...");
111 printf ("\nopkg_remove_package returned %d\n", err);
112
113 printf ("Listing upgradable packages...\n");
114 opkg_list_upgradable_packages (opkg, package_list_upgradable_callback, NULL);
115
116 err = opkg_upgrade_all (opkg, progress_callback, "Upgrading all...");
117 printf ("\nopkg_upgrade_all returned %d\n", err);
118
119 opkg_free (opkg);
120
121 return 0;
122 }