1N/A/**
1N/A * cluster - Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2002-2003 Richard Russon
1N/A *
1N/A * This function will locate the owner of any given sector or cluster range.
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifdef HAVE_STDIO_H
1N/A#include <stdio.h>
1N/A#endif
1N/A#ifdef HAVE_STDLIB_H
1N/A#include <stdlib.h>
1N/A#endif
1N/A#ifdef HAVE_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "cluster.h"
1N/A#include "utils.h"
1N/A#include "logging.h"
1N/A
1N/A/**
1N/A * cluster_find
1N/A */
1N/Aint cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data)
1N/A{
1N/A int j;
1N/A int result = -1;
1N/A struct mft_search_ctx *m_ctx = NULL;
1N/A ntfs_attr_search_ctx *a_ctx = NULL;
1N/A ATTR_RECORD *rec;
1N/A runlist *runs;
1N/A
1N/A if (!vol || !cb)
1N/A return -1;
1N/A
1N/A m_ctx = mft_get_search_ctx(vol);
1N/A m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD;
1N/A
1N/A while (mft_next_record(m_ctx) == 0) {
1N/A
1N/A if (!(m_ctx->flags_match & FEMR_BASE_RECORD))
1N/A continue;
1N/A
1N/A ntfs_log_verbose("Inode: %llu\n", (unsigned long long)
1N/A m_ctx->inode->mft_no);
1N/A
1N/A a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL);
1N/A
1N/A while ((rec = find_attribute(AT_UNUSED, a_ctx))) {
1N/A
1N/A if (!rec->non_resident) {
1N/A ntfs_log_verbose("0x%02x skipped - attr is resident\n", a_ctx->attr->type);
1N/A continue;
1N/A }
1N/A
1N/A runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL);
1N/A if (!runs) {
1N/A ntfs_log_error("Couldn't read the data runs.\n");
1N/A goto done;
1N/A }
1N/A
1N/A ntfs_log_verbose("\t[0x%02X]\n", a_ctx->attr->type);
1N/A
1N/A ntfs_log_verbose("\t\tVCN\tLCN\tLength\n");
1N/A for (j = 0; runs[j].length > 0; j++) {
1N/A LCN a_begin = runs[j].lcn;
1N/A LCN a_end = a_begin + runs[j].length - 1;
1N/A
1N/A if (a_begin < 0)
1N/A continue; // sparse, discontiguous, etc
1N/A
1N/A ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n",
1N/A (long long)runs[j].vcn,
1N/A (long long)runs[j].lcn,
1N/A (long long)(runs[j].lcn +
1N/A runs[j].length - 1),
1N/A (long long)runs[j].length);
1N/A //dprint list
1N/A
1N/A if ((a_begin > c_end) || (a_end < c_begin))
1N/A continue; // before or after search range
1N/A
1N/A if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data))
1N/A return 1;
1N/A }
1N/A }
1N/A
1N/A ntfs_attr_put_search_ctx(a_ctx);
1N/A a_ctx = NULL;
1N/A }
1N/A
1N/A result = 0;
1N/Adone:
1N/A ntfs_attr_put_search_ctx(a_ctx);
1N/A mft_put_search_ctx(m_ctx);
1N/A
1N/A return result;
1N/A}
1N/A