split.c 6.9 KB

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