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 * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 301558Srgrimes#ifndef lint 3136997Scharnier#if 0 321558Srgrimesstatic char sccsid[] = "@(#)itime.c 8.1 (Berkeley) 6/5/93"; 3336997Scharnier#endif 3436997Scharnierstatic const char rcsid[] = 3550476Speter "$FreeBSD$"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#include <sys/param.h> 3971787Sphk#include <sys/queue.h> 401558Srgrimes 411558Srgrimes#include <ufs/ufs/dinode.h> 421558Srgrimes 431558Srgrimes#include <protocols/dumprestore.h> 441558Srgrimes 451558Srgrimes#include <errno.h> 461558Srgrimes#include <fcntl.h> 47103949Smike#include <limits.h> 481558Srgrimes#include <stdio.h> 491558Srgrimes#include <stdlib.h> 501558Srgrimes#include <string.h> 51217769Smckusick#include <time.h> 5299562Siedowse#include <timeconv.h> 531558Srgrimes 541558Srgrimes#include "dump.h" 551558Srgrimes 5671787Sphkstruct dumptime { 5771787Sphk struct dumpdates dt_value; 5871787Sphk SLIST_ENTRY(dumptime) dt_list; 5971787Sphk}; 6071787SphkSLIST_HEAD(dthead, dumptime) dthead = SLIST_HEAD_INITIALIZER(dthead); 611558Srgrimesstruct dumpdates **ddatev = 0; 621558Srgrimesint nddates = 0; 631558Srgrimes 6492837Simpstatic void dumprecout(FILE *, const struct dumpdates *); 6592837Simpstatic int getrecord(FILE *, struct dumpdates *); 6692837Simpstatic int makedumpdate(struct dumpdates *, const char *); 6792837Simpstatic void readdumptimes(FILE *); 681558Srgrimes 691558Srgrimesvoid 7092837Simpinitdumptimes(void) 711558Srgrimes{ 721558Srgrimes FILE *df; 731558Srgrimes 741558Srgrimes if ((df = fopen(dumpdates, "r")) == NULL) { 751558Srgrimes if (errno != ENOENT) { 7674873Sobrien msg("WARNING: cannot read %s: %s\n", dumpdates, 771558Srgrimes strerror(errno)); 7874872Sobrien return; 791558Srgrimes } 801558Srgrimes /* 811558Srgrimes * Dumpdates does not exist, make an empty one. 821558Srgrimes */ 831558Srgrimes msg("WARNING: no file `%s', making an empty one\n", dumpdates); 841558Srgrimes if ((df = fopen(dumpdates, "w")) == NULL) { 8574873Sobrien msg("WARNING: cannot create %s: %s\n", dumpdates, 861558Srgrimes strerror(errno)); 8774872Sobrien return; 881558Srgrimes } 891558Srgrimes (void) fclose(df); 901558Srgrimes if ((df = fopen(dumpdates, "r")) == NULL) { 911558Srgrimes quit("cannot read %s even after creating it: %s\n", 921558Srgrimes dumpdates, strerror(errno)); 931558Srgrimes /* NOTREACHED */ 941558Srgrimes } 951558Srgrimes } 961558Srgrimes (void) flock(fileno(df), LOCK_SH); 971558Srgrimes readdumptimes(df); 981558Srgrimes (void) fclose(df); 991558Srgrimes} 1001558Srgrimes 1011558Srgrimesstatic void 10292837Simpreaddumptimes(FILE *df) 1031558Srgrimes{ 10486473Siedowse int i; 10586473Siedowse struct dumptime *dtwalk; 1061558Srgrimes 1071558Srgrimes for (;;) { 1081558Srgrimes dtwalk = (struct dumptime *)calloc(1, sizeof (struct dumptime)); 109203459Sdelphij if (getrecord(df, &(dtwalk->dt_value)) < 0) { 110203459Sdelphij free(dtwalk); 1111558Srgrimes break; 112203459Sdelphij } 1131558Srgrimes nddates++; 11471787Sphk SLIST_INSERT_HEAD(&dthead, dtwalk, dt_list); 1151558Srgrimes } 1161558Srgrimes 1171558Srgrimes /* 1181558Srgrimes * arrayify the list, leaving enough room for the additional 1191558Srgrimes * record that we may have to add to the ddate structure 1201558Srgrimes */ 1211558Srgrimes ddatev = (struct dumpdates **) 1221558Srgrimes calloc((unsigned) (nddates + 1), sizeof (struct dumpdates *)); 12371787Sphk dtwalk = SLIST_FIRST(&dthead); 12471787Sphk for (i = nddates - 1; i >= 0; i--, dtwalk = SLIST_NEXT(dtwalk, dt_list)) 1251558Srgrimes ddatev[i] = &dtwalk->dt_value; 1261558Srgrimes} 1271558Srgrimes 1281558Srgrimesvoid 12992837Simpgetdumptime(void) 1301558Srgrimes{ 13186473Siedowse struct dumpdates *ddp; 13286473Siedowse int i; 1331558Srgrimes char *fname; 1341558Srgrimes 1351558Srgrimes fname = disk; 1361558Srgrimes#ifdef FDEBUG 137179275Smckusick msg("Looking for name %s in dumpdates = %s for level = %d\n", 1381558Srgrimes fname, dumpdates, level); 1391558Srgrimes#endif 1401558Srgrimes spcl.c_ddate = 0; 141179275Smckusick lastlevel = 0; 1421558Srgrimes 1431558Srgrimes initdumptimes(); 1441558Srgrimes /* 1451558Srgrimes * Go find the entry with the same name for a lower increment 1461558Srgrimes * and older date 1471558Srgrimes */ 1481558Srgrimes ITITERATE(i, ddp) { 1491558Srgrimes if (strncmp(fname, ddp->dd_name, sizeof (ddp->dd_name)) != 0) 1501558Srgrimes continue; 1511558Srgrimes if (ddp->dd_level >= level) 1521558Srgrimes continue; 15398542Smckusick if (ddp->dd_ddate <= _time64_to_time(spcl.c_ddate)) 1541558Srgrimes continue; 15598542Smckusick spcl.c_ddate = _time_to_time64(ddp->dd_ddate); 1561558Srgrimes lastlevel = ddp->dd_level; 1571558Srgrimes } 1581558Srgrimes} 1591558Srgrimes 1601558Srgrimesvoid 16192837Simpputdumptime(void) 1621558Srgrimes{ 1631558Srgrimes FILE *df; 16486473Siedowse struct dumpdates *dtwalk; 16586473Siedowse int i; 1661558Srgrimes int fd; 1671558Srgrimes char *fname; 16885635Sdillon char *tmsg; 1691558Srgrimes 1701558Srgrimes if(uflag == 0) 1711558Srgrimes return; 1721558Srgrimes if ((df = fopen(dumpdates, "r+")) == NULL) 1731558Srgrimes quit("cannot rewrite %s: %s\n", dumpdates, strerror(errno)); 1741558Srgrimes fd = fileno(df); 1751558Srgrimes (void) flock(fd, LOCK_EX); 1761558Srgrimes fname = disk; 1771558Srgrimes free((char *)ddatev); 1781558Srgrimes ddatev = 0; 1791558Srgrimes nddates = 0; 1801558Srgrimes readdumptimes(df); 1811558Srgrimes if (fseek(df, 0L, 0) < 0) 1821558Srgrimes quit("fseek: %s\n", strerror(errno)); 1831558Srgrimes spcl.c_ddate = 0; 1841558Srgrimes ITITERATE(i, dtwalk) { 1851558Srgrimes if (strncmp(fname, dtwalk->dd_name, 1861558Srgrimes sizeof (dtwalk->dd_name)) != 0) 1871558Srgrimes continue; 1881558Srgrimes if (dtwalk->dd_level != level) 1891558Srgrimes continue; 1901558Srgrimes goto found; 1911558Srgrimes } 1921558Srgrimes /* 1931558Srgrimes * construct the new upper bound; 1941558Srgrimes * Enough room has been allocated. 1951558Srgrimes */ 1961558Srgrimes dtwalk = ddatev[nddates] = 1971558Srgrimes (struct dumpdates *)calloc(1, sizeof (struct dumpdates)); 1981558Srgrimes nddates += 1; 1991558Srgrimes found: 2001558Srgrimes (void) strncpy(dtwalk->dd_name, fname, sizeof (dtwalk->dd_name)); 2011558Srgrimes dtwalk->dd_level = level; 20298542Smckusick dtwalk->dd_ddate = _time64_to_time(spcl.c_date); 2031558Srgrimes 2041558Srgrimes ITITERATE(i, dtwalk) { 2051558Srgrimes dumprecout(df, dtwalk); 2061558Srgrimes } 2071558Srgrimes if (fflush(df)) 2081558Srgrimes quit("%s: %s\n", dumpdates, strerror(errno)); 2091558Srgrimes if (ftruncate(fd, ftell(df))) 2101558Srgrimes quit("ftruncate (%s): %s\n", dumpdates, strerror(errno)); 2111558Srgrimes (void) fclose(df); 21285635Sdillon if (spcl.c_date == 0) { 21385635Sdillon tmsg = "the epoch\n"; 21485635Sdillon } else { 21598542Smckusick time_t t = _time64_to_time(spcl.c_date); 21685635Sdillon tmsg = ctime(&t); 21785635Sdillon } 218179275Smckusick msg("level %d dump on %s", level, tmsg); 2191558Srgrimes} 2201558Srgrimes 2211558Srgrimesstatic void 22292837Simpdumprecout(FILE *file, const struct dumpdates *what) 2231558Srgrimes{ 2241558Srgrimes 225230047Smckusick if (strlen(what->dd_name) > DUMPFMTLEN) 226230047Smckusick quit("Name '%s' exceeds DUMPFMTLEN (%d) bytes\n", 227230047Smckusick what->dd_name, DUMPFMTLEN); 228230047Smckusick if (fprintf(file, DUMPOUTFMT, DUMPFMTLEN, what->dd_name, 22992837Simp what->dd_level, ctime(&what->dd_ddate)) < 0) 2301558Srgrimes quit("%s: %s\n", dumpdates, strerror(errno)); 2311558Srgrimes} 2321558Srgrimes 2331558Srgrimesint recno; 2341558Srgrimes 2351558Srgrimesstatic int 23692837Simpgetrecord(FILE *df, struct dumpdates *ddatep) 2371558Srgrimes{ 2381558Srgrimes char tbuf[BUFSIZ]; 2391558Srgrimes 2401558Srgrimes recno = 0; 2411558Srgrimes if ( (fgets(tbuf, sizeof (tbuf), df)) != tbuf) 2421558Srgrimes return(-1); 2431558Srgrimes recno++; 2441558Srgrimes if (makedumpdate(ddatep, tbuf) < 0) 2451558Srgrimes msg("Unknown intermediate format in %s, line %d\n", 2461558Srgrimes dumpdates, recno); 2471558Srgrimes 2481558Srgrimes#ifdef FDEBUG 249179275Smckusick msg("getrecord: %s %d %s", ddatep->dd_name, ddatep->dd_level, 2501558Srgrimes ddatep->dd_ddate == 0 ? "the epoch\n" : ctime(&ddatep->dd_ddate)); 2511558Srgrimes#endif 2521558Srgrimes return(0); 2531558Srgrimes} 2541558Srgrimes 2551558Srgrimesstatic int 25692837Simpmakedumpdate(struct dumpdates *ddp, const char *tbuf) 2571558Srgrimes{ 2581558Srgrimes char un_buf[128]; 2591558Srgrimes 2601558Srgrimes (void) sscanf(tbuf, DUMPINFMT, ddp->dd_name, &ddp->dd_level, un_buf); 2611558Srgrimes ddp->dd_ddate = unctime(un_buf); 2621558Srgrimes if (ddp->dd_ddate < 0) 2631558Srgrimes return(-1); 2641558Srgrimes return(0); 2651558Srgrimes} 266