File: download.c

package info (click to toggle)
cdebootstrap 0.3.4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 816 kB
  • ctags: 308
  • sloc: sh: 3,408; ansic: 2,522; makefile: 243
file content (243 lines) | stat: -rw-r--r-- 6,169 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * download.c
 *
 * Copyright (C) 2003 Bastian Blank <waldi@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $LastChangedBy: bastian $
 * $LastChangedDate: 2005-01-05 22:48:37 +0100 (Mi, 05 Jan 2005) $
 * $LastChangedRevision: 785 $
 */

#include <config.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "check.h"
#include "execute.h"
#include "frontend.h"
#include "download.h"
#include "packages.h"
#include "prepare.h"

#define BUFSIZE_source 128
#define BUFSIZE_target 1024

static char source[BUFSIZE_source];
static char target[BUFSIZE_target];

int decompress_file (const char *file, const char *extension)
{
  char buf[1024];

  snprintf (buf, sizeof (buf), "gunzip -f %s.%s 2>&1", file, extension);

  return execute (buf);
}

static inline int download_file (const char *source, const char *target, const char *message)
{
  log_message (LOG_MESSAGE_INFO_DOWNLOAD_RETRIEVE, message);
  return frontend_download (source, target);
}

static int download_file_extension (const char *source,
                                    const char *target, const char *extension, const char *message)
{
  char bufs[strlen (source) + strlen (extension) + 2];
  char buft[strlen (target) + strlen (extension) + 2];

  sprintf (bufs, "%s.%s", source, extension);
  sprintf (buft, "%s.%s", target, extension);
  return download_file (bufs, buft, message);
}

static int download_release (di_release **rel)
{
  build_source_indices_release (source, sizeof (source));
  build_target_indices_release (target, sizeof (target));

  if (download_file (source, target, "Release"))
    log_message (LOG_MESSAGE_ERROR_DOWNLOAD, "Release");

  log_message (LOG_MESSAGE_INFO_DOWNLOAD_PARSE, "Release");

  if (!(*rel = di_release_read_file (target)))
    log_message (LOG_MESSAGE_ERROR_PARSE, "Release");

  return 0;
}

static int download_packages_parse (const char *target, di_packages **packages, di_packages_allocator *allocator)
{
  log_message (LOG_MESSAGE_INFO_DOWNLOAD_PARSE, "Packages");

  if ((*packages = di_packages_minimal_read_file (target, allocator)))
    return 0;

  return 1;
}

static int download_packages (di_release *rel, di_packages **packages, di_packages_allocator *allocator)
{
  struct stat statbuf;

  build_source_indices_packages (source, sizeof (source));
  build_target_indices_packages (target, sizeof (target));

  /* yeah, the file already exists */
  if (!stat (target, &statbuf))
  {
    if (!check_packages (target, rel))
    {
      /* just parse them */
      download_packages_parse (target, packages, allocator);
      if (*packages)
        return 0;
    }
    /* if it is invalid, unlink them */
    unlink (target);
  }

  /* try to download the gzip compressed version ... */
  if (!download_file_extension (source, target, "gz", "Packages.gz"))
  {
    if (!check_packages_extension (target, "gz", rel))
    {
      /* ... decompress it ... */
      if (decompress_file (target, "gz"))
        log_message (LOG_MESSAGE_ERROR_DECOMPRESS, "Packages.gz");

      /* ... and parse them */
      download_packages_parse (target, packages, allocator);
      if (*packages)
        return 0;
    }
    /* don't bail out, try the next one */
  }

  /* download the uncompressed version ... */
  if (!download_file (source, target, "Packages"))
  {
    if (!check_packages (target, rel))
    {
      /* ... and parse them */
      download_packages_parse (target, packages, allocator);
      if (*packages)
        return 0;
    }

    /* now we have a parser error */
    log_message (LOG_MESSAGE_ERROR_PARSE, "Packages");
  }

  log_message (LOG_MESSAGE_ERROR_DOWNLOAD, "Packages");
  return 1;
}

static int download_indices (di_packages **packages, di_packages_allocator *allocator)
{
  di_release *rel;
  int ret;

  ret = download_release (&rel);
  if (ret)
    return 1;

  ret = suite_init_second (rel->codename);
  if (ret)
    return 1;

  frontend_progress_set (10);

  ret = download_packages (rel, packages, allocator);
  if (ret)
    return 1;

  frontend_progress_set (50);
  return 0;
}

static int download_debs (di_slist *install)
{
  int count = 0, ret, size = 0, size_done = 0, progress;
  struct stat statbuf;
  di_slist_node *node;
  di_package *p;

  for (node = install->head; node; node = node->next)
  {
    p = node->data;
    count++;
    size += p->size;
  }

  for (node = install->head; node; node = node->next)
  {
    p = node->data;
    size_done += p->size;
    progress = ((float) size_done/size) * 350 + 50;

    build_target_deb (target, sizeof (target), package_get_local_filename (p));

    if (!stat (target, &statbuf))
    {
      ret = check_deb (target, p, p->package);
      if (!ret)
      {
        frontend_progress_set (progress);
        continue;
      }
    }

    if (download_file (p->filename, target, p->package) || check_deb (target, p, p->package))
      log_message (LOG_MESSAGE_ERROR_DOWNLOAD, p->filename);

    frontend_progress_set (progress);
  }

  return 0;
}

int download (di_packages **packages, di_packages_allocator *allocator, di_slist **install)
{
  int ret;

  ret = download_indices (packages, allocator);
  if (ret)
    return 1;

  (*install) = suite_packages_list (*packages, allocator);
  if (!*install)
    log_message (LOG_MESSAGE_ERROR_INSTALL_LIST);

  ret = download_debs (*install);
  if (ret)
    return 1;

  return 0;
}

int download_init (void)
{
  return prepare_download ();
}