itime.c revision 1558
11558Srgrimes/*- 21558Srgrimes * Copyright (c) 1980, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 351558Srgrimesstatic char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#include <sys/param.h> 391558Srgrimes#include <sys/time.h> 401558Srgrimes#ifdef sunos 411558Srgrimes#include <sys/vnode.h> 421558Srgrimes 431558Srgrimes#include <ufs/fsdir.h> 441558Srgrimes#include <ufs/inode.h> 451558Srgrimes#include <ufs/fs.h> 461558Srgrimes#else 471558Srgrimes#include <ufs/ufs/dinode.h> 481558Srgrimes#endif 491558Srgrimes 501558Srgrimes#include <protocols/dumprestore.h> 511558Srgrimes 521558Srgrimes#include <errno.h> 531558Srgrimes#include <fcntl.h> 541558Srgrimes#include <stdio.h> 551558Srgrimes#ifdef __STDC__ 561558Srgrimes#include <stdlib.h> 571558Srgrimes#include <string.h> 581558Srgrimes#include <unistd.h> 591558Srgrimes#endif 601558Srgrimes 611558Srgrimes#include "dump.h" 621558Srgrimes 631558Srgrimesstruct dumpdates **ddatev = 0; 641558Srgrimesint nddates = 0; 651558Srgrimesint ddates_in = 0; 661558Srgrimesstruct dumptime *dthead = 0; 671558Srgrimes 681558Srgrimesstatic void dumprecout __P((FILE *, struct dumpdates *)); 691558Srgrimesstatic int getrecord __P((FILE *, struct dumpdates *)); 701558Srgrimesstatic int makedumpdate __P((struct dumpdates *, char *)); 711558Srgrimesstatic void readdumptimes __P((FILE *)); 721558Srgrimes 731558Srgrimesvoid 741558Srgrimesinitdumptimes() 751558Srgrimes{ 761558Srgrimes FILE *df; 771558Srgrimes 781558Srgrimes if ((df = fopen(dumpdates, "r")) == NULL) { 791558Srgrimes if (errno != ENOENT) { 801558Srgrimes quit("cannot read %s: %s\n", dumpdates, 811558Srgrimes strerror(errno)); 821558Srgrimes /* NOTREACHED */ 831558Srgrimes } 841558Srgrimes /* 851558Srgrimes * Dumpdates does not exist, make an empty one. 861558Srgrimes */ 871558Srgrimes msg("WARNING: no file `%s', making an empty one\n", dumpdates); 881558Srgrimes if ((df = fopen(dumpdates, "w")) == NULL) { 891558Srgrimes quit("cannot create %s: %s\n", dumpdates, 901558Srgrimes strerror(errno)); 911558Srgrimes /* NOTREACHED */ 921558Srgrimes } 931558Srgrimes (void) fclose(df); 941558Srgrimes if ((df = fopen(dumpdates, "r")) == NULL) { 951558Srgrimes quit("cannot read %s even after creating it: %s\n", 961558Srgrimes dumpdates, strerror(errno)); 971558Srgrimes /* NOTREACHED */ 981558Srgrimes } 991558Srgrimes } 1001558Srgrimes (void) flock(fileno(df), LOCK_SH); 1011558Srgrimes readdumptimes(df); 1021558Srgrimes (void) fclose(df); 1031558Srgrimes} 1041558Srgrimes 1051558Srgrimesstatic void 1061558Srgrimesreaddumptimes(df) 1071558Srgrimes FILE *df; 1081558Srgrimes{ 1091558Srgrimes register int i; 1101558Srgrimes register struct dumptime *dtwalk; 1111558Srgrimes 1121558Srgrimes for (;;) { 1131558Srgrimes dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 1141558Srgrimes if (getrecord(df, &(dtwalk->dt_value)) < 0) 1151558Srgrimes break; 1161558Srgrimes nddates++; 1171558Srgrimes dtwalk->dt_next = dthead; 1181558Srgrimes dthead = dtwalk; 1191558Srgrimes } 1201558Srgrimes 1211558Srgrimes ddates_in = 1; 1221558Srgrimes /* 1231558Srgrimes * arrayify the list, leaving enough room for the additional 1241558Srgrimes * record that we may have to add to the ddate structure 1251558Srgrimes */ 1261558Srgrimes ddatev = (struct dumpdates **) 1271558Srgrimes calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 1281558Srgrimes dtwalk = dthead; 1291558Srgrimes for (i = nddates - 1; i >= 0; i--, dtwalk = dtwalk->dt_next) 1301558Srgrimes ddatev[i] = &dtwalk->dt_value; 1311558Srgrimes} 1321558Srgrimes 1331558Srgrimesvoid 1341558Srgrimesgetdumptime() 1351558Srgrimes{ 1361558Srgrimes register struct dumpdates *ddp; 1371558Srgrimes register int i; 1381558Srgrimes char *fname; 1391558Srgrimes 1401558Srgrimes fname = disk; 1411558Srgrimes#ifdef FDEBUG 1421558Srgrimes msg("Looking for name %s in dumpdates = %s for level = %c\n", 1431558Srgrimes fname, dumpdates, level); 1441558Srgrimes#endif 1451558Srgrimes spcl.c_ddate = 0; 1461558Srgrimes lastlevel = '0'; 1471558Srgrimes 1481558Srgrimes initdumptimes(); 1491558Srgrimes /* 1501558Srgrimes * Go find the entry with the same name for a lower increment 1511558Srgrimes * and older date 1521558Srgrimes */ 1531558Srgrimes ITITERATE(i, ddp) { 1541558Srgrimes if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 1551558Srgrimes continue; 1561558Srgrimes if (ddp->dd_level >= level) 1571558Srgrimes continue; 1581558Srgrimes if (ddp->dd_ddate <= spcl.c_ddate) 1591558Srgrimes continue; 1601558Srgrimes spcl.c_ddate = ddp->dd_ddate; 1611558Srgrimes lastlevel = ddp->dd_level; 1621558Srgrimes } 1631558Srgrimes} 1641558Srgrimes 1651558Srgrimesvoid 1661558Srgrimesputdumptime() 1671558Srgrimes{ 1681558Srgrimes FILE *df; 1691558Srgrimes register struct dumpdates *dtwalk; 1701558Srgrimes register int i; 1711558Srgrimes int fd; 1721558Srgrimes char *fname; 1731558Srgrimes 1741558Srgrimes if(uflag == 0) 1751558Srgrimes return; 1761558Srgrimes if ((df = fopen(dumpdates, "r+")) == NULL) 1771558Srgrimes quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 1781558Srgrimes fd = fileno(df); 1791558Srgrimes (void) flock(fd, LOCK_EX); 1801558Srgrimes fname = disk; 1811558Srgrimes free((char *)ddatev); 1821558Srgrimes ddatev = 0; 1831558Srgrimes nddates = 0; 1841558Srgrimes dthead = 0; 1851558Srgrimes ddates_in = 0; 1861558Srgrimes readdumptimes(df); 1871558Srgrimes if (fseek(df, 0L, 0) < 0) 1881558Srgrimes quit("fseek: %s\n", strerror(errno)); 1891558Srgrimes spcl.c_ddate = 0; 1901558Srgrimes ITITERATE(i, dtwalk) { 1911558Srgrimes if (strncmp(fname, dtwalk->dd_name, 1921558Srgrimes sizeof (dtwalk->dd_name)) != 0) 1931558Srgrimes continue; 1941558Srgrimes if (dtwalk->dd_level != level) 1951558Srgrimes continue; 1961558Srgrimes goto found; 1971558Srgrimes } 1981558Srgrimes /* 1991558Srgrimes * construct the new upper bound; 2001558Srgrimes * Enough room has been allocated. 2011558Srgrimes */ 2021558Srgrimes dtwalk = ddatev[nddates] = 2031558Srgrimes (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 2041558Srgrimes nddates += 1; 2051558Srgrimes found: 2061558Srgrimes (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 2071558Srgrimes dtwalk->dd_level = level; 2081558Srgrimes dtwalk->dd_ddate = spcl.c_date; 2091558Srgrimes 2101558Srgrimes ITITERATE(i, dtwalk) { 2111558Srgrimes dumprecout(df, dtwalk); 2121558Srgrimes } 2131558Srgrimes if (fflush(df)) 2141558Srgrimes quit("%s: %s\n", dumpdates, strerror(errno)); 2151558Srgrimes if (ftruncate(fd, ftell(df))) 2161558Srgrimes quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 2171558Srgrimes (void) fclose(df); 2181558Srgrimes msg("level %c dump on %s", level, 2191558Srgrimes spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 2201558Srgrimes} 2211558Srgrimes 2221558Srgrimesstatic void 2231558Srgrimesdumprecout(file, what) 2241558Srgrimes FILE *file; 2251558Srgrimes struct dumpdates *what; 2261558Srgrimes{ 2271558Srgrimes 2281558Srgrimes if (fprintf(file, DUMPOUTFMT, 2291558Srgrimes what->dd_name, 2301558Srgrimes what->dd_level, 2311558Srgrimes ctime(&what->dd_ddate)) < 0) 2321558Srgrimes quit("%s: %s\n", dumpdates, strerror(errno)); 2331558Srgrimes} 2341558Srgrimes 2351558Srgrimesint recno; 2361558Srgrimes 2371558Srgrimesstatic int 2381558Srgrimesgetrecord(df, ddatep) 2391558Srgrimes FILE *df; 2401558Srgrimes struct dumpdates *ddatep; 2411558Srgrimes{ 2421558Srgrimes char tbuf[BUFSIZ]; 2431558Srgrimes 2441558Srgrimes recno = 0; 2451558Srgrimes if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 2461558Srgrimes return(-1); 2471558Srgrimes recno++; 2481558Srgrimes if (makedumpdate(ddatep, tbuf) < 0) 2491558Srgrimes msg("Unknown intermediate format in %s, line %d\n", 2501558Srgrimes dumpdates, recno); 2511558Srgrimes 2521558Srgrimes#ifdef FDEBUG 2531558Srgrimes msg("getrecord: %s %c %s", ddatep->dd_name, ddatep->dd_level, 2541558Srgrimes ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 2551558Srgrimes#endif 2561558Srgrimes return(0); 2571558Srgrimes} 2581558Srgrimes 2591558Srgrimesstatic int 2601558Srgrimesmakedumpdate(ddp, tbuf) 2611558Srgrimes struct dumpdates *ddp; 2621558Srgrimes char *tbuf; 2631558Srgrimes{ 2641558Srgrimes char un_buf[128]; 2651558Srgrimes 2661558Srgrimes (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 2671558Srgrimes ddp->dd_ddate = unctime(un_buf); 2681558Srgrimes if (ddp->dd_ddate < 0) 2691558Srgrimes return(-1); 2701558Srgrimes return(0); 2711558Srgrimes} 272