1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*
* cook - file construction tool
* Copyright (C) 1994, 1997, 1999 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate fingerprints
*/
#include <ac/errno.h>
#include <ac/fcntl.h>
#include <ac/unistd.h>
#include <ac/dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fp.h>
#include <str_list.h>
#include <mem.h>
fingerprint_ty *
fingerprint_new(mp)
fingerprint_methods_ty *mp;
{
fingerprint_ty *fp;
fp = mem_alloc(mp->size);
fp->method = mp;
mp->constructor(fp);
return fp;
}
void
fingerprint_delete(fp)
fingerprint_ty *fp;
{
fp->method->destructor(fp);
mem_free(fp);
}
static int fingerprint_scan_dir _((fingerprint_ty *, char *));
static int
fingerprint_scan_dir(fp, fn)
fingerprint_ty *fp;
char *fn;
{
DIR *dp;
string_list_ty wl;
string_ty *s;
size_t j;
/*
* Read the directory contents, and remember the names.
* Don't hash it just yet.
*/
dp = opendir(fn);
if (!dp)
return -1;
string_list_constructor(&wl);
for (;;)
{
struct dirent *dep;
dep = readdir(dp);
if (!dep)
break;
s = str_from_c(dep->d_name);
string_list_append(&wl, s);
str_free(s);
}
closedir(dp);
/*
* Sort the names before we hash anything. That way, even if
* the files in the directory get re-arranged, the hash stays
* the same. This is common when a file is deleted, and then
* re-created in identical form.
*/
string_list_sort(&wl);
/*
* Now hash the strings in the list. This takes advantage of
* the fact that all the strings are NUL terminated to introduce
* a unique separator.
*/
for (j = 0; j < wl.nstrings; ++j)
{
s = wl.string[j];
fingerprint_addn(fp, s->str_text, s->str_length + 1);
}
string_list_destructor(&wl);
return 0;
}
static int fingerprint_scan _((fingerprint_ty *, char *));
static int
fingerprint_scan(fp, fn)
fingerprint_ty *fp;
char *fn;
{
int fd;
unsigned char ibuf[1024];
long nbytes;
int err;
if (fn)
{
struct stat st;
/*
* The most common case is to obtain a fingerprint for
* a file. In the rare case that it is a directory,
* we need to do things differently.
*/
if (stat(fn, &st) == 0 && S_ISDIR(st.st_mode))
{
return fingerprint_scan_dir(fp, fn);
}
/*
* Assume it's a normal file.
* (Slow things will happen if, say, it's a tape device.)
*/
fd = open(fn, O_RDONLY|O_BINARY, 0666);
if (fd < 0)
return -1;
}
else
{
fd = 0;
}
for (;;)
{
nbytes = read(fd, ibuf, sizeof(ibuf));
if (nbytes < 0)
{
if (fn)
{
err = errno;
close(fd);
errno = err;
}
return -1;
}
if (nbytes == 0)
break;
fingerprint_addn(fp, ibuf, nbytes);
}
if (fn && close(fd) < 0)
return -1;
return 0;
}
int
fingerprint_file_hash(fp, fn, obuf)
fingerprint_ty *fp;
char *fn;
unsigned char *obuf;
{
if (fingerprint_scan(fp, fn))
return -1;
return fingerprint_hash(fp, obuf);
}
int
fingerprint_file_sum(fp, fn, obuf)
fingerprint_ty *fp;
char *fn;
char *obuf;
{
if (fingerprint_scan(fp, fn))
return -1;
fingerprint_sum(fp, obuf);
return 0;
}
void
fingerprint_add(p, c)
fingerprint_ty *p;
int c;
{
unsigned char buf;
buf = c;
fingerprint_addn(p, &buf, 1);
}
|