compression.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 == BZ_IO_ERROR) {
  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 compress_type_lzma:
  86. if (fd_in != 0) {
  87. m_dup2(fd_in, 0);
  88. close(fd_in);
  89. }
  90. if (fd_out != 1) {
  91. m_dup2(fd_out, 1);
  92. close(fd_out);
  93. }
  94. execlp(LZMA, "lzma", "-dc", (char *)0);
  95. ohshite(_("%s: failed to exec %s"), v.buf, "lzma -dc");
  96. case CAT:
  97. fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), v.buf);
  98. exit(0);
  99. default:
  100. exit(1);
  101. }
  102. }
  103. void compress_cat(enum compression_type type, int fd_in, int fd_out, const char *compression, char *desc, ...) {
  104. va_list al;
  105. struct varbuf v;
  106. char combuf[6];
  107. varbufinit(&v);
  108. va_start(al,desc);
  109. varbufvprintf(&v, desc, al);
  110. va_end(al);
  111. if(compression == NULL) compression= "9";
  112. else if(*compression == '0') type = CAT;
  113. switch(type) {
  114. case GZ:
  115. #ifdef WITH_ZLIB
  116. {
  117. int actualread, actualwrite;
  118. char buffer[4096];
  119. gzFile gzfile;
  120. strncpy(combuf, "w9", sizeof(combuf));
  121. combuf[1]= *compression;
  122. gzfile = gzdopen(1, combuf);
  123. while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
  124. if (actualread < 0 ) {
  125. if (errno == EINTR) continue;
  126. ohshite(_("%s: internal gzip error: read: `%s'"), v.buf, strerror(errno));
  127. }
  128. actualwrite= gzwrite(gzfile,buffer,actualread);
  129. if (actualwrite < 0 ) {
  130. int err = 0;
  131. const char *errmsg = gzerror(gzfile, &err);
  132. if (err == Z_ERRNO) {
  133. if (errno == EINTR) continue;
  134. errmsg= strerror(errno);
  135. }
  136. ohshite(_("%s: internal gzip error: write: `%s'"), v.buf, errmsg);
  137. }
  138. if (actualwrite != actualread)
  139. ohshite(_("%s: internal gzip error: read(%i) != write(%i)"), v.buf, actualread, actualwrite);
  140. }
  141. gzclose(gzfile);
  142. exit(0);
  143. }
  144. #else
  145. if (fd_in != 0) {
  146. m_dup2(fd_in, 0);
  147. close(fd_in);
  148. }
  149. if (fd_out != 1) {
  150. m_dup2(fd_out, 1);
  151. close(fd_out);
  152. }
  153. strncpy(combuf, "-9c", sizeof(combuf));
  154. combuf[1]= *compression;
  155. execlp(GZIP,"gzip",combuf,(char*)0); ohshit(_("%s: failed to exec gzip %s"), v.buf, combuf);
  156. #endif
  157. case BZ2:
  158. #ifdef WITH_BZ2
  159. {
  160. int actualread, actualwrite;
  161. char buffer[4096];
  162. BZFILE *bzfile;
  163. strncpy(combuf, "w9", sizeof(combuf));
  164. combuf[1]= *compression;
  165. bzfile = BZ2_bzdopen(1, combuf);
  166. while((actualread = read(0,buffer,sizeof(buffer))) > 0) {
  167. if (actualread < 0 ) {
  168. if (errno == EINTR) continue;
  169. ohshite(_("%s: internal bzip2 error: read: `%s'"), v.buf, strerror(errno));
  170. }
  171. actualwrite= BZ2_bzwrite(bzfile,buffer,actualread);
  172. if (actualwrite < 0 ) {
  173. int err = 0;
  174. const char *errmsg = BZ2_bzerror(bzfile, &err);
  175. if (err == BZ_IO_ERROR) {
  176. if (errno == EINTR) continue;
  177. errmsg= strerror(errno);
  178. }
  179. ohshite(_("%s: internal bzip2 error: write: `%s'"), v.buf, errmsg);
  180. }
  181. if (actualwrite != actualread)
  182. ohshite(_("%s: internal bzip2 error: read(%i) != write(%i)"), v.buf, actualread, actualwrite);
  183. }
  184. BZ2_bzclose(bzfile);
  185. exit(0);
  186. }
  187. #else
  188. if (fd_in != 0) {
  189. m_dup2(fd_in, 0);
  190. close(fd_in);
  191. }
  192. if (fd_out != 1) {
  193. m_dup2(fd_out, 1);
  194. close(fd_out);
  195. }
  196. strncpy(combuf, "-9c", sizeof(combuf));
  197. combuf[1]= *compression;
  198. execlp(BZIP2,"bzip2",combuf,(char*)0); ohshit(_("%s: failed to exec bzip2 %s"), v.buf, combuf);
  199. #endif
  200. case CAT:
  201. fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);
  202. exit(0);
  203. default:
  204. exit(1);
  205. }
  206. }