Rev 5731 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5731 | Rev 5737 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #include |
1 | #include |
2 | #include |
2 | #include |
3 | #include |
3 | #include |
4 | #include |
- | |
5 | #include |
- | |
6 | #include |
- | |
7 | #include |
4 | #include |
8 | #include |
5 | #include |
Line -... | Line 6... | ||
- | 6 | ||
9 | 7 | #include "getopt.h" |
|
10 | #include "package.h" |
8 | #include "package.h" |
Line 11... | Line 9... | ||
11 | #include "http.h" |
9 | #include "http.h" |
Line 12... | Line 10... | ||
12 | 10 | ||
Line 13... | Line -... | ||
13 | #define BUFFSIZE (64*1024) |
- | |
14 | - | ||
15 | char conbuf[256]; |
- | |
16 | 11 | #define BUFFSIZE (64*1024) |
|
17 | char *make_url(const char *name) |
- | |
18 | { |
- | |
Line 19... | Line 12... | ||
19 | static char url_buf[128] = "http://ftp.kolibrios.org/users/Serge/new/OS/"; |
12 | |
20 | strcpy(&url_buf[44], name); |
13 | extern char conbuf[256]; |
21 | return url_buf; |
14 | |
22 | }; |
15 | #define OPTION_STD_BASE 150 |
23 | 16 | ||
24 | char *make_tmp_path(const char *path) |
17 | enum option_values |
Line 25... | Line 18... | ||
25 | { |
18 | { |
26 | static char path_buf[64] = "/tmp0/1/"; |
19 | OPTION_HELP = OPTION_STD_BASE, |
27 | strcpy(&path_buf[8], path); |
20 | OPTION_LIST_PACKAGES, |
28 | return path_buf; |
21 | OPTION_LIST_INSTALLED |
29 | }; |
22 | }; |
30 | 23 | ||
Line 31... | Line -... | ||
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 | } else |
- | |
78 | { |
- | |
79 | tail = count+offset-BUFFSIZE; |
- | |
80 | count = BUFFSIZE - offset; |
- | |
81 | if(count) |
- | |
82 | { |
- | |
83 | memcpy(buf+offset, http->content_ptr, count); |
- | |
84 | offset = 0; |
- | |
85 | }; |
- | |
86 | - | ||
87 | write(fd, buf, BUFFSIZE); |
- | |
88 | - | ||
89 | if(tail) |
- | |
90 | { |
- | |
91 | memcpy(buf, http->content_ptr+count, tail); |
- | |
92 | offset = tail; |
- | |
93 | } |
- | |
94 | - | ||
95 | sprintf(conbuf, "%d bytes loaded\r", http->content_received); |
- | |
96 | con_write_asciiz(conbuf); |
- | |
97 | - | ||
98 | } |
- | |
99 | received = http->content_received; |
- | |
100 | } |
- | |
101 | else break; |
- | |
102 | - | ||
103 | }while( (http->flags & FLAG_GOT_ALL_DATA) == 0); |
- | |
104 | - | ||
105 | if(offset) |
- | |
106 | { |
- | |
107 | write(fd, buf, offset); |
- | |
108 | } |
- | |
109 | - | ||
110 | // ftruncate(fd, received); |
- | |
111 | close(fd); |
- | |
112 | - | ||
113 | if(http->content_ptr) |
- | |
114 | user_free(http->content_ptr); |
- | |
115 | http_free(http); |
- | |
116 | - | ||
117 | user_free(buf); |
- | |
118 | - | ||
119 | return received; |
24 | static const struct option longopts[] = |
120 | 25 | { |
|
- | 26 | {"list-packages", no_argument, NULL, OPTION_LIST_PACKAGES}, |
|
- | 27 | {"list-installed",no_argument, NULL, OPTION_LIST_INSTALLED}, |
|
- | 28 | {NULL,0,NULL,0} |
|
- | 29 | }; |
|
- | 30 | ||
- | 31 | int main(int argc, char *argv[]) |
|
121 | err_get: |
32 | { |
122 | printf("HTTP GET failed\n"); |
33 | LIST_HEAD(server_list); |
123 | return received; |
34 | LIST_HEAD(download_list); |
Line 124... | Line 35... | ||
124 | } |
35 | LIST_HEAD(cache_list); |
125 | 36 | LIST_HEAD(local_list); |
|
Line -... | Line 37... | ||
- | 37 | LIST_HEAD(task_list); |
|
- | 38 | ||
126 | int main(int argc, char *argv[]) |
39 | int count; |
Line 127... | Line 40... | ||
127 | { |
40 | char *cache_path; |
Line 128... | Line 41... | ||
128 | int count; |
41 | char *tmp_path; |
Line 129... | Line 42... | ||
129 | char *cache_path; |
42 | |
- | 43 | if(http_init()) |
|
- | 44 | goto err_init; |
|
- | 45 | ||
- | 46 | set_cwd("/tmp0/1"); |
|
- | 47 | ||
- | 48 | con_init(80, 25, 80, 250, "Kolibri package manager"); |
|
- | 49 | ||
- | 50 | tmp_path = make_tmp_path("packages.xml"); |
|
- | 51 | ||
- | 52 | count = http_load_file(tmp_path, make_url("packages.xml")); |
|
- | 53 | ||
- | 54 | if(count) |
|
- | 55 | build_server_list(&server_list, tmp_path); |
|
- | 56 | ||
- | 57 | while(1) |
|
- | 58 | { |
|
- | 59 | int val; |
|
- | 60 | int index; |
|
- | 61 | ||
- | 62 | val = getopt_long_only(argc, argv,"",longopts, &index); |
|
- | 63 | ||
- | 64 | if(val == -1) |
|
- | 65 | break; |
|
- | 66 | ||
- | 67 | switch(val) |
|
- | 68 | { |
|
- | 69 | case OPTION_LIST_PACKAGES: |
|
- | 70 | sprintf(conbuf,"available packages:\n\n"); |
|
- | 71 | con_write_asciiz(conbuf); |
|
- | 72 | print_pkg_list(&server_list); |
|
- | 73 | con_exit(0); |
|
- | 74 | return 0; |
|
- | 75 | ||
- | 76 | case OPTION_LIST_INSTALLED: |
|
130 | char *tmp_path; |
77 | sprintf(conbuf,"installed packages:\n\n"); |
131 | - | ||
132 | if(http_init()) |
78 | con_write_asciiz(conbuf); |
133 | goto err_init; |
79 | print_pkg_list(&local_list); |
134 | 80 | con_exit(0); |
|
Line 135... | Line -... | ||
135 | con_init(80, 25, 80, 250, "Kolibri package manager"); |
- | |
136 | - | ||
137 | tmp_path = make_tmp_path("packages.xml"); |
81 | return 0; |
138 | 82 | ||
139 | count = http_load_file(tmp_path, make_url("packages.xml")); |
83 | default: |
140 | 84 | break; |
|
Line 159... | Line 103... | ||
159 | { |
103 | { |
160 | sprintf(conbuf,"install package %s-%s\n", pkg->name, pkg->version); |
104 | sprintf(conbuf,"install package %s-%s\n", pkg->name, pkg->version); |
161 | con_write_asciiz(conbuf); |
105 | con_write_asciiz(conbuf); |
162 | }; |
106 | }; |
Line 163... | Line -... | ||
163 | - | ||
164 | set_cwd("/tmp0/1"); |
- | |
165 | 107 | ||
166 | do_install(&install_list); |
108 | do_install(&install_list); |
167 | }; |
109 | }; |
- | 110 | } |
|
Line 168... | Line 111... | ||
168 | } |
111 | #endif |
Line 169... | Line 112... | ||
169 | 112 | ||
Line 170... | Line 113... | ||
170 | con_exit(0); |
113 | con_exit(0); |
171 | 114 | ||
172 | return 0; |
115 | return 0; |
173 | 116 | ||
Line 174... | Line -... | ||
174 | err_init: |
- | |
175 | printf("HTTP library initialization failed\n"); |
- | |
176 | return -1; |
- | |
177 | } |
- | |
178 | - | ||
179 | int build_install_list(list_t *list, collection_t *collection) |
- | |
180 | { |
- | |
181 | pkg_group_t *gr; |
- | |
182 | int count = 0; |
- | |
183 | - | ||
184 | list_for_each_entry(gr, &collection->groups, list) |
- | |
185 | { |
- | |
186 | package_t *pkg, *tmp; |
- | |
187 | - | ||
188 | list_for_each_entry(tmp, &gr->packages, list) |
- | |
189 | { |
- | |
190 | pkg = (package_t*)malloc(sizeof(package_t)); |
- | |
191 | - | ||
192 | INIT_LIST_HEAD(&pkg->file_list); |
- | |
193 | pkg->id = tmp->id; |
- | |
194 | pkg->name = strdup(tmp->name); |
- | |
195 | pkg->version = strdup(tmp->version); |
- | |
196 | pkg->filename = strdup(tmp->filename); |
- | |
197 | pkg->description = strdup(tmp->description); |
- | |
198 | list_add_tail(&pkg->list, list); |
- | |
199 | count++; |
- | |
200 | } |
- | |
201 | }; |
- | |
202 | return count; |
- | |
203 | } |
- | |
204 | - | ||
205 | int build_download_list(list_t *download, list_t *src) |
- | |
206 | { |
- | |
207 | int count = 0; |
- | |
208 | char *cache_path; |
- | |
209 | package_t *pkg, *tmp; |
- | |
210 | fileinfo_t info; |
- | |
211 | list_for_each_entry(tmp, src, list) |
- | |
212 | { |
- | |
213 | cache_path = make_cache_path(tmp->filename); |
- | |
214 | - | ||
215 | if( get_fileinfo(cache_path, &info) != 0) |
- | |
216 | { |
- | |
217 | pkg = (package_t*)malloc(sizeof(package_t)); |
- | |
218 | - | ||
219 | INIT_LIST_HEAD(&pkg->file_list); |
- | |
220 | pkg->id = tmp->id; |
- | |
221 | pkg->name = strdup(tmp->name); |
- | |
222 | pkg->version = strdup(tmp->version); |
- | |
223 | pkg->filename = strdup(tmp->filename); |
- | |
224 | pkg->description = strdup(tmp->description); |
- | |
225 | list_add_tail(&pkg->list, download); |
- | |
226 | count++; |
- | |
227 | }; |
- | |
228 | } |
- | |
229 | return count; |
- | |
230 | }; |
- | |
231 | - | ||
232 | void do_download(list_t *download_list) |
- | |
233 | { |
- | |
234 | package_t *pkg, *tmp; |
- | |
235 | char *cache_path; |
- | |
236 | int count; |
- | |
237 | - | ||
238 | list_for_each_entry_safe(pkg, tmp, download_list, list) |
- | |
239 | { |
- | |
240 | sprintf(conbuf,"package %s-%s\n", pkg->name, pkg->version); |
- | |
241 | con_write_asciiz(conbuf); |
- | |
242 | cache_path = make_cache_path(pkg->filename); |
- | |
243 | count = http_load_file(cache_path, make_url(pkg->filename)); |
- | |
244 | sprintf(conbuf,"%s %d bytes loaded\n",cache_path, count); |
- | |
245 | con_write_asciiz(conbuf); |
- | |
246 | if( !test_archive(cache_path)) |
- | |
247 | list_del_pkg(pkg); |
- | |
248 | else |
- | |
249 | unlink(cache_path); |
- | |
250 | }; |
- | |
Line 251... | Line -... | ||
251 | } |
- | |
252 | - | ||
253 | void remove_missing_packages(list_t *install, list_t *missed) |
- | |
254 | { |
- | |
255 | package_t *mpkg, *mtmp, *ipkg, *itmp; |
- | |
256 | - | ||
257 | list_for_each_entry_safe(mpkg, mtmp, missed, list) |
- | |
258 | { |
- | |
259 | list_for_each_entry_safe(ipkg, itmp, install, list) |
- | |
260 | { |
- | |
261 | if(ipkg->id == mpkg->id) |
- | |
262 | { |
- | |
263 | sprintf(conbuf,"skip missing package %s-%s\n", ipkg->name, ipkg->version); |
- | |
264 | con_write_asciiz(conbuf); |
- |