NetBSD/external/bsd/pkg_install/dist/create/util.c
joerg d66ee6c3fd Import pkg_install-20090201. From the branch merge in pkgsrc:
- DB support is always included from libnbcompat if needed
- pkg_view and linkfarm are not installed any more; they are not moved
into the attic yet, so they can easily be installed as separte package
- common configuration file to customise the behavior of various
components; this supersedes the old audit-packages.conf
- support for PKSC7 signatures (using X509 certs) and GPG signatures for
packages in a secure way. See pkg_admin(8) for how to create them and
pkg_install.conf(5) for the options to use them
- audit-packages and download-vulnerability-list are wrapper scripts
  around pkg_admin. They try to mimic the classic options if used
  sanely.
  "pkg_admin audit" is now an order of magnitude faster than before
- pkg_add uses libarchive and libfetch instead of external ftp and tar:
  - progress bar is currently missing for downloads
  - "pkg_add -" is no longer supported
  - no adhoc check for conficts between dependencies and already
    installed packages
  - "pkg_add -s" has been replaced with an option in pkg_install.conf,
    verification of plain detached GPG signatures is no longer supported
  - optional check for vulnerabilities before adding a package
  - if /var and /usr/pkg are on different fileystems it is twice as fast
    now
  - conflicts due to overlapping plists are checked before installation
  - pkg_add no longer plays with the process limits
  - pkg_add and pkg_delete have a new destdir option; scripts have to
    either be modified to use PKG_DESTDIR or should be disabled
- pkg_add -u for now can't be used to update to the exact same version
- internal "rm -rf" and "mkdir_p" code
- all memory allocation failures are not explicitly fatal
- if a file is not removed due to a failed checksum, still remove the
  entry from pkgdb
2009-02-02 20:44:00 +00:00

164 lines
3.9 KiB
C

/*-
* Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <nbcompat.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_ERR_H
#include <err.h>
#endif
#if HAVE_PWD_H
#include <grp.h>
#endif
#if HAVE_PWD_H
#include <pwd.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "lib.h"
#include "create.h"
static void
update_ids(struct memory_file *file)
{
if (file->owner != NULL) {
uid_t uid;
if (uid_from_user(file->owner, &uid) == -1)
errx(2, "user %s unknown", file->owner);
file->st.st_uid = uid;
} else {
file->owner = user_from_uid(file->st.st_uid, 1);
}
if (file->group != NULL) {
gid_t gid;
if (gid_from_group(file->group, &gid) == -1)
errx(2, "group %s unknown", file->group);
file->group = file->group;
file->st.st_gid = gid;
} else {
file->group = group_from_gid(file->st.st_gid, 1);
}
}
struct memory_file *
make_memory_file(const char *archive_name, void *data, size_t len,
const char *owner, const char *group, mode_t mode)
{
struct memory_file *file;
file = xmalloc(sizeof(*file));
file->name = archive_name;
file->owner = owner;
file->group = group;
file->data = data;
file->len = len;
memset(&file->st, 0, sizeof(file->st));
file->st.st_atime = file->st.st_ctime = file->st.st_mtime = time(NULL);
file->st.st_nlink = 1;
file->st.st_size = len;
file->st.st_mode = mode | S_IFREG;
update_ids(file);
return file;
}
struct memory_file *
load_memory_file(const char *disk_name,
const char *archive_name, const char *owner, const char *group,
mode_t mode)
{
struct memory_file *file;
int fd;
file = xmalloc(sizeof(*file));
file->name = archive_name;
file->owner = owner;
file->group = group;
file->mode = mode;
fd = open(disk_name, O_RDONLY);
if (fd == -1)
err(2, "cannot open file %s", disk_name);
if (fstat(fd, &file->st) == -1)
err(2, "cannot stat file %s", disk_name);
update_ids(file);
if ((file->st.st_mode & S_IFMT) != S_IFREG)
errx(1, "meta data file %s is not regular file", disk_name);
if (file->st.st_size > SSIZE_MAX)
errx(2, "meta data file too large: %s", disk_name);
file->data = xmalloc(file->st.st_size);
if (read(fd, file->data, file->st.st_size) != file->st.st_size)
err(2, "cannot read file into memory %s", disk_name);
file->len = file->st.st_size;
close(fd);
return file;
}
void
free_memory_file(struct memory_file *file)
{
if (file != NULL) {
free(file->data);
free(file);
}
}