Subversion Repositories Kolibri OS

Rev

Rev 5725 | Rev 5727 | 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_cache_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. int http_load_file(const char *path, const char *url)
  28. {
  29.     http_t *http;
  30.     int     received = 0;
  31.     int     offset = 0;
  32.     int     tail;
  33.     char   *buf;
  34.     int     fd;
  35.     int     i;
  36.  
  37.     buf = user_alloc(BUFFSIZE);
  38.     for(i = 0; i < 16; i++)
  39.         buf[i*4096] = 0;
  40.  
  41.     fd = open(path, O_CREAT|O_WRONLY);
  42.     if(fd == -1)
  43.     {
  44.         user_free(buf);
  45.         return 0;
  46.     };
  47.  
  48.     http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
  49.     if(http == NULL)
  50.         goto err_get;
  51.  
  52.     do
  53.     {
  54.         if(http_receive_with_retry(http, 500) == 0)
  55.         {
  56.             int count;
  57.  
  58.             if(http->flags & 0xffff0000)
  59.                 break;
  60.  
  61.             count = http->content_received - received;
  62.             if(count+offset <= BUFFSIZE)
  63.             {
  64.                 memcpy(buf+offset, http->content_ptr, count);
  65.                 offset+= count;
  66.             }    
  67.  
  68.  
  69.             else
  70.             {
  71.                 tail  = count+offset-BUFFSIZE;
  72.                 count = BUFFSIZE - offset;
  73.                 if(count)
  74.                 {
  75.                     memcpy(buf+offset, http->content_ptr, count);
  76.                     offset = 0;
  77.                 };
  78.  
  79.                 write(fd, buf, BUFFSIZE);
  80.  
  81.                 if(tail)
  82.                 {
  83.                     memcpy(buf, http->content_ptr+count, tail);
  84.                     offset = tail;
  85.                 }
  86.             }
  87.             received = http->content_received;
  88.         }
  89.         else break;
  90.  
  91.     }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
  92.  
  93.     if(offset)
  94.     {
  95.         write(fd, buf, offset);
  96.     }
  97.  
  98. //    ftruncate(fd, received);
  99.     close(fd);
  100.  
  101.     if(http->content_ptr)
  102.         user_free(http->content_ptr);
  103.     http_free(http);
  104.  
  105.     user_free(buf);
  106.  
  107.     return received;
  108.  
  109. err_get:
  110.     printf("HTTP GET failed\n");
  111.     return received;
  112. }
  113.  
  114.  
  115. int main(int argc, char *argv[])
  116. {
  117.     int   count;
  118.     char *cache_path;
  119.  
  120.     if(http_init())
  121.         goto err_init;
  122.  
  123.     cache_path = make_cache_path("packages.xml");
  124.  
  125.     count = http_load_file(cache_path, make_url("packages.xml"));
  126.  
  127.     if(count)
  128.     {
  129.         collection_t *collection;
  130.         pkg_group_t *gr;
  131.  
  132.         collection = load_collection_file(cache_path);
  133.  
  134.         list_for_each_entry(gr, &collection->groups, list)
  135.         {
  136.             package_t   *pkg;
  137.  
  138.             list_for_each_entry(pkg, &gr->packages, list)
  139.             {
  140.                 printf("package %s-%s\n", pkg->name, pkg->version);
  141.                 cache_path = make_cache_path(pkg->filename);
  142.                 count = http_load_file(cache_path, make_url(pkg->filename));
  143.                 printf("%s loaded %d\n",cache_path, count);
  144.             }
  145.         };
  146.      }
  147.  
  148.     return 0;
  149.  
  150. err_init:
  151.     printf("HTTP library initialization failed\n");
  152.     return -1;
  153. }
  154.