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/>.
26 #include <sys/types.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>
56 #include <dpkg/options.h>
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
)) {
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)"),
80 * Simple structure to store information about a file.
83 struct file_info
*next
;
87 static struct file_info
*
88 file_info_new(const char *filename
)
92 fi
= m_malloc(sizeof(*fi
));
93 fi
->fn
= m_strdup(filename
);
100 file_info_free(struct file_info
*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)
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.
130 file_info_list_append(struct file_info
**head
, struct file_info
**tail
,
131 struct file_info
*fi
)
136 *tail
= (*tail
)->next
=fi
;
140 * Free the memory for all entries in a list of file_info structs.
143 file_info_list_free(struct file_info
*fi
)
146 struct file_info
*fl
;
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
);
167 if (strncmp(virtname
, BUILDCONTROLDIR
, strlen(BUILDCONTROLDIR
)) == 0)
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)"),
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
[] = {
201 MAINTSCRIPT_FILE_CONFIG
,
206 * Check control directory and file permissions.
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
++) {
227 varbuf_printf(&path
, "%s/%s", ctrldir
, *mscriptp
);
228 if (!lstat(path
.buf
, &mscriptstab
)) {
229 if (S_ISLNK(mscriptstab
.st_mode
))
231 if (!S_ISREG(mscriptstab
.st_mode
))
232 ohshit(_("maintainer script '%.50s' is not a plain file or symlink"),
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.
250 check_conffiles(const char *ctrldir
, const char *rootdir
)
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");
262 if (errno
== ENOENT
) {
263 varbuf_destroy(&controlfile
);
267 ohshite(_("error opening conffiles file"));
270 while (fgets(conffilenamebuf
, MAXCONFFILENAME
+ 1, cf
)) {
271 struct stat controlstab
;
272 char *conffilename
= conffilenamebuf
;
274 bool remove_on_upgrade
= false;
276 n
= strlen(conffilename
);
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"),
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]))
292 if (conffilename
[0] == '\0')
293 ohshit(_("empty and whitespace-only lines are not allowed in "
295 ohshit(_("line with conffile filename '%s' has leading white spaces"),
299 if (conffilename
[0] != '/') {
300 char *flag
= conffilename
;
301 char *flag_end
= strchr(flag
, ' ');
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
);
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;
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"),
331 if (!remove_on_upgrade
)
332 ohshit(_("conffile '%.250s' does not appear in package"), conffilename
);
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"),
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
);
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
);
356 ohshite(_("error reading conffiles file"));
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
)
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
);
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
)
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();
409 warning(P_("ignoring %d warning about the control file(s)",
410 "ignoring %d warnings about the control file(s)", warns
),
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.
427 gen_dest_pathname(const char *dir
, const char *dest
)
430 struct stat dest_stab
;
432 if (stat(dest
, &dest_stab
)) {
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. */
440 return m_strdup(dest
);
444 pathname
= m_malloc(strlen(dir
) + sizeof(DEBEXT
));
445 strcpy(pathname
, dir
);
446 path_trim_slash_slashdot(pathname
);
447 strcat(pathname
, DEBEXT
);
454 * Generate the pathname for the destination binary package from control file.
456 * @return The pathname for the package being built.
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
{
471 bool root_owner_group
;
475 * Pack the contents of a directory into a tarball.
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();
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]);
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. */
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",
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();
523 close(pipe_filenames
[1]);
524 compress_filter(tar_compress_params
, pipe_tarball
[0], fd_out
,
525 _("compressing tar member"));
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);
540 parse_timestamp(const char *value
)
546 timestamp
= strtoimax(value
, &end
, 10);
547 if (value
== end
|| *end
)
548 ohshit(_("unable to parse timestamp '%.255s'"), value
);
550 ohshite(_("unable to parse timestamp '%.255s'"), value
);
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
;
566 const char *timestamp_str
;
567 const char *dir
, *dest
;
573 /* Decode our arguments. */
576 badusage(_("--%s needs a <directory> argument"), cipaction
->olong
);
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. */
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
);
594 pkg
= check_control_area(ctrldir
, dir
);
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
);
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
);
620 ohshite(_("failed to make temporary file (%s)"), _("control member"));
621 /* Make sure it's gone, the fd will remain until we close it. */
623 ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
627 /* Select the compressor to use for our control archive. */
628 if (opt_uniform_compression
) {
629 control_compress_params
= compress_params
;
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
);
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
;
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"),
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);
675 internerr("unknown deb format version %d.%d", deb_format
.major
, deb_format
.minor
);
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. */
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
);
692 ohshite(_("failed to make temporary file (%s)"), _("data member"));
693 /* Make sure it's gone, the fd will remain until we close it. */
695 ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
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);
723 ohshite(_("unable to sync file '%s'"), ar
->name
);