split.c 6.7 KB

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