cluster.c revision 9663:ace9a2ac3683
1/**
2 * cluster - Part of the Linux-NTFS project.
3 *
4 * Copyright (c) 2002-2003 Richard Russon
5 *
6 * This function will locate the owner of any given sector or cluster range.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the Linux-NTFS
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24#include "config.h"
25
26#ifdef HAVE_STDIO_H
27#include <stdio.h>
28#endif
29#ifdef HAVE_STDLIB_H
30#include <stdlib.h>
31#endif
32#ifdef HAVE_STRING_H
33#include <string.h>
34#endif
35
36#include "cluster.h"
37#include "utils.h"
38#include "logging.h"
39
40/**
41 * cluster_find
42 */
43int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data)
44{
45	int j;
46	int result = -1;
47	struct mft_search_ctx *m_ctx = NULL;
48	ntfs_attr_search_ctx  *a_ctx = NULL;
49	ATTR_RECORD *rec;
50	runlist *runs;
51
52	if (!vol || !cb)
53		return -1;
54
55	m_ctx = mft_get_search_ctx(vol);
56	m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD;
57
58	while (mft_next_record(m_ctx) == 0) {
59
60		if (!(m_ctx->flags_match & FEMR_BASE_RECORD))
61			continue;
62
63		ntfs_log_verbose("Inode: %llu\n", (unsigned long long)
64				m_ctx->inode->mft_no);
65
66		a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL);
67
68		while ((rec = find_attribute(AT_UNUSED, a_ctx))) {
69
70			if (!rec->non_resident) {
71				ntfs_log_verbose("0x%02x skipped - attr is resident\n", a_ctx->attr->type);
72				continue;
73			}
74
75			runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL);
76			if (!runs) {
77				ntfs_log_error("Couldn't read the data runs.\n");
78				goto done;
79			}
80
81			ntfs_log_verbose("\t[0x%02X]\n", a_ctx->attr->type);
82
83			ntfs_log_verbose("\t\tVCN\tLCN\tLength\n");
84			for (j = 0; runs[j].length > 0; j++) {
85				LCN a_begin = runs[j].lcn;
86				LCN a_end   = a_begin + runs[j].length - 1;
87
88				if (a_begin < 0)
89					continue;	// sparse, discontiguous, etc
90
91				ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n",
92						(long long)runs[j].vcn,
93						(long long)runs[j].lcn,
94						(long long)(runs[j].lcn +
95						runs[j].length - 1),
96						(long long)runs[j].length);
97				//dprint list
98
99				if ((a_begin > c_end) || (a_end < c_begin))
100					continue;	// before or after search range
101
102				if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data))
103					return 1;
104			}
105		}
106
107		ntfs_attr_put_search_ctx(a_ctx);
108		a_ctx = NULL;
109	}
110
111	result = 0;
112done:
113	ntfs_attr_put_search_ctx(a_ctx);
114	mft_put_search_ctx(m_ctx);
115
116	return result;
117}
118
119