summaryrefslogtreecommitdiff
path: root/source/a/zerofree/sparsify.c
blob: 4c52e0075a0a05179d1256862e7f86d288dc3679 (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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * sparsify - a tool to make files on an ext2 filesystem sparse
 *
 * Copyright (C) 2004-2012 R M Yorston
 *
 * This file may be redistributed under the terms of the GNU General Public
 * License, version 2.
 */
#include <ext2fs/ext2fs.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define USAGE "usage: %s [-n] [-v] filesystem filename ...\n"

/* initially assume pre-ext4 API version */
#define API 140

#if defined(BLOCK_FLAG_READ_ONLY)
#undef API
#define API 141
#endif

#if defined(EXT2_FLAG_64BITS)
#undef API
#define API 142
#endif

struct process_data {
	unsigned char *buf;
	int verbose;
	int dryrun;
	blk_t count;
	blk_t blocks;
	blk_t total_blocks;
	int old_percent;
};

static int process(ext2_filsys fs, blk_t *blocknr, e2_blkcnt_t blockcnt,
					blk_t ref_block, int ref_offset, void *priv)
{
	struct process_data *p;
	errcode_t errcode;
	int i, group;
	int ret = 0;

	p = (struct process_data *)priv;

	p->blocks++;
	if ( blockcnt >= 0 ) {
		errcode = io_channel_read_blk(fs->io, *blocknr, 1, p->buf);
		if ( errcode ) {
			return BLOCK_ABORT;
		}

		for ( i=0; i < fs->blocksize; ++i ) {
			if ( p->buf[i] ) {
				break;
			}
		}

		if ( i == fs->blocksize ) {
			p->count++;

			if ( !p->dryrun ) {
				ext2fs_unmark_block_bitmap(fs->block_map, *blocknr);
				group = ext2fs_group_of_blk(fs, *blocknr);
#if API >= 142
				ext2fs_bg_free_blocks_count_set(fs, group,
							ext2fs_bg_free_blocks_count(fs, group)+1);
				ext2fs_free_blocks_count_add(fs->super, (blk64_t)1);
#else
				fs->group_desc[group].bg_free_blocks_count++;
				fs->super->s_free_blocks_count++;
#endif
#if API >= 141
				ext2fs_group_desc_csum_set(fs, group);
#endif
				*blocknr = 0;
				ret = BLOCK_CHANGED;
			}
		}

		if ( p->verbose ) {
			double percent;

			percent = 100.0 * (double)p->blocks/(double)p->total_blocks;

			if ( (int)(percent*10) != p->old_percent ) {
				fprintf(stderr, "\r%4.1f%%", percent);
				p->old_percent = (int)(percent*10);
			}
		}
	}

	return ret;
}

int main(int argc, char **argv)
{
	int verbose = 0;
	int dryrun = 0;
	errcode_t ret;
	int flags;
	int superblock = 0;
	int open_flags = EXT2_FLAG_RW;
	int iter_flags = 0;
	int blocksize = 0;
	ext2_filsys fs = NULL;
	struct ext2_inode inode;
	ext2_ino_t root, cwd, inum;
	int i, c;
	struct process_data pdata;

	while ( (c=getopt(argc, argv, "nv")) != -1 ) {
		switch (c) {
		case 'n' :
			dryrun = 1;
#if defined(BLOCK_FLAG_READ_ONLY)
			iter_flags |= BLOCK_FLAG_READ_ONLY;
#endif
			break;
		case 'v' :
			verbose = 1;
			break;
		default :
			fprintf(stderr, USAGE, argv[0]);
			return 1;
		}
	}

	if ( argc < optind+2 ) {
		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 ) {
		fprintf(stderr, "%s: filesystem %s is mounted\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;
	}

	pdata.buf = (unsigned char *)malloc(fs->blocksize);
	if ( pdata.buf == NULL ) {
		fprintf(stderr, "%s: out of memory (surely not?)\n", argv[0]);
		return 1;
	}

	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;
	}

	root = cwd = EXT2_ROOT_INO;

	for ( i=optind+1; i<argc; ++i ) {
		ret = ext2fs_namei(fs, root, cwd, argv[i], &inum);
		if ( ret ) {
			fprintf(stderr, "%s: failed to find file %s\n", argv[0], argv[i]);
			continue;
		}

		ret = ext2fs_read_inode(fs, inum, &inode);
		if ( ret ) {
			fprintf(stderr, "%s: failed to open inode %d\n", argv[0], inum);
			continue;
		}

		if ( !ext2fs_inode_has_valid_blocks(&inode) ) {
			fprintf(stderr, "%s: file %s has no valid blocks\n", argv[0],
					argv[i]);
			continue;
		}

#if defined(EXT4_EXTENTS_FL) && API < 141
		if ( inode.i_flags & EXT4_EXTENTS_FL ) {
			fprintf(stderr, "%s: unable to process %s, it uses extents\n",
					argv[0], argv[i]);
			continue;
		}
#endif

#if defined(EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && defined(EXT4_HUGE_FILE_FL)
		if ( (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
				&& (inode.i_flags & EXT4_HUGE_FILE_FL) ) {
			fprintf(stderr, "%s: unable to process %s, it's huge\n",
					argv[0], argv[i]);
			continue;
		}
#endif

		if ( verbose ) {
			printf("processing %s\n", argv[i]);
		}

		pdata.verbose = verbose;
		pdata.dryrun = dryrun;
		pdata.count = pdata.blocks = 0;
		pdata.total_blocks = inode.i_blocks/(fs->blocksize >> 9);
		pdata.old_percent = 1000;
		ret = ext2fs_block_iterate2(fs, inum, iter_flags, NULL,
				process, &pdata);
		if ( ret ) {
			fprintf(stderr, "%s: failed to process file %s\n", argv[0],
					argv[i]);
			continue;
		}

		if ( pdata.count && !dryrun ) {
			ext2fs_mark_bb_dirty(fs);
			ext2fs_mark_super_dirty(fs);

			ret = ext2fs_read_inode(fs, inum, &inode);
			if ( ret ) {
				fprintf(stderr, "%s: failed to open inode (%s)\n", argv[0],
						argv[i]);
				continue;
			}

#if API >= 141
			ret = ext2fs_iblk_sub_blocks(fs, &inode, (blk64_t)pdata.count);
			if ( ret ) {
				fprintf(stderr, "%s: failed to update block count (%s)\n",
						argv[0], argv[i]);
				continue;
			}
#else
			inode.i_blocks -= pdata.count * (fs->blocksize >> 9);
#endif

			ret = ext2fs_write_inode(fs, inum, &inode);
			if ( ret ) {
				fprintf(stderr, "%s: failed to write inode (%s)\n",
						argv[0], argv[i]);
				continue;
			}
		}

		if ( verbose ) {
			printf("\r%d/%d/%d %s\n", pdata.count, pdata.blocks,
					pdata.total_blocks, argv[i]);
		}
	}

	ret = ext2fs_close(fs);
	if ( ret ) {
		fprintf(stderr, "%s: error while closing filesystem\n", argv[0]);
		return 1;
	}

	return 0;
}