summaryrefslogtreecommitdiffstats
path: root/sbin/recoverdisk/recoverdisk.c
blob: d30055a2f1b5eff09599eae6f0509827d6dedd92 (plain)
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
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * $FreeBSD$
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/queue.h>
#include <sys/disk.h>
#include <sys/stat.h>

#define BIGSIZE		(1024 * 1024)
#define MEDIUMSIZE	(64 * 1024)
#define MINSIZE		(512)

struct lump {
	off_t			start;
	off_t			len;
	int			state;
	TAILQ_ENTRY(lump)	list;
};

static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);


static void
new_lump(off_t start, off_t len, int state)
{
	struct lump *lp;

	lp = malloc(sizeof *lp);
	if (lp == NULL)
		err(1, "Malloc failed");
	lp->start = start;
	lp->len = len;
	lp->state = state;
	TAILQ_INSERT_TAIL(&lumps, lp, list);
}

int
main(int argc, const char **argv)
{
	int fdr, fdw;
	struct lump *lp;
	off_t 	t, d;
	size_t i, j;
	int error, flags;
	u_char *buf;
	u_int sectorsize, minsize;
	time_t t1, t2;
	struct stat sb;


	if (argc < 2)
		errx(1, "Usage: %s source-drive [destination]", argv[0]);

	buf = malloc(BIGSIZE);
	if (buf == NULL)
		err(1, "Cannot allocate %d bytes buffer", BIGSIZE);
	fdr = open(argv[1], O_RDONLY);
	if (fdr < 0)
		err(1, "Cannot open read descriptor %s", argv[1]);

	error = fstat(fdr, &sb);
	if (error < 0)
		err(1, "fstat failed");
	flags = O_WRONLY;
	if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
		error = ioctl(fdr, DIOCGSECTORSIZE, &sectorsize);
		if (error < 0)
			err(1, "DIOCGSECTORSIZE failed");
		minsize = sectorsize;

		error = ioctl(fdr, DIOCGMEDIASIZE, &t);
		if (error < 0)
			err(1, "DIOCGMEDIASIZE failed");
	} else {
		sectorsize = 1;
		t = sb.st_size;
		minsize = MINSIZE;
		flags |= O_CREAT | O_TRUNC;
	}

	if (argc > 2) {
		fdw = open(argv[2], flags, DEFFILEMODE);
		if (fdw < 0)
			err(1, "Cannot open write descriptor %s", argv[2]);
	} else {
		fdw = -1;
	}

	new_lump(0, t, 0);
	d = 0;

	t1 = 0;
	for (;;) {
		lp = TAILQ_FIRST(&lumps);
		if (lp == NULL)
			break;
		TAILQ_REMOVE(&lumps, lp, list);
		while (lp->len > 0) {
			i = BIGSIZE;
			if (lp->len < BIGSIZE)
				i = lp->len;
			if (lp->state == 1)
				i = MEDIUMSIZE;
			if (lp->state > 1)
				i = minsize;
			time(&t2);
			if (t1 != t2 || lp->len < BIGSIZE) {
				printf("\r%13jd %7zu %13jd %3d %13jd %13jd %.8f",
				    (intmax_t)lp->start,
				    i, 
				    (intmax_t)lp->len,
				    lp->state,
				    (intmax_t)d,
				    (intmax_t)(t - d),
				    (double)d/(double)t);
				t1 = t2;
			}
			if (i == 0) {
				errx(1, "BOGUS i %10jd", (intmax_t)i);
			}
			fflush(stdout);
			j = pread(fdr, buf, i, lp->start);
			if (j == i) {
				d += i;
				if (fdw >= 0)
					j = pwrite(fdw, buf, i, lp->start);
				else
					j = i;
				if (j != i)
					printf("\nWrite error at %jd/%zu\n",
					    lp->start, i);
				lp->start += i;
				lp->len -= i;
				continue;
			}
			printf("\n%jd %zu failed %d\n", lp->start, i, errno);
			new_lump(lp->start, i, lp->state + 1);
			lp->start += i;
			lp->len -= i;
		}
		free(lp);
	}
	printf("\nCompleted\n");
	exit (0);
}

OpenPOWER on IntegriCloud