join.c 4.2 KB

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