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