extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * extract.c - extracting archives
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <dirent.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <assert.h>
  34. #include <ar.h>
  35. #ifdef USE_ZLIB
  36. #include <zlib.h>
  37. #endif
  38. #include <config.h>
  39. #include <dpkg.h>
  40. #include <dpkg-deb.h>
  41. #include <myopt.h>
  42. static void movecontrolfiles(const char *thing) {
  43. char buf[200];
  44. pid_t c1;
  45. sprintf(buf, "mv %s/* . && rmdir %s", thing, thing);
  46. if (!(c1= m_fork())) {
  47. execlp("sh","sh","-c",buf,(char*)0); ohshite(_("failed to exec sh -c mv foo/* &c"));
  48. }
  49. waitsubproc(c1,"sh -c mv foo/* &c",0,0);
  50. }
  51. static void readfail(FILE *a, const char *filename, const char *what) {
  52. if (ferror(a)) {
  53. ohshite(_("error reading %s from %.255s"),what,filename);
  54. } else {
  55. ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
  56. }
  57. }
  58. static unsigned long parseheaderlength(const char *inh, size_t len,
  59. const char *fn, const char *what) {
  60. char lintbuf[15];
  61. unsigned long r;
  62. char *endp;
  63. if (memchr(inh,0,len))
  64. ohshit(_("file `%.250s' is corrupt - %.250s length contains nulls"),fn,what);
  65. assert(sizeof(lintbuf) > len);
  66. memcpy(lintbuf,inh,len);
  67. lintbuf[len]= ' ';
  68. *strchr(lintbuf,' ')= 0;
  69. r= strtoul(lintbuf,&endp,10);
  70. if (*endp)
  71. ohshit(_("file `%.250s' is corrupt - bad digit (code %d) in %s"),fn,*endp,what);
  72. return r;
  73. }
  74. void extracthalf(const char *debar, const char *directory,
  75. const char *taroption, int admininfo) {
  76. char versionbuf[40];
  77. float versionnum;
  78. char ctrllenbuf[40], *infobuf;
  79. long ctrllennum, memberlen= 0;
  80. int dummy, l= 0;
  81. pid_t c1=0,c2,c3;
  82. unsigned char *ctrlarea= 0;
  83. int p1[2], p2[2];
  84. FILE *ar, *pi;
  85. struct stat stab;
  86. char nlc;
  87. char *cur;
  88. struct ar_hdr arh;
  89. int readfromfd, oldformat= 0, header_done, adminmember;
  90. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  91. fpos_t fpos;
  92. #endif
  93. #ifdef USE_ZLIB
  94. gzFile gzfile;
  95. char gzbuffer[4096];
  96. int gzactualread;
  97. #endif
  98. ar= fopen(debar,"r"); if (!ar) ohshite(_("failed to read archive `%.255s'"),debar);
  99. if (fstat(fileno(ar),&stab)) ohshite(_("failed to fstat archive"));
  100. if (!fgets(versionbuf,sizeof(versionbuf),ar)) readfail(ar,debar,_("version number"));
  101. if (!strcmp(versionbuf,"!<arch>\n")) {
  102. oldformat= 0;
  103. ctrllennum= 0;
  104. header_done= 0;
  105. for (;;) {
  106. if (fread(&arh,1,sizeof(arh),ar) != sizeof(arh))
  107. readfail(ar,debar,_("between members"));
  108. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  109. ohshit(_("file `%.250s' is corrupt - bad magic at end of first header"),debar);
  110. memberlen= parseheaderlength(arh.ar_size,sizeof(arh.ar_size),
  111. debar,"member length");
  112. if (memberlen<0)
  113. ohshit(_("file `%.250s' is corrupt - negative member length %ld"),debar,memberlen);
  114. if (!header_done) {
  115. if (memcmp(arh.ar_name,"debian-binary ",sizeof(arh.ar_name)) &&
  116. memcmp(arh.ar_name,"debian-binary/ ",sizeof(arh.ar_name)))
  117. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  118. infobuf= m_malloc(memberlen+1);
  119. if (fread(infobuf,1, memberlen + (memberlen&1), ar) != memberlen + (memberlen&1))
  120. readfail(ar,debar,_("header info member"));
  121. infobuf[memberlen]= 0;
  122. cur= strchr(infobuf,'\n');
  123. if (!cur) ohshit(_("archive has no newlines in header"));
  124. *cur= 0;
  125. cur= strchr(infobuf,'.');
  126. if (!cur) ohshit(_("archive has no dot in version number"));
  127. *cur= 0;
  128. if (strcmp(infobuf,"2"))
  129. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  130. *cur= '.';
  131. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  132. versionbuf[sizeof(versionbuf)-1]= 0;
  133. header_done= 1;
  134. } else if (arh.ar_name[0] == '_') {
  135. /* Members with `_' are noncritical, and if we don't understand them
  136. * we skip them.
  137. */
  138. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  139. } else {
  140. adminmember=
  141. (!memcmp(arh.ar_name,ADMINMEMBER,sizeof(arh.ar_name)) ||
  142. !memcmp(arh.ar_name,ADMINMEMBER_COMPAT,sizeof(arh.ar_name))) ? 1 :
  143. (!memcmp(arh.ar_name,DATAMEMBER,sizeof(arh.ar_name)) ||
  144. !memcmp(arh.ar_name,DATAMEMBER_COMPAT,sizeof(arh.ar_name))) ? 0 :
  145. -1;
  146. if (adminmember == -1) {
  147. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  148. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  149. }
  150. if (adminmember == 1) {
  151. if (ctrllennum != 0)
  152. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  153. ctrllennum= memberlen;
  154. }
  155. if (!adminmember != !admininfo) {
  156. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  157. } else {
  158. break; /* Yes ! - found it. */
  159. }
  160. }
  161. }
  162. if (admininfo >= 2)
  163. if (printf(_(" new debian package, version %s.\n"
  164. " size %ld bytes: control archive= %ld bytes.\n"),
  165. versionbuf, (long)stab.st_size, ctrllennum) == EOF ||
  166. fflush(stdout)) werr("stdout");
  167. } else if (!strncmp(versionbuf,"0.93",4) &&
  168. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  169. nlc == '\n') {
  170. oldformat= 1;
  171. l= strlen(versionbuf); if (l && versionbuf[l-1]=='\n') versionbuf[l-1]=0;
  172. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  173. readfail(ar,debar,_("ctrl information length"));
  174. if (sscanf(ctrllenbuf,"%ld%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  175. ohshit(_("archive has malformatted ctrl len `%s'"),ctrllenbuf);
  176. if (admininfo >= 2)
  177. if (printf(_(" old debian package, version %s.\n"
  178. " size %ld bytes: control archive= %ld, main archive= %ld.\n"),
  179. versionbuf, (long)stab.st_size, ctrllennum,
  180. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l)) == EOF ||
  181. fflush(stdout)) werr("stdout");
  182. ctrlarea= malloc(ctrllennum); if (!ctrlarea) ohshite("malloc ctrlarea failed");
  183. errno=0; if (fread(ctrlarea,1,ctrllennum,ar) != ctrllennum)
  184. readfail(ar,debar,_("ctrlarea"));
  185. } else {
  186. if (!strncmp(versionbuf,"!<arch>",7)) {
  187. if (fprintf(stderr,
  188. _("dpkg-deb: file looks like it might be an archive which has been\n"
  189. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"))
  190. == EOF) werr("stderr");
  191. }
  192. ohshit(_("`%.255s' is not a debian format archive"),debar);
  193. }
  194. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  195. if(fgetpos(ar, &fpos)) ohshit(_("fgetpos failed"));
  196. #endif
  197. fflush(ar);
  198. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  199. if(fsetpos(ar, &fpos)) ohshit(_("fsetpos failed"));
  200. #endif
  201. if (oldformat) {
  202. if (admininfo) {
  203. m_pipe(p1);
  204. if (!(c1= m_fork())) {
  205. close(p1[0]);
  206. if (!(pi= fdopen(p1[1],"w"))) ohshite(_("failed to fdopen p1 in paste"));
  207. errno=0; if (fwrite(ctrlarea,1,ctrllennum,pi) != ctrllennum)
  208. ohshit(_("failed to write to gzip -dc"));
  209. if (fclose(pi)) ohshit(_("failed to close gzip -dc"));
  210. exit(0);
  211. }
  212. close(p1[1]);
  213. readfromfd= p1[0];
  214. } else {
  215. if (lseek(fileno(ar),l+strlen(ctrllenbuf)+ctrllennum,SEEK_SET) == -1)
  216. ohshite(_("failed to syscall lseek to files archive portion"));
  217. c1= -1;
  218. readfromfd= fileno(ar);
  219. }
  220. } else {
  221. m_pipe(p1);
  222. if (!(c1= m_fork())) {
  223. close(p1[0]);
  224. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  225. if (close(p1[1]) == EOF) ohshite(_("failed to close pipe in copy"));
  226. exit(0);
  227. }
  228. close(p1[1]);
  229. readfromfd= p1[0];
  230. }
  231. if (taroption) m_pipe(p2);
  232. if (!(c2= m_fork())) {
  233. m_dup2(readfromfd,0);
  234. if (admininfo) close(p1[0]);
  235. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  236. #ifdef USE_ZLIB
  237. gzfile = gzdopen(0, "r");
  238. while ((gzactualread= gzread(gzfile,gzbuffer,sizeof(gzbuffer))) > 0) {
  239. if (gzactualread < 0 ) {
  240. int gzerr = 0;
  241. const char *errmsg = gzerror(gzfile, &gzerr);
  242. if (gzerr == Z_ERRNO) {
  243. if (errno == EINTR) continue;
  244. errmsg= strerror(errno);
  245. }
  246. ohshite(_("internal gzip error: `%s'"), errmsg);
  247. }
  248. write(1,gzbuffer,gzactualread);
  249. }
  250. exit(0);
  251. #else
  252. execlp(GZIP,"gzip","-dc",(char*)0); ohshite(_("failed to exec gzip -dc"));
  253. #endif
  254. }
  255. if (readfromfd != fileno(ar)) close(readfromfd);
  256. if (taroption) close(p2[1]);
  257. if (taroption && directory) {
  258. if (chdir(directory)) {
  259. if (errno == ENOENT) {
  260. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  261. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  262. } else {
  263. ohshite(_("failed to chdir to directory"));
  264. }
  265. }
  266. }
  267. if (taroption) {
  268. if (!(c3= m_fork())) {
  269. char buffer[30+2];
  270. if(strlen(taroption) > 30) internerr(taroption);
  271. strcpy(buffer, taroption);
  272. strcat(buffer, "f");
  273. m_dup2(p2[0],0);
  274. close(p2[0]);
  275. execlp(TAR,"tar",buffer,"-",(char*)0);
  276. ohshite(_("failed to exec tar"));
  277. }
  278. close(p2[0]);
  279. waitsubproc(c3,"tar",0,0);
  280. }
  281. waitsubproc(c2,"gzip -dc",1,0);
  282. if (c1 != -1) waitsubproc(c1,"paste",0,0);
  283. if (oldformat && admininfo) {
  284. if (versionnum == 0.931F) {
  285. movecontrolfiles(OLDOLDDEBDIR);
  286. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  287. movecontrolfiles(OLDDEBDIR);
  288. }
  289. }
  290. }
  291. static void controlextractvextract(int admin,
  292. const char *taroptions,
  293. const char *const *argv) {
  294. const char *debar, *directory;
  295. if (!(debar= *argv++))
  296. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  297. if (!(directory= *argv++)) {
  298. if (admin) directory= EXTRACTCONTROLDIR;
  299. else ohshit(_("--%s needs a target directory.\n"
  300. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  301. } else if (*argv) {
  302. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  303. }
  304. extracthalf(debar, directory, taroptions, admin);
  305. }
  306. void do_fsystarfile(const char *const *argv) {
  307. const char *debar;
  308. if (!(debar= *argv++))
  309. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  310. if (*argv)
  311. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  312. extracthalf(debar,0,0,0);
  313. }
  314. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  315. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  316. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }