Subversion Repositories Kolibri OS

Rev

Rev 5737 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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