~ubuntu-branches/ubuntu/lucid/dpkg/lucid

« back to all changes in this revision

Viewing changes to lib/compression.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2009-09-18 13:39:36 UTC
  • mfrom: (1.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20090918133936-dj8kjtc2qz3yqj7i
Tags: 1.15.4ubuntu1
* Resynchronise with Debian (LP: #427854). Remaining changes:
  Ubuntu-specific adjustments (probably):
  - Use i686 for lpia in cputable and triplettable.
  - Hack Dpkg::Arch to return i686 for lpia.
  - Move various Conflicts to Breaks, since upgrades from stable Ubuntu
    releases support Breaks.

  Miscellaneous bug fixes:
  - Avoid duplicate attempts to [f]close in obscure error situations which
    might conceiveably close wrong fds.
  - Revert change to stop outputting a newline after a postinst is run
    (Debian #392317).
  - Use the two-arg form of open in Dpkg::Control so that "-" can be
    passed to parse stdin as a control file (Debian #465340).

  Launchpad integration:
  - Add Launchpad-Bugs-Fixed handling in a few more places.

  Build options:
  - Point to https://wiki.ubuntu.com/DistCompilerFlags from
    dpkg-buildpackage(1).
  - Set default LDFLAGS to -Wl,-Bsymbolic-functions. (We've already taken
    this hit in Ubuntu.)
  - Implement handling of hardening-wrapper options via DEB_BUILD_OPTIONS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <config.h>
2
 
#include <compat.h>
3
 
 
4
 
#include <dpkg-i18n.h>
5
 
 
6
 
#include <stdlib.h>
7
 
#include <unistd.h>
8
 
#include <string.h>
9
 
#include <errno.h>
10
 
 
11
 
#ifdef WITH_ZLIB
12
 
#include <zlib.h>
13
 
#endif
14
 
#ifdef WITH_BZ2
15
 
#include <bzlib.h>
16
 
#endif
17
 
 
18
 
#include <dpkg.h>
19
 
#include "dpkg-db.h"
20
 
 
21
 
static void
22
 
fd_fd_filter(int fd_in, int fd_out,
23
 
             const char *file, const char *cmd, const char *args,
24
 
             const char *desc)
25
 
{
26
 
  if (fd_in != 0) {
27
 
    m_dup2(fd_in, 0);
28
 
    close(fd_in);
29
 
  }
30
 
  if (fd_out != 1) {
31
 
    m_dup2(fd_out, 1);
32
 
    close(fd_out);
33
 
  }
34
 
  execlp(file, cmd, args, NULL);
35
 
  ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, args);
36
 
}
37
 
 
38
 
void decompress_cat(enum compress_type type, int fd_in, int fd_out, char *desc, ...) {
39
 
  va_list al;
40
 
  struct varbuf v = VARBUF_INIT;
41
 
 
42
 
  va_start(al,desc);
43
 
  varbufvprintf(&v, desc, al);
44
 
  va_end(al);
45
 
 
46
 
  switch(type) {
47
 
    case compress_type_gzip:
48
 
#ifdef WITH_ZLIB
49
 
      {
50
 
        char buffer[4096];
51
 
        int actualread;
52
 
        gzFile gzfile = gzdopen(fd_in, "r");
53
 
        while ((actualread= gzread(gzfile,buffer,sizeof(buffer))) > 0) {
54
 
          if (actualread < 0 ) {
55
 
            int err = 0;
56
 
            const char *errmsg = gzerror(gzfile, &err);
57
 
            if (err == Z_ERRNO) {
58
 
              if (errno == EINTR) continue;
59
 
              errmsg= strerror(errno);
60
 
            }
61
 
          ohshite(_("%s: internal gzip error: `%s'"), v.buf, errmsg);
62
 
          }
63
 
          write(fd_out,buffer,actualread);
64
 
        }
65
 
      }
66
 
      exit(0);
67
 
#else
68
 
      fd_fd_filter(fd_in, fd_out, GZIP, "gzip", "-dc", v.buf);
69
 
#endif
70
 
    case compress_type_bzip2:
71
 
#ifdef WITH_BZ2
72
 
      {   
73
 
        char buffer[4096];
74
 
        int actualread;
75
 
        BZFILE *bzfile = BZ2_bzdopen(fd_in, "r");
76
 
        while ((actualread= BZ2_bzread(bzfile,buffer,sizeof(buffer))) > 0) {
77
 
          if (actualread < 0 ) {
78
 
            int err = 0;
79
 
            const char *errmsg = BZ2_bzerror(bzfile, &err);
80
 
            if (err == BZ_IO_ERROR) {
81
 
              if (errno == EINTR) continue;
82
 
              errmsg= strerror(errno);
83
 
            }
84
 
          ohshite(_("%s: internal bzip2 error: `%s'"), v.buf, errmsg);
85
 
          }
86
 
          write(fd_out,buffer,actualread);
87
 
        }
88
 
      }
89
 
      exit(0);
90
 
#else
91
 
      fd_fd_filter(fd_in, fd_out, BZIP2, "bzip2", "-dc", v.buf);
92
 
#endif
93
 
    case compress_type_lzma:
94
 
      fd_fd_filter(fd_in, fd_out, LZMA, "lzma", "-dc", v.buf);
95
 
    case compress_type_cat:
96
 
      fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), v.buf);
97
 
      exit(0);
98
 
    default:
99
 
      exit(1);
100
 
  }
101
 
}
102
 
 
103
 
void compress_cat(enum compress_type type, int fd_in, int fd_out, const char *compression, char *desc, ...) {
104
 
  va_list al;
105
 
  struct varbuf v = VARBUF_INIT;
106
 
  char combuf[6];
107
 
 
108
 
  va_start(al,desc);
109
 
  varbufvprintf(&v, desc, al);
110
 
  va_end(al);
111
 
 
112
 
  if(compression == NULL) compression= "9";
113
 
  else if (*compression == '0')
114
 
    type = compress_type_cat;
115
 
 
116
 
  switch(type) {
117
 
    case compress_type_gzip:
118
 
#ifdef WITH_ZLIB
119
 
      {
120
 
        int actualread, actualwrite;
121
 
        char buffer[4096];
122
 
        gzFile gzfile;
123
 
        strncpy(combuf, "w9", sizeof(combuf));
124
 
        combuf[1]= *compression;
125
 
        gzfile = gzdopen(1, combuf);
126
 
        while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
127
 
          if (actualread < 0 ) {
128
 
            if (errno == EINTR) continue;
129
 
            ohshite(_("%s: internal gzip error: read: `%s'"), v.buf, strerror(errno));
130
 
          }
131
 
          actualwrite= gzwrite(gzfile,buffer,actualread);
132
 
          if (actualwrite < 0 ) {
133
 
            int err = 0;
134
 
            const char *errmsg = gzerror(gzfile, &err);
135
 
            if (err == Z_ERRNO) {
136
 
              if (errno == EINTR) continue;
137
 
            errmsg= strerror(errno);
138
 
            }
139
 
            ohshite(_("%s: internal gzip error: write: `%s'"), v.buf, errmsg);
140
 
          }
141
 
          if (actualwrite != actualread)
142
 
            ohshite(_("%s: internal gzip error: read(%i) != write(%i)"), v.buf, actualread, actualwrite);
143
 
        }
144
 
        gzclose(gzfile);
145
 
        exit(0);
146
 
      }
147
 
#else
148
 
      strncpy(combuf, "-9c", sizeof(combuf));
149
 
      combuf[1]= *compression;
150
 
      fd_fd_filter(fd_in, fd_out, GZIP, "gzip", combuf, v.buf);
151
 
#endif
152
 
    case compress_type_bzip2:
153
 
#ifdef WITH_BZ2
154
 
      {
155
 
        int actualread, actualwrite;
156
 
        char buffer[4096];
157
 
        BZFILE *bzfile;
158
 
        strncpy(combuf, "w9", sizeof(combuf));
159
 
        combuf[1]= *compression;
160
 
        bzfile = BZ2_bzdopen(1, combuf);
161
 
        while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
162
 
          if (actualread < 0 ) {
163
 
            if (errno == EINTR) continue;
164
 
            ohshite(_("%s: internal bzip2 error: read: `%s'"), v.buf, strerror(errno));
165
 
          }
166
 
          actualwrite= BZ2_bzwrite(bzfile,buffer,actualread);
167
 
          if (actualwrite < 0 ) {
168
 
            int err = 0;
169
 
            const char *errmsg = BZ2_bzerror(bzfile, &err);
170
 
            if (err == BZ_IO_ERROR) {
171
 
              if (errno == EINTR) continue;
172
 
              errmsg= strerror(errno);
173
 
            }
174
 
            ohshite(_("%s: internal bzip2 error: write: `%s'"), v.buf, errmsg);
175
 
          }
176
 
          if (actualwrite != actualread)
177
 
            ohshite(_("%s: internal bzip2 error: read(%i) != write(%i)"), v.buf, actualread, actualwrite);
178
 
        }
179
 
        BZ2_bzclose(bzfile);
180
 
        exit(0);
181
 
      }
182
 
#else
183
 
      strncpy(combuf, "-9c", sizeof(combuf));
184
 
      combuf[1]= *compression;
185
 
      fd_fd_filter(fd_in, fd_out, BZIP2, "bzip2", combuf, v.buf);
186
 
#endif
187
 
    case compress_type_lzma:
188
 
      strncpy(combuf, "-9c", sizeof(combuf));
189
 
      combuf[1] = *compression;
190
 
      fd_fd_filter(fd_in, fd_out, LZMA, "lzma", combuf, v.buf);
191
 
    case compress_type_cat:
192
 
      fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);
193
 
      exit(0);
194
 
    default:
195
 
      exit(1);
196
 
  }
197
 
}