summaryrefslogtreecommitdiff
path: root/debug/clearfat/clearfat.c
blob: 69bc357b49f594a1c2458d0b461f7b37ff6fdee3 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
    clear_fat - a tool to clear unused space (for testing purposes)
    Copyright (C) 2000, 2007-2009 Free Software Foundation, Inc.

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <getopt.h>
#include "closeout.h"
#include "configmake.h"
#include "error.h"
#include "long-options.h"
#include "progname.h"
#include "xstrtol.h"

#include "../../libparted/fs/fat/fat.h"

#include <locale.h>

/* Take care of NLS matters.  */

#include "gettext.h"
#if ! ENABLE_NLS
# undef textdomain
# define textdomain(Domainname) /* empty */
# undef bindtextdomain
# define bindtextdomain(Domainname, Dirname) /* empty */
#endif

#undef _
#define _(msgid) gettext (msgid)

#ifndef DISCOVER_ONLY

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "clearfat"

#define AUTHORS \
  "<http://parted.alioth.debian.org/cgi-bin/trac.cgi/browser/AUTHORS>"

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("\
Usage: %s [OPTION]\n\
  or:  %s DEVICE MINOR\n"), PROGRAM_NAME, PROGRAM_NAME);
      fputs (_("\
Clear unused space on a FAT partition (a GNU Parted testing tool).\n\
\n\
"), stdout);
      fputs (_("      --help     display this help and exit\n"), stdout);
      fputs (_("      --version  output version information and exit\n"),
             stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}

#define CLEAR_BUFFER_SIZE		(1024 * 1024)
#define CLEAR_BUFFER_SECTORS		(CLEAR_BUFFER_SIZE/512)

static char buffer [CLEAR_BUFFER_SIZE];

/* generic clearing code ***************************************************/

static int
_clear_sectors (PedGeometry* geom, PedSector start, PedSector count)
{
	PedSector		pos;
	PedSector		to_go = count;

	for (pos = start;
	     pos < start + count;
	     pos += CLEAR_BUFFER_SECTORS, to_go -= CLEAR_BUFFER_SECTORS) {
		if (!ped_geometry_write (geom, buffer, start,
					 PED_MIN (CLEAR_BUFFER_SECTORS, to_go)))
			return 0;
	}

	return 1;
}

static int
_clear_sector_range (PedGeometry* geom, PedSector start, PedSector end)
{
	return _clear_sectors (geom, start, end - start + 1);
}

static int
_clear_sector (PedGeometry* geom, PedSector sector)
{
	return _clear_sectors (geom, sector, 1);
}

static int
_clear_partial_sector (PedGeometry* geom, PedSector sector,
		       int offset, int count)
{
	if (!ped_geometry_read (geom, buffer, sector, 1))
		goto error;
	memset (buffer + offset, 0, count);
	if (!ped_geometry_write (geom, buffer, sector, 1))
		goto error;

	memset (buffer, 0, 512);
	return 1;

error:
	memset (buffer, 0, 512);
	return 0;
}

static int
_clear_partial_range (PedGeometry* geom, PedSector sector, int start, int end)
{
	return _clear_partial_sector (geom, sector, start, end - start + 1);
}

static int
_clear_clusters (PedFileSystem* fs, FatCluster start, FatCluster count)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	return _clear_sectors (fs->geom, fat_cluster_to_sector(fs, start),
			       count * fs_info->cluster_sectors);
}

/* FAT code ******************************************************************/

static void
_clear_before_fat (PedFileSystem* fs)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	PedSector	sector;

	for (sector = 1; sector < fs_info->fat_offset; sector++) {
		if (sector == fs_info->info_sector_offset)
			continue;
		if (sector == fs_info->boot_sector_backup_offset)
			continue;
		_clear_sector (fs->geom, sector);
	}
}

static int
_calc_fat_entry_offset (PedFileSystem* fs, FatCluster cluster)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	switch (fs_info->fat_type) {
                case FAT_TYPE_FAT12:
                        PED_ASSERT (0, (void) 0);
                        break;

		case FAT_TYPE_FAT16:
			return cluster * 2;

		case FAT_TYPE_FAT32:
			return cluster * 4;
	}
	return 0;
}

static void
_clear_unused_fats (PedFileSystem* fs)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	PedSector	table_start;
	int		table_num;
	int		last_active_offset;
	PedSector	last_active_sector;
	int		last_active_sector_offset;

	last_active_offset
		= _calc_fat_entry_offset (fs, fs_info->fat->cluster_count);
	last_active_sector = last_active_offset / 512;
	last_active_sector_offset = last_active_offset % 512 + 4;

	for (table_num = 0; table_num < fs_info->fat_table_count; table_num++) {
		table_start = fs_info->fat_offset
			      + table_num * fs_info->fat_sectors;

		if (last_active_sector_offset < 512) {
			_clear_partial_range (
				fs->geom,
				table_start + last_active_sector,
				last_active_sector_offset,
				512);
		}

		if (last_active_sector < fs_info->fat_sectors - 2) {
			_clear_sector_range (
				fs->geom,
				table_start + last_active_sector + 1,
				table_start + fs_info->fat_sectors - 1);
		}
	}
}

static int
_clear_unused_clusters (PedFileSystem* fs)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	FatCluster	cluster;
	FatCluster	run_start = 0; /* shut gcc up! */
	FatCluster	run_length = 0;

	for (cluster = 2; cluster < fs_info->cluster_count + 2; cluster++) {
		if (fat_table_is_available (fs_info->fat, cluster)) {
			if (!run_length) {
				run_start = cluster;
				run_length = 1;
			} else {
				run_length++;
			}
		} else {
			if (run_length)
				_clear_clusters (fs, run_start, run_length);
			run_length = 0;
		}
	}

	if (run_length)
		_clear_clusters (fs, run_start, run_length);

	return 1;
}

static void
_clear_unused_fat (PedFileSystem* fs)
{
	memset (buffer, 0, CLEAR_BUFFER_SIZE);

	_clear_before_fat (fs);
	_clear_unused_fats (fs);
	_clear_unused_clusters (fs);
}

/* bureaucracy ***************************************************************/

int
main (int argc, char* argv[])
{
	PedDevice*		dev;
	PedDisk*		disk;
	PedPartition*		part;
	PedFileSystem*		fs;

        set_program_name (argv[0]);
        setlocale (LC_ALL, "");
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);

	atexit (close_stdout);

	parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                            usage, AUTHORS, (char const *) NULL);
	if (getopt_long (argc, argv, "", NULL, NULL) != -1)
	  usage (EXIT_FAILURE);

	if (argc - optind < 2)
          {
            error (0, 0, _("too few arguments"));
            usage (EXIT_FAILURE);
          }
	if (2 < argc - optind)
          {
            error (0, 0, _("too many arguments"));
            usage (EXIT_FAILURE);
          }

	unsigned long minor_dev_number;
	if (xstrtoul (argv[2], NULL, 10, &minor_dev_number, NULL)
	    || INT_MAX < minor_dev_number)
	  {
	    error (0, 0, _("invalid minor device number: %s"), argv[2]);
            usage (EXIT_FAILURE);
	  }

	dev = ped_device_get (argv [1]);
	if (!dev)
		goto error;
	if (!ped_device_open (dev))
		goto error;

	disk = ped_disk_new (dev);
	if (!disk)
		goto error_close_dev;

	part = ped_disk_get_partition (disk, minor_dev_number);
	if (!part) {
		printf ("Couldn't find partition `%s'\n", argv[2]);
		goto error_destroy_disk;
	}

	fs = ped_file_system_open (&part->geom);
	if (!fs)
		goto error_destroy_disk;

	if (strncmp (fs->type->name, "fat", 3)) {
		printf ("Not a FAT file system!\n");
		goto error_close_fs;
	}

	_clear_unused_fat (fs);

	ped_file_system_close (fs);
	ped_disk_destroy (disk);
	ped_device_close (dev);
	return 0;

error_close_fs:
	ped_file_system_close (fs);
error_destroy_disk:
	ped_disk_destroy (disk);
error_close_dev:
	ped_device_close (dev);
error:
	return 1;
}

#else /* DISCOVER_ONLY */

/* hack! */
int
main()
{
	printf ("You must compile libparted with full read/write support\n");
	return 1;
}

#endif /* DISCOVER_ONLY */