Fix UTIME_OMIT handling
[dragonfly.git] / sbin / dump / itime.c
blob3287be29da6f52ac915ffaf9e5b14caf1505cb1b
1 /*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
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. 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.
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.
29 * @(#)itime.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/dump/itime.c,v 1.3.2.1 2001/08/01 06:29:35 obrien Exp $
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/time.h>
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
40 #include <protocols/dumprestore.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #include "dump.h"
51 struct dumptime {
52 struct dumpdates dt_value;
53 SLIST_ENTRY(dumptime) dt_list;
55 SLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead);
56 int nddates = 0; /* number of records (might be zero) */
57 struct dumpdates **ddatev; /* the arrayfied version */
58 const char *dumpdates; /* name of the file containing dump date info */
59 int lastlevel; /* dump level of previous dump */
61 static void dumprecout(FILE *, struct dumpdates *);
62 static int getrecord(FILE *, struct dumpdates *);
63 static int makedumpdate(struct dumpdates *, char *);
64 static void readdumptimes(FILE *);
66 void
67 initdumptimes(void)
69 FILE *df;
71 if ((df = fopen(dumpdates, "r")) == NULL) {
72 if (errno != ENOENT) {
73 msg("WARNING: cannot read %s: %s\n", dumpdates,
74 strerror(errno));
75 return;
78 * Dumpdates does not exist, make an empty one.
80 msg("WARNING: no file `%s', making an empty one\n", dumpdates);
81 if ((df = fopen(dumpdates, "w")) == NULL) {
82 msg("WARNING: cannot create %s: %s\n", dumpdates,
83 strerror(errno));
84 return;
86 fclose(df);
87 if ((df = fopen(dumpdates, "r")) == NULL) {
88 quit("cannot read %s even after creating it: %s\n",
89 dumpdates, strerror(errno));
90 /* NOTREACHED */
93 flock(fileno(df), LOCK_SH);
94 readdumptimes(df);
95 fclose(df);
98 static void
99 readdumptimes(FILE *df)
101 int i;
102 struct dumptime *dtwalk;
104 for (;;) {
105 dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime));
106 if (getrecord(df, &(dtwalk->dt_value)) < 0)
107 break;
108 nddates++;
109 SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list);
113 * arrayify the list, leaving enough room for the additional
114 * record that we may have to add to the ddate structure
116 ddatev = (struct dumpdates **)
117 calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *));
118 dtwalk = SLIST_FIRST(&dthead);
119 for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list))
120 ddatev[i] = &dtwalk->dt_value;
123 void
124 getdumptime(void)
126 struct dumpdates *ddp;
127 int i;
128 char *fname;
130 fname = disk;
131 #ifdef FDEBUG
132 msg("Looking for name %s in dumpdates = %s for level = %c\n",
133 fname, dumpdates, level);
134 #endif
135 spcl.c_ddate = 0;
136 lastlevel = '0';
138 initdumptimes();
140 * Go find the entry with the same name for a lower increment
141 * and older date
143 ITITERATE(i, ddp) {
144 if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0)
145 continue;
146 if (ddp->dd_level >= level)
147 continue;
148 if (ddp->dd_ddate <= spcl.c_ddate)
149 continue;
150 spcl.c_ddate = ddp->dd_ddate;
151 lastlevel = ddp->dd_level;
155 void
156 putdumptime(void)
158 FILE *df;
159 struct dumpdates *dtwalk;
160 int i;
161 int fd;
162 char *fname;
164 if(uflag == 0)
165 return;
166 if ((df = fopen(dumpdates, "r+")) == NULL)
167 quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno));
168 fd = fileno(df);
169 flock(fd, LOCK_EX);
170 fname = disk;
171 free((char *)ddatev);
172 ddatev = NULL;
173 nddates = 0;
174 readdumptimes(df);
175 if (fseek(df, 0L, 0) < 0)
176 quit("fseek: %s\n", strerror(errno));
177 spcl.c_ddate = 0;
178 ITITERATE(i, dtwalk) {
179 if (strncmp(fname, dtwalk->dd_name,
180 sizeof (dtwalk->dd_name)) != 0)
181 continue;
182 if (dtwalk->dd_level != level)
183 continue;
184 goto found;
187 * construct the new upper bound;
188 * Enough room has been allocated.
190 dtwalk = ddatev[nddates] =
191 (struct dumpdates *)calloc(1, sizeof (struct dumpdates));
192 nddates += 1;
193 found:
194 strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name));
195 dtwalk->dd_level = level;
196 dtwalk->dd_ddate = spcl.c_date;
198 ITITERATE(i, dtwalk) {
199 dumprecout(df, dtwalk);
201 if (fflush(df))
202 quit("%s: %s\n", dumpdates, strerror(errno));
203 if (ftruncate(fd, ftell(df)))
204 quit("ftruncate (%s): %s\n", dumpdates, strerror(errno));
205 fclose(df);
206 msg("level %c dump on %s", level,
207 spcl.c_date == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_date));
210 static void
211 dumprecout(FILE *file, struct dumpdates *what)
214 if (fprintf(file, DUMPOUTFMT,
215 what->dd_name,
216 what->dd_level,
217 ctime(&what->dd_ddate)) < 0)
218 quit("%s: %s\n", dumpdates, strerror(errno));
221 int recno;
223 static int
224 getrecord(FILE *df, struct dumpdates *ddatep)
226 char tbuf[BUFSIZ];
228 recno = 0;
229 if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf)
230 return(-1);
231 recno++;
232 if (makedumpdate(ddatep, tbuf) < 0)
233 msg("Unknown intermediate format in %s, line %d\n",
234 dumpdates, recno);
236 #ifdef FDEBUG
237 msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level,
238 ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate));
239 #endif
240 return(0);
243 static int
244 makedumpdate(struct dumpdates *ddp, char *tbuf)
246 char un_buf[128];
248 sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf);
249 ddp->dd_ddate = unctime(un_buf);
250 if (ddp->dd_ddate < 0)
251 return(-1);
252 return(0);