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