File: blockdev-wipe.c

package info (click to toggle)
partman-crypto 135
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,272 kB
  • sloc: sh: 1,874; ansic: 174; makefile: 29
file content (184 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2006 David Härdeman <david@2gen.com>
 *
 * 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-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mount.h> /* for BLKGETSIZE definitions */
#include <getopt.h>
#include <string.h>
#include <time.h>

/* Progress indicator to output */
# define PROGRESS_INDICATOR "*\n"
/* How many progress indicators to output in total */
#define PROGRESS_PARTS 1000
/* How many bytes to write at a time (default) */
#define DEFAULT_WSIZE 4096
/* Debugging output */
#ifdef DEBUG
#define dprintf(x, ...) fprintf(stderr, x, ...)
#else
#define dprintf(x, ...)
#endif

static void die(const char *message, int perr)
{
	if (perr)
		perror(message);
	else
		fprintf(stderr, "Error: %s\n", message);
	exit(1);
}

static void usage(const char *message, const char *argv0)
{
	printf("%s: %s\n"
	       "Usage: %s [options] device\n"
	       "  -f=FILE\tread data to write from file instead of writing zero\n"
	       "  -s=NUM\twrite NUM bytes at a time (default %i)\n"
	       , argv0, message, argv0, DEFAULT_WSIZE);
	exit(1);
}

static unsigned long long dev_size(int fd)
{
	int ret;
	unsigned long long size;

	ret = ioctl(fd, BLKGETSIZE64, &size);
	if (ret < 0) {
		close(fd);
		die("failed to get device size", 1);
	}

	return size;
}

static int do_wipe(int source, int target, size_t wsize)
{
	unsigned long long size;
	unsigned long long done = 0;
	unsigned int previous_progress = 0;
	unsigned int progress = 0;
	time_t last_sync = 0;
	int i;
	ssize_t count;
	char *buf = calloc(wsize, 1);

	if (buf == NULL)
		die("buffer allocation failed", 1);

	size = dev_size(target);
	dprintf("Block size in bytes is %llu\n", size);
	/* From now on, try to make sure stdout is unbuffered */
	setbuf(stdout, NULL);

	while (done < size) {
		/* First read (if we have a source) */
		if (source) {
			count = read(source, buf, wsize);
			if (count != wsize)
				die("failed to read from source", 1);
		}

		/* Now write what we just read */
		count = write(target, buf, wsize);
		dprintf("Count is %zi\n", count);
		if (count < 0)
			die("failed to write to target", 1);

		/* Calculate progress */
		done += count;
		progress = ((done * PROGRESS_PARTS)/ size);
		dprintf("We just wrote %zi, done %llu\n", count, done);
		if (progress > previous_progress) {
			/* sync on every progress output, but at most once per second */
			time_t now = time(NULL);
			if (now != last_sync) {
				fdatasync(target);
				last_sync = now;
			}
			for (i = 0; i < progress - previous_progress; i++)
				printf(PROGRESS_INDICATOR);
			previous_progress = progress;
		}
	}

	free(buf);
	fdatasync(target);
	return 0;
}

int main(int argc, char **argv, char **envp)
{
	int target, source, ret;
	char *sourcefname = NULL;
	size_t wsize = DEFAULT_WSIZE;

	/* Parse options */
	while (1) {
		ret = getopt(argc, argv, "f:s:");
		if (ret == -1)
			break;
		switch(ret) {
			case 'f':
				sourcefname = optarg;
				break;
			case 's':
				wsize = (size_t)atol(optarg);
				if (wsize < 1)
					die("incorrect size", 0);
				break;
			default:
				die("unknown getopt failure", 0);
				break;
		}
	}
	if (argc - optind != 1)
		usage("you must specify one target device", argv[0]);

	/* Get target */
	target = open(argv[optind], O_WRONLY);
	if (target < 0)
		die("failed to open device", 1);

	/* Get source */
	if (sourcefname) {
		source = open(sourcefname, O_RDONLY);
		if (source < 0)
			die("failed to open source", 1);
	} else {
		source = 0;
	}

	/* Wipe device */
	ret = do_wipe(source, target, wsize);

	/* Clean up */
	close(source);
	close(target);
	if (ret)
		die("failed to wipe device", 0);
	return 0;
}