po: Update German man pages translation
[dpkg.git] / src / deb / build.c
bloba418dd12274d32659bb4aca7ad4a3e64c76f1a58
1 /*
2 * dpkg-deb - construction and deconstruction of *.deb archives
3 * build.c - building archives
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>
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.
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.
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/>.
23 #include <config.h>
24 #include <compat.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <time.h>
35 #include <dirent.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <stdio.h>
43 #include <dpkg/i18n.h>
44 #include <dpkg/c-ctype.h>
45 #include <dpkg/dpkg.h>
46 #include <dpkg/dpkg-db.h>
47 #include <dpkg/path.h>
48 #include <dpkg/treewalk.h>
49 #include <dpkg/varbuf.h>
50 #include <dpkg/fdio.h>
51 #include <dpkg/buffer.h>
52 #include <dpkg/subproc.h>
53 #include <dpkg/command.h>
54 #include <dpkg/compress.h>
55 #include <dpkg/ar.h>
56 #include <dpkg/options.h>
58 #include "dpkg-deb.h"
60 static void
61 control_treewalk_feed(const char *dir, int fd_out)
63 struct treeroot *tree;
64 struct treenode *node;
66 tree = treewalk_open(dir, TREEWALK_NONE, NULL);
67 for (node = treewalk_node(tree); node; node = treewalk_next(tree)) {
68 char *nodename;
70 nodename = str_fmt("./%s", treenode_get_virtname(node));
71 if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0)
72 ohshite(_("failed to write filename to tar pipe (%s)"),
73 _("control member"));
74 free(nodename);
76 treewalk_close(tree);
79 /**
80 * Simple structure to store information about a file.
82 struct file_info {
83 struct file_info *next;
84 char *fn;
87 static struct file_info *
88 file_info_new(const char *filename)
90 struct file_info *fi;
92 fi = m_malloc(sizeof(*fi));
93 fi->fn = m_strdup(filename);
94 fi->next = NULL;
96 return fi;
99 static void
100 file_info_free(struct file_info *fi)
102 free(fi->fn);
103 free(fi);
106 static struct file_info *
107 file_info_find_name(struct file_info *list, const char *filename)
109 struct file_info *node;
111 for (node = list; node; node = node->next)
112 if (strcmp(node->fn, filename) == 0)
113 return node;
115 return NULL;
119 * Add a new file_info struct to a single linked list of file_info structs.
121 * We perform a slight optimization to work around a ‘feature’ in tar: tar
122 * always recurses into subdirectories if you list a subdirectory. So if an
123 * entry is added and the previous entry in the list is its subdirectory we
124 * remove the subdirectory.
126 * After a file_info struct is added to a list it may no longer be freed, we
127 * assume full responsibility for its memory.
129 static void
130 file_info_list_append(struct file_info **head, struct file_info **tail,
131 struct file_info *fi)
133 if (*head == NULL)
134 *head = *tail = fi;
135 else
136 *tail = (*tail)->next =fi;
140 * Free the memory for all entries in a list of file_info structs.
142 static void
143 file_info_list_free(struct file_info *fi)
145 while (fi) {
146 struct file_info *fl;
148 fl=fi; fi=fi->next;
149 file_info_free(fl);
153 static void
154 file_treewalk_feed(const char *dir, int fd_out)
156 struct treeroot *tree;
157 struct treenode *node;
158 struct file_info *fi;
159 struct file_info *symlist = NULL;
160 struct file_info *symlist_end = NULL;
162 tree = treewalk_open(dir, TREEWALK_NONE, NULL);
163 for (node = treewalk_node(tree); node ; node = treewalk_next(tree)) {
164 const char *virtname = treenode_get_virtname(node);
165 char *nodename;
167 if (strncmp(virtname, BUILDCONTROLDIR, strlen(BUILDCONTROLDIR)) == 0)
168 continue;
170 nodename = str_fmt("./%s", virtname);
172 if (!opt_nocheck && strchr(nodename, '\n'))
173 ohshit(_("newline not allowed in pathname '%s'"), nodename);
175 /* We need to reorder the files so we can make sure that symlinks
176 * will not appear before their target. */
177 if (S_ISLNK(treenode_get_mode(node))) {
178 fi = file_info_new(nodename);
179 file_info_list_append(&symlist, &symlist_end, fi);
180 } else if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0) {
181 ohshite(_("failed to write filename to tar pipe (%s)"),
182 _("data member"));
185 free(nodename);
187 treewalk_close(tree);
189 for (fi = symlist; fi; fi = fi->next)
190 if (fd_write(fd_out, fi->fn, strlen(fi->fn) + 1) < 0)
191 ohshite(_("failed to write filename to tar pipe (%s)"), _("data member"));
193 file_info_list_free(symlist);
196 static const char *const maintainerscripts[] = {
197 PREINSTFILE,
198 POSTINSTFILE,
199 PRERMFILE,
200 POSTRMFILE,
201 MAINTSCRIPT_FILE_CONFIG,
202 NULL,
206 * Check control directory and file permissions.
208 static void
209 check_file_perms(const char *ctrldir)
211 struct varbuf path = VARBUF_INIT;
212 const char *const *mscriptp;
213 struct stat mscriptstab;
215 varbuf_printf(&path, "%s/", ctrldir);
216 if (lstat(path.buf, &mscriptstab))
217 ohshite(_("unable to stat control directory"));
218 if (!S_ISDIR(mscriptstab.st_mode))
219 ohshit(_("control directory is not a directory"));
220 if ((mscriptstab.st_mode & 07757) != 0755)
221 ohshit(_("control directory has bad permissions %03lo "
222 "(must be >=0755 and <=0775)"),
223 (unsigned long)(mscriptstab.st_mode & 07777));
225 for (mscriptp = maintainerscripts; *mscriptp; mscriptp++) {
226 varbuf_reset(&path);
227 varbuf_printf(&path, "%s/%s", ctrldir, *mscriptp);
228 if (!lstat(path.buf, &mscriptstab)) {
229 if (S_ISLNK(mscriptstab.st_mode))
230 continue;
231 if (!S_ISREG(mscriptstab.st_mode))
232 ohshit(_("maintainer script '%.50s' is not a plain file or symlink"),
233 *mscriptp);
234 if ((mscriptstab.st_mode & 07557) != 0555)
235 ohshit(_("maintainer script '%.50s' has bad permissions %03lo "
236 "(must be >=0555 and <=0775)"),
237 *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
238 } else if (errno != ENOENT) {
239 ohshite(_("maintainer script '%.50s' is not stattable"), *mscriptp);
243 varbuf_destroy(&path);
247 * Check if conffiles contains sane information.
249 static void
250 check_conffiles(const char *ctrldir, const char *rootdir)
252 FILE *cf;
253 struct varbuf controlfile = VARBUF_INIT;
254 char conffilenamebuf[MAXCONFFILENAME + 1];
255 struct file_info *conffiles_head = NULL;
256 struct file_info *conffiles_tail = NULL;
258 varbuf_printf(&controlfile, "%s/%s", ctrldir, CONFFILESFILE);
260 cf = fopen(controlfile.buf, "r");
261 if (cf == NULL) {
262 if (errno == ENOENT) {
263 varbuf_destroy(&controlfile);
264 return;
267 ohshite(_("error opening conffiles file"));
270 while (fgets(conffilenamebuf, MAXCONFFILENAME + 1, cf)) {
271 struct stat controlstab;
272 char *conffilename = conffilenamebuf;
273 int n;
274 bool remove_on_upgrade = false;
276 n = strlen(conffilename);
277 if (!n)
278 ohshite(_("empty string from fgets reading conffiles"));
280 if (conffilename[n - 1] != '\n')
281 ohshit(_("conffile name '%s' is too long, or missing final newline"),
282 conffilename);
284 conffilename[--n] = '\0';
286 if (c_isspace(conffilename[0])) {
287 /* The conffiles lines cannot start with whitespace; by handling this
288 * case now, we simplify the remaining code. Move past the whitespace
289 * to give a better error. */
290 while (c_isspace(conffilename[0]))
291 conffilename++;
292 if (conffilename[0] == '\0')
293 ohshit(_("empty and whitespace-only lines are not allowed in "
294 "conffiles"));
295 ohshit(_("line with conffile filename '%s' has leading white spaces"),
296 conffilename);
299 if (conffilename[0] != '/') {
300 char *flag = conffilename;
301 char *flag_end = strchr(flag, ' ');
303 if (flag_end) {
304 conffilename = flag_end + 1;
305 n -= conffilename - flag;
308 /* If no flag separator is found, assume a missing leading slash. */
309 if (flag_end == NULL || (conffilename[0] && conffilename[0] != '/'))
310 ohshit(_("conffile name '%s' is not an absolute pathname"), conffilename);
312 flag_end[0] = '\0';
314 /* Otherwise assume a missing filename after the flag separator. */
315 if (conffilename[0] == '\0')
316 ohshit(_("conffile name missing after flag '%s'"), flag);
318 if (strcmp(flag, "remove-on-upgrade") == 0)
319 remove_on_upgrade = true;
320 else
321 ohshit(_("unknown flag '%s' for conffile '%s'"), flag, conffilename);
324 varbuf_reset(&controlfile);
325 varbuf_printf(&controlfile, "%s%s", rootdir, conffilename);
326 if (lstat(controlfile.buf, &controlstab)) {
327 if (errno == ENOENT) {
328 if ((n > 1) && c_isspace(conffilename[n - 1]))
329 warning(_("conffile filename '%s' contains trailing white spaces"),
330 conffilename);
331 if (!remove_on_upgrade)
332 ohshit(_("conffile '%.250s' does not appear in package"), conffilename);
333 } else
334 ohshite(_("conffile '%.250s' is not stattable"), conffilename);
335 } else if (remove_on_upgrade) {
336 ohshit(_("conffile '%s' is present but is requested to be removed"),
337 conffilename);
338 } else if (!S_ISREG(controlstab.st_mode)) {
339 warning(_("conffile '%s' is not a plain file"), conffilename);
342 if (file_info_find_name(conffiles_head, conffilename)) {
343 warning(_("conffile name '%s' is duplicated"), conffilename);
344 } else {
345 struct file_info *conffile;
347 conffile = file_info_new(conffilename);
348 file_info_list_append(&conffiles_head, &conffiles_tail, conffile);
352 file_info_list_free(conffiles_head);
353 varbuf_destroy(&controlfile);
355 if (ferror(cf))
356 ohshite(_("error reading conffiles file"));
357 fclose(cf);
361 * Check the control file.
363 * @param ctrldir The directory from where to build the binary package.
364 * @return The pkginfo struct from the parsed control file.
366 static struct pkginfo *
367 check_control_file(const char *ctrldir)
369 struct pkginfo *pkg;
370 char *controlfile;
372 controlfile = str_fmt("%s/%s", ctrldir, CONTROLFILE);
373 parsedb(controlfile, pdb_parse_binary, &pkg);
375 if (strspn(pkg->set->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
376 strlen(pkg->set->name))
377 ohshit(_("package name has characters that aren't lowercase alphanums or '-+.'"));
378 if (pkg->available.arch->type == DPKG_ARCH_NONE ||
379 pkg->available.arch->type == DPKG_ARCH_EMPTY)
380 ohshit(_("package architecture is missing or empty"));
381 if (pkg->priority == PKG_PRIO_OTHER)
382 warning(_("'%s' contains user-defined Priority value '%s'"),
383 controlfile, pkg->otherpriority);
385 free(controlfile);
387 return pkg;
391 * Perform some sanity checks on the to-be-built package control area.
393 * @param ctrldir The directory from where to build the binary package.
394 * @return The pkginfo struct from the parsed control file.
396 static struct pkginfo *
397 check_control_area(const char *ctrldir, const char *rootdir)
399 struct pkginfo *pkg;
400 int warns;
402 /* Start by reading in the control file so we can check its contents. */
403 pkg = check_control_file(ctrldir);
404 check_file_perms(ctrldir);
405 check_conffiles(ctrldir, rootdir);
407 warns = warning_get_count();
408 if (warns)
409 warning(P_("ignoring %d warning about the control file(s)",
410 "ignoring %d warnings about the control file(s)", warns),
411 warns);
413 return pkg;
417 * Generate the pathname for the destination binary package.
419 * If the pathname cannot be computed, because the destination is a directory,
420 * then NULL will be returned.
422 * @param dir The directory from where to build the binary package.
423 * @param dest The destination name, either a file or directory name.
424 * @return The pathname for the package being built.
426 static char *
427 gen_dest_pathname(const char *dir, const char *dest)
429 if (dest) {
430 struct stat dest_stab;
432 if (stat(dest, &dest_stab)) {
433 if (errno != ENOENT)
434 ohshite(_("unable to check for existence of archive '%.250s'"), dest);
435 } else if (S_ISDIR(dest_stab.st_mode)) {
436 /* Need to compute the destination name from the package control file. */
437 return NULL;
440 return m_strdup(dest);
441 } else {
442 char *pathname;
444 pathname = m_malloc(strlen(dir) + sizeof(DEBEXT));
445 strcpy(pathname, dir);
446 path_trim_slash_slashdot(pathname);
447 strcat(pathname, DEBEXT);
449 return pathname;
454 * Generate the pathname for the destination binary package from control file.
456 * @return The pathname for the package being built.
458 static char *
459 gen_dest_pathname_from_pkg(const char *dir, struct pkginfo *pkg)
461 return str_fmt("%s/%s_%s_%s%s", dir, pkg->set->name,
462 versiondescribe(&pkg->available.version, vdew_never),
463 pkg->available.arch->name, DEBEXT);
466 typedef void filenames_feed_func(const char *dir, int fd_out);
468 struct tar_pack_options {
469 intmax_t timestamp;
470 const char *mode;
471 bool root_owner_group;
475 * Pack the contents of a directory into a tarball.
477 static void
478 tarball_pack(const char *dir, filenames_feed_func *tar_filenames_feeder,
479 struct tar_pack_options *options,
480 struct compress_params *tar_compress_params, int fd_out)
482 int pipe_filenames[2], pipe_tarball[2];
483 pid_t pid_tar, pid_comp;
485 /* Fork off a tar. We will feed it a list of filenames on stdin later. */
486 m_pipe(pipe_filenames);
487 m_pipe(pipe_tarball);
488 pid_tar = subproc_fork();
489 if (pid_tar == 0) {
490 struct command cmd;
491 char mtime[50];
493 m_dup2(pipe_filenames[0], 0);
494 close(pipe_filenames[0]);
495 close(pipe_filenames[1]);
496 m_dup2(pipe_tarball[1], 1);
497 close(pipe_tarball[0]);
498 close(pipe_tarball[1]);
500 if (chdir(dir))
501 ohshite(_("failed to chdir to '%.255s'"), dir);
503 snprintf(mtime, sizeof(mtime), "@%jd", options->timestamp);
505 command_init(&cmd, TAR, "tar -cf");
506 command_add_args(&cmd, "tar", "-cf", "-", "--format=gnu",
507 "--mtime", mtime, "--clamp-mtime", NULL);
508 /* Mode might become a positional argument, pass it before -T. */
509 if (options->mode)
510 command_add_args(&cmd, "--mode", options->mode, NULL);
511 if (options->root_owner_group)
512 command_add_args(&cmd, "--owner", "root:0", "--group", "root:0", NULL);
513 command_add_args(&cmd, "--null", "--no-unquote", "--no-recursion",
514 "-T", "-", NULL);
515 command_exec(&cmd);
517 close(pipe_filenames[0]);
518 close(pipe_tarball[1]);
520 /* Of course we should not forget to compress the archive as well. */
521 pid_comp = subproc_fork();
522 if (pid_comp == 0) {
523 close(pipe_filenames[1]);
524 compress_filter(tar_compress_params, pipe_tarball[0], fd_out,
525 _("compressing tar member"));
526 exit(0);
528 close(pipe_tarball[0]);
530 /* All the pipes are set, now lets start feeding filenames to tar. */
531 tar_filenames_feeder(dir, pipe_filenames[1]);
533 /* All done, clean up wait for tar and <compress> to finish their job. */
534 close(pipe_filenames[1]);
535 subproc_reap(pid_comp, _("<compress> from tar -cf"), 0);
536 subproc_reap(pid_tar, "tar -cf", 0);
539 static intmax_t
540 parse_timestamp(const char *value)
542 intmax_t timestamp;
543 char *end;
545 errno = 0;
546 timestamp = strtoimax(value, &end, 10);
547 if (value == end || *end)
548 ohshit(_("unable to parse timestamp '%.255s'"), value);
549 else if (errno != 0)
550 ohshite(_("unable to parse timestamp '%.255s'"), value);
552 return timestamp;
556 * Overly complex function that builds a .deb file.
559 do_build(const char *const *argv)
561 struct compress_params control_compress_params;
562 struct tar_pack_options tar_options;
563 struct dpkg_error err;
564 struct dpkg_ar *ar;
565 intmax_t timestamp;
566 const char *timestamp_str;
567 const char *dir, *dest;
568 char *ctrldir;
569 char *debar;
570 char *tfbuf;
571 int gzfd;
573 /* Decode our arguments. */
574 dir = *argv++;
575 if (!dir)
576 badusage(_("--%s needs a <directory> argument"), cipaction->olong);
578 dest = *argv++;
579 if (dest && *argv)
580 badusage(_("--%s takes at most two arguments"), cipaction->olong);
582 debar = gen_dest_pathname(dir, dest);
583 ctrldir = str_fmt("%s/%s", dir, BUILDCONTROLDIR);
585 /* Perform some sanity checks on the to-be-build package. */
586 if (opt_nocheck) {
587 if (debar == NULL)
588 ohshit(_("target is directory - cannot skip control file check"));
589 warning(_("not checking contents of control area"));
590 info(_("building an unknown package in '%s'."), debar);
591 } else {
592 struct pkginfo *pkg;
594 pkg = check_control_area(ctrldir, dir);
595 if (debar == NULL)
596 debar = gen_dest_pathname_from_pkg(dest, pkg);
597 info(_("building package '%s' in '%s'."), pkg->set->name, debar);
599 m_output(stdout, _("<standard output>"));
601 timestamp_str = getenv("SOURCE_DATE_EPOCH");
602 if (str_is_set(timestamp_str))
603 timestamp = parse_timestamp(timestamp_str);
604 else
605 timestamp = time(NULL);
607 /* Now that we have verified everything it is time to actually
608 * build something. Let's start by making the ar-wrapper. */
609 ar = dpkg_ar_create(debar, 0644);
611 dpkg_ar_set_mtime(ar, timestamp);
613 unsetenv("TAR_OPTIONS");
615 /* Create a temporary file to store the control data in. Immediately
616 * unlink our temporary file so others can't mess with it. */
617 tfbuf = path_make_temp_template("dpkg-deb");
618 gzfd = mkstemp(tfbuf);
619 if (gzfd < 0)
620 ohshite(_("failed to make temporary file (%s)"), _("control member"));
621 /* Make sure it's gone, the fd will remain until we close it. */
622 if (unlink(tfbuf))
623 ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
624 tfbuf);
625 free(tfbuf);
627 /* Select the compressor to use for our control archive. */
628 if (opt_uniform_compression) {
629 control_compress_params = compress_params;
630 } else {
631 control_compress_params = compress_params_deb0;
633 if (!compressor_check_params(&control_compress_params, &err))
634 internerr("invalid control member compressor params: %s", err.str);
637 /* Fork a tar to package the control-section of the package. */
638 tar_options.mode = "u+rw,go=rX";
639 tar_options.timestamp = timestamp;
640 tar_options.root_owner_group = true;
641 tarball_pack(ctrldir, control_treewalk_feed, &tar_options,
642 &control_compress_params, gzfd);
644 free(ctrldir);
646 if (lseek(gzfd, 0, SEEK_SET))
647 ohshite(_("failed to rewind temporary file (%s)"), _("control member"));
649 /* We have our first file for the ar-archive. Write a header for it
650 * to the package and insert it. */
651 if (deb_format.major == 0) {
652 struct stat controlstab;
653 char versionbuf[40];
655 if (fstat(gzfd, &controlstab))
656 ohshite(_("failed to stat temporary file (%s)"), _("control member"));
657 sprintf(versionbuf, "%-8s\n%jd\n", OLDARCHIVEVERSION,
658 (intmax_t)controlstab.st_size);
659 if (fd_write(ar->fd, versionbuf, strlen(versionbuf)) < 0)
660 ohshite(_("error writing '%s'"), debar);
661 if (fd_fd_copy(gzfd, ar->fd, -1, &err) < 0)
662 ohshit(_("cannot copy '%s' into archive '%s': %s"), _("control member"),
663 ar->name, err.str);
664 } else if (deb_format.major == 2) {
665 const char deb_magic[] = ARCHIVEVERSION "\n";
666 char adminmember[16 + 1];
668 sprintf(adminmember, "%s%s", ADMINMEMBER,
669 compressor_get_extension(control_compress_params.type));
671 dpkg_ar_put_magic(ar);
672 dpkg_ar_member_put_mem(ar, DEBMAGIC, deb_magic, strlen(deb_magic));
673 dpkg_ar_member_put_file(ar, adminmember, gzfd, -1);
674 } else {
675 internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
678 close(gzfd);
680 /* Control is done, now we need to archive the data. */
681 if (deb_format.major == 0) {
682 /* In old format, the data member is just concatenated after the
683 * control member, so we do not need a temporary file and can use
684 * the compression file descriptor. */
685 gzfd = ar->fd;
686 } else if (deb_format.major == 2) {
687 /* Start by creating a new temporary file. Immediately unlink the
688 * temporary file so others can't mess with it. */
689 tfbuf = path_make_temp_template("dpkg-deb");
690 gzfd = mkstemp(tfbuf);
691 if (gzfd < 0)
692 ohshite(_("failed to make temporary file (%s)"), _("data member"));
693 /* Make sure it's gone, the fd will remain until we close it. */
694 if (unlink(tfbuf))
695 ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
696 tfbuf);
697 free(tfbuf);
698 } else {
699 internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
702 /* Pack the directory into a tarball, feeding files from the callback. */
703 tar_options.mode = NULL;
704 tar_options.timestamp = timestamp;
705 tar_options.root_owner_group = opt_root_owner_group;
706 tarball_pack(dir, file_treewalk_feed, &tar_options, &compress_params, gzfd);
708 /* Okay, we have data.tar as well now, add it to the ar wrapper. */
709 if (deb_format.major == 2) {
710 char datamember[16 + 1];
712 sprintf(datamember, "%s%s", DATAMEMBER,
713 compressor_get_extension(compress_params.type));
715 if (lseek(gzfd, 0, SEEK_SET))
716 ohshite(_("failed to rewind temporary file (%s)"), _("data member"));
718 dpkg_ar_member_put_file(ar, datamember, gzfd, -1);
720 close(gzfd);
722 if (fsync(ar->fd))
723 ohshite(_("unable to sync file '%s'"), ar->name);
725 dpkg_ar_close(ar);
727 free(debar);
729 return 0;