Bring in the latest pkg_install sources from FreeBSD-5.
[dragonfly.git] / usr.sbin / pkg_install / lib / pen.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * Routines for managing the "play pen".
18  *
19  * $FreeBSD: src/usr.sbin/pkg_install/lib/pen.c,v 1.42 2004/07/28 16:03:13 stefanf Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/pen.c,v 1.3 2004/07/30 04:46:13 dillon Exp $
21  */
22
23 #include "lib.h"
24 #include <err.h>
25 #include <libgen.h>
26 #include <sys/signal.h>
27 #include <sys/param.h>
28 #include <sys/mount.h>
29
30 /* For keeping track of where we are */
31 static char PenLocation[FILENAME_MAX];
32 static char Previous[FILENAME_MAX];
33
34 char *
35 where_playpen(void)
36 {
37     return PenLocation;
38 }
39
40 /* Find a good place to play. */
41 static char *
42 find_play_pen(char *pen, off_t sz)
43 {
44     char *cp;
45     struct stat sb;
46
47     if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
48         return pen;
49     else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
50         sprintf(pen, "%s/instmp.XXXXXX", cp);
51     else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
52         sprintf(pen, "%s/instmp.XXXXXX", cp);
53     else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
54         strcpy(pen, "/var/tmp/instmp.XXXXXX");
55     else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
56         strcpy(pen, "/tmp/instmp.XXXXXX");
57     else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
58         strcpy(pen, "/usr/tmp/instmp.XXXXXX");
59     else {
60         cleanup(0);
61         errx(2,
62 "%s: can't find enough temporary space to extract the files, please set your\n"
63 "PKG_TMPDIR environment variable to a location with at least %ld bytes\n"
64 "free", __func__, (long)sz);
65         return NULL;
66     }
67     return pen;
68 }
69
70 #define MAX_STACK       20
71 static char *pstack[MAX_STACK];
72 static int pdepth = -1;
73
74 static void
75 pushPen(const char *pen)
76 {
77     if (++pdepth == MAX_STACK)
78         errx(2, "%s: stack overflow.\n", __func__);
79     pstack[pdepth] = strdup(pen);
80 }
81
82 static void
83 popPen(char *pen)
84 {
85     if (pdepth == -1) {
86         pen[0] = '\0';
87         return;
88     }
89     strcpy(pen, pstack[pdepth]);
90     free(pstack[pdepth--]);
91 }
92     
93 /*
94  * Make a temporary directory to play in and chdir() to it, returning
95  * pathname of previous working directory.
96  */
97 char *
98 make_playpen(char *pen, off_t sz)
99 {
100     if (!find_play_pen(pen, sz))
101         return NULL;
102
103     if (!mkdtemp(pen)) {
104         cleanup(0);
105         errx(2, "%s: can't mktemp '%s'", __func__, pen);
106     }
107     if (chmod(pen, 0700) == FAIL) {
108         cleanup(0);
109         errx(2, "%s: can't mkdir '%s'", __func__, pen);
110     }
111
112     if (Verbose) {
113         if (sz)
114             fprintf(stderr, "Requested space: %d bytes, free space: %lld bytes in %s\n", (int)sz, (long long)min_free(pen), pen);
115     }
116
117     if (min_free(pen) < sz) {
118         rmdir(pen);
119         cleanup(0);
120         errx(2, "%s: not enough free space to create '%s'.\n"
121              "Please set your PKG_TMPDIR environment variable to a location\n"
122              "with more space and\ntry the command again", __func__, pen);
123     }
124
125     if (!getcwd(Previous, FILENAME_MAX)) {
126         upchuck("getcwd");
127         return NULL;
128     }
129
130     if (chdir(pen) == FAIL) {
131         cleanup(0);
132         errx(2, "%s: can't chdir to '%s'", __func__, pen);
133     }
134
135     if (PenLocation[0])
136         pushPen(PenLocation);
137
138     strcpy(PenLocation, pen);
139     return Previous;
140 }
141
142 /* Convenience routine for getting out of playpen */
143 void
144 leave_playpen()
145 {
146     void (*oldsig)(int);
147
148     /* Don't interrupt while we're cleaning up */
149     oldsig = signal(SIGINT, SIG_IGN);
150     if (Previous[0]) {
151         if (chdir(Previous) == FAIL) {
152             cleanup(0);
153             errx(2, "%s: can't chdir back to '%s'", __func__, Previous);
154         }
155         Previous[0] = '\0';
156     }
157     if (PenLocation[0]) {
158         if (PenLocation[0] == '/' && vsystem("/bin/rm -rf %s", PenLocation))
159             warnx("couldn't remove temporary dir '%s'", PenLocation);
160         popPen(PenLocation);
161     }
162     signal(SIGINT, oldsig);
163 }
164
165 off_t
166 min_free(const char *tmpdir)
167 {
168     struct statfs buf;
169
170     if (statfs(tmpdir, &buf) != 0) {
171         warn("statfs");
172         return -1;
173     }
174     return (off_t)buf.f_bavail * (off_t)buf.f_bsize;
175 }