join.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * join.c - joining
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@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 <https://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 <fcntl.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. #include <dpkg/dpkg-db.h>
  32. #include <dpkg/buffer.h>
  33. #include <dpkg/options.h>
  34. #include "dpkg-split.h"
  35. void reassemble(struct partinfo **partlist, const char *outputfile) {
  36. struct dpkg_error err;
  37. int fd_out, fd_in;
  38. unsigned int i;
  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. fd_out = creat(outputfile, 0644);
  44. if (fd_out < 0)
  45. ohshite(_("unable to open output file '%.250s'"), outputfile);
  46. for (i=0; i<partlist[0]->maxpartn; i++) {
  47. struct partinfo *pi = partlist[i];
  48. fd_in = open(pi->filename, O_RDONLY);
  49. if (fd_in < 0)
  50. ohshite(_("unable to (re)open input part file '%.250s'"), pi->filename);
  51. if (fd_skip(fd_in, pi->headerlen, &err) < 0)
  52. ohshit(_("cannot skip split package header for '%s': %s"), pi->filename,
  53. err.str);
  54. if (fd_fd_copy(fd_in, fd_out, pi->thispartlen, &err) < 0)
  55. ohshit(_("cannot append split package part '%s' to '%s': %s"),
  56. pi->filename, outputfile, err.str);
  57. close(fd_in);
  58. printf("%d ",i+1);
  59. }
  60. if (fsync(fd_out))
  61. ohshite(_("unable to sync file '%s'"), outputfile);
  62. if (close(fd_out))
  63. ohshite(_("unable to close file '%s'"), outputfile);
  64. printf(_("done\n"));
  65. }
  66. void addtopartlist(struct partinfo **partlist,
  67. struct partinfo *pi, struct partinfo *refi) {
  68. int i;
  69. if (strcmp(pi->package,refi->package) ||
  70. strcmp(pi->version,refi->version) ||
  71. strcmp(pi->md5sum,refi->md5sum) ||
  72. pi->orglength != refi->orglength ||
  73. pi->maxpartn != refi->maxpartn ||
  74. pi->maxpartlen != refi->maxpartlen) {
  75. print_info(pi);
  76. print_info(refi);
  77. ohshit(_("files '%.250s' and '%.250s' are not parts of the same file"),
  78. pi->filename,refi->filename);
  79. }
  80. i= pi->thispartn-1;
  81. if (partlist[i])
  82. ohshit(_("there are several versions of part %d - at least '%.250s' and '%.250s'"),
  83. pi->thispartn, pi->filename, partlist[i]->filename);
  84. partlist[i]= pi;
  85. }
  86. int
  87. do_join(const char *const *argv)
  88. {
  89. const char *thisarg;
  90. struct partqueue *queue = NULL;
  91. struct partqueue *pq;
  92. struct partinfo *refi, **partlist;
  93. unsigned int i;
  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. struct partinfo *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 (!opt_outputfile) {
  118. char *p;
  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, "_");
  124. strcat(p, refi->arch ? refi->arch : "unknown");
  125. strcat(p,DEBEXT);
  126. opt_outputfile = p;
  127. }
  128. reassemble(partlist, opt_outputfile);
  129. return 0;
  130. }