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