Home | History | Annotate | Line # | Download | only in create
build.c revision 1.1.1.1.4.2
      1 /*	$NetBSD: build.c,v 1.1.1.1.4.2 2008/10/19 22:40:49 haad Exp $	*/
      2 
      3 #if HAVE_CONFIG_H
      4 #include "config.h"
      5 #endif
      6 #include <nbcompat.h>
      7 #if HAVE_SYS_CDEFS_H
      8 #include <sys/cdefs.h>
      9 #endif
     10 #ifndef lint
     11 #if 0
     12 static const char *rcsid = "from FreeBSD Id: perform.c,v 1.38 1997/10/13 15:03:51 jkh Exp";
     13 #else
     14 __RCSID("$NetBSD: build.c,v 1.1.1.1.4.2 2008/10/19 22:40:49 haad Exp $");
     15 #endif
     16 #endif
     17 
     18 /*-
     19  * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>.
     20  * All rights reserved.
     21  *
     22  * This code was developed as part of Google's Summer of Code 2007 program.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  *
     28  * 1. Redistributions of source code must retain the above copyright
     29  *    notice, this list of conditions and the following disclaimer.
     30  * 2. Redistributions in binary form must reproduce the above copyright
     31  *    notice, this list of conditions and the following disclaimer in
     32  *    the documentation and/or other materials provided with the
     33  *    distribution.
     34  *
     35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     38  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     39  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     40  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     41  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     43  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46  * SUCH DAMAGE.
     47  */
     48 
     49 /*
     50  * FreeBSD install - a package for the installation and maintainance
     51  * of non-core utilities.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  *
     62  * Jordan K. Hubbard
     63  * 18 July 1993
     64  *
     65  * This is the main body of the create module.
     66  *
     67  */
     68 
     69 #include "lib.h"
     70 #include "create.h"
     71 
     72 #if HAVE_ERR_H
     73 #include <err.h>
     74 #endif
     75 #if HAVE_GRP_H
     76 #include <grp.h>
     77 #endif
     78 #if HAVE_PWD_H
     79 #include <pwd.h>
     80 #endif
     81 #if HAVE_UNISTD_H
     82 #include <unistd.h>
     83 #endif
     84 #if HAVE_FCNTL_H
     85 #include <fcntl.h>
     86 #endif
     87 
     88 #include <archive.h>
     89 #include <archive_entry.h>
     90 
     91 static struct memory_file *contents_file;
     92 static struct memory_file *comment_file;
     93 static struct memory_file *desc_file;
     94 static struct memory_file *install_file;
     95 static struct memory_file *deinstall_file;
     96 static struct memory_file *display_file;
     97 static struct memory_file *build_version_file;
     98 static struct memory_file *build_info_file;
     99 static struct memory_file *size_pkg_file;
    100 static struct memory_file *size_all_file;
    101 static struct memory_file *preserve_file;
    102 static struct memory_file *views_file;
    103 
    104 static void
    105 write_meta_file(struct memory_file *file, struct archive *archive)
    106 {
    107 	struct archive_entry *entry;
    108 
    109 	entry = archive_entry_new();
    110 	archive_entry_set_pathname(entry, file->name);
    111 	archive_entry_copy_stat(entry, &file->st);
    112 
    113 	archive_entry_set_uname(entry, file->owner);
    114 	archive_entry_set_gname(entry, file->group);
    115 
    116 	if (archive_write_header(archive, entry))
    117 		errx(2, "cannot write to archive: %s", archive_error_string(archive));
    118 
    119 	archive_write_data(archive, file->data, file->len);
    120 
    121 	archive_entry_free(entry);
    122 }
    123 
    124 static void
    125 write_entry(struct archive *archive, struct archive_entry *entry)
    126 {
    127 	char buf[16384];
    128 	const char *name;
    129 	int fd;
    130 	off_t len;
    131 	ssize_t buf_len;
    132 
    133 	if (archive_entry_pathname(entry) == NULL) {
    134 		warnx("entry with NULL path");
    135 		return;
    136 	}
    137 
    138 	if (archive_write_header(archive, entry)) {
    139 		errx(2, "cannot write to archive: %s",
    140 		    archive_error_string(archive));
    141 	}
    142 
    143 	/* Only regular files can have data. */
    144 	if (archive_entry_filetype(entry) != AE_IFREG ||
    145 	    archive_entry_size(entry) == 0) {
    146 		archive_entry_free(entry);
    147 		return;
    148 	}
    149 
    150 	name = archive_entry_pathname(entry);
    151 
    152 	if ((fd = open(name, O_RDONLY)) == -1)
    153 		err(2, "cannot open data file %s", name);
    154 
    155 	len = archive_entry_size(entry);
    156 
    157 	while (len > 0) {
    158 		buf_len = (len > sizeof(buf)) ? sizeof(buf) : (ssize_t)len;
    159 
    160 		if ((buf_len = read(fd, buf, buf_len)) == 0)
    161 			break;
    162 		else if (buf_len < 0)
    163 			err(2, "cannot read from %s", name);
    164 
    165 		archive_write_data(archive, buf, (size_t)buf_len);
    166 		len -= buf_len;
    167 	}
    168 
    169 	close(fd);
    170 
    171 	archive_entry_free(entry);
    172 }
    173 
    174 static void
    175 write_normal_file(const char *name, struct archive *archive,
    176     struct archive_entry_linkresolver *resolver,
    177     const char *owner, const char *group)
    178 {
    179 	char buf[16384];
    180 	ssize_t buf_len;
    181 	struct archive_entry *entry, *sparse_entry;
    182 	struct stat st;
    183 
    184 	if (lstat(name, &st) == -1)
    185 		err(2, "lstat failed for file %s", name);
    186 
    187 	entry = archive_entry_new();
    188 	archive_entry_set_pathname(entry, name);
    189 	archive_entry_copy_stat(entry, &st);
    190 
    191 	if (owner != NULL) {
    192 		uid_t uid;
    193 
    194 		archive_entry_set_uname(entry, owner);
    195 		if (uid_from_user(owner, &uid) == -1)
    196 			errx(2, "user %s unknown", owner);
    197 		archive_entry_set_uid(entry, uid);
    198 	} else {
    199 		archive_entry_set_uname(entry, user_from_uid(st.st_uid, 1));
    200 	}
    201 
    202 	if (group != NULL) {
    203 		gid_t gid;
    204 
    205 		archive_entry_set_gname(entry, group);
    206 		if (gid_from_group(group, &gid) == -1)
    207 			errx(2, "group %s unknown", group);
    208 		archive_entry_set_gid(entry, gid);
    209 	} else {
    210 		archive_entry_set_gname(entry, group_from_gid(st.st_gid, 1));
    211 	}
    212 
    213 	if ((st.st_mode & S_IFMT) == S_IFLNK) {
    214 		buf_len = readlink(name, buf, sizeof buf);
    215 		if (buf_len < 0)
    216 			err(2, "cannot read symlink %s", name);
    217 		buf[buf_len] = '\0';
    218 		archive_entry_set_symlink(entry, buf);
    219 	}
    220 
    221 	archive_entry_linkify(resolver, &entry, &sparse_entry);
    222 
    223 	if (entry != NULL)
    224 		write_entry(archive, entry);
    225 	if (sparse_entry != NULL)
    226 		write_entry(archive, sparse_entry);
    227 }
    228 
    229 static void
    230 make_dist(const char *pkg, const char *suffix, const package_t *plist)
    231 {
    232 	char *archive_name;
    233 	const char *owner, *group;
    234 	const plist_t *p;
    235 	struct archive *archive;
    236 	struct archive_entry *entry, *sparse_entry;
    237 	struct archive_entry_linkresolver *resolver;
    238 	char *initial_cwd;
    239 
    240 	archive = archive_write_new();
    241 	archive_write_set_format_pax_restricted(archive);
    242 	if ((resolver = archive_entry_linkresolver_new()) == NULL)
    243 		errx(2, "cannot create link resolver");
    244 	archive_entry_linkresolver_set_strategy(resolver,
    245 	    archive_format(archive));
    246 
    247 	if (strcmp(suffix, "tbz") == 0 || strcmp(suffix, "tar.bz2") == 0)
    248 		archive_write_set_compression_bzip2(archive);
    249 	else if (strcmp(suffix, "tgz") == 0 || strcmp(suffix, "tar.gz") == 0)
    250 		archive_write_set_compression_gzip(archive);
    251 	else
    252 		archive_write_set_compression_none(archive);
    253 
    254 	if (asprintf(&archive_name, "%s.%s", pkg, suffix) == -1)
    255 		err(2, "cannot compute output name");
    256 
    257 	if (archive_write_open_file(archive, archive_name))
    258 		errx(2, "cannot create archive: %s", archive_error_string(archive));
    259 
    260 	free(archive_name);
    261 
    262 	owner = DefaultOwner;
    263 	group = DefaultGroup;
    264 
    265 	write_meta_file(contents_file, archive);
    266 	write_meta_file(comment_file, archive);
    267 	write_meta_file(desc_file, archive);
    268 
    269 	if (Install)
    270 		write_meta_file(install_file, archive);
    271 	if (DeInstall)
    272 		write_meta_file(deinstall_file, archive);
    273 	if (Display)
    274 		write_meta_file(display_file, archive);
    275 	if (BuildVersion)
    276 		write_meta_file(build_version_file, archive);
    277 	if (BuildInfo)
    278 		write_meta_file(build_info_file, archive);
    279 	if (SizePkg)
    280 		write_meta_file(size_pkg_file, archive);
    281 	if (SizeAll)
    282 		write_meta_file(size_all_file, archive);
    283 	if (Preserve)
    284 		write_meta_file(preserve_file, archive);
    285 	if (create_views)
    286 		write_meta_file(views_file, archive);
    287 
    288 	initial_cwd = getcwd(NULL, 0);
    289 
    290 	for (p = plist->head; p; p = p->next) {
    291 		if (p->type == PLIST_FILE) {
    292 			write_normal_file(p->name, archive, resolver, owner, group);
    293 		} else if (p->type == PLIST_CWD || p->type == PLIST_SRC) {
    294 
    295 			/* XXX let PLIST_SRC override PLIST_CWD */
    296 			if (p->type == PLIST_CWD && p->next != NULL &&
    297 			    p->next->type == PLIST_SRC) {
    298 				continue;
    299 			}
    300 			chdir(p->name);
    301 		} else if (p->type == PLIST_IGNORE) {
    302 			p = p->next;
    303 		} else if (p->type == PLIST_CHOWN) {
    304 			if (p->name != NULL)
    305 				owner = p->name;
    306 			else
    307 				owner = DefaultOwner;
    308 		} else if (p->type == PLIST_CHGRP) {
    309 			if (p->name != NULL)
    310 				group = p->name;
    311 			else
    312 				group = DefaultGroup;
    313 		}
    314 	}
    315 
    316 	entry = NULL;
    317 	archive_entry_linkify(resolver, &entry, &sparse_entry);
    318 	while (entry != NULL) {
    319 		write_entry(archive, entry);
    320 		entry = NULL;
    321 		archive_entry_linkify(resolver, &entry, &sparse_entry);
    322 	}
    323 
    324 	archive_entry_linkresolver_free(resolver);
    325 
    326 	if (archive_write_close(archive))
    327 		errx(2, "cannot finish archive: %s", archive_error_string(archive));
    328 	archive_write_finish(archive);
    329 
    330 	chdir(initial_cwd);
    331 	free(initial_cwd);
    332 }
    333 
    334 static struct memory_file *
    335 load_and_add(package_t *plist, const char *input_name,
    336     const char *target_name, mode_t perm)
    337 {
    338 	struct memory_file *file;
    339 
    340 	file = load_memory_file(input_name, target_name, DefaultOwner,
    341 	    DefaultGroup, perm);
    342 	add_plist(plist, PLIST_IGNORE, NULL);
    343 	add_plist(plist, PLIST_FILE, target_name);
    344 
    345 	return file;
    346 }
    347 
    348 static struct memory_file *
    349 make_and_add(package_t *plist, const char *target_name,
    350     char *content, mode_t perm)
    351 {
    352 	struct memory_file *file;
    353 
    354 	file = make_memory_file(target_name, content, strlen(content),
    355 	    DefaultOwner, DefaultGroup, perm);
    356 	add_plist(plist, PLIST_IGNORE, NULL);
    357 	add_plist(plist, PLIST_FILE, target_name);
    358 
    359 	return file;
    360 }
    361 
    362 int
    363 pkg_build(const char *pkg, const char *full_pkg, const char *suffix,
    364     package_t *plist)
    365 {
    366 	char *plist_buf;
    367 	size_t plist_len;
    368 
    369 	/* Now put the release specific items in */
    370 	add_plist(plist, PLIST_CWD, ".");
    371 	comment_file = make_and_add(plist, COMMENT_FNAME, Comment, 0444);
    372 	desc_file = make_and_add(plist, DESC_FNAME, Desc, 0444);
    373 
    374 	if (Install) {
    375 		install_file = load_and_add(plist, Install, INSTALL_FNAME,
    376 		    0555);
    377 	}
    378 	if (DeInstall) {
    379 		deinstall_file = load_and_add(plist, DeInstall,
    380 		    DEINSTALL_FNAME, 0555);
    381 	}
    382 	if (Display) {
    383 		display_file = load_and_add(plist, Display,
    384 		    DISPLAY_FNAME, 0444);
    385 		add_plist(plist, PLIST_DISPLAY, DISPLAY_FNAME);
    386 	}
    387 	if (BuildVersion) {
    388 		build_version_file = load_and_add(plist, BuildVersion,
    389 		    BUILD_VERSION_FNAME, 0444);
    390 	}
    391 	if (BuildInfo) {
    392 		build_info_file = load_and_add(plist, BuildInfo,
    393 		    BUILD_INFO_FNAME, 0444);
    394 	}
    395 	if (SizePkg) {
    396 		size_pkg_file = load_and_add(plist, SizePkg,
    397 		    SIZE_PKG_FNAME, 0444);
    398 	}
    399 	if (SizeAll) {
    400 		size_all_file = load_and_add(plist, SizeAll,
    401 		    SIZE_ALL_FNAME, 0444);
    402 	}
    403 	if (Preserve) {
    404 		preserve_file = load_and_add(plist, Preserve,
    405 		    PRESERVE_FNAME, 0444);
    406 	}
    407 	if (create_views)
    408 		views_file = make_and_add(plist, VIEWS_FNAME, "", 0444);
    409 
    410 	/* Finally, write out the packing list */
    411 	stringify_plist(plist, &plist_buf, &plist_len, realprefix);
    412 	contents_file = make_memory_file(CONTENTS_FNAME, plist_buf, plist_len,
    413 	    DefaultOwner, DefaultGroup, 0644);
    414 
    415 	/* And stick it into a tar ball */
    416 	make_dist(pkg, suffix, plist);
    417 
    418 	return TRUE;		/* Success */
    419 }
    420