build.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * build.c - building archives
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <dirent.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <stdbool.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <dpkg/i18n.h>
  39. #include <dpkg/c-ctype.h>
  40. #include <dpkg/dpkg.h>
  41. #include <dpkg/dpkg-db.h>
  42. #include <dpkg/path.h>
  43. #include <dpkg/treewalk.h>
  44. #include <dpkg/varbuf.h>
  45. #include <dpkg/fdio.h>
  46. #include <dpkg/buffer.h>
  47. #include <dpkg/subproc.h>
  48. #include <dpkg/command.h>
  49. #include <dpkg/compress.h>
  50. #include <dpkg/ar.h>
  51. #include <dpkg/options.h>
  52. #include "dpkg-deb.h"
  53. static void
  54. control_treewalk_feed(const char *dir, int fd_out)
  55. {
  56. struct treeroot *tree;
  57. struct treenode *node;
  58. tree = treewalk_open(dir, TREEWALK_NONE, NULL);
  59. for (node = treewalk_node(tree); node; node = treewalk_next(tree)) {
  60. char *nodename;
  61. nodename = str_fmt("./%s", treenode_get_virtname(node));
  62. if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0)
  63. ohshite(_("failed to write filename to tar pipe (%s)"),
  64. _("control member"));
  65. free(nodename);
  66. }
  67. treewalk_close(tree);
  68. }
  69. /**
  70. * Simple structure to store information about a file.
  71. */
  72. struct file_info {
  73. struct file_info *next;
  74. char *fn;
  75. };
  76. static struct file_info *
  77. file_info_new(const char *filename)
  78. {
  79. struct file_info *fi;
  80. fi = m_malloc(sizeof(*fi));
  81. fi->fn = m_strdup(filename);
  82. fi->next = NULL;
  83. return fi;
  84. }
  85. static void
  86. file_info_free(struct file_info *fi)
  87. {
  88. free(fi->fn);
  89. free(fi);
  90. }
  91. static struct file_info *
  92. file_info_find_name(struct file_info *list, const char *filename)
  93. {
  94. struct file_info *node;
  95. for (node = list; node; node = node->next)
  96. if (strcmp(node->fn, filename) == 0)
  97. return node;
  98. return NULL;
  99. }
  100. /**
  101. * Add a new file_info struct to a single linked list of file_info structs.
  102. *
  103. * We perform a slight optimization to work around a ‘feature’ in tar: tar
  104. * always recurses into subdirectories if you list a subdirectory. So if an
  105. * entry is added and the previous entry in the list is its subdirectory we
  106. * remove the subdirectory.
  107. *
  108. * After a file_info struct is added to a list it may no longer be freed, we
  109. * assume full responsibility for its memory.
  110. */
  111. static void
  112. file_info_list_append(struct file_info **head, struct file_info **tail,
  113. struct file_info *fi)
  114. {
  115. if (*head == NULL)
  116. *head = *tail = fi;
  117. else
  118. *tail = (*tail)->next =fi;
  119. }
  120. /**
  121. * Free the memory for all entries in a list of file_info structs.
  122. */
  123. static void
  124. file_info_list_free(struct file_info *fi)
  125. {
  126. while (fi) {
  127. struct file_info *fl;
  128. fl=fi; fi=fi->next;
  129. file_info_free(fl);
  130. }
  131. }
  132. static void
  133. file_treewalk_feed(const char *dir, int fd_out)
  134. {
  135. struct treeroot *tree;
  136. struct treenode *node;
  137. struct file_info *fi;
  138. struct file_info *symlist = NULL;
  139. struct file_info *symlist_end = NULL;
  140. tree = treewalk_open(dir, TREEWALK_NONE, NULL);
  141. for (node = treewalk_node(tree); node ; node = treewalk_next(tree)) {
  142. const char *virtname = treenode_get_virtname(node);
  143. char *nodename;
  144. if (strncmp(virtname, BUILDCONTROLDIR, strlen(BUILDCONTROLDIR)) == 0)
  145. continue;
  146. nodename = str_fmt("./%s", virtname);
  147. if (strchr(nodename, '\n'))
  148. ohshit(_("newline not allowed in pathname '%s'"), nodename);
  149. /* We need to reorder the files so we can make sure that symlinks
  150. * will not appear before their target. */
  151. if (S_ISLNK(treenode_get_mode(node))) {
  152. fi = file_info_new(nodename);
  153. file_info_list_append(&symlist, &symlist_end, fi);
  154. } else {
  155. if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0)
  156. ohshite(_("failed to write filename to tar pipe (%s)"),
  157. _("data member"));
  158. }
  159. free(nodename);
  160. }
  161. treewalk_close(tree);
  162. for (fi = symlist; fi; fi = fi->next)
  163. if (fd_write(fd_out, fi->fn, strlen(fi->fn) + 1) < 0)
  164. ohshite(_("failed to write filename to tar pipe (%s)"), _("data member"));
  165. file_info_list_free(symlist);
  166. }
  167. static const char *const maintainerscripts[] = {
  168. PREINSTFILE,
  169. POSTINSTFILE,
  170. PRERMFILE,
  171. POSTRMFILE,
  172. NULL,
  173. };
  174. /**
  175. * Check control directory and file permissions.
  176. */
  177. static void
  178. check_file_perms(const char *ctrldir)
  179. {
  180. struct varbuf path = VARBUF_INIT;
  181. const char *const *mscriptp;
  182. struct stat mscriptstab;
  183. varbuf_printf(&path, "%s/", ctrldir);
  184. if (lstat(path.buf, &mscriptstab))
  185. ohshite(_("unable to stat control directory"));
  186. if (!S_ISDIR(mscriptstab.st_mode))
  187. ohshit(_("control directory is not a directory"));
  188. if ((mscriptstab.st_mode & 07757) != 0755)
  189. ohshit(_("control directory has bad permissions %03lo "
  190. "(must be >=0755 and <=0775)"),
  191. (unsigned long)(mscriptstab.st_mode & 07777));
  192. for (mscriptp = maintainerscripts; *mscriptp; mscriptp++) {
  193. varbuf_reset(&path);
  194. varbuf_printf(&path, "%s/%s", ctrldir, *mscriptp);
  195. if (!lstat(path.buf, &mscriptstab)) {
  196. if (S_ISLNK(mscriptstab.st_mode))
  197. continue;
  198. if (!S_ISREG(mscriptstab.st_mode))
  199. ohshit(_("maintainer script '%.50s' is not a plain file or symlink"),
  200. *mscriptp);
  201. if ((mscriptstab.st_mode & 07557) != 0555)
  202. ohshit(_("maintainer script '%.50s' has bad permissions %03lo "
  203. "(must be >=0555 and <=0775)"),
  204. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  205. } else if (errno != ENOENT) {
  206. ohshite(_("maintainer script '%.50s' is not stattable"), *mscriptp);
  207. }
  208. }
  209. varbuf_destroy(&path);
  210. }
  211. /**
  212. * Check if conffiles contains sane information.
  213. */
  214. static void
  215. check_conffiles(const char *ctrldir, const char *rootdir)
  216. {
  217. FILE *cf;
  218. struct varbuf controlfile = VARBUF_INIT;
  219. char conffilename[MAXCONFFILENAME + 1];
  220. struct file_info *conffiles_head = NULL;
  221. struct file_info *conffiles_tail = NULL;
  222. varbuf_printf(&controlfile, "%s/%s", ctrldir, CONFFILESFILE);
  223. cf = fopen(controlfile.buf, "r");
  224. if (cf == NULL) {
  225. if (errno == ENOENT)
  226. return;
  227. ohshite(_("error opening conffiles file"));
  228. }
  229. while (fgets(conffilename, MAXCONFFILENAME + 1, cf)) {
  230. struct stat controlstab;
  231. int n;
  232. n = strlen(conffilename);
  233. if (!n)
  234. ohshite(_("empty string from fgets reading conffiles"));
  235. if (conffilename[n - 1] != '\n')
  236. ohshit(_("conffile name '%s' is too long, or missing final newline"),
  237. conffilename);
  238. conffilename[n - 1] = '\0';
  239. varbuf_reset(&controlfile);
  240. varbuf_printf(&controlfile, "%s/%s", rootdir, conffilename);
  241. if (lstat(controlfile.buf, &controlstab)) {
  242. if (errno == ENOENT) {
  243. if ((n > 1) && c_isspace(conffilename[n - 2]))
  244. warning(_("conffile filename '%s' contains trailing white spaces"),
  245. conffilename);
  246. ohshit(_("conffile '%.250s' does not appear in package"), conffilename);
  247. } else
  248. ohshite(_("conffile '%.250s' is not stattable"), conffilename);
  249. } else if (!S_ISREG(controlstab.st_mode)) {
  250. warning(_("conffile '%s' is not a plain file"), conffilename);
  251. }
  252. if (file_info_find_name(conffiles_head, conffilename)) {
  253. warning(_("conffile name '%s' is duplicated"), conffilename);
  254. } else {
  255. struct file_info *conffile;
  256. conffile = file_info_new(conffilename);
  257. file_info_list_append(&conffiles_head, &conffiles_tail, conffile);
  258. }
  259. }
  260. file_info_list_free(conffiles_head);
  261. varbuf_destroy(&controlfile);
  262. if (ferror(cf))
  263. ohshite(_("error reading conffiles file"));
  264. fclose(cf);
  265. }
  266. /**
  267. * Check the control file.
  268. *
  269. * @param dir The directory from where to build the binary package.
  270. * @return The pkginfo struct from the parsed control file.
  271. */
  272. static struct pkginfo *
  273. check_control_file(const char *ctrldir)
  274. {
  275. struct pkginfo *pkg;
  276. char *controlfile;
  277. controlfile = str_fmt("%s/%s", ctrldir, CONTROLFILE);
  278. parsedb(controlfile, pdb_parse_binary, &pkg);
  279. if (strspn(pkg->set->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
  280. strlen(pkg->set->name))
  281. ohshit(_("package name has characters that aren't lowercase alphanums or '-+.'"));
  282. if (pkg->available.arch->type == DPKG_ARCH_NONE ||
  283. pkg->available.arch->type == DPKG_ARCH_EMPTY)
  284. ohshit(_("package architecture is missing or empty"));
  285. if (pkg->priority == PKG_PRIO_OTHER)
  286. warning(_("'%s' contains user-defined Priority value '%s'"),
  287. controlfile, pkg->otherpriority);
  288. free(controlfile);
  289. return pkg;
  290. }
  291. /**
  292. * Perform some sanity checks on the to-be-built package control area.
  293. *
  294. * @param dir The directory from where to build the binary package.
  295. * @return The pkginfo struct from the parsed control file.
  296. */
  297. static struct pkginfo *
  298. check_control_area(const char *ctrldir, const char *rootdir)
  299. {
  300. struct pkginfo *pkg;
  301. int warns;
  302. /* Start by reading in the control file so we can check its contents. */
  303. pkg = check_control_file(ctrldir);
  304. check_file_perms(ctrldir);
  305. check_conffiles(ctrldir, rootdir);
  306. warns = warning_get_count();
  307. if (warns)
  308. warning(P_("ignoring %d warning about the control file(s)",
  309. "ignoring %d warnings about the control file(s)", warns),
  310. warns);
  311. return pkg;
  312. }
  313. /**
  314. * Generate the pathname for the destination binary package.
  315. *
  316. * If the pathname cannot be computed, because the destination is a directory,
  317. * then NULL will be returned.
  318. *
  319. * @param dir The directory from where to build the binary package.
  320. * @param dest The destination name, either a file or directory name.
  321. * @return The pathname for the package being built.
  322. */
  323. static char *
  324. gen_dest_pathname(const char *dir, const char *dest)
  325. {
  326. if (dest) {
  327. struct stat dest_stab;
  328. if (stat(dest, &dest_stab)) {
  329. if (errno != ENOENT)
  330. ohshite(_("unable to check for existence of archive '%.250s'"), dest);
  331. } else if (S_ISDIR(dest_stab.st_mode)) {
  332. /* Need to compute the destination name from the package control file. */
  333. return NULL;
  334. }
  335. return m_strdup(dest);
  336. } else {
  337. char *pathname;
  338. pathname = m_malloc(strlen(dir) + sizeof(DEBEXT));
  339. strcpy(pathname, dir);
  340. path_trim_slash_slashdot(pathname);
  341. strcat(pathname, DEBEXT);
  342. return pathname;
  343. }
  344. }
  345. /**
  346. * Generate the pathname for the destination binary package from control file.
  347. *
  348. * @return The pathname for the package being built.
  349. */
  350. static char *
  351. gen_dest_pathname_from_pkg(const char *dir, struct pkginfo *pkg)
  352. {
  353. return str_fmt("%s/%s_%s_%s%s", dir, pkg->set->name,
  354. versiondescribe(&pkg->available.version, vdew_never),
  355. pkg->available.arch->name, DEBEXT);
  356. }
  357. typedef void filenames_feed_func(const char *dir, int fd_out);
  358. /**
  359. * Pack the contents of a directory into a tarball.
  360. */
  361. static void
  362. tarball_pack(const char *dir, filenames_feed_func *tar_filenames_feeder,
  363. time_t timestamp, const char *mode,
  364. struct compress_params *tar_compress_params, int fd_out)
  365. {
  366. int pipe_filenames[2], pipe_tarball[2];
  367. pid_t pid_tar, pid_comp;
  368. /* Fork off a tar. We will feed it a list of filenames on stdin later. */
  369. m_pipe(pipe_filenames);
  370. m_pipe(pipe_tarball);
  371. pid_tar = subproc_fork();
  372. if (pid_tar == 0) {
  373. struct command cmd;
  374. char mtime[50];
  375. m_dup2(pipe_filenames[0], 0);
  376. close(pipe_filenames[0]);
  377. close(pipe_filenames[1]);
  378. m_dup2(pipe_tarball[1], 1);
  379. close(pipe_tarball[0]);
  380. close(pipe_tarball[1]);
  381. if (chdir(dir))
  382. ohshite(_("failed to chdir to '%.255s'"), dir);
  383. snprintf(mtime, sizeof(mtime), "@%ld", timestamp);
  384. command_init(&cmd, TAR, "tar -cf");
  385. command_add_args(&cmd, "tar", "-cf", "-", "--format=gnu",
  386. "--mtime", mtime, "--clamp-mtime", NULL);
  387. /* Mode might become a positional argument, pass it before -T. */
  388. if (mode)
  389. command_add_args(&cmd, "--mode", mode, NULL);
  390. command_add_args(&cmd, "--null", "--no-unquote", "--no-recursion",
  391. "-T", "-", NULL);
  392. command_exec(&cmd);
  393. }
  394. close(pipe_filenames[0]);
  395. close(pipe_tarball[1]);
  396. /* Of course we should not forget to compress the archive as well. */
  397. pid_comp = subproc_fork();
  398. if (pid_comp == 0) {
  399. close(pipe_filenames[1]);
  400. compress_filter(tar_compress_params, pipe_tarball[0], fd_out,
  401. _("compressing tar member"));
  402. exit(0);
  403. }
  404. close(pipe_tarball[0]);
  405. /* All the pipes are set, now lets start feeding filenames to tar. */
  406. tar_filenames_feeder(dir, pipe_filenames[1]);
  407. /* All done, clean up wait for tar and <compress> to finish their job. */
  408. close(pipe_filenames[1]);
  409. subproc_reap(pid_comp, _("<compress> from tar -cf"), 0);
  410. subproc_reap(pid_tar, "tar -cf", 0);
  411. }
  412. static time_t
  413. parse_timestamp(const char *value)
  414. {
  415. time_t timestamp;
  416. char *end;
  417. errno = 0;
  418. timestamp = strtol(value, &end, 10);
  419. if (value == end || *end || errno != 0)
  420. ohshite(_("unable to parse timestamp '%.255s'"), value);
  421. return timestamp;
  422. }
  423. /**
  424. * Overly complex function that builds a .deb file.
  425. */
  426. int
  427. do_build(const char *const *argv)
  428. {
  429. struct compress_params control_compress_params;
  430. struct dpkg_error err;
  431. struct dpkg_ar *ar;
  432. time_t timestamp;
  433. const char *timestamp_str;
  434. const char *dir, *dest;
  435. char *ctrldir;
  436. char *debar;
  437. char *tfbuf;
  438. int gzfd;
  439. /* Decode our arguments. */
  440. dir = *argv++;
  441. if (!dir)
  442. badusage(_("--%s needs a <directory> argument"), cipaction->olong);
  443. dest = *argv++;
  444. if (dest && *argv)
  445. badusage(_("--%s takes at most two arguments"), cipaction->olong);
  446. debar = gen_dest_pathname(dir, dest);
  447. ctrldir = str_fmt("%s/%s", dir, BUILDCONTROLDIR);
  448. /* Perform some sanity checks on the to-be-build package. */
  449. if (nocheckflag) {
  450. if (debar == NULL)
  451. ohshit(_("target is directory - cannot skip control file check"));
  452. warning(_("not checking contents of control area"));
  453. info(_("building an unknown package in '%s'."), debar);
  454. } else {
  455. struct pkginfo *pkg;
  456. pkg = check_control_area(ctrldir, dir);
  457. if (debar == NULL)
  458. debar = gen_dest_pathname_from_pkg(dest, pkg);
  459. info(_("building package '%s' in '%s'."), pkg->set->name, debar);
  460. }
  461. m_output(stdout, _("<standard output>"));
  462. timestamp_str = getenv("SOURCE_DATE_EPOCH");
  463. if (timestamp_str)
  464. timestamp = parse_timestamp(timestamp_str);
  465. else
  466. timestamp = time(NULL);
  467. /* Now that we have verified everything it is time to actually
  468. * build something. Let's start by making the ar-wrapper. */
  469. ar = dpkg_ar_create(debar, 0644);
  470. dpkg_ar_set_mtime(ar, timestamp);
  471. unsetenv("TAR_OPTIONS");
  472. /* Create a temporary file to store the control data in. Immediately
  473. * unlink our temporary file so others can't mess with it. */
  474. tfbuf = path_make_temp_template("dpkg-deb");
  475. gzfd = mkstemp(tfbuf);
  476. if (gzfd == -1)
  477. ohshite(_("failed to make temporary file (%s)"), _("control member"));
  478. /* Make sure it's gone, the fd will remain until we close it. */
  479. if (unlink(tfbuf))
  480. ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
  481. tfbuf);
  482. free(tfbuf);
  483. /* Select the compressor to use for our control archive. */
  484. if (opt_uniform_compression) {
  485. control_compress_params = compress_params;
  486. } else {
  487. control_compress_params.type = COMPRESSOR_TYPE_GZIP;
  488. control_compress_params.strategy = COMPRESSOR_STRATEGY_NONE;
  489. control_compress_params.level = -1;
  490. if (!compressor_check_params(&control_compress_params, &err))
  491. internerr("invalid control member compressor params: %s", err.str);
  492. }
  493. /* Fork a tar to package the control-section of the package. */
  494. tarball_pack(ctrldir, control_treewalk_feed, timestamp, "u+rw,go=rX",
  495. &control_compress_params, gzfd);
  496. free(ctrldir);
  497. if (lseek(gzfd, 0, SEEK_SET))
  498. ohshite(_("failed to rewind temporary file (%s)"), _("control member"));
  499. /* We have our first file for the ar-archive. Write a header for it
  500. * to the package and insert it. */
  501. if (deb_format.major == 0) {
  502. struct stat controlstab;
  503. char versionbuf[40];
  504. if (fstat(gzfd, &controlstab))
  505. ohshite(_("failed to stat temporary file (%s)"), _("control member"));
  506. sprintf(versionbuf, "%-8s\n%jd\n", OLDARCHIVEVERSION,
  507. (intmax_t)controlstab.st_size);
  508. if (fd_write(ar->fd, versionbuf, strlen(versionbuf)) < 0)
  509. ohshite(_("error writing '%s'"), debar);
  510. if (fd_fd_copy(gzfd, ar->fd, -1, &err) < 0)
  511. ohshit(_("cannot copy '%s' into archive '%s': %s"), _("control member"),
  512. ar->name, err.str);
  513. } else if (deb_format.major == 2) {
  514. const char deb_magic[] = ARCHIVEVERSION "\n";
  515. char adminmember[16 + 1];
  516. sprintf(adminmember, "%s%s", ADMINMEMBER,
  517. compressor_get_extension(control_compress_params.type));
  518. dpkg_ar_put_magic(ar);
  519. dpkg_ar_member_put_mem(ar, DEBMAGIC, deb_magic, strlen(deb_magic));
  520. dpkg_ar_member_put_file(ar, adminmember, gzfd, -1);
  521. } else {
  522. internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
  523. }
  524. close(gzfd);
  525. /* Control is done, now we need to archive the data. */
  526. if (deb_format.major == 0) {
  527. /* In old format, the data member is just concatenated after the
  528. * control member, so we do not need a temporary file and can use
  529. * the compression file descriptor. */
  530. gzfd = ar->fd;
  531. } else if (deb_format.major == 2) {
  532. /* Start by creating a new temporary file. Immediately unlink the
  533. * temporary file so others can't mess with it. */
  534. tfbuf = path_make_temp_template("dpkg-deb");
  535. gzfd = mkstemp(tfbuf);
  536. if (gzfd == -1)
  537. ohshite(_("failed to make temporary file (%s)"), _("data member"));
  538. /* Make sure it's gone, the fd will remain until we close it. */
  539. if (unlink(tfbuf))
  540. ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
  541. tfbuf);
  542. free(tfbuf);
  543. } else {
  544. internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
  545. }
  546. /* Pack the directory into a tarball, feeding files from the callback. */
  547. tarball_pack(dir, file_treewalk_feed, timestamp, NULL, &compress_params, gzfd);
  548. /* Okay, we have data.tar as well now, add it to the ar wrapper. */
  549. if (deb_format.major == 2) {
  550. char datamember[16 + 1];
  551. sprintf(datamember, "%s%s", DATAMEMBER,
  552. compressor_get_extension(compress_params.type));
  553. if (lseek(gzfd, 0, SEEK_SET))
  554. ohshite(_("failed to rewind temporary file (%s)"), _("data member"));
  555. dpkg_ar_member_put_file(ar, datamember, gzfd, -1);
  556. close(gzfd);
  557. }
  558. if (fsync(ar->fd))
  559. ohshite(_("unable to sync file '%s'"), ar->name);
  560. dpkg_ar_close(ar);
  561. free(debar);
  562. return 0;
  563. }