Rev 5726 | Rev 5728 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5726 | Rev 5727 | ||
---|---|---|---|
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 "collection.h" |
7 | #include "collection.h" |
8 | #include "http.h" |
8 | #include "http.h" |
9 | 9 | ||
10 | #define BUFFSIZE (64*1024) |
10 | #define BUFFSIZE (64*1024) |
11 | 11 | ||
12 | 12 | ||
13 | char *make_url(const char *name) |
13 | char *make_url(const char *name) |
14 | { |
14 | { |
15 | static char url_buf[128] = "http://ftp.kolibrios.org/users/Serge/new/OS/"; |
15 | static char url_buf[128] = "http://ftp.kolibrios.org/users/Serge/new/OS/"; |
16 | strcpy(&url_buf[44], name); |
16 | strcpy(&url_buf[44], name); |
17 | return url_buf; |
17 | return url_buf; |
18 | }; |
18 | }; |
19 | 19 | ||
20 | char *make_cache_path(const char *path) |
20 | char *make_tmp_path(const char *path) |
21 | { |
21 | { |
22 | static char path_buf[64] = "/tmp0/1/"; |
22 | static char path_buf[64] = "/tmp0/1/"; |
23 | strcpy(&path_buf[8], path); |
23 | strcpy(&path_buf[8], path); |
24 | return path_buf; |
24 | return path_buf; |
25 | }; |
25 | }; |
- | 26 | ||
- | 27 | char *make_cache_path(const char *path) |
|
- | 28 | { |
|
- | 29 | static char path_buf[64] = "/kolibrios/kpm/cache/"; |
|
- | 30 | strcpy(&path_buf[21], path); |
|
- | 31 | return path_buf; |
|
- | 32 | }; |
|
26 | 33 | ||
27 | int http_load_file(const char *path, const char *url) |
34 | int http_load_file(const char *path, const char *url) |
28 | { |
35 | { |
29 | http_t *http; |
36 | http_t *http; |
30 | int received = 0; |
37 | int received = 0; |
31 | int offset = 0; |
38 | int offset = 0; |
32 | int tail; |
39 | int tail; |
33 | char *buf; |
40 | char *buf; |
34 | int fd; |
41 | int fd; |
35 | int i; |
42 | int i; |
36 | 43 | ||
37 | buf = user_alloc(BUFFSIZE); |
44 | buf = user_alloc(BUFFSIZE); |
38 | for(i = 0; i < 16; i++) |
45 | for(i = 0; i < 16; i++) |
39 | buf[i*4096] = 0; |
46 | buf[i*4096] = 0; |
40 | 47 | ||
41 | fd = open(path, O_CREAT|O_WRONLY); |
48 | fd = open(path, O_CREAT|O_WRONLY); |
42 | if(fd == -1) |
49 | if(fd == -1) |
43 | { |
50 | { |
44 | user_free(buf); |
51 | user_free(buf); |
45 | return 0; |
52 | return 0; |
46 | }; |
53 | }; |
47 | 54 | ||
48 | http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL); |
55 | http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL); |
49 | if(http == NULL) |
56 | if(http == NULL) |
50 | goto err_get; |
57 | goto err_get; |
51 | 58 | ||
52 | do |
59 | do |
53 | { |
60 | { |
54 | if(http_receive_with_retry(http, 500) == 0) |
61 | if(http_receive_with_retry(http, 500) == 0) |
55 | { |
62 | { |
56 | int count; |
63 | int count; |
57 | 64 | ||
58 | if(http->flags & 0xffff0000) |
65 | if(http->flags & 0xffff0000) |
59 | break; |
66 | break; |
60 | 67 | ||
61 | count = http->content_received - received; |
68 | count = http->content_received - received; |
62 | if(count+offset <= BUFFSIZE) |
69 | if(count+offset <= BUFFSIZE) |
63 | { |
70 | { |
64 | memcpy(buf+offset, http->content_ptr, count); |
71 | memcpy(buf+offset, http->content_ptr, count); |
65 | offset+= count; |
72 | offset+= count; |
66 | } |
73 | } |
67 | else |
74 | else |
68 | { |
75 | { |
69 | tail = count+offset-BUFFSIZE; |
76 | tail = count+offset-BUFFSIZE; |
70 | count = BUFFSIZE - offset; |
77 | count = BUFFSIZE - offset; |
71 | if(count) |
78 | if(count) |
72 | { |
79 | { |
73 | memcpy(buf+offset, http->content_ptr, count); |
80 | memcpy(buf+offset, http->content_ptr, count); |
74 | offset = 0; |
81 | offset = 0; |
75 | }; |
82 | }; |
76 | 83 | ||
77 | write(fd, buf, BUFFSIZE); |
84 | write(fd, buf, BUFFSIZE); |
78 | 85 | ||
79 | if(tail) |
86 | if(tail) |
80 | { |
87 | { |
81 | memcpy(buf, http->content_ptr+count, tail); |
88 | memcpy(buf, http->content_ptr+count, tail); |
82 | offset = tail; |
89 | offset = tail; |
83 | } |
90 | } |
84 | } |
91 | } |
85 | received = http->content_received; |
92 | received = http->content_received; |
86 | } |
93 | } |
87 | else break; |
94 | else break; |
88 | 95 | ||
89 | }while( (http->flags & FLAG_GOT_ALL_DATA) == 0); |
96 | }while( (http->flags & FLAG_GOT_ALL_DATA) == 0); |
90 | 97 | ||
91 | if(offset) |
98 | if(offset) |
92 | { |
99 | { |
93 | write(fd, buf, offset); |
100 | write(fd, buf, offset); |
94 | } |
101 | } |
95 | 102 | ||
96 | // ftruncate(fd, received); |
103 | // ftruncate(fd, received); |
97 | close(fd); |
104 | close(fd); |
98 | 105 | ||
99 | if(http->content_ptr) |
106 | if(http->content_ptr) |
100 | user_free(http->content_ptr); |
107 | user_free(http->content_ptr); |
101 | http_free(http); |
108 | http_free(http); |
102 | 109 | ||
103 | user_free(buf); |
110 | user_free(buf); |
104 | 111 | ||
105 | return received; |
112 | return received; |
106 | 113 | ||
107 | err_get: |
114 | err_get: |
108 | printf("HTTP GET failed\n"); |
115 | printf("HTTP GET failed\n"); |
109 | return received; |
116 | return received; |
110 | } |
117 | } |
111 | 118 | ||
112 | 119 | ||
113 | int main(int argc, char *argv[]) |
120 | int main(int argc, char *argv[]) |
114 | { |
121 | { |
115 | int count; |
122 | int count; |
116 | char *cache_path; |
123 | char *cache_path; |
- | 124 | char *tmp_path; |
|
117 | 125 | ||
118 | if(http_init()) |
126 | if(http_init()) |
119 | goto err_init; |
127 | goto err_init; |
120 | 128 | ||
121 | cache_path = make_cache_path("packages.xml"); |
129 | tmp_path = make_tmp_path("packages.xml"); |
122 | 130 | ||
123 | count = http_load_file(cache_path, make_url("packages.xml")); |
131 | count = http_load_file(tmp_path, make_url("packages.xml")); |
124 | 132 | ||
125 | if(count) |
133 | if(count) |
126 | { |
134 | { |
127 | collection_t *collection; |
135 | collection_t *collection; |
128 | pkg_group_t *gr; |
136 | package_t *pkg; |
- | 137 | LIST_HEAD(install_list); |
|
- | 138 | LIST_HEAD(download_list); |
|
129 | 139 | ||
130 | collection = load_collection_file(cache_path); |
140 | collection = load_collection_file(tmp_path); |
131 | 141 | ||
- | 142 | if(collection && build_install_list(&install_list, collection)) |
|
132 | list_for_each_entry(gr, &collection->groups, list) |
143 | { |
133 | { |
- | |
134 | package_t *pkg; |
144 | if(build_download_list(&download_list, &install_list)) |
135 | 145 | { |
|
136 | list_for_each_entry(pkg, &gr->packages, list) |
146 | list_for_each_entry(pkg, &download_list, list) |
137 | { |
147 | { |
138 | printf("package %s-%s\n", pkg->name, pkg->version); |
148 | printf("package %s-%s\n", pkg->name, pkg->version); |
139 | cache_path = make_cache_path(pkg->filename); |
149 | cache_path = make_cache_path(pkg->filename); |
140 | count = http_load_file(cache_path, make_url(pkg->filename)); |
150 | count = http_load_file(cache_path, make_url(pkg->filename)); |
141 | printf("%s loaded %d\n",cache_path, count); |
151 | printf("%s loaded %d bytes\n",cache_path, count); |
- | 152 | }; |
|
142 | } |
153 | }; |
143 | }; |
154 | }; |
144 | } |
155 | } |
145 | 156 | ||
146 | return 0; |
157 | return 0; |
147 | 158 | ||
148 | err_init: |
159 | err_init: |
149 | printf("HTTP library initialization failed\n"); |
160 | printf("HTTP library initialization failed\n"); |
150 | return -1; |
161 | return -1; |
151 | }=>> |
162 | }=>> |