Subversion Repositories Kolibri OS

Rev

Rev 5731 | Go to most recent revision | Show entire file | Regard 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 24...
31
char *make_cache_path(const char *path)
24
static const struct option longopts[] =
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
 
25
{
48
    buf = user_alloc(BUFFSIZE);
26
    {"list-packages", no_argument, NULL, OPTION_LIST_PACKAGES},
49
    for(i = 0; i < 16; i++)
27
    {"list-installed",no_argument, NULL, OPTION_LIST_INSTALLED},
50
        buf[i*4096] = 0;
-
 
51
 
-
 
52
    fd = open(path, O_CREAT|O_WRONLY);
28
    {NULL,0,NULL,0}
53
    if(fd == -1)
29
};
54
    {
30
 
Line 55... Line -...
55
        user_free(buf);
-
 
56
        return 0;
-
 
57
    };
-
 
58
 
-
 
59
    http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL);
31
int main(int argc, char *argv[])
-
 
32
{
-
 
33
    LIST_HEAD(server_list);
Line 60... Line -...
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;
34
    LIST_HEAD(download_list);
73
            if(count+offset <= BUFFSIZE)
-
 
74
            {
-
 
75
                memcpy(buf+offset, http->content_ptr, count);
-
 
76
                offset+= count;
35
    LIST_HEAD(cache_list);
Line 77... Line 36...
77
            }
            else
36
    LIST_HEAD(local_list);
Line 78... Line -...
78
            {
-
 
79
                tail  = count+offset-BUFFSIZE;
-
 
80
                count = BUFFSIZE - offset;
37
    LIST_HEAD(task_list);
81
                if(count)
-
 
82
                {
-
 
Line 83... Line -...
83
                    memcpy(buf+offset, http->content_ptr, count);
-
 
84
                    offset = 0;
38
 
Line 85... Line -...
85
                };
-
 
86
 
39
    int   count;
87
                write(fd, buf, BUFFSIZE);
-
 
88
 
-
 
Line -... Line 40...
-
 
40
    char *cache_path;
89
                if(tail)
41
    char *tmp_path;
Line 90... Line 42...
90
                {
42
 
91
                    memcpy(buf, http->content_ptr+count, tail);
43
    if(http_init())
92
                    offset = tail;
-
 
93
                }
-
 
94
 
-
 
95
                sprintf(conbuf, "%d bytes loaded\r", http->content_received);
-
 
96
                con_write_asciiz(conbuf);
44
        goto err_init;
97
 
-
 
98
            }
-
 
99
            received = http->content_received;
-
 
100
        }
45
 
Line 101... Line 46...
101
        else break;
46
    set_cwd("/tmp0/1");
Line -... Line 47...
-
 
47
 
102
 
48
    con_init(80, 25, 80, 250, "Kolibri package manager");
Line 103... Line -...
103
    }while( (http->flags & FLAG_GOT_ALL_DATA) == 0);
-
 
104
 
-
 
105
    if(offset)
49
 
106
    {
-
 
107
        write(fd, buf, offset);
-
 
108
    }
-
 
109
 
50
    tmp_path = make_tmp_path("packages.xml");
110
//    ftruncate(fd, received);
51
 
111
    close(fd);
52
    count = http_load_file(tmp_path, make_url("packages.xml"));
112
 
53
 
113
    if(http->content_ptr)
-
 
114
        user_free(http->content_ptr);
54
    if(count)
115
    http_free(http);
55
        build_server_list(&server_list, tmp_path);
116
 
-
 
117
    user_free(buf);
56
 
Line -... Line 57...
-
 
57
    while(1)
-
 
58
    {
-
 
59
        int val;
118
 
60
        int index;
-
 
61
 
-
 
62
        val = getopt_long_only(argc, argv,"",longopts, &index);
Line -... Line 63...
-
 
63
 
119
    return received;
64
        if(val == -1)
-
 
65
            break;
-
 
66
 
Line 120... Line 67...
120
 
67
        switch(val)
121
err_get:
68
        {
122
    printf("HTTP GET failed\n");
-
 
123
    return received;
69
            case OPTION_LIST_PACKAGES:
124
}
70
                sprintf(conbuf,"available packages:\n\n");
125
 
71
                con_write_asciiz(conbuf);
Line 126... Line -...
126
int main(int argc, char *argv[])
-
 
127
{
-
 
128
    int   count;
72
                print_pkg_list(&server_list);
129
    char *cache_path;
73
                con_exit(0);
130
    char *tmp_path;
74
                return 0;
131
 
75
 
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
    {
-
 
Line 213... Line -...
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
    };
-
 
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);
-