Subversion Repositories Kolibri OS

Rev

Rev 5725 | Rev 5727 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5725 serge 1
#include 
2
#include 
3
#include 
4
#include 
5
#include 
6
#include 
7
#include "collection.h"
8
#include "http.h"
9
 
10
#define BUFFSIZE  (64*1024)
11
 
5726 serge 12
 
13
char *make_url(const char *name)
5725 serge 14
{
5726 serge 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
};
5725 serge 19
 
5726 serge 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
};
5725 serge 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
                tail  = count+offset-BUFFSIZE;
70
                count = BUFFSIZE - offset;
71
                if(count)
72
                {
73
                    memcpy(buf+offset, http->content_ptr, count);
74
                    offset = 0;
75
                };
76
77
                write(fd, buf, BUFFSIZE);
78
 
79
                if(tail)
80
 
81
                    memcpy(buf, http->content_ptr+count, tail);
82
                    offset = tail;
83
                }
84
            }
85
            received = http->content_received;
86
        }
87
        else break;
88
89
    }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
90
 
91
    if(offset)
92
 
93
        write(fd, buf, offset);
94
    }
95
96
//    ftruncate(fd, received);
97
 
98
99
    if(http->content_ptr)
100
 
101
    http_free(http);
102
103
    user_free(buf);
104
 
105
    return received;
106
 
107
err_get:
108
 
109
    return received;
110
}
111
112
113
 
114
 
115
    int   count;
116
    char *cache_path;
117
5726 serge 118
    if(http_init())
5725 serge 119
 
120
121
    cache_path = make_cache_path("packages.xml");
122
 
5726 serge 123
    count = http_load_file(cache_path, make_url("packages.xml"));
5725 serge 124
 
5726 serge 125
    if(count)
126
 
5725 serge 127
        collection_t *collection;
128
        pkg_group_t *gr;
129
130
        collection = load_collection_file(cache_path);
131
 
5726 serge 132
        list_for_each_entry(gr, &collection->groups, list)
5725 serge 133
 
134
            package_t   *pkg;
135
136
            list_for_each_entry(pkg, &gr->packages, list)
137
 
138
                printf("package %s-%s\n", pkg->name, pkg->version);
139
                cache_path = make_cache_path(pkg->filename);
140
                count = http_load_file(cache_path, make_url(pkg->filename));
5726 serge 141
                printf("%s loaded %d\n",cache_path, count);
142
            }
143
        };
5725 serge 144
     }
145
146
    return 0;
147
 
148
err_init:
149
 
150
    return -1;
151
}
152