Subversion Repositories Kolibri OS

Rev

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

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