This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zerofree.c
195 lines (167 loc) · 4.08 KB
/
zerofree.c
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
/*
* zerofree - a tool to zero free blocks in an ext2 filesystem
*
* Copyright (C) 2004-2012 R M Yorston
*
* This file may be redistributed under the terms of the GNU General Public
* License, version 2.
*
* Changes:
*
* 2010-10-17 Allow non-zero fill value. Patch from Jacob Nevins.
* 2007-08-12 Allow use on filesystems mounted read-only. Patch from
* Jan Krämer.
*/
#include <ext2fs/ext2fs.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define USAGE "usage: %s [-n] [-v] [-d] [-f fillval] filesystem\n"
int main(int argc, char **argv)
{
errcode_t ret;
int flags;
int superblock = 0;
int open_flags = EXT2_FLAG_RW;
int blocksize = 0;
ext2_filsys fs = NULL;
unsigned long blk;
unsigned char *buf;
unsigned char *empty;
int i, c;
unsigned int free, modified;
double percent;
int old_percent;
unsigned int fillval = 0;
int verbose = 0;
int dryrun = 0;
int discard = 0;
while ( (c=getopt(argc, argv, "nvdf:")) != -1 ) {
switch (c) {
case 'n' :
dryrun = 1;
break;
case 'v' :
verbose = 1;
break;
case 'd':
discard = 1;
break;
case 'f' :
{
char *endptr;
fillval = strtol(optarg, &endptr, 0);
if ( !*optarg || *endptr ) {
fprintf(stderr, "%s: invalid argument to -f\n", argv[0]);
return 1;
} else if ( fillval > 0xFFu ) {
fprintf(stderr, "%s: fill value must be 0-255\n", argv[0]);
return 1;
}
}
break;
default :
fprintf(stderr, USAGE, argv[0]);
return 1;
}
}
if ( argc != optind+1 ) {
fprintf(stderr, USAGE, argv[0]);
return 1;
}
ret = ext2fs_check_if_mounted(argv[optind], &flags);
if ( ret ) {
fprintf(stderr, "%s: failed to determine filesystem mount state %s\n",
argv[0], argv[optind]);
return 1;
}
if ( (flags & EXT2_MF_MOUNTED) && !(flags & EXT2_MF_READONLY) ) {
fprintf(stderr, "%s: filesystem %s is mounted rw\n",
argv[0], argv[optind]);
return 1;
}
ret = ext2fs_open(argv[optind], open_flags, superblock, blocksize,
unix_io_manager, &fs);
if ( ret ) {
fprintf(stderr, "%s: failed to open filesystem %s\n",
argv[0], argv[optind]);
return 1;
}
empty = (unsigned char *)malloc(fs->blocksize);
buf = (unsigned char *)malloc(fs->blocksize);
if ( empty == NULL || buf == NULL ) {
fprintf(stderr, "%s: out of memory (surely not?)\n", argv[0]);
return 1;
}
memset(empty, fillval, fs->blocksize);
ret = ext2fs_read_inode_bitmap(fs);
if ( ret ) {
fprintf(stderr, "%s: error while reading inode bitmap\n", argv[0]);
return 1;
}
ret = ext2fs_read_block_bitmap(fs);
if ( ret ) {
fprintf(stderr, "%s: error while reading block bitmap\n", argv[0]);
return 1;
}
free = modified = 0;
percent = 0.0;
old_percent = -1;
if ( verbose ) {
fprintf(stderr, "\r%4.1f%%", percent);
}
for ( blk=fs->super->s_first_data_block;
blk < fs->super->s_blocks_count; blk++ ) {
if ( ext2fs_test_block_bitmap(fs->block_map, blk) ) {
continue;
}
++free;
percent = 100.0 * (double)free/
(double)fs->super->s_free_blocks_count;
if ( verbose && (int)(percent*10) != old_percent ) {
fprintf(stderr, "\r%4.1f%%", percent);
old_percent = (int)(percent*10);
}
if (!discard) {
ret = io_channel_read_blk(fs->io, blk, 1, buf);
if ( ret ) {
fprintf(stderr, "%s: error while reading block\n", argv[0]);
return 1;
}
for ( i=0; i < fs->blocksize; ++i ) {
if ( buf[i] != fillval ) {
break;
}
}
if ( i == fs->blocksize ) {
continue;
}
}
++modified;
if ( !dryrun ) {
if (!discard) {
ret = io_channel_write_blk(fs->io, blk, 1, empty);
if ( ret ) {
fprintf(stderr, "%s: error while writing block\n", argv[0]);
return 1;
}
} else { /* discard */
ret = io_channel_discard(fs->io, blk, 1);
if ( ret ) {
fprintf(stderr, "%s: error while discarding block\n", argv[0]);
return 1;
}
}
}
}
if ( verbose ) {
printf("\r%u/%u/%u\n", modified, free, fs->super->s_blocks_count);
}
ret = ext2fs_close(fs);
if ( ret ) {
fprintf(stderr, "%s: error while closing filesystem\n", argv[0]);
return 1;
}
return 0;
}