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