join.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * join.c - joining
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <assert.h>
  23. #include <limits.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include <dpkg/myopt.h>
  31. #include "dpkg-split.h"
  32. void reassemble(struct partinfo **partlist, const char *outputfile) {
  33. FILE *output, *input;
  34. void *buffer;
  35. struct partinfo *pi;
  36. unsigned int i;
  37. size_t nr,buffersize;
  38. printf(_("Putting package %s together from %d parts: "),
  39. partlist[0]->package,partlist[0]->maxpartn);
  40. buffersize= partlist[0]->maxpartlen;
  41. for (i=0; i<partlist[0]->maxpartn; i++)
  42. if (partlist[0]->headerlen > buffersize) buffersize= partlist[0]->headerlen;
  43. buffer= m_malloc(partlist[0]->maxpartlen);
  44. output= fopen(outputfile,"w");
  45. if (!output) ohshite(_("unable to open output file `%.250s'"),outputfile);
  46. for (i=0; i<partlist[0]->maxpartn; i++) {
  47. pi= partlist[i];
  48. input= fopen(pi->filename,"r");
  49. if (!input) ohshite(_("unable to (re)open input part file `%.250s'"),pi->filename);
  50. assert(pi->headerlen <= buffersize);
  51. nr= fread(buffer,1,pi->headerlen,input);
  52. if (nr != pi->headerlen) rerreof(input,pi->filename);
  53. assert(pi->thispartlen <= buffersize);
  54. printf("%d ",i+1);
  55. nr= fread(buffer,1,pi->thispartlen,input);
  56. if (nr != pi->thispartlen) rerreof(input,pi->filename);
  57. if (pi->thispartlen & 1)
  58. if (getc(input) == EOF) rerreof(input,pi->filename);
  59. if (ferror(input)) rerr(pi->filename);
  60. fclose(input);
  61. nr= fwrite(buffer,1,pi->thispartlen,output);
  62. if (nr != pi->thispartlen) werr(outputfile);
  63. }
  64. if (fclose(output)) werr(outputfile);
  65. printf(_("done\n"));
  66. }
  67. void addtopartlist(struct partinfo **partlist,
  68. struct partinfo *pi, struct partinfo *refi) {
  69. int i;
  70. if (strcmp(pi->package,refi->package) ||
  71. strcmp(pi->version,refi->version) ||
  72. strcmp(pi->md5sum,refi->md5sum) ||
  73. pi->orglength != refi->orglength ||
  74. pi->maxpartn != refi->maxpartn ||
  75. pi->maxpartlen != refi->maxpartlen) {
  76. print_info(pi);
  77. print_info(refi);
  78. ohshit(_("files `%.250s' and `%.250s' are not parts of the same file"),
  79. pi->filename,refi->filename);
  80. }
  81. i= pi->thispartn-1;
  82. if (partlist[i])
  83. ohshit(_("there are several versions of part %d - at least `%.250s' and `%.250s'"),
  84. pi->thispartn, pi->filename, partlist[i]->filename);
  85. partlist[i]= pi;
  86. }
  87. void do_join(const char *const *argv) {
  88. char *p;
  89. const char *thisarg;
  90. struct partqueue *pq;
  91. struct partinfo *refi, *pi, **partlist;
  92. unsigned int i;
  93. assert(!queue);
  94. if (!*argv)
  95. badusage(_("--%s requires one or more part file arguments"),
  96. cipaction->olong);
  97. while ((thisarg= *argv++)) {
  98. pq= nfmalloc(sizeof(struct partqueue));
  99. mustgetpartinfo(thisarg,&pq->info);
  100. pq->nextinqueue= queue;
  101. queue= pq;
  102. }
  103. refi= NULL;
  104. for (pq= queue; pq; pq= pq->nextinqueue)
  105. if (!refi || pq->info.thispartn < refi->thispartn) refi= &pq->info;
  106. assert(refi);
  107. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  108. for (i = 0; i < refi->maxpartn; i++)
  109. partlist[i] = NULL;
  110. for (pq= queue; pq; pq= pq->nextinqueue) {
  111. pi= &pq->info;
  112. addtopartlist(partlist,pi,refi);
  113. }
  114. for (i=0; i<refi->maxpartn; i++) {
  115. if (!partlist[i]) ohshit(_("part %d is missing"),i+1);
  116. }
  117. if (!outputfile) {
  118. p= nfmalloc(strlen(refi->package)+1+strlen(refi->version)+sizeof(DEBEXT));
  119. strcpy(p,refi->package);
  120. strcat(p,"-");
  121. strcat(p,refi->version);
  122. strcat(p,DEBEXT);
  123. outputfile= p;
  124. }
  125. reassemble(partlist,outputfile);
  126. }