File: packages.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 (180 lines) | stat: -rw-r--r-- 4,608 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
/*
 * packages.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-03-23 19:58:25 +0100 (Mi, 23 Mär 2005) $
 * $LastChangedRevision: 862 $
 */

#include <config.h>

#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#ifdef ENABLE_AR_INTERNAL
#include <ar.h>
#include <stdio.h>
#endif

#include "download.h"
#include "execute.h"
#include "log.h"
#include "packages.h"

const char *package_get_local_filename (di_package *package)
{
  const char *tmp = strrchr (package->filename, '/');
  if (tmp)
    return tmp + 1;
  return package->filename;
}

#ifdef ENABLE_AR_INTERNAL
static long package_extract_self_parse_header_length (const char *inh, size_t len)
{
  char lintbuf[15];
  long r;
  char *endp;

  if (memchr (inh, 0, len))
    return -1;
  memcpy (lintbuf, inh, len);
  lintbuf[len] = ' ';
  *strchr(lintbuf,' ') = 0;
  r = strtoul(lintbuf, &endp, 10);
  if (*endp)
    return -1;
  return r;
}

static int package_extract_self (const char *file, const char *command)
{
  char buf[8192], *infobuf = NULL, versionbuf[40];
  ssize_t memberlen, copied = 0;
  FILE *ar = NULL, *pi = NULL;
  char *cur;
  struct ar_hdr arh;
  int header_done = 0;
  int ret = -1;

  ar = fopen (file, "r");
  if (!ar)
    goto error;
  if (!fgets (versionbuf, sizeof (versionbuf), ar))
    goto error;

  if (!strcmp (versionbuf, ARMAG))
  {
    while (1)
    {
      if (fread (&arh, 1, sizeof(arh), ar) != sizeof (arh))
        goto error;
      if (memcmp (arh.ar_fmag, ARFMAG, sizeof (arh.ar_fmag)))
        goto error;
      memberlen = package_extract_self_parse_header_length (arh.ar_size, sizeof (arh.ar_size));
      if (memberlen < 0)
        goto error;

      if (!header_done)
      {
        if (memcmp (arh.ar_name,"debian-binary   ",sizeof (arh.ar_name)) &&
            memcmp (arh.ar_name,"debian-binary/  ",sizeof (arh.ar_name)))
          goto error;
        infobuf = di_new (char, memberlen + 1);
        if (fread (infobuf, 1, memberlen + (memberlen & 1), ar) == (size_t) (memberlen + (memberlen & 1)))
        infobuf[memberlen]= 0;
        cur = strchr (infobuf, '\n');
        if (!cur)
          goto error;
        *cur = 0;
        cur = strchr (infobuf, '.');
        if (!cur)
          goto error;
        *cur = 0;
        if (strcmp (infobuf, "2"))
          goto error;
        *cur = '.';
        strncpy (versionbuf, infobuf, sizeof(versionbuf));
        versionbuf[sizeof (versionbuf) - 1]= 0;
        header_done = 1;
      }
      else if (!memcmp (arh.ar_name,"data.tar.gz     ",sizeof (arh.ar_name)) ||
               !memcmp (arh.ar_name,"data.tar.gz/    ",sizeof (arh.ar_name)))
        break;
      else
        fseek (ar, memberlen + (memberlen & 1), SEEK_CUR);
    }
  }
  else
    goto error;

  log_text (DI_LOG_LEVEL_DEBUG, "Execute \"%s\"", command);

  pi = popen (command, "w");
  if (!pi)
    goto error;

  while (copied < memberlen)
  {
    size_t r, s;
    r = memberlen - copied;
    if (r > sizeof (buf))
      r = sizeof (buf);
    r = fread (buf, 1, r, ar);
    if (!r)
      goto error;
    s = fwrite (buf, 1, r, pi);
    if (r != s)
      goto error;
    copied += s;
  }

  ret = fclose (pi);
  pi = NULL;

  log_text (DI_LOG_LEVEL_DEBUG, "Return code: %d", ret);

error:
  di_free (infobuf);
  if (ar)
    fclose (ar);
  if (pi)
    fclose (pi);

  return ret;
}
#endif /* ENABLE_AR_INTERNAL */

int package_extract (di_package *package)
{
  char buf_file[PATH_MAX];
  char buf[PATH_MAX];

  build_target_deb (buf_file, PATH_MAX, package_get_local_filename (package));
#ifndef ENABLE_AR_INTERNAL
  snprintf (buf, PATH_MAX, "ar -p %s data.tar.gz | tar -xz -C %s -f -", buf_file, install_root);
  return execute (buf);
#else
  snprintf (buf, PATH_MAX, "tar -xz -C %s -f -", install_root);
  return package_extract_self (buf_file, buf);
#endif
}