Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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