Add -D flag which computes the digest of each regular files in the archive

using specified algorithm (currently md5, rmd160 or sha1) and write them
to standard output.
This commit is contained in:
enami 2004-07-27 02:17:06 +00:00
parent 0ac1c38100
commit 5d75faa43f
5 changed files with 114 additions and 15 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.9 2003/08/07 10:04:37 agc Exp $ */
/* $NetBSD: extern.h,v 1.10 2004/07/27 02:17:06 enami Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -45,6 +45,8 @@ void createlinks __P((void));
long deletefile __P((char *, ino_t, int));
void deleteino __P((ino_t));
void delwhiteout __P((struct entry *));
const struct digest_desc *
digest_lookup __P((const char *));
ino_t dirlookup __P((const char *));
void dumpsymtable __P((char *, int32_t));
void extractdirs __P((int));

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.23 2003/08/07 10:04:37 agc Exp $ */
/* $NetBSD: main.c,v 1.24 2004/07/27 02:17:06 enami Exp $ */
/*
* Copyright (c) 1983, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
#if 0
static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: main.c,v 1.23 2003/08/07 10:04:37 agc Exp $");
__RCSID("$NetBSD: main.c,v 1.24 2004/07/27 02:17:06 enami Exp $");
#endif
#endif /* not lint */
@ -63,7 +63,7 @@ __RCSID("$NetBSD: main.c,v 1.23 2003/08/07 10:04:37 agc Exp $");
#include "extern.h"
int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
int hflag = 1, mflag = 1, Nflag = 0;
int hflag = 1, mflag = 1, Dflag = 0, Nflag = 0;
char command = '\0';
int32_t dumpnum = 1;
int32_t volno = 0;
@ -100,7 +100,7 @@ main(argc, argv)
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = _PATH_TMP;
obsolete(&argc, &argv);
while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tuvxy")) != -1)
while ((ch = getopt(argc, argv, "b:cD:df:himNRrs:tuvxy")) != -1)
switch(ch) {
case 'b':
/* Change default tape blocksize. */
@ -114,6 +114,12 @@ main(argc, argv)
case 'c':
cvtflag = 1;
break;
case 'D':
ddesc = digest_lookup(optarg);
if (ddesc == NULL)
err(1, "unknown digest algorithm: %s", optarg);
Dflag = 1;
break;
case 'd':
dflag = 1;
break;

View File

@ -1,4 +1,4 @@
.\" $NetBSD: restore.8,v 1.41 2003/08/07 10:04:37 agc Exp $
.\" $NetBSD: restore.8,v 1.42 2004/07/27 02:17:06 enami Exp $
.\"
.\" Copyright (c) 1985, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)restore.8 8.4 (Berkeley) 5/1/95
.\"
.Dd May 26, 2003
.Dd July 27, 2004
.Dt RESTORE 8
.Os
.Sh NAME
@ -41,18 +41,21 @@
.Fl i
.Op Fl cdhmuvyN
.Op Fl b Ar blocksize
.Op Fl D Ar algorithm
.Op Fl f Ar file
.Op Fl s Ar fileno
.Nm
.Fl R
.Op Fl cduvyN
.Op Fl b Ar blocksize
.Op Fl D Ar algorithm
.Op Fl f Ar file
.Op Fl s Ar fileno
.Nm
.Fl r
.Op Fl cduvyN
.Op Fl b Ar blocksize
.Op Fl D Ar algorithm
.Op Fl f Ar file
.Op Fl s Ar fileno
.Nm
@ -66,6 +69,7 @@
.Fl x
.Op Fl cdhmuvyN
.Op Fl b Ar blocksize
.Op Fl D Ar algorithm
.Op Fl f Ar file
.Op Fl s Ar fileno
.Op Ar
@ -277,6 +281,20 @@ The
.Fl c
flag disables this check, and only allows reading a dump in the old
format.
.It Fl D Ar algorithm
Computes the digest of each regular files using the
.Ar algorithm
and output to standard output.
The
.Ar algorithm
is one of
.Cm md5 ,
.Cm rmd160
or
.Cm sha1 .
This option doesn't imply
.Fl N
flag.
.It Fl d
Enable debugging.
.It Fl f Ar file

View File

@ -1,4 +1,4 @@
/* $NetBSD: restore.h,v 1.14 2003/08/07 10:04:38 agc Exp $ */
/* $NetBSD: restore.h,v 1.15 2004/07/27 02:17:06 enami Exp $ */
/*
* Copyright (c) 1983, 1993
@ -41,6 +41,7 @@
*/
extern int cvtflag; /* convert from old to new tape format */
extern int bflag; /* set input block size */
extern int Dflag; /* output digest of files */
extern int dflag; /* print out debugging info */
extern int hflag; /* restore hierarchies */
extern int mflag; /* restore by name instead of inode number */
@ -66,6 +67,17 @@ extern char *tmpdir; /* where to store temporary files */
extern int oldinofmt; /* reading tape with old format inodes */
extern int Bcvt; /* need byte swapping on inodes and dirs */
struct digest_desc {
const char *dd_name;
void (*dd_init)(void *);
void (*dd_update)(void *, const u_char *, u_int);
char *(*dd_end)(void *, void *);
};
extern const struct digest_desc *ddesc;
extern const struct digest_desc md5_desc;
extern const struct digest_desc rmd160_desc;
extern const struct digest_desc sha1_desc;
/*
* Each file in the file system is described by one of these entries
*/

View File

@ -1,4 +1,4 @@
/* $NetBSD: tape.c,v 1.49 2003/08/07 10:04:38 agc Exp $ */
/* $NetBSD: tape.c,v 1.50 2004/07/27 02:17:06 enami Exp $ */
/*
* Copyright (c) 1983, 1993
@ -39,7 +39,7 @@
#if 0
static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
#else
__RCSID("$NetBSD: tape.c,v 1.49 2003/08/07 10:04:38 agc Exp $");
__RCSID("$NetBSD: tape.c,v 1.50 2004/07/27 02:17:06 enami Exp $");
#endif
#endif /* not lint */
@ -61,6 +61,10 @@ __RCSID("$NetBSD: tape.c,v 1.49 2003/08/07 10:04:38 agc Exp $");
#include <time.h>
#include <unistd.h>
#include <md5.h>
#include <rmd160.h>
#include <sha1.h>
#include "restore.h"
#include "extern.h"
@ -89,6 +93,35 @@ static int pathlen;
int oldinofmt; /* old inode format conversion required */
int Bcvt; /* Swap Bytes (for CCI or sun) */
const struct digest_desc *ddesc;
const struct digest_desc digest_descs[] = {
{ "MD5",
(void (*)(void *))MD5Init,
(void (*)(void *, const u_char *, u_int))MD5Update,
(char *(*)(void *, void *))MD5End, },
{ "SHA1",
(void (*)(void *))SHA1Init,
(void (*)(void *, const u_char *, u_int))SHA1Update,
(char *(*)(void *, void *))SHA1End, },
{ "RMD160",
(void (*)(void *))RMD160Init,
(void (*)(void *, const u_char *, u_int))RMD160Update,
(char *(*)(void *, void *))RMD160End, },
{ NULL },
};
static union digest_context {
MD5_CTX dc_md5;
SHA1_CTX dc_sha1;
RMD160_CTX dc_rmd160;
} dcontext;
union digest_buffer {
char db_md5[32 + 1];
char db_sha1[40 + 1];
char db_rmd160[40 + 1];
};
#define FLUSHTAPEBUF() blkcnt = ntrec + 1
union u_ospcl {
@ -136,6 +169,19 @@ static void xtrskip __P((char *, long));
static void swap_header __P((struct s_spcl *));
static void swap_old_header __P((struct s_ospcl *));
const struct digest_desc *
digest_lookup(name)
const char *name;
{
const struct digest_desc *dd;
for (dd = digest_descs; dd->dd_name != NULL; dd++)
if (strcasecmp(dd->dd_name, name) == 0)
return (dd);
return (NULL);
}
/*
* Set up an input source
*/
@ -548,6 +594,7 @@ int
extractfile(name)
char *name;
{
union digest_buffer dbuffer;
int flags;
uid_t uid;
gid_t gid;
@ -670,20 +717,28 @@ extractfile(name)
case IFREG:
vprintf(stdout, "extract file %s\n", name);
if (Nflag) {
skipfile();
return (GOOD);
}
if (uflag)
(void) unlink(name);
if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
if (!Nflag && (ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
0600)) < 0) {
fprintf(stderr, "%s: cannot create file: %s\n",
name, strerror(errno));
skipfile();
return (FAIL);
}
if (Dflag)
(*ddesc->dd_init)(&dcontext);
getfile(xtrfile, xtrskip);
if (Dflag) {
(*ddesc->dd_end)(&dcontext, &dbuffer);
for (ep = lookupname(name); ep != NULL;
ep = ep->e_links)
fprintf(stdout, "%s (%s) = %s\n",
ddesc->dd_name, myname(ep),
(char *)&dbuffer);
}
if (Nflag)
return (GOOD);
if (setbirth)
(void) futimes(ofile, ctimep);
(void) futimes(ofile, mtimep);
@ -809,6 +864,8 @@ xtrfile(buf, size)
long size;
{
if (Dflag)
(*ddesc->dd_update)(&dcontext, buf, size);
if (Nflag)
return;
if (write(ofile, buf, (int) size) == -1) {
@ -829,6 +886,10 @@ xtrskip(buf, size)
long size;
{
if (Dflag)
(*ddesc->dd_update)(&dcontext, buf, size);
if (Nflag)
return;
if (lseek(ofile, size, SEEK_CUR) == -1) {
fprintf(stderr,
"seek error extracting inode %d, name %s\nlseek: %s\n",