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 "compat.h"
  37 #include "cluster.h"
  38 #include "utils.h"
  39 #include "logging.h"
  40 
  41 /**
  42  * cluster_find
  43  */
  44 int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data)
  45 {
  46         int j;
  47         int result = -1;
  48         struct mft_search_ctx *m_ctx = NULL;
  49         ntfs_attr_search_ctx  *a_ctx = NULL;
  50         ATTR_RECORD *rec;
  51         runlist *runs;
  52 
  53         if (!vol || !cb)
  54                 return -1;
  55 
  56         m_ctx = mft_get_search_ctx(vol);
  57         m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD;
  58 
  59         while (mft_next_record(m_ctx) == 0) {
  60 
  61                 if (!(m_ctx->flags_match & FEMR_BASE_RECORD))
  62                         continue;
  63 
  64                 ntfs_log_verbose("Inode: %llu\n", (unsigned long long)
  65                                 m_ctx->inode->mft_no);
  66 
  67                 a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL);
  68 
  69                 while ((rec = find_attribute(AT_UNUSED, a_ctx))) {
  70 
  71                         if (!rec->non_resident) {
  72                                 ntfs_log_verbose("0x%02x skipped - attr is resident\n", a_ctx->attr->type);
  73                                 continue;
  74                         }
  75 
  76                         runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL);
  77                         if (!runs) {
  78                                 ntfs_log_error("Couldn't read the data runs.\n");
  79                                 goto done;
  80                         }
  81 
  82                         ntfs_log_verbose("\t[0x%02X]\n", a_ctx->attr->type);
  83 
  84                         ntfs_log_verbose("\t\tVCN\tLCN\tLength\n");
  85                         for (j = 0; runs[j].length > 0; j++) {
  86                                 LCN a_begin = runs[j].lcn;
  87                                 LCN a_end   = a_begin + runs[j].length - 1;
  88 
  89                                 if (a_begin < 0)
  90                                         continue;       // sparse, discontiguous, etc
  91 
  92                                 ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n",
  93                                                 (long long)runs[j].vcn,
  94                                                 (long long)runs[j].lcn,
  95                                                 (long long)(runs[j].lcn +
  96                                                 runs[j].length - 1),
  97                                                 (long long)runs[j].length);
  98                                 //dprint list
  99 
 100                                 if ((a_begin > c_end) || (a_end < c_begin))
 101                                         continue;       // before or after search range
 102 
 103                                 if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data))
 104                                         return 1;
 105                         }
 106                 }
 107 
 108                 ntfs_attr_put_search_ctx(a_ctx);
 109                 a_ctx = NULL;
 110         }
 111 
 112         result = 0;
 113 done:
 114         ntfs_attr_put_search_ctx(a_ctx);
 115         mft_put_search_ctx(m_ctx);
 116 
 117         return result;
 118 }
 119