split.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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-2011 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 <stdint.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/i18n.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/path.h>
  40. #include <dpkg/subproc.h>
  41. #include <dpkg/buffer.h>
  42. #include <dpkg/ar.h>
  43. #include <dpkg/options.h>
  44. #include "dpkg-split.h"
  45. static char *
  46. deb_field(const char *filename, const char *field)
  47. {
  48. pid_t pid;
  49. int p[2];
  50. struct varbuf buf = VARBUF_INIT;
  51. char *end;
  52. m_pipe(p);
  53. pid = subproc_fork();
  54. if (pid == 0) {
  55. /* Child writes to pipe. */
  56. m_dup2(p[1], 1);
  57. close(p[0]);
  58. close(p[1]);
  59. execlp(BACKEND, BACKEND, "--field", filename, field, NULL);
  60. ohshite(_("unable to execute %s (%s)"),
  61. _("package field value extraction"), BACKEND);
  62. }
  63. close(p[1]);
  64. /* Parant reads from pipe. */
  65. varbuf_reset(&buf);
  66. fd_vbuf_copy(p[0], &buf, -1, _("package field value extraction"));
  67. varbuf_end_str(&buf);
  68. close(p[0]);
  69. subproc_wait_check(pid, _("package field value extraction"), PROCPIPE);
  70. /* Trim down trailing junk. */
  71. for (end = buf.buf + strlen(buf.buf) - 1; end - buf.buf >= 1; end--)
  72. if (isspace(*end))
  73. *end = '\0';
  74. else
  75. break;
  76. return varbuf_detach(&buf);
  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 (isupper(*s))
  87. *d = tolower(*s);
  88. else if (islower(*s) || 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. int fd_src;
  100. struct stat st;
  101. char hash[MD5HASHLEN + 1];
  102. char *package, *version, *arch;
  103. int nparts, curpart;
  104. off_t partsize;
  105. off_t cur_partsize, last_partsize;
  106. char *prefixdir = NULL, *msdos_prefix = NULL;
  107. struct varbuf file_dst = VARBUF_INIT;
  108. struct varbuf partmagic = VARBUF_INIT;
  109. struct varbuf partname = VARBUF_INIT;
  110. fd_src = open(file_src, O_RDONLY);
  111. if (fd_src < 0)
  112. ohshite(_("unable to open source file `%.250s'"), file_src);
  113. if (fstat(fd_src, &st))
  114. ohshite(_("unable to fstat source file"));
  115. if (!S_ISREG(st.st_mode))
  116. ohshit(_("source file `%.250s' not a plain file"), file_src);
  117. fd_md5(fd_src, hash, -1, "md5hash");
  118. lseek(fd_src, 0, SEEK_SET);
  119. /* FIXME: Use libdpkg-deb. */
  120. package = deb_field(file_src, "Package");
  121. version = deb_field(file_src, "Version");
  122. arch = deb_field(file_src, "Architecture");
  123. partsize = maxpartsize - HEADERALLOWANCE;
  124. last_partsize = st.st_size % partsize;
  125. if (last_partsize == 0)
  126. last_partsize = partsize;
  127. nparts = (st.st_size + partsize - 1) / partsize;
  128. setvbuf(stdout, NULL, _IONBF, 0);
  129. printf(P_("Splitting package %s into %d part: ",
  130. "Splitting package %s into %d parts: ", nparts),
  131. package, nparts);
  132. if (msdos) {
  133. char *t;
  134. t = m_strdup(prefix);
  135. prefixdir = m_strdup(dirname(t));
  136. free(t);
  137. msdos_prefix = m_strdup(path_basename(prefix));
  138. prefix = clean_msdos_filename(msdos_prefix);
  139. }
  140. for (curpart = 1; curpart <= nparts; curpart++) {
  141. int fd_dst;
  142. varbuf_reset(&file_dst);
  143. /* Generate output filename. */
  144. if (msdos) {
  145. char *refname;
  146. int prefix_max;
  147. m_asprintf(&refname, "%dof%d", curpart, nparts);
  148. prefix_max = max(8 - strlen(refname), 0);
  149. varbuf_printf(&file_dst, "%s/%.*s%.8s.deb",
  150. prefixdir, prefix_max, prefix, refname);
  151. free(refname);
  152. } else {
  153. varbuf_printf(&file_dst, "%s.%dof%d.deb",
  154. prefix, curpart, nparts);
  155. }
  156. if (curpart == nparts)
  157. cur_partsize = last_partsize;
  158. else
  159. cur_partsize = partsize;
  160. if (cur_partsize > maxpartsize) {
  161. ohshit(_("Header is too long, making part too long. "
  162. "Your package name or version\n"
  163. "numbers must be extraordinarily long, "
  164. "or something. Giving up.\n"));
  165. }
  166. /* Split the data. */
  167. fd_dst = creat(file_dst.buf, 0644);
  168. if (fd_dst < 0)
  169. ohshite(_("unable to open file '%s'"), file_dst.buf);
  170. /* Write the ar header. */
  171. dpkg_ar_put_magic(file_dst.buf, fd_dst);
  172. /* Write the debian-split part. */
  173. varbuf_printf(&partmagic,
  174. "%s\n%s\n%s\n%s\n%jd\n%jd\n%d/%d\n%s\n",
  175. SPLITVERSION, package, version, hash,
  176. (intmax_t)st.st_size, (intmax_t)partsize,
  177. curpart, nparts, arch);
  178. dpkg_ar_member_put_mem(file_dst.buf, fd_dst, PARTMAGIC,
  179. partmagic.buf, partmagic.used);
  180. varbuf_reset(&partmagic);
  181. /* Write the data part. */
  182. varbuf_printf(&partname, "data.%d", curpart);
  183. dpkg_ar_member_put_file(file_dst.buf, fd_dst, partname.buf,
  184. fd_src, cur_partsize);
  185. varbuf_reset(&partname);
  186. close(fd_dst);
  187. printf("%d ", curpart);
  188. }
  189. varbuf_destroy(&file_dst);
  190. varbuf_destroy(&partname);
  191. varbuf_destroy(&partmagic);
  192. free(prefixdir);
  193. free(msdos_prefix);
  194. close(fd_src);
  195. printf(_("done\n"));
  196. return 0;
  197. }
  198. int
  199. do_split(const char *const *argv)
  200. {
  201. const char *sourcefile, *prefix;
  202. sourcefile = *argv++;
  203. if (!sourcefile)
  204. badusage(_("--split needs a source filename argument"));
  205. prefix = *argv++;
  206. if (prefix && *argv)
  207. badusage(_("--split takes at most a source filename and destination prefix"));
  208. if (!prefix) {
  209. char *palloc;
  210. int l;
  211. l = strlen(sourcefile);
  212. palloc = nfmalloc(l + 1);
  213. strcpy(palloc, sourcefile);
  214. if (strcmp(palloc + l - (sizeof(DEBEXT) - 1), DEBEXT) == 0) {
  215. l -= (sizeof(DEBEXT) - 1);
  216. palloc[l] = '\0';
  217. }
  218. prefix = palloc;
  219. }
  220. mksplit(sourcefile, prefix, opt_maxpartsize, opt_msdos);
  221. return 0;
  222. }