2004-03-07 01:42:28 +08:00
|
|
|
/**
|
|
|
|
* cluster - Part of the Linux-NTFS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2003 Richard Russon
|
2014-03-11 17:51:47 +08:00
|
|
|
* Copyright (c) 2014 Jean-Pierre Andre
|
2004-03-07 01:42:28 +08:00
|
|
|
*
|
|
|
|
* This function will locate the owner of any given sector or cluster range.
|
|
|
|
*
|
|
|
|
* 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 2 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 (in the main directory of the Linux-NTFS
|
|
|
|
* distribution in the file COPYING); if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_STDIO_H
|
2004-03-07 01:42:28 +08:00
|
|
|
#include <stdio.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-03-07 01:42:28 +08:00
|
|
|
#include <stdlib.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-03-07 01:42:28 +08:00
|
|
|
#include <string.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
#include "cluster.h"
|
|
|
|
#include "utils.h"
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "logging.h"
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cluster_find
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int result = -1;
|
|
|
|
struct mft_search_ctx *m_ctx = NULL;
|
|
|
|
ntfs_attr_search_ctx *a_ctx = NULL;
|
2014-03-11 17:51:47 +08:00
|
|
|
s64 count;
|
|
|
|
BOOL found;
|
2004-03-07 01:42:28 +08:00
|
|
|
ATTR_RECORD *rec;
|
|
|
|
runlist *runs;
|
|
|
|
|
|
|
|
if (!vol || !cb)
|
|
|
|
return -1;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
m_ctx = mft_get_search_ctx(vol);
|
2004-03-07 01:42:28 +08:00
|
|
|
m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD;
|
2014-03-11 17:51:47 +08:00
|
|
|
count = 0;
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
while (mft_next_record(m_ctx) == 0) {
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
if (!(m_ctx->flags_match & FEMR_BASE_RECORD))
|
|
|
|
continue;
|
|
|
|
|
2005-10-29 08:08:43 +08:00
|
|
|
ntfs_log_verbose("Inode: %llu\n", (unsigned long long)
|
2004-03-10 00:00:13 +08:00
|
|
|
m_ctx->inode->mft_no);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2014-03-11 17:51:47 +08:00
|
|
|
found = FALSE;
|
2005-10-27 06:54:57 +08:00
|
|
|
while ((rec = find_attribute(AT_UNUSED, a_ctx))) {
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
if (!rec->non_resident) {
|
2014-03-11 17:43:31 +08:00
|
|
|
ntfs_log_verbose("0x%02x skipped - attr is resident\n",
|
|
|
|
(int)le32_to_cpu(a_ctx->attr->type));
|
2004-03-07 01:42:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!runs) {
|
2005-10-29 08:08:43 +08:00
|
|
|
ntfs_log_error("Couldn't read the data runs.\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:43:31 +08:00
|
|
|
ntfs_log_verbose("\t[0x%02X]\n",
|
|
|
|
(int)le32_to_cpu(a_ctx->attr->type));
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-29 08:08:43 +08:00
|
|
|
ntfs_log_verbose("\t\tVCN\tLCN\tLength\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
for (j = 0; runs[j].length > 0; j++) {
|
|
|
|
LCN a_begin = runs[j].lcn;
|
|
|
|
LCN a_end = a_begin + runs[j].length - 1;
|
|
|
|
|
|
|
|
if (a_begin < 0)
|
|
|
|
continue; // sparse, discontiguous, etc
|
|
|
|
|
2005-10-29 08:08:43 +08:00
|
|
|
ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n",
|
2004-03-10 00:00:13 +08:00
|
|
|
(long long)runs[j].vcn,
|
|
|
|
(long long)runs[j].lcn,
|
|
|
|
(long long)(runs[j].lcn +
|
|
|
|
runs[j].length - 1),
|
|
|
|
(long long)runs[j].length);
|
2004-03-07 01:42:28 +08:00
|
|
|
//dprint list
|
|
|
|
|
|
|
|
if ((a_begin > c_end) || (a_end < c_begin))
|
|
|
|
continue; // before or after search range
|
|
|
|
|
|
|
|
if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data))
|
|
|
|
return 1;
|
2014-03-11 17:51:47 +08:00
|
|
|
found = TRUE;
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
ntfs_attr_put_search_ctx(a_ctx);
|
2004-03-07 01:42:28 +08:00
|
|
|
a_ctx = NULL;
|
2014-03-11 17:51:47 +08:00
|
|
|
if (found)
|
|
|
|
count++;
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 17:51:47 +08:00
|
|
|
if (count > 1)
|
|
|
|
ntfs_log_info("* %lld inodes found\n",(long long)count);
|
|
|
|
else
|
|
|
|
ntfs_log_info("* %s inode found\n", (count ? "one" : "no"));
|
2004-03-07 01:42:28 +08:00
|
|
|
result = 0;
|
|
|
|
done:
|
2005-10-27 06:54:57 +08:00
|
|
|
ntfs_attr_put_search_ctx(a_ctx);
|
|
|
|
mft_put_search_ctx(m_ctx);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|