join.c 4.4 KB

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