Subversion Repositories Kolibri OS

Rev

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/stat.h>
  4. #include <sys/unistd.h>
  5. #include <fcntl.h>
  6. #include <kos32sys.h>
  7. #include "collection.h"
  8. #include "http.h"
  9.  
  10. #define BUFFSIZE  (64*1024)
  11.  
  12.  
  13. char *make_url(const char *name)
  14. {
  15.     static char url_buf[128] = "http://ftp.kolibrios.org/users/Serge/new/OS/";
  16.     strcpy(&url_buf[44], name);
  17.     return url_buf;
  18. };
  19.  
  20. char *make_tmp_path(const char *path)
  21. {
  22.     static char path_buf[64] = "/tmp0/1/";
  23.     strcpy(&path_buf[8], path);
  24.     return path_buf;
  25. };
  26.  
  27. char *make_cache_path(const char *path)
  28. {
  29.     static char path_buf[64] = "/kolibrios/kpm/cache/";
  30.     strcpy(&path_buf[21], path);
  31.     return path_buf;
  32. };
  33.  
  34. int http_load_file(const char *path, const char *url)
  35. {
  36.     http_t *http;
  37.     int     received = 0;
  38.     int     offset = 0;
  39.     int     tail;
  40.     char   *buf;
  41.     int     fd;
  42.     int     i;
  43.  
  44.     buf = user_alloc(BUFFSIZE);
  45.     for(i = 0; i < 16; i++)
  46.         buf[i*4096] = 0;
  47.  
  48.     fd = open(path, O_CREAT|O_WRONLY);
  49.     if(fd == -1)
  50.     {
  51.         user_free(buf);
  52.         return 0;
  53.     };
  54.  
  55.     http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
  56.     if(http == NULL)
  57.         goto err_get;
  58.  
  59.     do
  60.     {
  61.         if(http_receive_with_retry(http, 500) == 0)
  62.         {
  63.             int count;
  64.  
  65.             if(http->flags & 0xffff0000)
  66.                 break;
  67.  
  68.             count = http->content_received - received;
  69.             if(count+offset <= BUFFSIZE)
  70.             {
  71.                 memcpy(buf+offset, http->content_ptr, count);
  72.                 offset+= count;
  73.             }
  74.  
  75.  
  76.             else
  77.             {
  78.                 tail  = count+offset-BUFFSIZE;
  79.                 count = BUFFSIZE - offset;
  80.                 if(count)
  81.                 {
  82.                     memcpy(buf+offset, http->content_ptr, count);
  83.                     offset = 0;
  84.                 };
  85.  
  86.                 write(fd, buf, BUFFSIZE);
  87.  
  88.                 if(tail)
  89.                 {
  90.                     memcpy(buf, http->content_ptr+count, tail);
  91.                     offset = tail;
  92.                 }
  93.             }
  94.             received = http->content_received;
  95.         }
  96.         else break;
  97.  
  98.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  99.  
  100.     if(offset)
  101.     {
  102.         write(fd, buf, offset);
  103.     }
  104.  
  105. //    ftruncate(fd, received);
  106.     close(fd);
  107.  
  108.     if(http->content_ptr)
  109.         user_free(http->content_ptr);
  110.     http_free(http);
  111.  
  112.     user_free(buf);
  113.  
  114.     return received;
  115.  
  116. err_get:
  117.     printf("HTTP GET failed\n");
  118.     return received;
  119. }
  120.  
  121.  
  122. int main(int argc, char *argv[])
  123. {
  124.     int   count;
  125.     char *cache_path;
  126.     char *tmp_path;
  127.  
  128.     if(http_init())
  129.         goto err_init;
  130.  
  131.     tmp_path = make_tmp_path("packages.xml");
  132.  
  133.     count = http_load_file(tmp_path, make_url("packages.xml"));
  134.  
  135.     if(count)
  136.     {
  137.         collection_t *collection;
  138.         package_t   *pkg;
  139.         LIST_HEAD(install_list);
  140.         LIST_HEAD(download_list);
  141.  
  142.         collection = load_collection_file(tmp_path);
  143.  
  144.         if(collection && build_install_list(&install_list, collection))
  145.         {
  146.             if(build_download_list(&download_list, &install_list))
  147.             {
  148.                 list_for_each_entry(pkg, &download_list, list)
  149.                 {
  150.                     printf("package %s-%s\n", pkg->name, pkg->version);
  151.                     cache_path = make_cache_path(pkg->filename);
  152.                     count = http_load_file(cache_path, make_url(pkg->filename));
  153.                     printf("%s loaded %d bytes\n",cache_path, count);
  154.                 };
  155.             };
  156.         };
  157.      }
  158.  
  159.     return 0;
  160.  
  161. err_init:
  162.     printf("HTTP library initialization failed\n");
  163.     return -1;
  164. }
  165.