join.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <dpkg/i18n.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(P_("Putting package %s together from %d part: ",
  40. "Putting package %s together from %d parts: ",
  41. partlist[0]->maxpartn),
  42. partlist[0]->package,partlist[0]->maxpartn);
  43. buffersize= partlist[0]->maxpartlen;
  44. for (i=0; i<partlist[0]->maxpartn; i++)
  45. if (partlist[0]->headerlen > buffersize) buffersize= partlist[0]->headerlen;
  46. buffer= m_malloc(partlist[0]->maxpartlen);
  47. output= fopen(outputfile,"w");
  48. if (!output) ohshite(_("unable to open output file `%.250s'"),outputfile);
  49. for (i=0; i<partlist[0]->maxpartn; i++) {
  50. pi= partlist[i];
  51. input= fopen(pi->filename,"r");
  52. if (!input) ohshite(_("unable to (re)open input part file `%.250s'"),pi->filename);
  53. assert(pi->headerlen <= buffersize);
  54. nr= fread(buffer,1,pi->headerlen,input);
  55. if (nr != pi->headerlen) rerreof(input,pi->filename);
  56. assert(pi->thispartlen <= buffersize);
  57. printf("%d ",i+1);
  58. nr= fread(buffer,1,pi->thispartlen,input);
  59. if (nr != pi->thispartlen) rerreof(input,pi->filename);
  60. if (pi->thispartlen & 1)
  61. if (getc(input) == EOF) rerreof(input,pi->filename);
  62. if (ferror(input)) rerr(pi->filename);
  63. fclose(input);
  64. nr= fwrite(buffer,1,pi->thispartlen,output);
  65. if (nr != pi->thispartlen) werr(outputfile);
  66. }
  67. if (fflush(output))
  68. ohshite(_("unable to flush file '%s'"), outputfile);
  69. if (fsync(fileno(output)))
  70. ohshite(_("unable to sync file '%s'"), outputfile);
  71. if (fclose(output)) werr(outputfile);
  72. free(buffer);
  73. printf(_("done\n"));
  74. }
  75. void addtopartlist(struct partinfo **partlist,
  76. struct partinfo *pi, struct partinfo *refi) {
  77. int i;
  78. if (strcmp(pi->package,refi->package) ||
  79. strcmp(pi->version,refi->version) ||
  80. strcmp(pi->md5sum,refi->md5sum) ||
  81. pi->orglength != refi->orglength ||
  82. pi->maxpartn != refi->maxpartn ||
  83. pi->maxpartlen != refi->maxpartlen) {
  84. print_info(pi);
  85. print_info(refi);
  86. ohshit(_("files `%.250s' and `%.250s' are not parts of the same file"),
  87. pi->filename,refi->filename);
  88. }
  89. i= pi->thispartn-1;
  90. if (partlist[i])
  91. ohshit(_("there are several versions of part %d - at least `%.250s' and `%.250s'"),
  92. pi->thispartn, pi->filename, partlist[i]->filename);
  93. partlist[i]= pi;
  94. }
  95. void do_join(const char *const *argv) {
  96. char *p;
  97. const char *thisarg;
  98. struct partqueue *pq;
  99. struct partinfo *refi, *pi, **partlist;
  100. unsigned int i;
  101. assert(!queue);
  102. if (!*argv)
  103. badusage(_("--%s requires one or more part file arguments"),
  104. cipaction->olong);
  105. while ((thisarg= *argv++)) {
  106. pq= nfmalloc(sizeof(struct partqueue));
  107. mustgetpartinfo(thisarg,&pq->info);
  108. pq->nextinqueue= queue;
  109. queue= pq;
  110. }
  111. refi= NULL;
  112. for (pq= queue; pq; pq= pq->nextinqueue)
  113. if (!refi || pq->info.thispartn < refi->thispartn) refi= &pq->info;
  114. assert(refi);
  115. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  116. for (i = 0; i < refi->maxpartn; i++)
  117. partlist[i] = NULL;
  118. for (pq= queue; pq; pq= pq->nextinqueue) {
  119. pi= &pq->info;
  120. addtopartlist(partlist,pi,refi);
  121. }
  122. for (i=0; i<refi->maxpartn; i++) {
  123. if (!partlist[i]) ohshit(_("part %d is missing"),i+1);
  124. }
  125. if (!opt_outputfile) {
  126. p= nfmalloc(strlen(refi->package)+1+strlen(refi->version)+sizeof(DEBEXT));
  127. strcpy(p,refi->package);
  128. strcat(p,"-");
  129. strcat(p,refi->version);
  130. strcat(p,DEBEXT);
  131. opt_outputfile = p;
  132. }
  133. reassemble(partlist, opt_outputfile);
  134. }