Subversion Repositories Kolibri OS

Rev

Rev 5726 | Rev 5728 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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