xref: /original-bsd/sbin/dump/itime.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)itime.c	8.1 (Berkeley) 06/05/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #ifdef sunos
15 #include <sys/vnode.h>
16 
17 #include <ufs/fsdir.h>
18 #include <ufs/inode.h>
19 #include <ufs/fs.h>
20 #else
21 #include <ufs/ufs/dinode.h>
22 #endif
23 
24 #include <protocols/dumprestore.h>
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #ifdef __STDC__
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #endif
34 
35 #include "dump.h"
36 
37 struct	dumpdates **ddatev = 0;
38 int	nddates = 0;
39 int	ddates_in = 0;
40 struct	dumptime *dthead = 0;
41 
42 static	void dumprecout __P((FILE *, struct dumpdates *));
43 static	int getrecord __P((FILE *, struct dumpdates *));
44 static	int makedumpdate __P((struct dumpdates *, char *));
45 static	void readdumptimes __P((FILE *));
46 
47 void
48 initdumptimes()
49 {
50 	FILE *df;
51 
52 	if ((df = fopen(dumpdates, "r")) == NULL) {
53 		if (errno != ENOENT) {
54 			quit("cannot read %s: %s\n", dumpdates,
55 			    strerror(errno));
56 			/* NOTREACHED */
57 		}
58 		/*
59 		 * Dumpdates does not exist, make an empty one.
60 		 */
61 		msg("WARNING: no file `%s', making an empty one\n", dumpdates);
62 		if ((df = fopen(dumpdates, "w")) == NULL) {
63 			quit("cannot create %s: %s\n", dumpdates,
64 			    strerror(errno));
65 			/* NOTREACHED */
66 		}
67 		(void) fclose(df);
68 		if ((df = fopen(dumpdates, "r")) == NULL) {
69 			quit("cannot read %s even after creating it: %s\n",
70 			    dumpdates, strerror(errno));
71 			/* NOTREACHED */
72 		}
73 	}
74 	(void) flock(fileno(df), LOCK_SH);
75 	readdumptimes(df);
76 	(void) fclose(df);
77 }
78 
79 static void
80 readdumptimes(df)
81 	FILE *df;
82 {
83 	register int i;
84 	register struct	dumptime *dtwalk;
85 
86 	for (;;) {
87 		dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
88 		if (getrecord(df, &(dtwalk->dt_value)) < 0)
89 			break;
90 		nddates++;
91 		dtwalk->dt_next = dthead;
92 		dthead = dtwalk;
93 	}
94 
95 	ddates_in = 1;
96 	/*
97 	 *	arrayify the list, leaving enough room for the additional
98 	 *	record that we may have to add to the ddate structure
99 	 */
100 	ddatev = (struct dumpdates **)
101 		calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
102 	dtwalk = dthead;
103 	for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next)
104 		ddatev[i] = &dtwalk->dt_value;
105 }
106 
107 void
108 getdumptime()
109 {
110 	register struct dumpdates *ddp;
111 	register int i;
112 	char *fname;
113 
114 	fname = disk;
115 #ifdef FDEBUG
116 	msg("Looking for name %s in dumpdates = %s for level = %c\n",
117 		fname, dumpdates, level);
118 #endif
119 	spcl.c_ddate = 0;
120 	lastlevel = '0';
121 
122 	initdumptimes();
123 	/*
124 	 *	Go find the entry with the same name for a lower increment
125 	 *	and older date
126 	 */
127 	ITITERATE(i, ddp) {
128 		if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
129 			continue;
130 		if (ddp->dd_level >= level)
131 			continue;
132 		if (ddp->dd_ddate <= spcl.c_ddate)
133 			continue;
134 		spcl.c_ddate = ddp->dd_ddate;
135 		lastlevel = ddp->dd_level;
136 	}
137 }
138 
139 void
140 putdumptime()
141 {
142 	FILE *df;
143 	register struct dumpdates *dtwalk;
144 	register int i;
145 	int fd;
146 	char *fname;
147 
148 	if(uflag == 0)
149 		return;
150 	if ((df = fopen(dumpdates, "r+")) == NULL)
151 		quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
152 	fd = fileno(df);
153 	(void) flock(fd, LOCK_EX);
154 	fname = disk;
155 	free((char *)ddatev);
156 	ddatev = 0;
157 	nddates = 0;
158 	dthead = 0;
159 	ddates_in = 0;
160 	readdumptimes(df);
161 	if (fseek(df, 0L, 0) < 0)
162 		quit("fseek: %s\n", strerror(errno));
163 	spcl.c_ddate = 0;
164 	ITITERATE(i, dtwalk) {
165 		if (strncmp(fname, dtwalk->dd_name,
166 				sizeof (dtwalk->dd_name)) != 0)
167 			continue;
168 		if (dtwalk->dd_level != level)
169 			continue;
170 		goto found;
171 	}
172 	/*
173 	 *	construct the new upper bound;
174 	 *	Enough room has been allocated.
175 	 */
176 	dtwalk = ddatev[nddates] =
177 		(struct dumpdates *)calloc(1, sizeof (struct dumpdates));
178 	nddates += 1;
179   found:
180 	(void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
181 	dtwalk->dd_level = level;
182 	dtwalk->dd_ddate = spcl.c_date;
183 
184 	ITITERATE(i, dtwalk) {
185 		dumprecout(df, dtwalk);
186 	}
187 	if (fflush(df))
188 		quit("%s: %s\n", dumpdates, strerror(errno));
189 	if (ftruncate(fd, ftell(df)))
190 		quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
191 	(void) fclose(df);
192 	msg("level %c dump on %s", level,
193 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
194 }
195 
196 static void
197 dumprecout(file, what)
198 	FILE *file;
199 	struct dumpdates *what;
200 {
201 
202 	if (fprintf(file, DUMPOUTFMT,
203 		    what->dd_name,
204 		    what->dd_level,
205 		    ctime(&what->dd_ddate)) < 0)
206 		quit("%s: %s\n", dumpdates, strerror(errno));
207 }
208 
209 int	recno;
210 
211 static int
212 getrecord(df, ddatep)
213 	FILE *df;
214 	struct dumpdates *ddatep;
215 {
216 	char tbuf[BUFSIZ];
217 
218 	recno = 0;
219 	if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
220 		return(-1);
221 	recno++;
222 	if (makedumpdate(ddatep, tbuf) < 0)
223 		msg("Unknown intermediate format in %s, line %d\n",
224 			dumpdates, recno);
225 
226 #ifdef FDEBUG
227 	msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
228 	    ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
229 #endif
230 	return(0);
231 }
232 
233 static int
234 makedumpdate(ddp, tbuf)
235 	struct dumpdates *ddp;
236 	char *tbuf;
237 {
238 	char un_buf[128];
239 
240 	(void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
241 	ddp->dd_ddate = unctime(un_buf);
242 	if (ddp->dd_ddate < 0)
243 		return(-1);
244 	return(0);
245 }
246