Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
3 | * All rights reserved. | |
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 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)dumpitime.c 5.7 (Berkeley) 3/7/91"; | |
36 | #endif /* not lint */ | |
37 | ||
38 | #include <sys/param.h> | |
39 | #include <ufs/dir.h> | |
40 | #include <ufs/dinode.h> | |
41 | #include <fcntl.h> | |
42 | #include <protocols/dumprestore.h> | |
43 | #include <errno.h> | |
44 | #include <stdio.h> | |
45 | #ifdef __STDC__ | |
46 | #include <time.h> | |
47 | #include <unistd.h> | |
48 | #include <stdlib.h> | |
49 | #include <string.h> | |
50 | #endif | |
51 | #include "dump.h" | |
52 | ||
53 | struct dumpdates **ddatev = 0; | |
54 | int nddates = 0; | |
55 | int ddates_in = 0; | |
56 | struct dumptime *dthead = 0; | |
57 | ||
58 | void readdumptimes(); | |
59 | int getrecord(); | |
60 | int makedumpdate(); | |
61 | ||
62 | static void dumprecout(); | |
63 | ||
64 | void | |
65 | initdumptimes() | |
66 | { | |
67 | FILE *df; | |
68 | ||
69 | if ((df = fopen(dumpdates, "r")) == NULL) { | |
70 | if (errno == ENOENT) { | |
71 | msg("WARNING: no file `%s'\n", dumpdates); | |
72 | return; | |
73 | } | |
74 | quit("cannot read %s: %s\n", dumpdates, strerror(errno)); | |
75 | /* NOTREACHED */ | |
76 | } | |
77 | (void) flock(fileno(df), LOCK_SH); | |
78 | readdumptimes(df); | |
79 | fclose(df); | |
80 | } | |
81 | ||
82 | void | |
83 | readdumptimes(df) | |
84 | FILE *df; | |
85 | { | |
86 | register int i; | |
87 | register struct dumptime *dtwalk; | |
88 | ||
89 | for (;;) { | |
90 | dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); | |
91 | if (getrecord(df, &(dtwalk->dt_value)) < 0) | |
92 | break; | |
93 | nddates++; | |
94 | dtwalk->dt_next = dthead; | |
95 | dthead = dtwalk; | |
96 | } | |
97 | ||
98 | ddates_in = 1; | |
99 | /* | |
100 | * arrayify the list, leaving enough room for the additional | |
101 | * record that we may have to add to the ddate structure | |
102 | */ | |
103 | ddatev = (struct dumpdates **) | |
104 | calloc(nddates + 1, sizeof (struct dumpdates *)); | |
105 | dtwalk = dthead; | |
106 | for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) | |
107 | ddatev[i] = &dtwalk->dt_value; | |
108 | } | |
109 | ||
110 | void | |
111 | getdumptime() | |
112 | { | |
113 | register struct dumpdates *ddp; | |
114 | register int i; | |
115 | char *fname; | |
116 | ||
117 | fname = disk; | |
118 | #ifdef FDEBUG | |
119 | msg("Looking for name %s in dumpdates = %s for level = %c\n", | |
120 | fname, dumpdates, level); | |
121 | #endif | |
122 | spcl.c_ddate = 0; | |
123 | lastlevel = '0'; | |
124 | ||
125 | initdumptimes(); | |
126 | /* | |
127 | * Go find the entry with the same name for a lower increment | |
128 | * and older date | |
129 | */ | |
130 | ITITERATE(i, ddp) { | |
131 | if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) | |
132 | continue; | |
133 | if (ddp->dd_level >= level) | |
134 | continue; | |
135 | if (ddp->dd_ddate <= spcl.c_ddate) | |
136 | continue; | |
137 | spcl.c_ddate = ddp->dd_ddate; | |
138 | lastlevel = ddp->dd_level; | |
139 | } | |
140 | } | |
141 | ||
142 | void | |
143 | putdumptime() | |
144 | { | |
145 | FILE *df; | |
146 | register struct dumpdates *dtwalk; | |
147 | register int i; | |
148 | int fd; | |
149 | char *fname; | |
150 | ||
151 | if(uflag == 0) | |
152 | return; | |
153 | if ((df = fopen(dumpdates, "r+")) == NULL) | |
154 | quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); | |
155 | fd = fileno(df); | |
156 | (void) flock(fd, LOCK_EX); | |
157 | fname = disk; | |
158 | free(ddatev); | |
159 | ddatev = 0; | |
160 | nddates = 0; | |
161 | dthead = 0; | |
162 | ddates_in = 0; | |
163 | readdumptimes(df); | |
164 | if (fseek(df, 0L, 0) < 0) | |
165 | quit("fseek: %s\n", strerror(errno)); | |
166 | spcl.c_ddate = 0; | |
167 | ITITERATE(i, dtwalk) { | |
168 | if (strncmp(fname, dtwalk->dd_name, | |
169 | sizeof (dtwalk->dd_name)) != 0) | |
170 | continue; | |
171 | if (dtwalk->dd_level != level) | |
172 | continue; | |
173 | goto found; | |
174 | } | |
175 | /* | |
176 | * construct the new upper bound; | |
177 | * Enough room has been allocated. | |
178 | */ | |
179 | dtwalk = ddatev[nddates] = | |
180 | (struct dumpdates *)calloc(1, sizeof(struct dumpdates)); | |
181 | nddates += 1; | |
182 | found: | |
183 | (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); | |
184 | dtwalk->dd_level = level; | |
185 | dtwalk->dd_ddate = spcl.c_date; | |
186 | ||
187 | ITITERATE(i, dtwalk) { | |
188 | dumprecout(df, dtwalk); | |
189 | } | |
190 | if (fflush(df)) | |
191 | quit("%s: %s\n", dumpdates, strerror(errno)); | |
192 | if (ftruncate(fd, ftell(df))) | |
193 | quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); | |
194 | (void) fclose(df); | |
195 | msg("level %c dump on %s", level, | |
196 | spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); | |
197 | } | |
198 | ||
199 | static void | |
200 | dumprecout(file, what) | |
201 | FILE *file; | |
202 | struct dumpdates *what; | |
203 | { | |
204 | ||
205 | if (fprintf(file, DUMPOUTFMT, | |
206 | what->dd_name, | |
207 | what->dd_level, | |
208 | ctime(&what->dd_ddate)) < 0) | |
209 | quit("%s: %s\n", dumpdates, strerror(errno)); | |
210 | } | |
211 | ||
212 | int recno; | |
213 | int | |
214 | getrecord(df, ddatep) | |
215 | FILE *df; | |
216 | struct dumpdates *ddatep; | |
217 | { | |
218 | char buf[BUFSIZ]; | |
219 | ||
220 | recno = 0; | |
221 | if ( (fgets(buf, BUFSIZ, df)) != buf) | |
222 | return(-1); | |
223 | recno++; | |
224 | if (makedumpdate(ddatep, buf) < 0) | |
225 | msg("Unknown intermediate format in %s, line %d\n", | |
226 | dumpdates, recno); | |
227 | ||
228 | #ifdef FDEBUG | |
229 | msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, | |
230 | ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); | |
231 | #endif | |
232 | return(0); | |
233 | } | |
234 | ||
235 | time_t unctime(); | |
236 | ||
237 | int | |
238 | makedumpdate(ddp, buf) | |
239 | struct dumpdates *ddp; | |
240 | char *buf; | |
241 | { | |
242 | char un_buf[128]; | |
243 | ||
244 | sscanf(buf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); | |
245 | ddp->dd_ddate = unctime(un_buf); | |
246 | if (ddp->dd_ddate < 0) | |
247 | return(-1); | |
248 | return(0); | |
249 | } |