Subversion Repositories Kolibri OS

Rev

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