2
0

libopkg_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libgen.h>
  5. #include <opkg.h>
  6. int opkg_state_changed;
  7. pkg_t *find_pkg = NULL;
  8. #define TEST_PACKAGE "aspell"
  9. static void progress_callback(const opkg_progress_data_t * progress, void *data)
  10. {
  11. printf("\r%s %3d%%\n", (char *)data, progress->percentage);
  12. fflush(stdout);
  13. }
  14. static void list_pkg(pkg_t * pkg)
  15. {
  16. char *v = pkg_version_str_alloc(pkg);
  17. printf("%s - %s\n", pkg->name, v);
  18. free(v);
  19. }
  20. static void package_list_installed_callback(pkg_t * pkg, void *data)
  21. {
  22. if (pkg->state_status == SS_INSTALLED)
  23. list_pkg(pkg);
  24. }
  25. static void package_list_callback(pkg_t * pkg, void *data)
  26. {
  27. static int install_count = 0;
  28. static int total_count = 0;
  29. if (pkg->state_status == SS_INSTALLED)
  30. install_count++;
  31. total_count++;
  32. printf("\rPackage count: %d Installed, %d Total Available",
  33. install_count, total_count);
  34. fflush(stdout);
  35. if (!find_pkg) {
  36. /* store the first package to print out later */
  37. find_pkg = pkg;
  38. }
  39. }
  40. static void package_list_upgradable_callback(pkg_t * pkg, void *data)
  41. {
  42. list_pkg(pkg);
  43. }
  44. static void print_package(pkg_t * pkg)
  45. {
  46. char *v = pkg_version_str_alloc(pkg);
  47. const char *tags = pkg_get_string(pkg, PKG_TAGS);
  48. printf("Name: %s\n"
  49. "Version: %s\n"
  50. "Repository: %s\n"
  51. "Architecture: %s\n"
  52. "Description: %s\n"
  53. "Tags: %s\n"
  54. "Size: %lu\n"
  55. "Status: %d\n",
  56. pkg->name,
  57. v,
  58. pkg->src->name,
  59. pkg_get_architecture(pkg),
  60. pkg_get_string(pkg, PKG_DESCRIPTION),
  61. tags ? tags : "",
  62. (unsigned long) pkg_get_int(pkg, PKG_SIZE), pkg->state_status);
  63. free(v);
  64. }
  65. int main(int argc, char **argv)
  66. {
  67. pkg_t *pkg;
  68. int err;
  69. if (argc < 2) {
  70. printf("Usage: %s command\n"
  71. "\nCommands:\n"
  72. "\tupdate - Update package lists\n"
  73. "\tfind [package] - Print details of the specified package\n"
  74. "\tinstall [package] - Install the specified package\n"
  75. "\tupgrade [package] - Upgrade the specified package\n"
  76. "\tlist upgrades - List the available upgrades\n"
  77. "\tlist all - List all available packages\n"
  78. "\tlist installed - List all the installed packages\n"
  79. "\tremove [package] - Remove the specified package\n"
  80. "\trping - Reposiroties ping, check the accessibility of repositories\n"
  81. "\ttest - Run test script\n", basename(argv[0]));
  82. exit(0);
  83. }
  84. setenv("OFFLINE_ROOT", "/tmp", 0);
  85. if (opkg_new()) {
  86. printf("opkg_new() failed. This sucks.\n");
  87. print_error_list();
  88. return 1;
  89. }
  90. switch (argv[1][0]) {
  91. case 'f':
  92. pkg = opkg_find_package(argv[2], NULL, NULL, NULL);
  93. if (pkg) {
  94. print_package(pkg);
  95. } else
  96. printf("Package \"%s\" not found!\n", find_pkg->name);
  97. break;
  98. case 'i':
  99. err =
  100. opkg_install_package(argv[2], progress_callback,
  101. "Installing...");
  102. printf("\nopkg_install_package returned %d\n", err);
  103. break;
  104. case 'u':
  105. if (argv[1][2] == 'd') {
  106. err =
  107. opkg_update_package_lists(progress_callback,
  108. "Updating...");
  109. printf("\nopkg_update_package_lists returned %d\n",
  110. err);
  111. break;
  112. } else {
  113. if (argc < 3) {
  114. err =
  115. opkg_upgrade_all(progress_callback,
  116. "Upgrading all...");
  117. printf("\nopkg_upgrade_all returned %d\n", err);
  118. } else {
  119. err =
  120. opkg_upgrade_package(argv[2],
  121. progress_callback,
  122. "Upgrading...");
  123. printf("\nopkg_upgrade_package returned %d\n",
  124. err);
  125. }
  126. }
  127. break;
  128. case 'l':
  129. if (argc < 3) {
  130. printf
  131. ("Please specify one either all, installed or upgrades\n");
  132. } else {
  133. switch (argv[2][0]) {
  134. case 'u':
  135. printf("Listing upgradable packages...\n");
  136. opkg_list_upgradable_packages
  137. (package_list_upgradable_callback, NULL);
  138. break;
  139. case 'a':
  140. printf("Listing all packages...\n");
  141. opkg_list_packages(package_list_callback, NULL);
  142. printf("\n");
  143. break;
  144. case 'i':
  145. printf("Listing installed packages...\n");
  146. opkg_list_packages
  147. (package_list_installed_callback, NULL);
  148. break;
  149. default:
  150. printf("Unknown list option \"%s\"\n", argv[2]);
  151. }
  152. }
  153. break;
  154. case 'r':
  155. if (argv[1][1] == 'e') {
  156. err =
  157. opkg_remove_package(argv[2], progress_callback,
  158. "Removing...");
  159. printf("\nopkg_remove_package returned %d\n", err);
  160. break;
  161. } else if (argv[1][1] == 'p') {
  162. err = opkg_repository_accessibility_check();
  163. printf
  164. ("\nopkg_repository_accessibility_check returned (%d)\n",
  165. err);
  166. break;
  167. }
  168. default:
  169. printf("Unknown command \"%s\"\n", argv[1]);
  170. }
  171. opkg_free();
  172. return 0;
  173. }