summaryrefslogtreecommitdiff
path: root/ntfsprogs/ntfscluster.c (plain)
blob: ff16a0f2c0bde0f747c47d3952f79129cb6ee7c5
1/**
2 * ntfscluster - Part of the Linux-NTFS project.
3 *
4 * Copyright (c) 2002-2003 Richard Russon
5 * Copyright (c) 2005 Anton Altaparmakov
6 * Copyright (c) 2005-2006 Szabolcs Szakacsits
7 *
8 * This utility will locate the owner of any given sector or cluster.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program (in the main directory of the Linux-NTFS
22 * distribution in the file COPYING); if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#include "config.h"
27
28#ifdef HAVE_STDIO_H
29#include <stdio.h>
30#endif
31#ifdef HAVE_GETOPT_H
32#include <getopt.h>
33#endif
34#ifdef HAVE_STDLIB_H
35#include <stdlib.h>
36#endif
37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
40#ifdef HAVE_LIMITS_H
41#include <limits.h>
42#endif
43
44#include "ntfscluster.h"
45#include "types.h"
46#include "attrib.h"
47#include "utils.h"
48#include "volume.h"
49#include "debug.h"
50#include "dir.h"
51#include "cluster.h"
52/* #include "version.h" */
53#include "logging.h"
54
55static const char *EXEC_NAME = "ntfscluster";
56static struct options opts;
57
58/**
59 * version - Print version information about the program
60 *
61 * Print a copyright statement and a brief description of the program.
62 *
63 * Return: none
64 */
65static void version(void)
66{
67 ntfs_log_info("\n%s v%s (libntfs-3g) - Find the owner of any given sector or "
68 "cluster.\n\n", EXEC_NAME, VERSION);
69 ntfs_log_info("Copyright (c) 2002-2003 Richard Russon\n");
70 ntfs_log_info("Copyright (c) 2005 Anton Altaparmakov\n");
71 ntfs_log_info("Copyright (c) 2005-2006 Szabolcs Szakacsits\n");
72 ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
73}
74
75/**
76 * usage - Print a list of the parameters to the program
77 *
78 * Print a list of the parameters and options for the program.
79 *
80 * Return: none
81 */
82static void usage(void)
83{
84 ntfs_log_info("\nUsage: %s [options] device\n"
85 " -i, --info Print information about the volume (default)\n"
86 "\n"
87 " -c, --cluster RANGE Look for objects in this range of clusters\n"
88 " -s, --sector RANGE Look for objects in this range of sectors\n"
89 " -I, --inode NUM Show information about this inode\n"
90 " -F, --filename NAME Show information about this file\n"
91 /* " -l, --last Find the last file on the volume\n" */
92 "\n"
93 " -f, --force Use less caution\n"
94 " -q, --quiet Less output\n"
95 " -v, --verbose More output\n"
96 " -V, --version Version information\n"
97 " -h, --help Print this help\n\n",
98 EXEC_NAME);
99 ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
100}
101
102/**
103 * parse_options - Read and validate the programs command line
104 *
105 * Read the command line, verify the syntax and parse the options.
106 * This function is very long, but quite simple.
107 *
108 * Return: 1 Success
109 * 0 Error, one or more problems
110 */
111static int parse_options(int argc, char **argv)
112{
113 static const char *sopt = "-c:F:fh?I:ilqs:vV";
114 static const struct option lopt[] = {
115 { "cluster", required_argument, NULL, 'c' },
116 { "filename", required_argument, NULL, 'F' },
117 { "force", no_argument, NULL, 'f' },
118 { "help", no_argument, NULL, 'h' },
119 { "info", no_argument, NULL, 'i' },
120 { "inode", required_argument, NULL, 'I' },
121 { "last", no_argument, NULL, 'l' },
122 { "quiet", no_argument, NULL, 'q' },
123 { "sector", required_argument, NULL, 's' },
124 { "verbose", no_argument, NULL, 'v' },
125 { "version", no_argument, NULL, 'V' },
126 { NULL, 0, NULL, 0 }
127 };
128
129 int c = -1;
130 int err = 0;
131 int ver = 0;
132 int help = 0;
133 int levels = 0;
134 char *end = NULL;
135
136 opterr = 0; /* We'll handle the errors, thank you. */
137
138 opts.action = act_none;
139 opts.range_begin = -1;
140 opts.range_end = -1;
141
142 while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
143 switch (c) {
144 case 1: /* A non-option argument */
145 if (!opts.device) {
146 opts.device = argv[optind-1];
147 } else {
148 opts.device = NULL;
149 err++;
150 }
151 break;
152
153 case 'c':
154 if ((opts.action == act_none) &&
155 (utils_parse_range(optarg, &opts.range_begin, &opts.range_end, FALSE)))
156 opts.action = act_cluster;
157 else
158 opts.action = act_error;
159 break;
160 case 'F':
161 if (opts.action == act_none) {
162 opts.action = act_file;
163 opts.filename = optarg;
164 } else {
165 opts.action = act_error;
166 }
167 break;
168 case 'f':
169 opts.force++;
170 break;
171 case 'h':
172 case '?':
173 if (strncmp (argv[optind-1], "--log-", 6) == 0) {
174 if (!ntfs_log_parse_option (argv[optind-1]))
175 err++;
176 break;
177 }
178 help++;
179 break;
180 case 'I':
181 if (opts.action == act_none) {
182 opts.action = act_inode;
183 opts.inode = strtol(optarg, &end, 0);
184 if (end && *end)
185 err++;
186 } else {
187 opts.action = act_error;
188 }
189 break;
190 case 'i':
191 if (opts.action == act_none)
192 opts.action = act_info;
193 else
194 opts.action = act_error;
195 break;
196 case 'l':
197 if (opts.action == act_none)
198 opts.action = act_last;
199 else
200 opts.action = act_error;
201 break;
202 case 'q':
203 opts.quiet++;
204 ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
205 break;
206 case 's':
207 if ((opts.action == act_none) &&
208 (utils_parse_range(optarg, &opts.range_begin, &opts.range_end, FALSE)))
209 opts.action = act_sector;
210 else
211 opts.action = act_error;
212 break;
213 case 'v':
214 opts.verbose++;
215 ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
216 break;
217 case 'V':
218 ver++;
219 break;
220 default:
221 if ((optopt == 'c') || (optopt == 's'))
222 ntfs_log_error("Option '%s' requires an argument.\n", argv[optind-1]);
223 else
224 ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
225 err++;
226 break;
227 }
228 }
229
230 /* Make sure we're in sync with the log levels */
231 levels = ntfs_log_get_levels();
232 if (levels & NTFS_LOG_LEVEL_VERBOSE)
233 opts.verbose++;
234 if (!(levels & NTFS_LOG_LEVEL_QUIET))
235 opts.quiet++;
236
237 if (help || ver) {
238 opts.quiet = 0;
239 } else {
240 if (opts.action == act_none)
241 opts.action = act_info;
242 if (opts.action == act_info)
243 opts.quiet = 0;
244
245 if (opts.device == NULL) {
246 if (argc > 1)
247 ntfs_log_error("You must specify exactly one device.\n");
248 err++;
249 }
250
251 if (opts.quiet && opts.verbose) {
252 ntfs_log_error("You may not use --quiet and --verbose at the same time.\n");
253 err++;
254 }
255
256 if (opts.action == act_error) {
257 ntfs_log_error("You may only specify one action: --info, --cluster, --sector or --last.\n");
258 err++;
259 } else if (opts.range_begin > opts.range_end) {
260 ntfs_log_error("The range must be in ascending order.\n");
261 err++;
262 }
263 }
264
265 if (ver)
266 version();
267 if (help || err)
268 usage();
269
270 return (!err && !help && !ver);
271}
272
273
274/**
275 * info
276 */
277static int info(ntfs_volume *vol)
278{
279 u64 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u;
280 int cb, sb, cps;
281 u64 uc = 0, mc = 0, fc = 0;
282
283 struct mft_search_ctx *m_ctx;
284 ntfs_attr_search_ctx *a_ctx;
285 runlist_element *rl;
286 ATTR_RECORD *rec;
287 int z;
288 int inuse = 0;
289
290 m_ctx = mft_get_search_ctx(vol);
291 m_ctx->flags_search = FEMR_IN_USE | FEMR_METADATA | FEMR_BASE_RECORD | FEMR_NOT_BASE_RECORD;
292 while (mft_next_record(m_ctx) == 0) {
293
294 if (!(m_ctx->flags_match & FEMR_IN_USE))
295 continue;
296
297 inuse++;
298
299 a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL);
300
301 while ((rec = find_attribute(AT_UNUSED, a_ctx))) {
302
303 if (!rec->non_resident)
304 continue;
305
306 rl = ntfs_mapping_pairs_decompress(vol, rec, NULL);
307
308 for (z = 0; rl[z].length > 0; z++)
309 {
310 if (rl[z].lcn >= 0) {
311 if (m_ctx->flags_match & FEMR_METADATA)
312 mc += rl[z].length;
313 else
314 uc += rl[z].length;
315 }
316
317 }
318
319 free(rl);
320 }
321
322 ntfs_attr_put_search_ctx(a_ctx);
323 }
324 mft_put_search_ctx(m_ctx);
325
326 cb = vol->cluster_size_bits;
327 sb = vol->sector_size_bits;
328 cps = cb - sb;
329
330 fc = vol->nr_clusters-mc-uc;
331 fc <<= cb;
332 mc <<= cb;
333 uc <<= cb;
334
335 a = vol->sector_size;
336 b = vol->cluster_size;
337 c = 1 << cps;
338 d = vol->nr_clusters << cb;
339 e = vol->nr_clusters;
340 f = vol->nr_clusters >> cps;
341 g = vol->mft_na->initialized_size >> vol->mft_record_size_bits;
342 h = inuse;
343 i = h * 100 / g;
344 j = fc;
345 k = fc >> sb;
346 l = fc >> cb;
347 m = fc * 100 / b / e;
348 n = uc;
349 o = uc >> sb;
350 p = uc >> cb;
351 q = uc * 100 / b / e;
352 r = mc;
353 s = mc >> sb;
354 t = mc >> cb;
355 u = mc * 100 / b / e;
356
357 ntfs_log_info("bytes per sector : %llu\n", (unsigned long long)a);
358 ntfs_log_info("bytes per cluster : %llu\n", (unsigned long long)b);
359 ntfs_log_info("sectors per cluster : %llu\n", (unsigned long long)c);
360 ntfs_log_info("bytes per volume : %llu\n", (unsigned long long)d);
361 ntfs_log_info("sectors per volume : %llu\n", (unsigned long long)e);
362 ntfs_log_info("clusters per volume : %llu\n", (unsigned long long)f);
363 ntfs_log_info("initialized mft records : %llu\n", (unsigned long long)g);
364 ntfs_log_info("mft records in use : %llu\n", (unsigned long long)h);
365 ntfs_log_info("mft records percentage : %llu\n", (unsigned long long)i);
366 ntfs_log_info("bytes of free space : %llu\n", (unsigned long long)j);
367 ntfs_log_info("sectors of free space : %llu\n", (unsigned long long)k);
368 ntfs_log_info("clusters of free space : %llu\n", (unsigned long long)l);
369 ntfs_log_info("percentage free space : %llu\n", (unsigned long long)m);
370 ntfs_log_info("bytes of user data : %llu\n", (unsigned long long)n);
371 ntfs_log_info("sectors of user data : %llu\n", (unsigned long long)o);
372 ntfs_log_info("clusters of user data : %llu\n", (unsigned long long)p);
373 ntfs_log_info("percentage user data : %llu\n", (unsigned long long)q);
374 ntfs_log_info("bytes of metadata : %llu\n", (unsigned long long)r);
375 ntfs_log_info("sectors of metadata : %llu\n", (unsigned long long)s);
376 ntfs_log_info("clusters of metadata : %llu\n", (unsigned long long)t);
377 ntfs_log_info("percentage metadata : %llu\n", (unsigned long long)u);
378
379 return 0;
380}
381
382/**
383 * dump_file
384 */
385static int dump_file(ntfs_volume *vol, ntfs_inode *ino)
386{
387 char buffer[1024];
388 ntfs_attr_search_ctx *ctx;
389 ATTR_RECORD *rec;
390 int i;
391 runlist *runs;
392
393 utils_inode_get_name(ino, buffer, sizeof(buffer));
394
395 ntfs_log_info("Dump: %s\n", buffer);
396
397 ctx = ntfs_attr_get_search_ctx(ino, NULL);
398
399 while ((rec = find_attribute(AT_UNUSED, ctx))) {
400 ntfs_log_info(" 0x%02x - ", rec->type);
401 if (rec->non_resident) {
402 ntfs_log_info("non-resident\n");
403 runs = ntfs_mapping_pairs_decompress(vol, rec, NULL);
404 if (runs) {
405 ntfs_log_info(" VCN LCN Length\n");
406 for (i = 0; runs[i].length > 0; i++) {
407 ntfs_log_info(" %8lld %8lld %8lld\n",
408 (long long)runs[i].vcn,
409 (long long)runs[i].lcn,
410 (long long)
411 runs[i].length);
412 }
413 free(runs);
414 }
415 } else {
416 ntfs_log_info("resident\n");
417 }
418 }
419
420 ntfs_attr_put_search_ctx(ctx);
421 return 0;
422}
423
424/**
425 * print_match
426 */
427static int print_match(ntfs_inode *ino, ATTR_RECORD *attr,
428 runlist_element *run, void *data __attribute__((unused)))
429{
430 char *buffer;
431
432 if (!ino || !attr || !run)
433 return 1;
434
435 buffer = malloc(MAX_PATH);
436 if (!buffer) {
437 ntfs_log_error("!buffer\n");
438 return 1;
439 }
440
441 utils_inode_get_name(ino, buffer, MAX_PATH);
442 ntfs_log_info("Inode %llu %s", (unsigned long long)ino->mft_no, buffer);
443
444 utils_attr_get_name(ino->vol, attr, buffer, MAX_PATH);
445 ntfs_log_info("/%s\n", buffer);
446
447 free(buffer);
448 return 0;
449}
450
451/**
452 * find_last
453 */
454static int find_last(ntfs_inode *ino, ATTR_RECORD *attr, runlist_element *run,
455 void *data)
456{
457 struct match *m;
458
459 if (!ino || !attr || !run || !data)
460 return 1;
461
462 m = data;
463
464 if ((run->lcn + run->length) > m->lcn) {
465 m->inum = ino->mft_no;
466 m->lcn = run->lcn + run->length;
467 }
468
469 return 0;
470}
471
472/**
473 * main - Begin here
474 *
475 * Start from here.
476 *
477 * Return: 0 Success, the program worked
478 * 1 Error, something went wrong
479 */
480int main(int argc, char *argv[])
481{
482 ntfs_volume *vol;
483 ntfs_inode *ino = NULL;
484 struct match m;
485 int result = 1;
486
487 ntfs_log_set_handler(ntfs_log_handler_outerr);
488
489 if (!parse_options(argc, argv))
490 return 1;
491
492 utils_set_locale();
493
494 vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY |
495 (opts.force ? NTFS_MNT_RECOVER : 0));
496 if (!vol)
497 return 1;
498
499 switch (opts.action) {
500 case act_sector:
501 if (opts.range_begin == opts.range_end)
502 ntfs_log_quiet("Searching for sector %llu\n",
503 (unsigned long long)opts.range_begin);
504 else
505 ntfs_log_quiet("Searching for sector range %llu-%llu\n", (unsigned long long)opts.range_begin, (unsigned long long)opts.range_end);
506 /* Convert to clusters */
507 opts.range_begin >>= (vol->cluster_size_bits - vol->sector_size_bits);
508 opts.range_end >>= (vol->cluster_size_bits - vol->sector_size_bits);
509 result = cluster_find(vol, opts.range_begin, opts.range_end, (cluster_cb*)&print_match, NULL);
510 break;
511 case act_cluster:
512 if (opts.range_begin == opts.range_end)
513 ntfs_log_quiet("Searching for cluster %llu\n",
514 (unsigned long long)opts.range_begin);
515 else
516 ntfs_log_quiet("Searching for cluster range %llu-%llu\n", (unsigned long long)opts.range_begin, (unsigned long long)opts.range_end);
517 result = cluster_find(vol, opts.range_begin, opts.range_end, (cluster_cb*)&print_match, NULL);
518 break;
519 case act_file:
520 ino = ntfs_pathname_to_inode(vol, NULL, opts.filename);
521 if (ino)
522 result = dump_file(vol, ino);
523 break;
524 case act_inode:
525 ino = ntfs_inode_open(vol, opts.inode);
526 if (ino) {
527 result = dump_file(vol, ino);
528 ntfs_inode_close(ino);
529 } else {
530 ntfs_log_error("Cannot open inode %llu\n",
531 (unsigned long long)opts.inode);
532 }
533 break;
534 case act_last:
535 memset(&m, 0, sizeof(m));
536 m.lcn = -1;
537 result = cluster_find(vol, 0, LONG_MAX, (cluster_cb*)&find_last, &m);
538 if (m.lcn >= 0) {
539 ino = ntfs_inode_open(vol, m.inum);
540 if (ino) {
541 result = dump_file(vol, ino);
542 ntfs_inode_close(ino);
543 } else {
544 ntfs_log_error("Cannot open inode %llu\n",
545 (unsigned long long)
546 opts.inode);
547 }
548 result = 0;
549 } else {
550 result = 1;
551 }
552 break;
553 case act_info:
554 default:
555 result = info(vol);
556 break;
557 }
558
559 ntfs_umount(vol, FALSE);
560 return result;
561}
562
563
564