2 * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 update_ids(struct memory_file *file)
70 if (file->owner != NULL) {
73 if (uid_from_user(file->owner, &uid) == -1)
74 errx(2, "user %s unknown", file->owner);
75 file->st.st_uid = uid;
77 file->owner = user_from_uid(file->st.st_uid, 1);
80 if (file->group != NULL) {
83 if (gid_from_group(file->group, &gid) == -1)
84 errx(2, "group %s unknown", file->group);
85 file->group = file->group;
86 file->st.st_gid = gid;
88 file->group = group_from_gid(file->st.st_gid, 1);
93 make_memory_file(const char *archive_name, void *data, size_t len,
94 const char *owner, const char *group, mode_t mode)
96 struct memory_file *file;
98 file = xmalloc(sizeof(*file));
99 file->name = archive_name;
105 memset(&file->st, 0, sizeof(file->st));
107 file->st.st_atime = file->st.st_ctime = file->st.st_mtime = time(NULL);
109 file->st.st_nlink = 1;
110 file->st.st_size = len;
111 file->st.st_mode = mode | S_IFREG;
119 load_memory_file(const char *disk_name,
120 const char *archive_name, const char *owner, const char *group,
123 struct memory_file *file;
126 file = xmalloc(sizeof(*file));
127 file->name = archive_name;
132 fd = open(disk_name, O_RDONLY);
134 err(2, "cannot open file %s", disk_name);
135 if (fstat(fd, &file->st) == -1)
136 err(2, "cannot stat file %s", disk_name);
140 if ((file->st.st_mode & S_IFMT) != S_IFREG)
141 errx(1, "meta data file %s is not regular file", disk_name);
142 if (file->st.st_size > SSIZE_MAX)
143 errx(2, "meta data file too large: %s", disk_name);
144 file->data = xmalloc(file->st.st_size);
146 if (read(fd, file->data, file->st.st_size) != file->st.st_size)
147 err(2, "cannot read file into memory %s", disk_name);
149 file->len = file->st.st_size;
157 free_memory_file(struct memory_file *file)