Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / sysinstall / tape.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last attempt in the `sysinstall' line, the next
5  * generation being slated to essentially a complete rewrite.
6  *
7  * $FreeBSD: src/release/sysinstall/tape.c,v 1.22.2.1 2002/07/02 22:25:39 jhb Exp $
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    verbatim and that no modifications are made prior to this
18  *    point in the file.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36
37 /* These routines deal with getting things off of tape media */
38
39 #include "sysinstall.h"
40 #include <sys/fcntl.h>
41 #include <sys/param.h>
42
43 static Boolean tapeInitted;
44
45 char *
46 mediaTapeBlocksize(void)
47 {
48     char *cp = variable_get(VAR_TAPE_BLOCKSIZE);
49
50     return cp ? cp : DEFAULT_TAPE_BLOCKSIZE;
51 }
52
53 Boolean
54 mediaInitTape(Device *dev)
55 {
56     /* This is REALLY gross, but we need to do the init later in get due to the fact
57      * that media is initialized BEFORE a filesystem is mounted now.
58      */
59     return TRUE;
60 }
61
62 FILE *
63 mediaGetTape(Device *dev, char *file, Boolean probe)
64 {
65     char buf[PATH_MAX];
66     FILE *fp;
67
68     int i;
69
70     if (!tapeInitted) {
71         WINDOW *w = savescr();
72
73         msgDebug("Tape init routine called for %s (private dir is %s)\n", 
74             dev->name, (char *)dev->private);
75         Mkdir(dev->private);
76         if (chdir(dev->private)) {
77             msgConfirm("Unable to CD to %s before extracting tape!\n"
78                        "Tape media is not selected and thus cannot be installed from.", 
79                        (char *)dev->private);
80             return (FILE *)IO_ERROR;
81         }
82         /* We know the tape is already in the drive, so go for it */
83         msgNotify("First extracting distributions from %s...", dev->description);
84         if (!strcmp(dev->name, "rft0"))
85             i = vsystem("ft | cpio -idum %s --block-size %s", cpioVerbosity(), mediaTapeBlocksize());
86         else
87             i = vsystem("cpio -idum %s --block-size %s -I %s", cpioVerbosity(), mediaTapeBlocksize(), dev->devname);
88         if (!i) {
89             tapeInitted = TRUE;
90             msgDebug("Tape initialized successfully.\n");
91         }
92         else {
93             msgConfirm("Tape extract command failed with status %d!\n"
94                        "Unable to use tape media.", i);
95             restorescr(w);
96             return (FILE *)IO_ERROR;
97         }
98         restorescr(w);
99     }
100
101     sprintf(buf, "%s/%s", (char *)dev->private, file);
102     if (isDebug())
103         msgDebug("Request for %s from tape (looking in %s)\n", file, buf);
104     if (file_readable(buf))
105         fp = fopen(buf, "r");
106     else {
107         sprintf(buf, "%s/releases/%s", (char *)dev->private, file);
108         fp = fopen(buf, "r");
109     }
110     /* Nuke the files behind us to save space */
111     if (fp)
112         unlink(buf);
113     return fp;
114 }
115
116 void
117 mediaShutdownTape(Device *dev)
118 {
119     if (!tapeInitted)
120         return;
121     if (file_readable((char *)dev->private)) {
122         msgDebug("Cleaning up results of tape extract in %s..",
123                   (char *)dev->private);
124         (void)vsystem("rm -rf %s", (char *)dev->private);
125     }
126     tapeInitted = FALSE;
127 }