compression.c 5.6 KB

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