split.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * split.c - splitting archives
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2010 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <limits.h>
  27. #include <fcntl.h>
  28. #include <libgen.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/subproc.h>
  39. #include <dpkg/buffer.h>
  40. #include <dpkg/ar.h>
  41. #include <dpkg/myopt.h>
  42. #include "dpkg-split.h"
  43. static char *
  44. deb_field(const char *filename, const char *field)
  45. {
  46. pid_t pid;
  47. int p[2];
  48. struct varbuf buf = VARBUF_INIT;
  49. char *end;
  50. m_pipe(p);
  51. pid = subproc_fork();
  52. if (pid == 0) {
  53. /* Child writes to pipe. */
  54. m_dup2(p[1], 1);
  55. close(p[0]);
  56. close(p[1]);
  57. execlp(BACKEND, BACKEND, "--field", filename, field, NULL);
  58. ohshite(_("unable to execute %s (%s)"),
  59. _("package field value extraction"), BACKEND);
  60. }
  61. close(p[1]);
  62. /* Parant reads from pipe. */
  63. varbufreset(&buf);
  64. fd_vbuf_copy(p[0], &buf, -1, _("package field value extraction"));
  65. varbufaddc(&buf, '\0');
  66. close(p[0]);
  67. subproc_wait_check(pid, _("package field value extraction"), PROCPIPE);
  68. /* Trim down trailing junk. */
  69. for (end = buf.buf + strlen(buf.buf) - 1; end - buf.buf >= 1; end--)
  70. if (isspace(*end))
  71. *end = '\0';
  72. else
  73. break;
  74. return varbuf_detach(&buf);
  75. }
  76. /* Cleanup filename for use in crippled msdos systems. */
  77. static char *
  78. clean_msdos_filename(char *filename)
  79. {
  80. char *d, *s;
  81. for (s = d = filename; *s; d++, s++) {
  82. if (*s == '+')
  83. *d = 'x';
  84. else if (isupper(*s))
  85. *d = tolower(*s);
  86. else if (islower(*s) || isdigit(*s))
  87. *d = *s;
  88. else
  89. s++;
  90. }
  91. return filename;
  92. }
  93. static int
  94. mksplit(const char *file_src, const char *prefix, size_t maxpartsize,
  95. bool msdos)
  96. {
  97. int fd_src;
  98. struct stat st;
  99. char hash[MD5HASHLEN + 1];
  100. char *package, *version;
  101. int nparts, curpart;
  102. off_t partsize;
  103. off_t last_partsize;
  104. char *prefixdir = NULL, *msdos_prefix = NULL;
  105. struct varbuf file_dst = VARBUF_INIT;
  106. struct varbuf partmagic = VARBUF_INIT;
  107. struct varbuf partname = VARBUF_INIT;
  108. fd_src = open(file_src, O_RDONLY);
  109. if (fd_src < 0)
  110. ohshite(_("unable to open source file `%.250s'"), file_src);
  111. if (fstat(fd_src, &st))
  112. ohshite(_("unable to fstat source file"));
  113. if (!S_ISREG(st.st_mode))
  114. ohshit(_("source file `%.250s' not a plain file"), file_src);
  115. fd_md5(fd_src, hash, -1, "md5hash");
  116. lseek(fd_src, 0, SEEK_SET);
  117. /* FIXME: Use libdpkg-deb. */
  118. package = deb_field(file_src, "Package");
  119. version = deb_field(file_src, "Version");
  120. partsize = maxpartsize - HEADERALLOWANCE;
  121. last_partsize = st.st_size % partsize;
  122. if (last_partsize == 0)
  123. last_partsize = partsize;
  124. nparts = (st.st_size + partsize - 1) / partsize;
  125. setvbuf(stdout, NULL, _IONBF, 0);
  126. printf(P_("Splitting package %s into %d part: ",
  127. "Splitting package %s into %d parts: ", nparts),
  128. package, nparts);
  129. if (msdos) {
  130. char *t;
  131. t = m_strdup(prefix);
  132. prefixdir = m_strdup(dirname(t));
  133. free(t);
  134. t = m_strdup(prefix);
  135. msdos_prefix = m_strdup(basename(t));
  136. free(t);
  137. prefix = clean_msdos_filename(msdos_prefix);
  138. }
  139. for (curpart = 1; curpart <= nparts; curpart++) {
  140. int fd_dst;
  141. varbufreset(&file_dst);
  142. /* Generate output filename. */
  143. if (msdos) {
  144. struct varbuf refname = VARBUF_INIT;
  145. int prefix_max;
  146. varbufprintf(&refname, "%dof%d", curpart, nparts);
  147. prefix_max = max(8 - strlen(refname.buf), 0);
  148. varbufprintf(&file_dst, "%s/%.*s%.8s.deb",
  149. prefixdir, prefix_max, prefix,
  150. refname.buf);
  151. varbuf_destroy(&refname);
  152. } else {
  153. varbufprintf(&file_dst, "%s.%dof%d.deb",
  154. prefix, curpart, nparts);
  155. }
  156. if (curpart == nparts)
  157. partsize = last_partsize;
  158. if ((size_t)partsize > maxpartsize) {
  159. ohshit(_("Header is too long, making part too long. "
  160. "Your package name or version\n"
  161. "numbers must be extraordinarily long, "
  162. "or something. Giving up.\n"));
  163. }
  164. /* Split the data. */
  165. fd_dst = creat(file_dst.buf, 0644);
  166. if (fd_dst < 0)
  167. ohshite(_("unable to open file '%s'"), file_dst.buf);
  168. /* Write the ar header. */
  169. dpkg_ar_put_magic(file_dst.buf, fd_dst);
  170. /* Write the debian-split part. */
  171. varbufprintf(&partmagic, "%s\n%s\n%s\n%s\n%zu\n%zu\n%d/%d\n",
  172. SPLITVERSION, package, version, hash,
  173. st.st_size, partsize, curpart, nparts);
  174. dpkg_ar_member_put_mem(file_dst.buf, fd_dst, PARTMAGIC,
  175. partmagic.buf, partmagic.used);
  176. varbufreset(&partmagic);
  177. /* Write the data part. */
  178. varbufprintf(&partname, "data.%d", curpart);
  179. dpkg_ar_member_put_file(file_dst.buf, fd_dst, partname.buf,
  180. fd_src, partsize);
  181. varbufreset(&partname);
  182. close(fd_dst);
  183. printf("%d ", curpart);
  184. }
  185. varbuf_destroy(&file_dst);
  186. varbuf_destroy(&partname);
  187. varbuf_destroy(&partmagic);
  188. free(prefixdir);
  189. free(msdos_prefix);
  190. close(fd_src);
  191. printf(_("done\n"));
  192. return 0;
  193. }
  194. void
  195. do_split(const char *const *argv)
  196. {
  197. const char *sourcefile, *prefix;
  198. sourcefile = *argv++;
  199. if (!sourcefile)
  200. badusage(_("--split needs a source filename argument"));
  201. prefix = *argv++;
  202. if (prefix && *argv)
  203. badusage(_("--split takes at most a source filename and destination prefix"));
  204. if (!prefix) {
  205. char *palloc;
  206. int l;
  207. l = strlen(sourcefile);
  208. palloc = nfmalloc(l + 1);
  209. strcpy(palloc, sourcefile);
  210. if (!strcmp(palloc + l - (sizeof(DEBEXT) - 1), DEBEXT)) {
  211. l -= (sizeof(DEBEXT) - 1);
  212. palloc[l] = '\0';
  213. }
  214. prefix = palloc;
  215. }
  216. mksplit(sourcefile, prefix, opt_maxpartsize, opt_msdos);
  217. exit(0);
  218. }