Rev 5737 | Details | Compare with Previous | 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); |
||
5809 | serge | 146 | pkg->group = strdup(tmp->group); |
5737 | serge | 147 | pkg->filename = strdup(tmp->filename); |
148 | pkg->description = strdup(tmp->description); |
||
149 | list_add_tail(&pkg->list, download); |
||
150 | count++; |
||
151 | }; |
||
152 | } |
||
153 | return count; |
||
154 | }; |
||
155 | |||
156 | void do_download(list_t *download_list) |
||
157 | { |
||
158 | package_t *pkg, *tmp; |
||
159 | char *cache_path; |
||
160 | int count; |
||
161 | |||
162 | list_for_each_entry_safe(pkg, tmp, download_list, list) |
||
163 | { |
||
164 | sprintf(conbuf,"package %s-%s\n", pkg->name, pkg->version); |
||
165 | con_write_asciiz(conbuf); |
||
166 | cache_path = make_cache_path(pkg->filename); |
||
167 | count = http_load_file(cache_path, make_url(pkg->filename)); |
||
168 | sprintf(conbuf,"%s %d bytes loaded\n",cache_path, count); |
||
169 | con_write_asciiz(conbuf); |
||
5809 | serge | 170 | |
5737 | serge | 171 | if( !test_archive(cache_path)) |
172 | list_del_pkg(pkg); |
||
173 | else |
||
174 | unlink(cache_path); |
||
175 | }; |
||
176 | } |
||
177 | |||
178 | void remove_missing_packages(list_t *install, list_t *missed) |
||
179 | { |
||
180 | package_t *mpkg, *mtmp, *ipkg, *itmp; |
||
181 | |||
182 | list_for_each_entry_safe(mpkg, mtmp, missed, list) |
||
183 | { |
||
184 | list_for_each_entry_safe(ipkg, itmp, install, list) |
||
185 | { |
||
186 | if(ipkg->id == mpkg->id) |
||
187 | { |
||
188 | sprintf(conbuf,"skip missing package %s-%s\n", ipkg->name, ipkg->version); |
||
189 | con_write_asciiz(conbuf); |
||
190 | list_del_pkg(ipkg); |
||
191 | }; |
||
192 | } |
||
193 | list_del_pkg(mpkg); |
||
194 | }; |
||
195 | }; |
||
196 | |||
5809 | serge | 197 | int copy_list(list_t *list, list_t *src) |
5737 | serge | 198 | { |
5809 | serge | 199 | package_t *pkg, *tmp; |
5737 | serge | 200 | int count = 0; |
201 | |||
5809 | serge | 202 | list_for_each_entry(tmp, src, list) |
5737 | serge | 203 | { |
5809 | serge | 204 | pkg = (package_t*)malloc(sizeof(package_t)); |
5737 | serge | 205 | |
5809 | serge | 206 | INIT_LIST_HEAD(&pkg->file_list); |
207 | pkg->id = tmp->id; |
||
208 | pkg->name = strdup(tmp->name); |
||
209 | pkg->version = strdup(tmp->version); |
||
210 | pkg->group = strdup(tmp->group); |
||
211 | pkg->filename = strdup(tmp->filename); |
||
212 | pkg->description = strdup(tmp->description); |
||
213 | list_add_tail(&pkg->list, list); |
||
214 | count++; |
||
5737 | serge | 215 | }; |
216 | return count; |
||
217 | } |
||
218 | |||
219 | int build_server_list(list_t *slist, const char *path) |
||
220 | { |
||
221 | collection_t *collection; |
||
222 | package_t *pkg; |
||
223 | LIST_HEAD(install_list); |
||
224 | LIST_HEAD(download_list); |
||
225 | int count = 0; |
||
226 | |||
227 | collection = load_collection_file(path); |
||
228 | |||
229 | if(collection) |
||
230 | { |
||
5809 | serge | 231 | package_t *pkg, *tmp; |
5737 | serge | 232 | |
5809 | serge | 233 | list_for_each_entry(tmp, &collection->packages, list) |
5737 | serge | 234 | { |
5809 | serge | 235 | pkg = (package_t*)malloc(sizeof(package_t)); |
5737 | serge | 236 | |
5809 | serge | 237 | INIT_LIST_HEAD(&pkg->file_list); |
238 | pkg->id = tmp->id; |
||
239 | pkg->name = strdup(tmp->name); |
||
240 | pkg->version = strdup(tmp->version); |
||
241 | pkg->group = strdup(tmp->group); |
||
242 | pkg->filename = strdup(tmp->filename); |
||
243 | pkg->description = strdup(tmp->description); |
||
244 | list_add_tail(&pkg->list, slist); |
||
245 | count++; |
||
5737 | serge | 246 | }; |
247 | }; |
||
248 | return count; |
||
249 | } |
||
250 | |||
251 | void print_pkg_list(list_t *list) |
||
252 | { |
||
253 | package_t *pkg; |
||
254 | |||
255 | list_for_each_entry(pkg, list, list) |
||
256 | { |
||
5809 | serge | 257 | sprintf(conbuf,"%s-%s-%s\n", pkg->name, pkg->version, pkg->group); |
5737 | serge | 258 | con_write_asciiz(conbuf); |
259 | } |
||
260 | } |
||
5809 | serge | 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 | }=>> |