new fstab format
[unix-history] / usr / src / sbin / dump / itime.c
CommitLineData
c02e5137 1static char *sccsid = "@(#)itime.c 1.10 (Berkeley) %G%";
51e45c89 2
31dd475e 3#include "dump.h"
51e45c89 4#include <sys/file.h>
31dd475e
BJ
5
6char *prdate(d)
7 time_t d;
8{
9 char *p;
10
11 if(d == 0)
12 return("the epoch");
13 p = ctime(&d);
14 p[24] = 0;
15 return(p);
16}
17
18struct idates **idatev = 0;
19int nidates = 0;
20int idates_in = 0;
21struct itime *ithead = 0;
22
23inititimes()
24{
25 FILE *df;
26 register int i;
27 register struct itime *itwalk;
51e45c89 28 int fd;
31dd475e
BJ
29
30 if (idates_in)
31 return;
51e45c89
KM
32 if ((fd = open(increm, FSHLOCK|FRDONLY, 0600)) < 0) {
33 perror(increm);
34 return;
35 }
36 if ((df = fdopen(fd, "r")) == NULL) {
31dd475e
BJ
37 nidates = 0;
38 ithead = 0;
39 } else {
40 do{
41 itwalk=(struct itime *)calloc(1,sizeof (struct itime));
42 if (getrecord(df, &(itwalk->it_value)) < 0)
43 break;
44 nidates++;
45 itwalk->it_next = ithead;
46 ithead = itwalk;
47 } while (1);
48 fclose(df);
49 }
50
51 idates_in = 1;
52 /*
53 * arrayify the list, leaving enough room for the additional
54 * record that we may have to add to the idate structure
55 */
56 idatev = (struct idates **)calloc(nidates + 1,sizeof (struct idates *));
57 for (i = nidates-1, itwalk = ithead; i >= 0; i--, itwalk = itwalk->it_next)
58 idatev[i] = &itwalk->it_value;
59}
60
61getitime()
62{
63 register struct idates *ip;
64 register int i;
65 char *fname;
66
67 fname = disk;
68#ifdef FDEBUG
69 msg("Looking for name %s in increm = %s for delta = %c\n",
70 fname, increm, incno);
71#endif
72 spcl.c_ddate = 0;
73
74 inititimes();
75 /*
76 * Go find the entry with the same name for a lower increment
77 * and older date
78 */
79 ITITERATE(i, ip){
80 if(strncmp(fname, ip->id_name,
81 sizeof (ip->id_name)) != 0)
82 continue;
83 if (ip->id_incno >= incno)
84 continue;
85 if (ip->id_ddate <= spcl.c_ddate)
86 continue;
87 spcl.c_ddate = ip->id_ddate;
c02e5137 88 lastincno = ip->id_incno;
31dd475e
BJ
89 }
90}
91
92putitime()
93{
94 FILE *df;
95 register struct idates *itwalk;
96 register int i;
51e45c89 97 int fd;
31dd475e
BJ
98 char *fname;
99
100 if(uflag == 0)
101 return;
51e45c89
KM
102 if ((fd = open(temp, FCREATE|FEXLOCK|FRDWR, 0600)) < 0) {
103 perror(temp);
104 dumpabort();
105 }
106 if ((df = fdopen(fd, "w")) == NULL) {
107 perror(temp);
108 dumpabort();
109 }
31dd475e 110 fname = disk;
9dcdb908
KM
111 free(idatev);
112 idatev = 0;
113 nidates = 0;
114 ithead = 0;
115 idates_in = 0;
116 inititimes();
31dd475e
BJ
117
118 spcl.c_ddate = 0;
119 ITITERATE(i, itwalk){
120 if (strncmp(fname, itwalk->id_name,
121 sizeof (itwalk->id_name)) != 0)
122 continue;
123 if (itwalk->id_incno != incno)
124 continue;
125 goto found;
126 }
127 /*
128 * construct the new upper bound;
129 * Enough room has been allocated.
130 */
131 itwalk = idatev[nidates] =
132 (struct idates *)calloc(1, sizeof(struct idates));
133 nidates += 1;
134 found:
135 strncpy(itwalk->id_name, fname, sizeof (itwalk->id_name));
136 itwalk->id_incno = incno;
137 itwalk->id_ddate = spcl.c_date;
138
31dd475e
BJ
139 ITITERATE(i, itwalk){
140 recout(df, itwalk);
141 }
51e45c89
KM
142 if (rename(temp, increm) < 0) {
143 perror("rename");
144 (void) unlink(temp);
145 dumpabort();
146 }
147 (void) chmod(increm, 0644);
148 (void) fclose(df);
31dd475e
BJ
149 msg("level %c dump on %s\n", incno, prdate(spcl.c_date));
150}
151
152recout(file, what)
153 FILE *file;
154 struct idates *what;
155{
156 fprintf(file, DUMPOUTFMT,
157 what->id_name,
158 what->id_incno,
159 ctime(&(what->id_ddate))
160 );
161}
162
163int recno;
164int getrecord(df, idatep)
165 FILE *df;
166 struct idates *idatep;
167{
168 char buf[BUFSIZ];
169
170 recno = 0;
171 if ( (fgets(buf, BUFSIZ, df)) != buf)
172 return(-1);
173 recno++;
174 if (makeidate(idatep, buf) < 0)
175 msg("Unknown intermediate format in %s, line %d\n",
51e45c89 176 increm, recno);
31dd475e
BJ
177
178#ifdef FDEBUG
179 msg("getrecord: %s %c %s\n",
180 idatep->id_name, idatep->id_incno, prdate(idatep->id_ddate));
181#endif
182 return(0);
183}
184
31dd475e
BJ
185time_t unctime();
186
187int makeidate(ip, buf)
188 struct idates *ip;
189 char *buf;
190{
191 char un_buf[128];
192
193 sscanf(buf, DUMPINFMT, ip->id_name, &ip->id_incno, un_buf);
194 ip->id_ddate = unctime(un_buf);
195 if (ip->id_ddate < 0)
196 return(-1);
197 return(0);
198}
199
003a2a9e 200/*
b6407c9d 201 * This is an estimation of the number of TP_BSIZE blocks in the file.
003a2a9e
KM
202 * It assumes that there are no unallocated blocks; hence
203 * the estimate may be high
204 */
31dd475e
BJ
205est(ip)
206 struct dinode *ip;
207{
208 long s;
209
210 esize++;
b6407c9d 211 /* calc number of TP_BSIZE blocks */
184c56be 212 s = howmany(ip->di_size, TP_BSIZE);
b6407c9d
KM
213 if (ip->di_size > sblock->fs_bsize * NDADDR) {
214 /* calc number of indirect blocks on the dump tape */
092ee994 215 s += howmany(s - NDADDR * sblock->fs_bsize / TP_BSIZE,
b6407c9d 216 TP_NINDIR);
31dd475e 217 }
b6407c9d 218 esize += s;
31dd475e
BJ
219}
220
221bmapest(map)
b6407c9d 222 char *map;
31dd475e
BJ
223{
224 register i, n;
225
226 n = -1;
b6407c9d 227 for (i = 0; i < msiz; i++)
31dd475e
BJ
228 if(map[i])
229 n = i;
230 if(n < 0)
231 return;
b6407c9d 232 n++;
31dd475e 233 esize++;
b6407c9d 234 esize += howmany(n * sizeof map[0], TP_BSIZE);
31dd475e 235}