Subversion Repositories Kolibri OS

Rev

Rev 5729 | Rev 5737 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <sys/unistd.h>
  6. #include <fcntl.h>
  7. #include <kos32sys.h>
  8. #include <sys/kos_io.h>
  9.  
  10. #include "package.h"
  11. #include "http.h"
  12.  
  13. #define BUFFSIZE  (64*1024)
  14.  
  15. char conbuf[256];
  16.  
  17. char *make_url(const char *name)
  18. {
  19.     static char url_buf[128] = "http://ftp.kolibrios.org/users/Serge/new/OS/";
  20.     strcpy(&url_buf[44], name);
  21.     return url_buf;
  22. };
  23.  
  24. char *make_tmp_path(const char *path)
  25. {
  26.     static char path_buf[64] = "/tmp0/1/";
  27.     strcpy(&path_buf[8], path);
  28.     return path_buf;
  29. };
  30.  
  31. char *make_cache_path(const char *path)
  32. {
  33.     static char path_buf[64] = "/kolibrios/kpm/cache/";
  34.     strcpy(&path_buf[21], path);
  35.     return path_buf;
  36. };
  37.  
  38. int http_load_file(const char *path, const char *url)
  39. {
  40.     http_t *http;
  41.     int     received = 0;
  42.     int     offset = 0;
  43.     int     tail;
  44.     char   *buf;
  45.     int     fd;
  46.     int     i;
  47.  
  48.     buf = user_alloc(BUFFSIZE);
  49.     for(i = 0; i < 16; i++)
  50.         buf[i*4096] = 0;
  51.  
  52.     fd = open(path, O_CREAT|O_WRONLY);
  53.     if(fd == -1)
  54.     {
  55.         user_free(buf);
  56.         return 0;
  57.     };
  58.  
  59.     http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
  60.     if(http == NULL)
  61.         goto err_get;
  62.  
  63.     do
  64.     {
  65.         if(http_receive_with_retry(http, 500) == 0)
  66.         {
  67.             int count;
  68.  
  69. //            if(http->flags & 0xffff0000)
  70. //                break;
  71.  
  72.             count = http->content_received - received;
  73.             if(count+offset <= BUFFSIZE)
  74.             {
  75.                 memcpy(buf+offset, http->content_ptr, count);
  76.                 offset+= count;
  77.             }
  78.             else
  79.             {
  80.                 tail  = count+offset-BUFFSIZE;
  81.                 count = BUFFSIZE - offset;
  82.                 if(count)
  83.                 {
  84.                     memcpy(buf+offset, http->content_ptr, count);
  85.                     offset = 0;
  86.                 };
  87.  
  88.                 write(fd, buf, BUFFSIZE);
  89.  
  90.                 if(tail)
  91.                 {
  92.                     memcpy(buf, http->content_ptr+count, tail);
  93.                     offset = tail;
  94.                 }
  95.  
  96.                 sprintf(conbuf, "%d bytes loaded\r", http->content_received);
  97.                 con_write_asciiz(conbuf);
  98.  
  99.             }
  100.             received = http->content_received;
  101.         }
  102.         else break;
  103.  
  104.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  105.  
  106.     if(offset)
  107.     {
  108.         write(fd, buf, offset);
  109.     }
  110.  
  111. //    ftruncate(fd, received);
  112.     close(fd);
  113.  
  114.     if(http->content_ptr)
  115.         user_free(http->content_ptr);
  116.     http_free(http);
  117.  
  118.     user_free(buf);
  119.  
  120.     return received;
  121.  
  122. err_get:
  123.     printf("HTTP GET failed\n");
  124.     return received;
  125. }
  126.  
  127. int main(int argc, char *argv[])
  128. {
  129.     int   count;
  130.     char *cache_path;
  131.     char *tmp_path;
  132.  
  133.     if(http_init())
  134.         goto err_init;
  135.  
  136.     con_init(80, 25, 80, 250, "Kolibri package manager");
  137.  
  138.     tmp_path = make_tmp_path("packages.xml");
  139.  
  140.     count = http_load_file(tmp_path, make_url("packages.xml"));
  141.  
  142.     if(count)
  143.     {
  144.         collection_t *collection;
  145.         package_t   *pkg;
  146.         LIST_HEAD(install_list);
  147.         LIST_HEAD(download_list);
  148.  
  149.         collection = load_collection_file(tmp_path);
  150.  
  151.         if(collection && build_install_list(&install_list, collection))
  152.         {
  153.             if(build_download_list(&download_list, &install_list))
  154.                 do_download(&download_list);
  155.  
  156.             if(!list_empty(&download_list))
  157.                 remove_missing_packages(&install_list, &download_list);
  158.  
  159.             list_for_each_entry(pkg, &install_list, list)
  160.             {
  161.                 sprintf(conbuf,"install package %s-%s\n", pkg->name, pkg->version);
  162.                 con_write_asciiz(conbuf);
  163.             };
  164.  
  165.             set_cwd("/tmp0/1");
  166.  
  167.             do_install(&install_list);
  168.         };
  169.     }
  170.  
  171.     con_exit(0);
  172.  
  173.     return 0;
  174.  
  175. err_init:
  176.     printf("HTTP library initialization failed\n");
  177.     return -1;
  178. }
  179.  
  180. int build_install_list(list_t *list, collection_t *collection)
  181. {
  182.     pkg_group_t *gr;
  183.     int count = 0;
  184.  
  185.     list_for_each_entry(gr, &collection->groups, list)
  186.     {
  187.         package_t   *pkg, *tmp;
  188.  
  189.         list_for_each_entry(tmp, &gr->packages, list)
  190.         {
  191.             pkg = (package_t*)malloc(sizeof(package_t));
  192.  
  193.             INIT_LIST_HEAD(&pkg->file_list);
  194.             pkg->id       = tmp->id;
  195.             pkg->name     = strdup(tmp->name);
  196.             pkg->version  = strdup(tmp->version);
  197.             pkg->filename = strdup(tmp->filename);
  198.             pkg->description = strdup(tmp->description);
  199.             list_add_tail(&pkg->list, list);
  200.             count++;
  201.         }
  202.     };
  203.     return count;
  204. }
  205.  
  206. int build_download_list(list_t *download, list_t *src)
  207. {
  208.     int count = 0;
  209.     char *cache_path;
  210.     package_t   *pkg, *tmp;
  211.     fileinfo_t  info;
  212.     list_for_each_entry(tmp, src, list)
  213.     {
  214.         cache_path = make_cache_path(tmp->filename);
  215.  
  216.         if( get_fileinfo(cache_path, &info) != 0)
  217.         {
  218.             pkg = (package_t*)malloc(sizeof(package_t));
  219.  
  220.             INIT_LIST_HEAD(&pkg->file_list);
  221.             pkg->id       = tmp->id;
  222.             pkg->name     = strdup(tmp->name);
  223.             pkg->version  = strdup(tmp->version);
  224.             pkg->filename = strdup(tmp->filename);
  225.             pkg->description = strdup(tmp->description);
  226.             list_add_tail(&pkg->list, download);
  227.             count++;
  228.         };
  229.     }
  230.     return count;
  231. };
  232.  
  233. void do_download(list_t *download_list)
  234. {
  235.     package_t   *pkg, *tmp;
  236.     char        *cache_path;
  237.     int         count;
  238.  
  239.     list_for_each_entry_safe(pkg, tmp, download_list, list)
  240.     {
  241.         sprintf(conbuf,"package %s-%s\n", pkg->name, pkg->version);
  242.         con_write_asciiz(conbuf);
  243.         cache_path = make_cache_path(pkg->filename);
  244.         count = http_load_file(cache_path, make_url(pkg->filename));
  245.         sprintf(conbuf,"%s %d bytes loaded\n",cache_path, count);
  246.         con_write_asciiz(conbuf);
  247.         if( !test_archive(cache_path))
  248.             list_del_pkg(pkg);
  249.         else
  250.             unlink(cache_path);
  251.     };
  252. }
  253.  
  254. void remove_missing_packages(list_t *install, list_t *missed)
  255. {
  256.     package_t   *mpkg, *mtmp, *ipkg, *itmp;
  257.  
  258.     list_for_each_entry_safe(mpkg, mtmp, missed, list)
  259.     {
  260.         list_for_each_entry_safe(ipkg, itmp, install, list)
  261.         {
  262.             if(ipkg->id == mpkg->id)
  263.             {
  264.                 sprintf(conbuf,"skip missing package %s-%s\n", ipkg->name, ipkg->version);
  265.                 con_write_asciiz(conbuf);
  266.                 list_del_pkg(ipkg);
  267.             };
  268.         }
  269.         list_del_pkg(mpkg);
  270.     };
  271. };
  272.  
  273.  
  274.