2 * FreeBSD install - a package for the installation and maintenance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * Routines for dealing with the packing list.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
30 /* Add an MD5 checksum entry for a file or link */
32 add_cksum(Package *pkg, PackingList p, const char *fname)
34 char *cp = NULL, buf[33];
36 if (issymlink(fname)) {
38 char lnk[FILENAME_MAX];
40 if ((len = readlink(fname, lnk, FILENAME_MAX)) > 0)
41 cp = MD5Data((unsigned char *)lnk, len, buf);
42 } else if (isfile(fname)) {
43 /* Don't record MD5 checksum for device nodes and such */
44 cp = MD5File(fname, buf);
48 PackingList tmp = new_plist_entry();
50 tmp->name = copy_string(strconcat("MD5:", cp));
51 tmp->type = PLIST_COMMENT;
60 /* Check a list for files that require preconversion */
62 check_list(const char *home, Package *pkg)
64 const char *where = home;
65 const char *there = NULL;
66 char name[FILENAME_MAX];
70 for (p = pkg->head; p != NULL; p = p->next)
75 where = (p->name == NULL) ? prefix : p->name;
88 snprintf(name, sizeof(name), "%s/%s", there, p->name);
90 snprintf(name, sizeof(name), "%s%s/%s",
91 BaseDir && where && where[0] == '/' ? BaseDir : "", where, p->name);
93 add_cksum(pkg, p, name);
101 trylink(const char *from, const char *to)
103 if (link(from, to) == 0)
105 if (errno == ENOENT) {
106 /* try making the container directory */
107 char *cp = strrchr(to, '/');
109 vsystem("/bin/mkdir -p %.*s", cp - to,
111 return link(from, to);
116 #define STARTSTRING "/usr/bin/tar cf -"
117 #define TOOBIG(str) (int)strlen(str) + 6 + (int)strlen(home) + where_count > maxargs
118 #define PUSHOUT() /* push out string */ \
119 if (where_count > (int)sizeof(STARTSTRING)-1) { \
120 strcat(where_args, "|/usr/bin/tar xpf -"); \
121 if (system(where_args)) { \
123 errx(2, "%s: can't invoke tar pipeline", __func__); \
125 memset(where_args, 0, maxargs); \
127 strcpy(where_args, STARTSTRING); \
128 where_count = sizeof(STARTSTRING)-1; \
132 * Copy unmarked files in packing list to playpen - marked files
133 * have already been copied in an earlier pass through the list.
136 copy_plist(const char *home, Package *plist)
138 PackingList p = plist->head;
139 const char *where = home;
140 const char *there = NULL, *mythere;
141 char *where_args, *prefix = NULL;
142 const char *last_chdir, *root = "/";
144 int where_count = 0, add_count;
148 maxargs = sysconf(_SC_ARG_MAX);
150 * Some slop for the tar cmd text,
153 where_args = malloc(maxargs);
156 errx(2, "%s: can't get argument list space", __func__);
159 memset(where_args, 0, maxargs);
160 strcpy(where_args, STARTSTRING);
161 where_count = sizeof(STARTSTRING)-1;
164 if (stat(".", &stb) == 0)
167 curdir = (dev_t) -1; /*
168 * It's ok if this is a valid dev_t;
169 * this is just a hint for an
174 if (p->type == PLIST_CWD)
178 where = p->name == NULL ? prefix : p->name;
180 else if (p->type == PLIST_SRC)
182 else if (p->type == PLIST_IGNORE)
184 else if (p->type == PLIST_FILE && !p->marked) {
185 char fn[FILENAME_MAX];
188 /* First, look for it in the "home" dir */
189 sprintf(fn, "%s/%s", home, p->name);
191 if (lstat(fn, &stb) == 0 && stb.st_dev == curdir &&
192 S_ISREG(stb.st_mode)) {
194 * If we can link it to the playpen, that avoids a copy
197 if (p->name[0] != '/') {
199 * Don't link abspn stuff--it doesn't come from
202 if (trylink(fn, p->name) == 0) {
211 if (p->name[0] == '/') {
212 add_count = snprintf(&where_args[where_count],
213 maxargs - where_count,
215 last_chdir == root ? "" : "-C /",
219 add_count = snprintf(&where_args[where_count],
220 maxargs - where_count,
222 last_chdir == home ? "" : "-C ",
223 last_chdir == home ? "" : home,
227 if (add_count < 0 || add_count >= maxargs - where_count) {
229 errx(2, "%s: oops, miscounted strings!", __func__);
231 where_count += add_count;
234 * Otherwise, try along the actual extraction path..
237 if (p->name[0] == '/')
239 else mythere = there;
241 snprintf(fn, sizeof(fn), "%s/%s", mythere, p->name);
243 snprintf(fn, sizeof(fn), "%s%s/%s",
244 BaseDir && where && where[0] == '/' ? BaseDir : "", where, p->name);
245 if (lstat(fn, &stb) == 0 && stb.st_dev == curdir &&
246 S_ISREG(stb.st_mode)) {
248 * If we can link it to the playpen, that avoids a copy
251 if (trylink(fn, p->name) == 0) {
256 if (TOOBIG(p->name)) {
259 if (last_chdir == (mythere ? mythere : where))
260 add_count = snprintf(&where_args[where_count],
261 maxargs - where_count,
264 add_count = snprintf(&where_args[where_count],
265 maxargs - where_count,
267 mythere ? mythere : where,
269 if (add_count < 0 || add_count >= maxargs - where_count) {
271 errx(2, "%s: oops, miscounted strings!", __func__);
273 where_count += add_count;
274 last_chdir = (mythere ? mythere : where);