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