2 * Copyright (c) 2010 Doug Rabson
3 * Copyright (c) 2011 Andriy Gapon
4 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/queue.h>
47 pager_output(const char *line)
50 fprintf(stderr, "%s", line);
54 #define printf(...) fprintf(stderr, __VA_ARGS__)
60 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
62 int fd = *(int *)priv;
64 if (pread(fd, buf, bytes, off) != bytes)
70 zfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off)
72 const znode_phys_t *zp = (const znode_phys_t *) dn->dn_bonus;
77 if (off + n > zp->zp_size)
78 n = zp->zp_size - off;
80 rc = dnode_read(spa, dn, off, buf, n);
88 main(int argc, char** argv)
90 char buf[512], hash[33];
93 struct zfsmount zfsmnt;
101 int i, failures, *fd;
105 static char *av[] = {
112 "/boot/kernel/kernel",
115 argc = sizeof(av) / sizeof(av[0]) - 1;
118 for (i = 1; i < argc; i++) {
119 if (strcmp(argv[i], "-") == 0)
122 fd = malloc(sizeof(fd[0]) * (i - 1));
124 errx(1, "Unable to allocate memory.");
125 for (i = 1; i < argc; i++) {
126 if (strcmp(argv[i], "-") == 0)
128 fd[i - 1] = open(argv[i], O_RDONLY);
129 if (fd[i - 1] == -1) {
130 warn("open(%s) failed", argv[i]);
133 if (vdev_probe(vdev_read, &fd[i - 1], NULL) != 0) {
134 warnx("vdev_probe(%s) failed", argv[i]);
139 spa = STAILQ_FIRST(&zfs_pools);
141 fprintf(stderr, "no pools\n");
145 if (zfs_spa_init(spa)) {
146 fprintf(stderr, "can't init pool\n");
154 if (zfs_get_root(spa, &rootobj)) {
155 fprintf(stderr, "can't get root\n");
159 if (zfs_mount(spa, rootobj, &zfsmnt)) {
161 if (zfs_mount(spa, 0, &zfsmnt)) {
162 fprintf(stderr, "can't mount\n");
168 for (++i, failures = 0; i < argc; i++) {
169 if (zfs_lookup(&zfsmnt, argv[i], &dn)) {
170 fprintf(stderr, "%s: can't lookup\n", argv[i]);
175 if (zfs_dnode_stat(spa, &dn, &sb)) {
176 fprintf(stderr, "%s: can't stat\n", argv[i]);
184 n = sb.st_size - off;
185 n = n > sizeof(buf) ? sizeof(buf) : n;
186 n = zfs_read(spa, &dn, buf, n, off);
188 fprintf(stderr, "%s: zfs_read failed\n",
193 MD5Update(&ctx, buf, n);
195 } while (off < sb.st_size);
196 if (off < sb.st_size)
199 printf("%s %s\n", hash, argv[i]);
202 return (failures == 0 ? 0 : 1);