compression.c 5.6 KB

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