build.c 19 KB

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