split.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 © 2008-2015 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 <https://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 <time.h>
  31. #include <unistd.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/c-ctype.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/parsedump.h>
  40. #include <dpkg/path.h>
  41. #include <dpkg/string.h>
  42. #include <dpkg/subproc.h>
  43. #include <dpkg/buffer.h>
  44. #include <dpkg/ar.h>
  45. #include <dpkg/options.h>
  46. #include "dpkg-split.h"
  47. /**
  48. * Parse the control file from a .deb package into a struct pkginfo.
  49. */
  50. static struct pkginfo *
  51. deb_parse_control(const char *filename)
  52. {
  53. struct parsedb_state *ps;
  54. struct pkginfo *pkg;
  55. pid_t pid;
  56. int p[2];
  57. m_pipe(p);
  58. pid = subproc_fork();
  59. if (pid == 0) {
  60. /* Child writes to pipe. */
  61. m_dup2(p[1], 1);
  62. close(p[0]);
  63. close(p[1]);
  64. execlp(BACKEND, BACKEND, "--info", filename, "control", NULL);
  65. ohshite(_("unable to execute %s (%s)"),
  66. _("package field value extraction"), BACKEND);
  67. }
  68. close(p[1]);
  69. /* Parent reads from pipe. */
  70. ps = parsedb_new(_("<dpkg-deb --info pipe>"), p[0], pdb_parse_binary);
  71. parsedb_load(ps);
  72. parsedb_parse(ps, &pkg);
  73. parsedb_close(ps);
  74. close(p[0]);
  75. subproc_reap(pid, _("package field value extraction"), SUBPROC_NOPIPE);
  76. return pkg;
  77. }
  78. /* Cleanup filename for use in crippled msdos systems. */
  79. static char *
  80. clean_msdos_filename(char *filename)
  81. {
  82. char *d, *s;
  83. for (s = d = filename; *s; d++, s++) {
  84. if (*s == '+')
  85. *d = 'x';
  86. else if (c_isupper(*s))
  87. *d = c_tolower(*s);
  88. else if (c_islower(*s) || c_isdigit(*s))
  89. *d = *s;
  90. else
  91. s++;
  92. }
  93. return filename;
  94. }
  95. static int
  96. mksplit(const char *file_src, const char *prefix, off_t maxpartsize,
  97. bool msdos)
  98. {
  99. struct pkginfo *pkg;
  100. struct dpkg_error err;
  101. int fd_src;
  102. struct stat st;
  103. const char *version;
  104. char hash[MD5HASHLEN + 1];
  105. int nparts, curpart;
  106. off_t partsize;
  107. off_t cur_partsize, last_partsize;
  108. char *prefixdir = NULL, *msdos_prefix = NULL;
  109. struct varbuf file_dst = VARBUF_INIT;
  110. struct varbuf partmagic = VARBUF_INIT;
  111. struct varbuf partname = VARBUF_INIT;
  112. fd_src = open(file_src, O_RDONLY);
  113. if (fd_src < 0)
  114. ohshite(_("unable to open source file '%.250s'"), file_src);
  115. if (fstat(fd_src, &st))
  116. ohshite(_("unable to fstat source file"));
  117. if (!S_ISREG(st.st_mode))
  118. ohshit(_("source file '%.250s' not a plain file"), file_src);
  119. if (fd_md5(fd_src, hash, -1, &err) < 0)
  120. ohshit(_("cannot compute MD5 hash for file '%s': %s"),
  121. file_src, err.str);
  122. lseek(fd_src, 0, SEEK_SET);
  123. pkg = deb_parse_control(file_src);
  124. version = versiondescribe(&pkg->available.version, vdew_always);
  125. partsize = maxpartsize - HEADERALLOWANCE;
  126. last_partsize = st.st_size % partsize;
  127. if (last_partsize == 0)
  128. last_partsize = partsize;
  129. nparts = (st.st_size + partsize - 1) / partsize;
  130. printf(P_("Splitting package %s into %d part: ",
  131. "Splitting package %s into %d parts: ", nparts),
  132. pkg->set->name, nparts);
  133. if (msdos) {
  134. char *t;
  135. t = m_strdup(prefix);
  136. prefixdir = m_strdup(dirname(t));
  137. free(t);
  138. msdos_prefix = m_strdup(path_basename(prefix));
  139. prefix = clean_msdos_filename(msdos_prefix);
  140. }
  141. for (curpart = 1; curpart <= nparts; curpart++) {
  142. int fd_dst;
  143. varbuf_reset(&file_dst);
  144. /* Generate output filename. */
  145. if (msdos) {
  146. char *refname;
  147. int prefix_max;
  148. m_asprintf(&refname, "%dof%d", curpart, nparts);
  149. prefix_max = max(8 - strlen(refname), 0);
  150. varbuf_printf(&file_dst, "%s/%.*s%.8s.deb",
  151. prefixdir, prefix_max, prefix, refname);
  152. free(refname);
  153. } else {
  154. varbuf_printf(&file_dst, "%s.%dof%d.deb",
  155. prefix, curpart, nparts);
  156. }
  157. if (curpart == nparts)
  158. cur_partsize = last_partsize;
  159. else
  160. cur_partsize = partsize;
  161. if (cur_partsize > maxpartsize) {
  162. ohshit(_("header is too long, making part too long; "
  163. "the package name or version\n"
  164. "numbers must be extraordinarily long, "
  165. "or something; giving up"));
  166. }
  167. /* Split the data. */
  168. fd_dst = creat(file_dst.buf, 0644);
  169. if (fd_dst < 0)
  170. ohshite(_("unable to open file '%s'"), file_dst.buf);
  171. /* Write the ar header. */
  172. dpkg_ar_put_magic(file_dst.buf, fd_dst);
  173. /* Write the debian-split part. */
  174. varbuf_printf(&partmagic,
  175. "%s\n%s\n%s\n%s\n%jd\n%jd\n%d/%d\n%s\n",
  176. SPLITVERSION, pkg->set->name, version, hash,
  177. (intmax_t)st.st_size, (intmax_t)partsize,
  178. curpart, nparts, pkg->available.arch->name);
  179. dpkg_ar_member_put_mem(file_dst.buf, fd_dst, PARTMAGIC,
  180. partmagic.buf, partmagic.used);
  181. varbuf_reset(&partmagic);
  182. /* Write the data part. */
  183. varbuf_printf(&partname, "data.%d", curpart);
  184. dpkg_ar_member_put_file(file_dst.buf, fd_dst, partname.buf,
  185. fd_src, cur_partsize);
  186. varbuf_reset(&partname);
  187. close(fd_dst);
  188. printf("%d ", curpart);
  189. }
  190. varbuf_destroy(&file_dst);
  191. varbuf_destroy(&partname);
  192. varbuf_destroy(&partmagic);
  193. free(prefixdir);
  194. free(msdos_prefix);
  195. close(fd_src);
  196. printf(_("done\n"));
  197. return 0;
  198. }
  199. int
  200. do_split(const char *const *argv)
  201. {
  202. const char *sourcefile, *prefix;
  203. sourcefile = *argv++;
  204. if (!sourcefile)
  205. badusage(_("--split needs a source filename argument"));
  206. prefix = *argv++;
  207. if (prefix && *argv)
  208. badusage(_("--split takes at most a source filename and destination prefix"));
  209. if (!prefix) {
  210. size_t sourcefile_len = strlen(sourcefile);
  211. if (str_match_end(sourcefile, DEBEXT))
  212. sourcefile_len -= strlen(DEBEXT);
  213. prefix = nfstrnsave(sourcefile, sourcefile_len);
  214. }
  215. mksplit(sourcefile, prefix, opt_maxpartsize, opt_msdos);
  216. return 0;
  217. }