Subversion Repositories Kolibri OS

Rev

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

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