Subversion Repositories Kolibri OS

Rev

Rev 5728 | Rev 5731 | 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.             else
  78.             {
  79.                 tail  = count+offset-BUFFSIZE;
  80.                 count = BUFFSIZE - offset;
  81.                 if(count)
  82.                 {
  83.                     memcpy(buf+offset, http->content_ptr, count);
  84.                     offset = 0;
  85.                 };
  86.  
  87.                 write(fd, buf, BUFFSIZE);
  88.  
  89.                 if(tail)
  90.                 {
  91.                     memcpy(buf, http->content_ptr+count, tail);
  92.                     offset = tail;
  93.                 }
  94.             }
  95.             received = http->content_received;
  96.         }
  97.         else break;
  98.  
  99.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  100.  
  101.     if(offset)
  102.     {
  103.         write(fd, buf, offset);
  104.     }
  105.  
  106. //    ftruncate(fd, received);
  107.     close(fd);
  108.  
  109.     if(http->content_ptr)
  110.         user_free(http->content_ptr);
  111.     http_free(http);
  112.  
  113.     user_free(buf);
  114.  
  115.     return received;
  116.  
  117. err_get:
  118.     printf("HTTP GET failed\n");
  119.     return received;
  120. }
  121.  
  122.  
  123. int main(int argc, char *argv[])
  124. {
  125.     int   count;
  126.     char *cache_path;
  127.     char *tmp_path;
  128.  
  129.     if(http_init())
  130.         goto err_init;
  131.  
  132.     tmp_path = make_tmp_path("packages.xml");
  133.  
  134.     count = http_load_file(tmp_path, make_url("packages.xml"));
  135.  
  136.     if(count)
  137.     {
  138.         collection_t *collection;
  139.         package_t   *pkg;
  140.         LIST_HEAD(install_list);
  141.         LIST_HEAD(download_list);
  142.  
  143.         collection = load_collection_file(tmp_path);
  144.  
  145.         if(collection && build_install_list(&install_list, collection))
  146.         {
  147.             if(build_download_list(&download_list, &install_list))
  148.                 do_download(&download_list);
  149.  
  150.             if(!list_empty(&download_list))
  151.                 remove_missing_packages(&install_list, &download_list);
  152.  
  153.             list_for_each_entry(pkg, &install_list, list)
  154.                 printf("install package %s-%s\n", pkg->name, pkg->version);
  155.  
  156.             set_cwd("/tmp0/1");
  157.  
  158.             do_install(&install_list);
  159.         };
  160.      }
  161.  
  162.     return 0;
  163.  
  164. err_init:
  165.     printf("HTTP library initialization failed\n");
  166.     return -1;
  167. }
  168.  
  169. int build_install_list(list_t *list, collection_t *collection)
  170. {
  171.     pkg_group_t *gr;
  172.     int count = 0;
  173.  
  174.     list_for_each_entry(gr, &collection->groups, list)
  175.     {
  176.         package_t   *pkg, *tmp;
  177.  
  178.         list_for_each_entry(tmp, &gr->packages, list)
  179.         {
  180.             pkg = (package_t*)malloc(sizeof(package_t));
  181.  
  182.             INIT_LIST_HEAD(&pkg->file_list);
  183.             pkg->id       = tmp->id;
  184.             pkg->name     = strdup(tmp->name);
  185.             pkg->version  = strdup(tmp->version);
  186.             pkg->filename = strdup(tmp->filename);
  187.             pkg->description = strdup(tmp->description);
  188.             list_add_tail(&pkg->list, list);
  189.             count++;
  190.         }
  191.     };
  192.     return count;
  193. }
  194.  
  195. int build_download_list(list_t *download, list_t *src)
  196. {
  197.     int count = 0;
  198.     char *cache_path;
  199.     package_t   *pkg, *tmp;
  200.     fileinfo_t  info;
  201.     list_for_each_entry(tmp, src, list)
  202.     {
  203.         cache_path = make_cache_path(tmp->filename);
  204.  
  205.         if( get_fileinfo(cache_path, &info) != 0)
  206.         {
  207.             pkg = (package_t*)malloc(sizeof(package_t));
  208.  
  209.             INIT_LIST_HEAD(&pkg->file_list);
  210.             pkg->id       = tmp->id;
  211.             pkg->name     = strdup(tmp->name);
  212.             pkg->version  = strdup(tmp->version);
  213.             pkg->filename = strdup(tmp->filename);
  214.             pkg->description = strdup(tmp->description);
  215.             list_add_tail(&pkg->list, download);
  216.             count++;
  217.         };
  218.     }
  219.     return count;
  220. };
  221.  
  222. void do_download(list_t *download_list)
  223. {
  224.     package_t   *pkg, *tmp;
  225.     char        *cache_path;
  226.     int         count;
  227.  
  228.     list_for_each_entry_safe(pkg, tmp, download_list, list)
  229.     {
  230.         printf("package %s-%s\n", pkg->name, pkg->version);
  231.         cache_path = make_cache_path(pkg->filename);
  232.         count = http_load_file(cache_path, make_url(pkg->filename));
  233.         printf("%s loaded %d bytes\n",cache_path, count);
  234.         if( !test_archive(cache_path))
  235.             list_del_pkg(pkg);
  236.         else
  237.             unlink(cache_path);
  238.     };
  239. }
  240.  
  241. void remove_missing_packages(list_t *install, list_t *missed)
  242. {
  243.     package_t   *mpkg, *mtmp, *ipkg, *itmp;
  244.  
  245.     list_for_each_entry_safe(mpkg, mtmp, missed, list)
  246.     {
  247.         list_for_each_entry_safe(ipkg, itmp, install, list)
  248.         {
  249.             if(ipkg->id == mpkg->id)
  250.             {
  251.                 printf("skip missing package %s-%s\n", ipkg->name, ipkg->version);
  252.                 list_del_pkg(ipkg);
  253.             };
  254.         }
  255.         list_del_pkg(mpkg);
  256.     };
  257. };
  258.  
  259.  
  260.