Subversion Repositories Kolibri OS

Rev

Rev 5727 | Rev 5729 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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