2 * FreeBSD install - a package for the installation and maintainance
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: src/usr.sbin/pkg_install/create/pl.c,v 1.13.2.8 2002/08/31 19:31:02 obrien Exp $");
30 /* Check a list for files that require preconversion */
32 check_list(const char *home, Package *pkg)
34 const char *where = home;
35 const char *there = NULL;
36 char *cp, name[FILENAME_MAX], buf[33];
39 for (p = pkg->head; p != NULL; p = p->next)
55 sprintf(name, "%s/%s", there ? there : where, p->name);
56 if (issymlink(name)) {
58 char lnk[FILENAME_MAX];
60 if ((len = readlink(name, lnk, FILENAME_MAX)) > 0)
61 cp = MD5Data((unsigned char *)lnk, len, buf);
62 } else if (isfile(name)) {
63 /* Don't record MD5 checksum for device nodes and such */
64 cp = MD5File(name, buf);
68 PackingList tmp = new_plist_entry();
70 tmp->name = copy_string(strconcat("MD5:", cp));
71 tmp->type = PLIST_COMMENT;
86 trylink(const char *from, const char *to)
88 if (link(from, to) == 0)
90 if (errno == ENOENT) {
91 /* try making the container directory */
92 char *cp = strrchr(to, '/');
94 vsystem("mkdir -p %.*s", cp - to,
96 return link(from, to);
101 #define STARTSTRING "tar cf -"
102 #define TOOBIG(str) (int)strlen(str) + 6 + (int)strlen(home) + where_count > maxargs
103 #define PUSHOUT() /* push out string */ \
104 if (where_count > (int)sizeof(STARTSTRING)-1) { \
105 strcat(where_args, "|tar xpf -"); \
106 if (system(where_args)) { \
108 errx(2, "%s: can't invoke tar pipeline", __func__); \
110 memset(where_args, 0, maxargs); \
112 strcpy(where_args, STARTSTRING); \
113 where_count = sizeof(STARTSTRING)-1; \
117 * Copy unmarked files in packing list to playpen - marked files
118 * have already been copied in an earlier pass through the list.
121 copy_plist(const char *home, Package *plist)
123 PackingList p = plist->head;
124 const char *where = home;
125 const char *there = NULL, *mythere;
127 const char *last_chdir, *root = "/";
128 int maxargs, where_count = 0, add_count;
132 maxargs = sysconf(_SC_ARG_MAX);
134 * Some slop for the tar cmd text,
137 where_args = malloc(maxargs);
140 errx(2, "%s: can't get argument list space", __func__);
143 memset(where_args, 0, maxargs);
144 strcpy(where_args, STARTSTRING);
145 where_count = sizeof(STARTSTRING)-1;
148 if (stat(".", &stb) == 0)
151 curdir = (dev_t) -1; /*
152 * It's ok if this is a valid dev_t;
153 * this is just a hint for an
158 if (p->type == PLIST_CWD)
160 else if (p->type == PLIST_SRC)
162 else if (p->type == PLIST_IGNORE)
164 else if (p->type == PLIST_FILE && !p->marked) {
165 char fn[FILENAME_MAX];
168 /* First, look for it in the "home" dir */
169 sprintf(fn, "%s/%s", home, p->name);
171 if (lstat(fn, &stb) == 0 && stb.st_dev == curdir &&
172 S_ISREG(stb.st_mode)) {
174 * If we can link it to the playpen, that avoids a copy
177 if (p->name[0] != '/') {
179 * Don't link abspn stuff--it doesn't come from
182 if (trylink(fn, p->name) == 0) {
191 if (p->name[0] == '/') {
192 add_count = snprintf(&where_args[where_count],
193 maxargs - where_count,
195 last_chdir == root ? "" : "-C /",
199 add_count = snprintf(&where_args[where_count],
200 maxargs - where_count,
202 last_chdir == home ? "" : "-C ",
203 last_chdir == home ? "" : home,
207 if (add_count < 0 || add_count > maxargs - where_count) {
209 errx(2, "%s: oops, miscounted strings!", __func__);
211 where_count += add_count;
214 * Otherwise, try along the actual extraction path..
217 if (p->name[0] == '/')
219 else mythere = there;
220 sprintf(fn, "%s/%s", mythere ? mythere : where, p->name);
221 if (lstat(fn, &stb) == 0 && stb.st_dev == curdir &&
222 S_ISREG(stb.st_mode)) {
224 * If we can link it to the playpen, that avoids a copy
227 if (trylink(fn, p->name) == 0) {
232 if (TOOBIG(p->name)) {
235 if (last_chdir == (mythere ? mythere : where))
236 add_count = snprintf(&where_args[where_count],
237 maxargs - where_count,
240 add_count = snprintf(&where_args[where_count],
241 maxargs - where_count,
243 mythere ? mythere : where,
245 if (add_count < 0 || add_count > maxargs - where_count) {
247 errx(2, "%s: oops, miscounted strings!", __func__);
249 where_count += add_count;
250 last_chdir = (mythere ? mythere : where);