1N/A/**
1N/A * ntfsundelete - Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2002-2005 Richard Russon
1N/A * Copyright (c) 2004-2005 Holger Ohmacht
1N/A * Copyright (c) 2005 Anton Altaparmakov
1N/A * Copyright (c) 2007 Yura Pakhuchiy
1N/A *
1N/A * This utility will recover deleted files from an NTFS volume.
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_FEATURES_H
1N/A#include <features.h>
1N/A#endif
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#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_TYPES_H
1N/A#include <sys/types.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_STAT_H
1N/A#include <sys/stat.h>
1N/A#endif
1N/A#ifdef HAVE_UNISTD_H
1N/A#include <unistd.h>
1N/A#endif
1N/A#ifdef HAVE_FCNTL_H
1N/A#include <fcntl.h>
1N/A#endif
1N/A#ifdef HAVE_GETOPT_H
1N/A#include <getopt.h>
1N/A#endif
1N/A#ifdef HAVE_TIME_H
1N/A#include <time.h>
1N/A#endif
1N/A#ifdef HAVE_LIMITS_H
1N/A#include <limits.h>
1N/A#endif
1N/A#ifdef HAVE_STDARG_H
1N/A#include <stdarg.h>
1N/A#endif
1N/A#ifdef HAVE_UTIME_H
1N/A#include <utime.h>
1N/A#endif
1N/A#include <regex.h>
1N/A
1N/A#if !defined(REG_NOERROR) || (REG_NOERROR != 0)
1N/A#define REG_NOERROR 0
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "ntfsundelete.h"
1N/A#include "bootsect.h"
1N/A#include "mft.h"
1N/A#include "attrib.h"
1N/A#include "layout.h"
1N/A#include "inode.h"
1N/A#include "device.h"
1N/A#include "utils.h"
1N/A#include "debug.h"
1N/A#include "ntfstime.h"
1N/A#include "version.h"
1N/A#include "logging.h"
1N/A
1N/Astatic const char *EXEC_NAME = "ntfsundelete";
1N/Astatic const char *MFTFILE = "mft";
1N/Astatic const char *UNNAMED = "<unnamed>";
1N/Astatic const char *NONE = "<none>";
1N/Astatic const char *UNKNOWN = "unknown";
1N/Astatic struct options opts;
1N/A
1N/Atypedef struct
1N/A{
1N/A u32 begin;
1N/A u32 end;
1N/A} range;
1N/A
1N/Astatic short with_regex; /* Flag Regular expression available */
1N/Astatic short avoid_duplicate_printing; /* Flag No duplicate printing of file infos */
1N/Astatic range *ranges; /* Array containing all Inode-Ranges for undelete */
1N/Astatic long nr_entries; /* Number of range entries */
1N/A
1N/A/**
1N/A * parse_inode_arg - parses the inode expression
1N/A *
1N/A * Parses the optarg after parameter -u for valid ranges
1N/A *
1N/A * Return: Number of correct inode specifications or -1 for error
1N/A */
1N/Astatic int parse_inode_arg(void)
1N/A{
1N/A int p;
1N/A u32 imax;
1N/A u32 range_begin;
1N/A u32 range_end;
1N/A u32 range_temp;
1N/A u32 inode;
1N/A char *opt_arg_ptr;
1N/A char *opt_arg_temp;
1N/A char *opt_arg_end1;
1N/A char *opt_arg_end2;
1N/A
1N/A /* Check whether optarg is available or not */
1N/A nr_entries = 0;
1N/A if (optarg == NULL)
1N/A return (0); /* bailout if no optarg */
1N/A
1N/A /* init variables */
1N/A p = strlen(optarg);
1N/A imax = p;
1N/A opt_arg_ptr = optarg;
1N/A opt_arg_end1 = optarg;
1N/A opt_arg_end2 = &(optarg[p]);
1N/A
1N/A /* alloc mem for range table */
1N/A ranges = (range *) malloc((p + 1) * sizeof(range));
1N/A if (ranges == NULL) {
1N/A ntfs_log_error("ERROR: Couldn't alloc mem for parsing inodes!\n");
1N/A return (-1);
1N/A }
1N/A
1N/A /* loop */
1N/A while ((opt_arg_end1 != opt_arg_end2) && (p > 0)) {
1N/A /* Try to get inode */
1N/A inode = strtoul(opt_arg_ptr, &opt_arg_end1, 0);
1N/A p--;
1N/A
1N/A /* invalid char at begin */
1N/A if ((opt_arg_ptr == opt_arg_end1) || (opt_arg_ptr == opt_arg_end2)) {
1N/A ntfs_log_error("ERROR: Invalid Number: %s\n", opt_arg_ptr);
1N/A return (-1);
1N/A }
1N/A
1N/A /* RANGE - Check for range */
1N/A if (opt_arg_end1[0] == '-') {
1N/A /* get range end */
1N/A opt_arg_temp = opt_arg_end1;
1N/A opt_arg_end1 = & (opt_arg_temp[1]);
1N/A if (opt_arg_temp >= opt_arg_end2) {
1N/A ntfs_log_error("ERROR: Missing range end!\n");
1N/A return (-1);
1N/A }
1N/A range_begin = inode;
1N/A
1N/A /* get count */
1N/A range_end = strtoul(opt_arg_end1, &opt_arg_temp, 0);
1N/A if (opt_arg_temp == opt_arg_end1) {
1N/A ntfs_log_error("ERROR: Invalid Number: %s\n", opt_arg_temp);
1N/A return (-1);
1N/A }
1N/A
1N/A /* check for correct values */
1N/A if (range_begin > range_end) {
1N/A range_temp = range_end;
1N/A range_end = range_begin;
1N/A range_begin = range_temp;
1N/A }
1N/A
1N/A /* put into struct */
1N/A ranges[nr_entries].begin = range_begin;
1N/A ranges[nr_entries].end = range_end;
1N/A nr_entries++;
1N/A
1N/A /* Last check */
1N/A opt_arg_ptr = & (opt_arg_temp[1]);
1N/A if (opt_arg_ptr >= opt_arg_end2)
1N/A break;
1N/A } else if (opt_arg_end1[0] == ',') {
1N/A /* SINGLE VALUE, BUT CONTINUING */
1N/A /* put inode into range list */
1N/A ranges[nr_entries].begin = inode;
1N/A ranges[nr_entries].end = inode;
1N/A nr_entries++;
1N/A
1N/A /* Next inode */
1N/A opt_arg_ptr = & (opt_arg_end1[1]);
1N/A if (opt_arg_ptr >= opt_arg_end2) {
1N/A ntfs_log_error("ERROR: Missing new value at end of input!\n");
1N/A return (-1);
1N/A }
1N/A continue;
1N/A } else { /* SINGLE VALUE, END */
1N/A ranges[nr_entries].begin = inode;
1N/A ranges[nr_entries].end = inode;
1N/A nr_entries++;
1N/A }
1N/A }
1N/A return (nr_entries);
1N/A}
1N/A
1N/A/**
1N/A * version - Print version information about the program
1N/A *
1N/A * Print a copyright statement and a brief description of the program.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void version(void)
1N/A{
1N/A ntfs_log_info("\n%s v%s (libntfs %s) - Recover deleted files from an "
1N/A "NTFS Volume.\n\n", EXEC_NAME, VERSION,
1N/A ntfs_libntfs_version());
1N/A ntfs_log_info("Copyright (c) 2002-2005 Richard Russon\n"
1N/A "Copyright (c) 2004-2005 Holger Ohmacht\n"
1N/A "Copyright (c) 2005 Anton Altaparmakov\n"
1N/A "Copyright (c) 2007 Yura Pakhuchiy\n");
1N/A ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A/**
1N/A * usage - Print a list of the parameters to the program
1N/A *
1N/A * Print a list of the parameters and options for the program.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void usage(void)
1N/A{
1N/A ntfs_log_info("\nUsage: %s [options] device\n"
1N/A " -s, --scan Scan for files (default)\n"
1N/A " -p, --percentage NUM Minimum percentage recoverable\n"
1N/A " -m, --match PATTERN Only work on files with matching names\n"
1N/A " -C, --case Case sensitive matching\n"
1N/A " -S, --size RANGE Match files of this size\n"
1N/A " -t, --time SINCE Last referenced since this time\n"
1N/A "\n"
1N/A " -u, --undelete Undelete mode\n"
1N/A " -i, --inodes RANGE Recover these inodes\n"
1N/A //" -I, --interactive Interactive mode\n"
1N/A " -o, --output FILE Save with this filename\n"
1N/A " -O, --optimistic Undelete in-use clusters as well\n"
1N/A " -d, --destination DIR Destination directory\n"
1N/A " -b, --byte NUM Fill missing parts with this byte\n"
1N/A " -T, --truncate Truncate 100%% recoverable file to exact size.\n"
1N/A " -P, --parent Show parent directory\n"
1N/A "\n"
1N/A " -c, --copy RANGE Write a range of MFT records to a file\n"
1N/A "\n"
1N/A " -f, --force Use less caution\n"
1N/A " -q, --quiet Less output\n"
1N/A " -v, --verbose More output\n"
1N/A " -V, --version Display version information\n"
1N/A " -h, --help Display this help\n\n",
1N/A EXEC_NAME);
1N/A ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
1N/A}
1N/A
1N/A/**
1N/A * transform - Convert a shell style pattern to a regex
1N/A * @pattern: String to be converted
1N/A * @regex: Resulting regular expression is put here
1N/A *
1N/A * This will transform patterns, such as "*.doc" to true regular expressions.
1N/A * The function will also place '^' and '$' around the expression to make it
1N/A * behave as the user would expect
1N/A *
1N/A * Before After
1N/A * . \.
1N/A * * .*
1N/A * ? .
1N/A *
1N/A * Notes:
1N/A * The returned string must be freed by the caller.
1N/A * If transform fails, @regex will not be changed.
1N/A *
1N/A * Return: 1, Success, the string was transformed
1N/A * 0, An error occurred
1N/A */
1N/Astatic int transform(const char *pattern, char **regex)
1N/A{
1N/A char *result;
1N/A int length, i, j;
1N/A
1N/A if (!pattern || !regex)
1N/A return 0;
1N/A
1N/A length = strlen(pattern);
1N/A if (length < 1) {
1N/A ntfs_log_error("Pattern to transform is empty\n");
1N/A return 0;
1N/A }
1N/A
1N/A for (i = 0; pattern[i]; i++) {
1N/A if ((pattern[i] == '*') || (pattern[i] == '.'))
1N/A length++;
1N/A }
1N/A
1N/A result = malloc(length + 3);
1N/A if (!result) {
1N/A ntfs_log_error("Couldn't allocate memory in transform()\n");
1N/A return 0;
1N/A }
1N/A
1N/A result[0] = '^';
1N/A
1N/A for (i = 0, j = 1; pattern[i]; i++, j++) {
1N/A if (pattern[i] == '*') {
1N/A result[j] = '.';
1N/A j++;
1N/A result[j] = '*';
1N/A } else if (pattern[i] == '.') {
1N/A result[j] = '\\';
1N/A j++;
1N/A result[j] = '.';
1N/A } else if (pattern[i] == '?') {
1N/A result[j] = '.';
1N/A } else {
1N/A result[j] = pattern[i];
1N/A }
1N/A }
1N/A
1N/A result[j] = '$';
1N/A result[j+1] = 0;
1N/A ntfs_log_debug("Pattern '%s' replaced with regex '%s'.\n", pattern,
1N/A result);
1N/A
1N/A *regex = result;
1N/A return 1;
1N/A}
1N/A
1N/A/**
1N/A * parse_time - Convert a time abbreviation to seconds
1N/A * @string: The string to be converted
1N/A * @since: The absolute time referred to
1N/A *
1N/A * Strings representing times will be converted into a time_t. The numbers will
1N/A * be regarded as seconds unless suffixed.
1N/A *
1N/A * Suffix Description
1N/A * [yY] Year
1N/A * [mM] Month
1N/A * [wW] Week
1N/A * [dD] Day
1N/A * [sS] Second
1N/A *
1N/A * Therefore, passing "1W" will return the time_t representing 1 week ago.
1N/A *
1N/A * Notes:
1N/A * Only the first character of the suffix is read.
1N/A * If parse_time fails, @since will not be changed
1N/A *
1N/A * Return: 1 Success
1N/A * 0 Error, the string was malformed
1N/A */
1N/Astatic int parse_time(const char *value, time_t *since)
1N/A{
1N/A long long result;
1N/A time_t now;
1N/A char *suffix = NULL;
1N/A
1N/A if (!value || !since)
1N/A return -1;
1N/A
1N/A ntfs_log_trace("Parsing time '%s' ago.\n", value);
1N/A
1N/A result = strtoll(value, &suffix, 10);
1N/A if (result < 0 || errno == ERANGE) {
1N/A ntfs_log_error("Invalid time '%s'.\n", value);
1N/A return 0;
1N/A }
1N/A
1N/A if (!suffix) {
1N/A ntfs_log_error("Internal error, strtoll didn't return a suffix.\n");
1N/A return 0;
1N/A }
1N/A
1N/A if (strlen(suffix) > 1) {
1N/A ntfs_log_error("Invalid time suffix '%s'. Use Y, M, W, D or H.\n", suffix);
1N/A return 0;
1N/A }
1N/A
1N/A switch (suffix[0]) {
1N/A case 'y': case 'Y': result *= 12;
1N/A case 'm': case 'M': result *= 4;
1N/A case 'w': case 'W': result *= 7;
1N/A case 'd': case 'D': result *= 24;
1N/A case 'h': case 'H': result *= 3600;
1N/A case 0:
1N/A break;
1N/A
1N/A default:
1N/A ntfs_log_error("Invalid time suffix '%s'. Use Y, M, W, D or H.\n", suffix);
1N/A return 0;
1N/A }
1N/A
1N/A now = time(NULL);
1N/A
1N/A ntfs_log_debug("Time now = %lld, Time then = %lld.\n", (long long) now,
1N/A (long long) result);
1N/A *since = now - result;
1N/A return 1;
1N/A}
1N/A
1N/A/**
1N/A * parse_options - Read and validate the programs command line
1N/A *
1N/A * Read the command line, verify the syntax and parse the options.
1N/A * This function is very long, but quite simple.
1N/A *
1N/A * Return: 1 Success
1N/A * 0 Error, one or more problems
1N/A */
1N/Astatic int parse_options(int argc, char *argv[])
1N/A{
1N/A static const char *sopt = "-b:Cc:d:fh?i:m:o:OPp:sS:t:TuqvV";
1N/A static const struct option lopt[] = {
1N/A { "byte", required_argument, NULL, 'b' },
1N/A { "case", no_argument, NULL, 'C' },
1N/A { "copy", required_argument, NULL, 'c' },
1N/A { "destination", required_argument, NULL, 'd' },
1N/A { "force", no_argument, NULL, 'f' },
1N/A { "help", no_argument, NULL, 'h' },
1N/A { "inodes", required_argument, NULL, 'i' },
1N/A //{ "interactive", no_argument, NULL, 'I' },
1N/A { "match", required_argument, NULL, 'm' },
1N/A { "optimistic", no_argument, NULL, 'O' },
1N/A { "output", required_argument, NULL, 'o' },
1N/A { "parent", no_argument, NULL, 'P' },
1N/A { "percentage", required_argument, NULL, 'p' },
1N/A { "quiet", no_argument, NULL, 'q' },
1N/A { "scan", no_argument, NULL, 's' },
1N/A { "size", required_argument, NULL, 'S' },
1N/A { "time", required_argument, NULL, 't' },
1N/A { "truncate", no_argument, NULL, 'T' },
1N/A { "undelete", no_argument, NULL, 'u' },
1N/A { "verbose", no_argument, NULL, 'v' },
1N/A { "version", no_argument, NULL, 'V' },
1N/A { NULL, 0, NULL, 0 }
1N/A };
1N/A
1N/A int c = -1;
1N/A char *end = NULL;
1N/A int err = 0;
1N/A int ver = 0;
1N/A int help = 0;
1N/A int levels = 0;
1N/A
1N/A opterr = 0; /* We'll handle the errors, thank you. */
1N/A
1N/A opts.mode = MODE_NONE;
1N/A opts.uinode = -1;
1N/A opts.percent = -1;
1N/A opts.fillbyte = -1;
1N/A while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
1N/A switch (c) {
1N/A case 1: /* A non-option argument */
1N/A if (!opts.device) {
1N/A opts.device = argv[optind-1];
1N/A } else {
1N/A opts.device = NULL;
1N/A err++;
1N/A }
1N/A break;
1N/A case 'b':
1N/A if (opts.fillbyte == (char)-1) {
1N/A end = NULL;
1N/A opts.fillbyte = strtol(optarg, &end, 0);
1N/A if (end && *end)
1N/A err++;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'C':
1N/A opts.match_case++;
1N/A break;
1N/A case 'c':
1N/A if (opts.mode == MODE_NONE) {
1N/A if (!utils_parse_range(optarg,
1N/A &opts.mft_begin, &opts.mft_end, TRUE))
1N/A err++;
1N/A opts.mode = MODE_COPY;
1N/A } else {
1N/A opts.mode = MODE_ERROR;
1N/A }
1N/A break;
1N/A case 'd':
1N/A if (!opts.dest)
1N/A opts.dest = optarg;
1N/A else
1N/A err++;
1N/A break;
1N/A case 'f':
1N/A opts.force++;
1N/A break;
1N/A case 'h':
1N/A case '?':
1N/A if (ntfs_log_parse_option (argv[optind-1]))
1N/A break;
1N/A help++;
1N/A break;
1N/A case 'i':
1N/A end = NULL;
1N/A /* parse inodes */
1N/A if (parse_inode_arg() == -1)
1N/A err++;
1N/A if (end && *end)
1N/A err++;
1N/A break;
1N/A case 'm':
1N/A if (!opts.match) {
1N/A if (!transform(optarg, &opts.match)) {
1N/A err++;
1N/A } else {
1N/A /* set regex-flag on true ;) */
1N/A with_regex= 1;
1N/A }
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'o':
1N/A if (!opts.output) {
1N/A opts.output = optarg;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'O':
1N/A if (!opts.optimistic) {
1N/A opts.optimistic++;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'P':
1N/A if (!opts.parent) {
1N/A opts.parent++;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'p':
1N/A if (opts.percent == -1) {
1N/A end = NULL;
1N/A opts.percent = strtol(optarg, &end, 0);
1N/A if (end && ((*end != '%') && (*end != 0)))
1N/A err++;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'q':
1N/A opts.quiet++;
1N/A ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
1N/A break;
1N/A case 's':
1N/A if (opts.mode == MODE_NONE)
1N/A opts.mode = MODE_SCAN;
1N/A else
1N/A opts.mode = MODE_ERROR;
1N/A break;
1N/A case 'S':
1N/A if ((opts.size_begin > 0) || (opts.size_end > 0) ||
1N/A !utils_parse_range(optarg, &opts.size_begin,
1N/A &opts.size_end, TRUE)) {
1N/A err++;
1N/A }
1N/A break;
1N/A case 't':
1N/A if (opts.since == 0) {
1N/A if (!parse_time(optarg, &opts.since))
1N/A err++;
1N/A } else {
1N/A err++;
1N/A }
1N/A break;
1N/A case 'T':
1N/A opts.truncate++;
1N/A break;
1N/A case 'u':
1N/A if (opts.mode == MODE_NONE) {
1N/A opts.mode = MODE_UNDELETE;
1N/A } else {
1N/A opts.mode = MODE_ERROR;
1N/A }
1N/A break;
1N/A case 'v':
1N/A opts.verbose++;
1N/A ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
1N/A break;
1N/A case 'V':
1N/A ver++;
1N/A break;
1N/A default:
1N/A if (((optopt == 'b') || (optopt == 'c') ||
1N/A (optopt == 'd') || (optopt == 'm') ||
1N/A (optopt == 'o') || (optopt == 'p') ||
1N/A (optopt == 'S') || (optopt == 't') ||
1N/A (optopt == 'u')) && (!optarg)) {
1N/A ntfs_log_error("Option '%s' requires an argument.\n", argv[optind-1]);
1N/A } else {
1N/A ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
1N/A }
1N/A err++;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A /* Make sure we're in sync with the log levels */
1N/A levels = ntfs_log_get_levels();
1N/A if (levels & NTFS_LOG_LEVEL_VERBOSE)
1N/A opts.verbose++;
1N/A if (!(levels & NTFS_LOG_LEVEL_QUIET))
1N/A opts.quiet++;
1N/A
1N/A if (help || ver) {
1N/A opts.quiet = 0;
1N/A } else {
1N/A if (opts.device == NULL) {
1N/A if (argc > 1)
1N/A ntfs_log_error("You must specify exactly one device.\n");
1N/A err++;
1N/A }
1N/A
1N/A if (opts.mode == MODE_NONE) {
1N/A opts.mode = MODE_SCAN;
1N/A }
1N/A
1N/A switch (opts.mode) {
1N/A case MODE_SCAN:
1N/A if (opts.output || opts.dest || opts.truncate ||
1N/A (opts.fillbyte != (char)-1)) {
1N/A ntfs_log_error("Scan can only be used with --percent, "
1N/A "--match, --ignore-case, --size and --time.\n");
1N/A err++;
1N/A }
1N/A if (opts.match_case && !opts.match) {
1N/A ntfs_log_error("The --case option doesn't make sense without the --match option\n");
1N/A err++;
1N/A }
1N/A break;
1N/A
1N/A case MODE_UNDELETE:
1N/A /*if ((opts.percent != -1) || (opts.size_begin > 0) || (opts.size_end > 0)) {
1N/A ntfs_log_error("Undelete can only be used with "
1N/A "--output, --destination, --byte and --truncate.\n");
1N/A err++;
1N/A }*/
1N/A break;
1N/A case MODE_COPY:
1N/A if ((opts.fillbyte != (char)-1) || opts.truncate ||
1N/A (opts.percent != -1) ||
1N/A opts.match || opts.match_case ||
1N/A (opts.size_begin > 0) ||
1N/A (opts.size_end > 0)) {
1N/A ntfs_log_error("Copy can only be used with --output and --destination.\n");
1N/A err++;
1N/A }
1N/A break;
1N/A default:
1N/A ntfs_log_error("You can only select one of Scan, Undelete or Copy.\n");
1N/A err++;
1N/A }
1N/A
1N/A if ((opts.percent < -1) || (opts.percent > 100)) {
1N/A ntfs_log_error("Percentage value must be in the range 0 - 100.\n");
1N/A err++;
1N/A }
1N/A
1N/A if (opts.quiet) {
1N/A if (opts.verbose) {
1N/A ntfs_log_error("You may not use --quiet and --verbose at the same time.\n");
1N/A err++;
1N/A } else if (opts.mode == MODE_SCAN) {
1N/A ntfs_log_error("You may not use --quiet when scanning a volume.\n");
1N/A err++;
1N/A }
1N/A }
1N/A
1N/A if (opts.parent && !opts.verbose) {
1N/A ntfs_log_error("To use --parent, you must also use --verbose.\n");
1N/A err++;
1N/A }
1N/A }
1N/A
1N/A if (opts.fillbyte == (char)-1)
1N/A opts.fillbyte = 0;
1N/A
1N/A if (ver)
1N/A version();
1N/A if (help || err)
1N/A usage();
1N/A
1N/A return (!err && !help && !ver);
1N/A}
1N/A
1N/A/**
1N/A * free_file - Release the resources used by a file object
1N/A * @file: The unwanted file object
1N/A *
1N/A * This will free up the memory used by a file object and iterate through the
1N/A * object's children, freeing their resources too.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void free_file(struct ufile *file)
1N/A{
1N/A struct list_head *item, *tmp;
1N/A
1N/A if (!file)
1N/A return;
1N/A
1N/A list_for_each_safe(item, tmp, &file->name) { /* List of filenames */
1N/A struct filename *f = list_entry(item, struct filename, list);
1N/A ntfs_log_debug("freeing filename '%s'", f->name ? f->name :
1N/A NONE);
1N/A if (f->name)
1N/A free(f->name);
1N/A if (f->parent_name) {
1N/A ntfs_log_debug(" and parent filename '%s'",
1N/A f->parent_name);
1N/A free(f->parent_name);
1N/A }
1N/A ntfs_log_debug(".\n");
1N/A free(f);
1N/A }
1N/A
1N/A list_for_each_safe(item, tmp, &file->data) { /* List of data streams */
1N/A struct data *d = list_entry(item, struct data, list);
1N/A ntfs_log_debug("Freeing data stream '%s'.\n", d->name ?
1N/A d->name : UNNAMED);
1N/A if (d->name)
1N/A free(d->name);
1N/A if (d->runlist)
1N/A free(d->runlist);
1N/A free(d);
1N/A }
1N/A
1N/A free(file->mft);
1N/A free(file);
1N/A}
1N/A
1N/A/**
1N/A * verify_parent - confirm a record is parent of a file
1N/A * @name: a filename of the file
1N/A * @rec: the mft record of the possible parent
1N/A *
1N/A * Check that @rec is the parent of the file represented by @name.
1N/A * If @rec is a directory, but it is created after @name, then we
1N/A * can't determine whether @rec is really @name's parent.
1N/A *
1N/A * Return: @rec's filename, either same name space as @name or lowest space.
1N/A * NULL if can't determine parenthood or on error.
1N/A */
1N/Astatic FILE_NAME_ATTR* verify_parent(struct filename* name, MFT_RECORD* rec)
1N/A{
1N/A ATTR_RECORD *attr30;
1N/A FILE_NAME_ATTR *filename_attr = NULL, *lowest_space_name = NULL;
1N/A ntfs_attr_search_ctx *ctx;
1N/A int found_same_space = 1;
1N/A
1N/A if (!name || !rec)
1N/A return NULL;
1N/A
1N/A if (!(rec->flags & MFT_RECORD_IS_DIRECTORY)) {
1N/A return NULL;
1N/A }
1N/A
1N/A ctx = ntfs_attr_get_search_ctx(NULL, rec);
1N/A if (!ctx) {
1N/A ntfs_log_error("ERROR: Couldn't create a search context.\n");
1N/A return NULL;
1N/A }
1N/A
1N/A attr30 = find_attribute(AT_FILE_NAME, ctx);
1N/A if (!attr30) {
1N/A return NULL;
1N/A }
1N/A
1N/A filename_attr = (FILE_NAME_ATTR*)((char*)attr30 + le16_to_cpu(attr30->u.res.value_offset));
1N/A /* if name is older than this dir -> can't determine */
1N/A if (ntfs2utc(filename_attr->creation_time) > name->date_c) {
1N/A return NULL;
1N/A }
1N/A
1N/A if (filename_attr->file_name_type != name->name_space) {
1N/A found_same_space = 0;
1N/A lowest_space_name = filename_attr;
1N/A
1N/A while (!found_same_space && (attr30 = find_attribute(AT_FILE_NAME, ctx))) {
1N/A filename_attr = (FILE_NAME_ATTR*)((char*)attr30 + le16_to_cpu(attr30->u.res.value_offset));
1N/A
1N/A if (filename_attr->file_name_type == name->name_space) {
1N/A found_same_space = 1;
1N/A } else {
1N/A if (filename_attr->file_name_type < lowest_space_name->file_name_type) {
1N/A lowest_space_name = filename_attr;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A
1N/A return (found_same_space ? filename_attr : lowest_space_name);
1N/A}
1N/A
1N/A/**
1N/A * get_parent_name - Find the name of a file's parent.
1N/A * @name: the filename whose parent's name to find
1N/A */
1N/Astatic void get_parent_name(struct filename* name, ntfs_volume* vol)
1N/A{
1N/A ntfs_attr* mft_data;
1N/A MFT_RECORD* rec;
1N/A FILE_NAME_ATTR* filename_attr;
1N/A long long inode_num;
1N/A
1N/A if (!name || !vol)
1N/A return;
1N/A
1N/A rec = calloc(1, vol->mft_record_size);
1N/A if (!rec) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in "
1N/A "get_parent_name()\n");
1N/A return;
1N/A }
1N/A
1N/A mft_data = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!mft_data) {
1N/A ntfs_log_perror("ERROR: Couldn't open $MFT/$DATA");
1N/A } else {
1N/A inode_num = MREF_LE(name->parent_mref);
1N/A
1N/A if (ntfs_attr_pread(mft_data, vol->mft_record_size * inode_num,
1N/A vol->mft_record_size, rec) < 1) {
1N/A ntfs_log_error("ERROR: Couldn't read MFT Record %lld"
1N/A ".\n", inode_num);
1N/A } else if ((filename_attr = verify_parent(name, rec))) {
1N/A if (ntfs_ucstombs(filename_attr->file_name,
1N/A filename_attr->file_name_length,
1N/A &name->parent_name, 0) < 0) {
1N/A ntfs_log_debug("ERROR: Couldn't translate "
1N/A "filename to current "
1N/A "locale.\n");
1N/A name->parent_name = NULL;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (mft_data) {
1N/A ntfs_attr_close(mft_data);
1N/A }
1N/A
1N/A if (rec) {
1N/A free(rec);
1N/A }
1N/A
1N/A return;
1N/A}
1N/A
1N/A/**
1N/A * get_filenames - Read an MFT Record's $FILENAME attributes
1N/A * @file: The file object to work with
1N/A *
1N/A * A single file may have more than one filename. This is quite common.
1N/A * Windows creates a short DOS name for each long name, e.g. LONGFI~1.XYZ,
1N/A * LongFiLeName.xyZ.
1N/A *
1N/A * The filenames that are found are put in filename objects and added to a
1N/A * linked list of filenames in the file object. For convenience, the unicode
1N/A * filename is converted into the current locale and stored in the filename
1N/A * object.
1N/A *
1N/A * One of the filenames is picked (the one with the lowest numbered namespace)
1N/A * and its locale friendly name is put in pref_name.
1N/A *
1N/A * Return: n The number of $FILENAME attributes found
1N/A * -1 Error
1N/A */
1N/Astatic int get_filenames(struct ufile *file, ntfs_volume* vol)
1N/A{
1N/A ATTR_RECORD *rec;
1N/A FILE_NAME_ATTR *attr;
1N/A ntfs_attr_search_ctx *ctx;
1N/A struct filename *name;
1N/A int count = 0;
1N/A int space = 4;
1N/A
1N/A if (!file)
1N/A return -1;
1N/A
1N/A ctx = ntfs_attr_get_search_ctx(NULL, file->mft);
1N/A if (!ctx)
1N/A return -1;
1N/A
1N/A while ((rec = find_attribute(AT_FILE_NAME, ctx))) {
1N/A /* We know this will always be resident. */
1N/A attr = (FILE_NAME_ATTR *)((char *)rec +
1N/A le16_to_cpu(rec->u.res.value_offset));
1N/A
1N/A name = calloc(1, sizeof(*name));
1N/A if (!name) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in "
1N/A "get_filenames().\n");
1N/A count = -1;
1N/A break;
1N/A }
1N/A
1N/A name->uname = attr->file_name;
1N/A name->uname_len = attr->file_name_length;
1N/A name->name_space = attr->file_name_type;
1N/A name->size_alloc = sle64_to_cpu(attr->allocated_size);
1N/A name->size_data = sle64_to_cpu(attr->data_size);
1N/A name->flags = attr->file_attributes;
1N/A
1N/A name->date_c = ntfs2utc(attr->creation_time);
1N/A name->date_a = ntfs2utc(attr->last_data_change_time);
1N/A name->date_m = ntfs2utc(attr->last_mft_change_time);
1N/A name->date_r = ntfs2utc(attr->last_access_time);
1N/A
1N/A if (ntfs_ucstombs(name->uname, name->uname_len, &name->name,
1N/A 0) < 0) {
1N/A ntfs_log_debug("ERROR: Couldn't translate filename to "
1N/A "current locale.\n");
1N/A }
1N/A
1N/A name->parent_name = NULL;
1N/A
1N/A if (opts.parent) {
1N/A name->parent_mref = attr->parent_directory;
1N/A get_parent_name(name, vol);
1N/A }
1N/A
1N/A if (name->name_space < space) {
1N/A file->pref_name = name->name;
1N/A file->pref_pname = name->parent_name;
1N/A space = name->name_space;
1N/A }
1N/A
1N/A file->max_size = max(file->max_size, name->size_alloc);
1N/A file->max_size = max(file->max_size, name->size_data);
1N/A
1N/A list_add_tail(&name->list, &file->name);
1N/A count++;
1N/A }
1N/A
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_log_debug("File has %d names.\n", count);
1N/A return count;
1N/A}
1N/A
1N/A/**
1N/A * get_data - Read an MFT Record's $DATA attributes
1N/A * @file: The file object to work with
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A *
1N/A * A file may have more than one data stream. All files will have an unnamed
1N/A * data stream which contains the file's data. Some Windows applications store
1N/A * extra information in a separate stream.
1N/A *
1N/A * The streams that are found are put in data objects and added to a linked
1N/A * list of data streams in the file object.
1N/A *
1N/A * Return: n The number of $FILENAME attributes found
1N/A * -1 Error
1N/A */
1N/Astatic int get_data(struct ufile *file, ntfs_volume *vol)
1N/A{
1N/A ATTR_RECORD *rec;
1N/A ntfs_attr_search_ctx *ctx;
1N/A int count = 0;
1N/A struct data *data;
1N/A
1N/A if (!file)
1N/A return -1;
1N/A
1N/A ctx = ntfs_attr_get_search_ctx(NULL, file->mft);
1N/A if (!ctx)
1N/A return -1;
1N/A
1N/A while ((rec = find_attribute(AT_DATA, ctx))) {
1N/A data = calloc(1, sizeof(*data));
1N/A if (!data) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in "
1N/A "get_data().\n");
1N/A count = -1;
1N/A break;
1N/A }
1N/A
1N/A data->resident = !rec->non_resident;
1N/A data->compressed = (rec->flags & ATTR_IS_COMPRESSED) ? 1 : 0;
1N/A data->encrypted = (rec->flags & ATTR_IS_ENCRYPTED) ? 1 : 0;
1N/A
1N/A if (rec->name_length) {
1N/A data->uname = (ntfschar *)((char *)rec +
1N/A le16_to_cpu(rec->name_offset));
1N/A data->uname_len = rec->name_length;
1N/A
1N/A if (ntfs_ucstombs(data->uname, data->uname_len,
1N/A &data->name, 0) < 0) {
1N/A ntfs_log_error("ERROR: Cannot translate name "
1N/A "into current locale.\n");
1N/A }
1N/A }
1N/A
1N/A if (data->resident) {
1N/A data->size_data = le32_to_cpu(rec->u.res.value_length);
1N/A data->data = (char*)rec +
1N/A le16_to_cpu(rec->u.res.value_offset);
1N/A } else {
1N/A data->size_alloc = sle64_to_cpu(rec->u.nonres.allocated_size);
1N/A data->size_data = sle64_to_cpu(rec->u.nonres.data_size);
1N/A data->size_init = sle64_to_cpu(rec->u.nonres.initialized_size);
1N/A data->size_vcn = sle64_to_cpu(rec->u.nonres.highest_vcn) + 1;
1N/A }
1N/A
1N/A data->runlist = ntfs_mapping_pairs_decompress(vol, rec, NULL);
1N/A if (!data->runlist) {
1N/A ntfs_log_debug("Couldn't decompress the data runs.\n");
1N/A }
1N/A
1N/A file->max_size = max(file->max_size, data->size_data);
1N/A file->max_size = max(file->max_size, data->size_init);
1N/A
1N/A list_add_tail(&data->list, &file->data);
1N/A count++;
1N/A }
1N/A
1N/A ntfs_attr_put_search_ctx(ctx);
1N/A ntfs_log_debug("File has %d data streams.\n", count);
1N/A return count;
1N/A}
1N/A
1N/A/**
1N/A * read_record - Read an MFT record into memory
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A * @record: The record number to read
1N/A *
1N/A * Read the specified MFT record and gather as much information about it as
1N/A * possible.
1N/A *
1N/A * Return: Pointer A ufile object containing the results
1N/A * NULL Error
1N/A */
1N/Astatic struct ufile * read_record(ntfs_volume *vol, long long record)
1N/A{
1N/A ATTR_RECORD *attr10, *attr20, *attr90;
1N/A struct ufile *file;
1N/A ntfs_attr *mft;
1N/A
1N/A if (!vol)
1N/A return NULL;
1N/A
1N/A file = calloc(1, sizeof(*file));
1N/A if (!file) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in read_record()\n");
1N/A return NULL;
1N/A }
1N/A
1N/A INIT_LIST_HEAD(&file->name);
1N/A INIT_LIST_HEAD(&file->data);
1N/A file->inode = record;
1N/A
1N/A file->mft = malloc(vol->mft_record_size);
1N/A if (!file->mft) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in read_record()\n");
1N/A free_file(file);
1N/A return NULL;
1N/A }
1N/A
1N/A mft = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!mft) {
1N/A ntfs_log_perror("ERROR: Couldn't open $MFT/$DATA");
1N/A free_file(file);
1N/A return NULL;
1N/A }
1N/A
1N/A if (ntfs_attr_mst_pread(mft, vol->mft_record_size * record, 1, vol->mft_record_size, file->mft) < 1) {
1N/A ntfs_log_error("ERROR: Couldn't read MFT Record %lld.\n", record);
1N/A ntfs_attr_close(mft);
1N/A free_file(file);
1N/A return NULL;
1N/A }
1N/A
1N/A ntfs_attr_close(mft);
1N/A mft = NULL;
1N/A
1N/A attr10 = find_first_attribute(AT_STANDARD_INFORMATION, file->mft);
1N/A attr20 = find_first_attribute(AT_ATTRIBUTE_LIST, file->mft);
1N/A attr90 = find_first_attribute(AT_INDEX_ROOT, file->mft);
1N/A
1N/A ntfs_log_debug("Attributes present: %s %s %s.\n", attr10?"0x10":"",
1N/A attr20?"0x20":"", attr90?"0x90":"");
1N/A
1N/A if (attr10) {
1N/A STANDARD_INFORMATION *si;
1N/A si = (STANDARD_INFORMATION *) ((char *) attr10 + le16_to_cpu(attr10->u.res.value_offset));
1N/A file->date = ntfs2utc(si->last_data_change_time);
1N/A }
1N/A
1N/A if (attr20 || !attr10)
1N/A file->attr_list = 1;
1N/A if (attr90)
1N/A file->directory = 1;
1N/A
1N/A if (get_filenames(file, vol) < 0) {
1N/A ntfs_log_error("ERROR: Couldn't get filenames.\n");
1N/A }
1N/A if (get_data(file, vol) < 0) {
1N/A ntfs_log_error("ERROR: Couldn't get data streams.\n");
1N/A }
1N/A
1N/A return file;
1N/A}
1N/A
1N/A/**
1N/A * calc_percentage - Calculate how much of the file is recoverable
1N/A * @file: The file object to work with
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A *
1N/A * Read through all the $DATA streams and determine if each cluster in each
1N/A * stream is still free disk space. This is just measuring the potential for
1N/A * recovery. The data may have still been overwritten by a another file which
1N/A * was then deleted.
1N/A *
1N/A * Files with a resident $DATA stream will have a 100% potential.
1N/A *
1N/A * N.B. If $DATA attribute spans more than one MFT record (i.e. badly
1N/A * fragmented) then only the data in this segment will be used for the
1N/A * calculation.
1N/A *
1N/A * N.B. Currently, compressed and encrypted files cannot be recovered, so they
1N/A * will return 0%.
1N/A *
1N/A * Return: n The percentage of the file that _could_ be recovered
1N/A * -1 Error
1N/A */
1N/Astatic int calc_percentage(struct ufile *file, ntfs_volume *vol)
1N/A{
1N/A runlist_element *rl = NULL;
1N/A struct list_head *pos;
1N/A struct data *data;
1N/A long long i, j;
1N/A long long start, end;
1N/A int clusters_inuse, clusters_free;
1N/A int percent = 0;
1N/A
1N/A if (!file || !vol)
1N/A return -1;
1N/A
1N/A if (file->directory) {
1N/A ntfs_log_debug("Found a directory: not recoverable.\n");
1N/A return 0;
1N/A }
1N/A
1N/A if (list_empty(&file->data)) {
1N/A ntfs_log_verbose("File has no data streams.\n");
1N/A return 0;
1N/A }
1N/A
1N/A list_for_each(pos, &file->data) {
1N/A data = list_entry(pos, struct data, list);
1N/A clusters_inuse = 0;
1N/A clusters_free = 0;
1N/A
1N/A if (data->encrypted) {
1N/A ntfs_log_verbose("File is encrypted, recovery is "
1N/A "impossible.\n");
1N/A continue;
1N/A }
1N/A
1N/A if (data->compressed) {
1N/A ntfs_log_verbose("File is compressed, recovery not yet "
1N/A "implemented.\n");
1N/A continue;
1N/A }
1N/A
1N/A if (data->resident) {
1N/A ntfs_log_verbose("File is resident, therefore "
1N/A "recoverable.\n");
1N/A percent = 100;
1N/A data->percent = 100;
1N/A continue;
1N/A }
1N/A
1N/A rl = data->runlist;
1N/A if (!rl) {
1N/A ntfs_log_verbose("File has no runlist, hence no data."
1N/A "\n");
1N/A continue;
1N/A }
1N/A
1N/A if (rl[0].length <= 0) {
1N/A ntfs_log_verbose("File has an empty runlist, hence no "
1N/A "data.\n");
1N/A continue;
1N/A }
1N/A
1N/A if (rl[0].lcn == LCN_RL_NOT_MAPPED) { /* extended mft record */
1N/A ntfs_log_verbose("Missing segment at beginning, %lld "
1N/A "clusters\n", (long long)rl[0].length);
1N/A clusters_inuse += rl[0].length;
1N/A rl++;
1N/A }
1N/A
1N/A for (i = 0; rl[i].length > 0; i++) {
1N/A if (rl[i].lcn == LCN_RL_NOT_MAPPED) {
1N/A ntfs_log_verbose("Missing segment at end, %lld "
1N/A "clusters\n",
1N/A (long long)rl[i].length);
1N/A clusters_inuse += rl[i].length;
1N/A continue;
1N/A }
1N/A
1N/A if (rl[i].lcn == LCN_HOLE) {
1N/A clusters_free += rl[i].length;
1N/A continue;
1N/A }
1N/A
1N/A start = rl[i].lcn;
1N/A end = rl[i].lcn + rl[i].length;
1N/A
1N/A for (j = start; j < end; j++) {
1N/A if (utils_cluster_in_use(vol, j))
1N/A clusters_inuse++;
1N/A else
1N/A clusters_free++;
1N/A }
1N/A }
1N/A
1N/A if ((clusters_inuse + clusters_free) == 0) {
1N/A ntfs_log_error("ERROR: Unexpected error whilst "
1N/A "calculating percentage for inode %lld\n",
1N/A file->inode);
1N/A continue;
1N/A }
1N/A
1N/A data->percent = (clusters_free * 100) /
1N/A (clusters_inuse + clusters_free);
1N/A
1N/A percent = max(percent, data->percent);
1N/A }
1N/A
1N/A ntfs_log_verbose("File is %d%% recoverable\n", percent);
1N/A return percent;
1N/A}
1N/A
1N/A/**
1N/A * dump_record - Print everything we know about an MFT record
1N/A * @file: The file to work with
1N/A *
1N/A * Output the contents of the file object. This will print everything that has
1N/A * been read from the MFT record, or implied by various means.
1N/A *
1N/A * Because of the redundant nature of NTFS, there will be some duplication of
1N/A * information, though it will have been read from different sources.
1N/A *
1N/A * N.B. If the filename is missing, or couldn't be converted to the current
1N/A * locale, "<none>" will be displayed.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void dump_record(struct ufile *file)
1N/A{
1N/A char buffer[20];
1N/A const char *name;
1N/A struct list_head *item;
1N/A int i;
1N/A
1N/A if (!file)
1N/A return;
1N/A
1N/A ntfs_log_quiet("MFT Record %lld\n", file->inode);
1N/A ntfs_log_quiet("Type: %s\n", (file->directory) ? "Directory" : "File");
1N/A strftime(buffer, sizeof(buffer), "%F %R", localtime(&file->date));
1N/A ntfs_log_quiet("Date: %s\n", buffer);
1N/A
1N/A if (file->attr_list)
1N/A ntfs_log_quiet("Metadata may span more than one MFT record\n");
1N/A
1N/A list_for_each(item, &file->name) {
1N/A struct filename *f = list_entry(item, struct filename, list);
1N/A
1N/A if (f->name)
1N/A name = f->name;
1N/A else
1N/A name = NONE;
1N/A
1N/A ntfs_log_quiet("Filename: (%d) %s\n", f->name_space, f->name);
1N/A ntfs_log_quiet("File Flags: ");
1N/A if (f->flags & FILE_ATTR_SYSTEM)
1N/A ntfs_log_quiet("System ");
1N/A if (f->flags & FILE_ATTR_DIRECTORY)
1N/A ntfs_log_quiet("Directory ");
1N/A if (f->flags & FILE_ATTR_SPARSE_FILE)
1N/A ntfs_log_quiet("Sparse ");
1N/A if (f->flags & FILE_ATTR_REPARSE_POINT)
1N/A ntfs_log_quiet("Reparse ");
1N/A if (f->flags & FILE_ATTR_COMPRESSED)
1N/A ntfs_log_quiet("Compressed ");
1N/A if (f->flags & FILE_ATTR_ENCRYPTED)
1N/A ntfs_log_quiet("Encrypted ");
1N/A if (!(f->flags & (FILE_ATTR_SYSTEM | FILE_ATTR_DIRECTORY |
1N/A FILE_ATTR_SPARSE_FILE | FILE_ATTR_REPARSE_POINT |
1N/A FILE_ATTR_COMPRESSED | FILE_ATTR_ENCRYPTED))) {
1N/A ntfs_log_quiet("%s", NONE);
1N/A }
1N/A
1N/A ntfs_log_quiet("\n");
1N/A
1N/A if (opts.parent) {
1N/A ntfs_log_quiet("Parent: %s\n", f->parent_name ?
1N/A f->parent_name : "<non-determined>");
1N/A }
1N/A
1N/A ntfs_log_quiet("Size alloc: %lld\n", f->size_alloc);
1N/A ntfs_log_quiet("Size data: %lld\n", f->size_data);
1N/A
1N/A strftime(buffer, sizeof(buffer), "%F %R",
1N/A localtime(&f->date_c));
1N/A ntfs_log_quiet("Date C: %s\n", buffer);
1N/A strftime(buffer, sizeof(buffer), "%F %R",
1N/A localtime(&f->date_a));
1N/A ntfs_log_quiet("Date A: %s\n", buffer);
1N/A strftime(buffer, sizeof(buffer), "%F %R",
1N/A localtime(&f->date_m));
1N/A ntfs_log_quiet("Date M: %s\n", buffer);
1N/A strftime(buffer, sizeof(buffer), "%F %R",
1N/A localtime(&f->date_r));
1N/A ntfs_log_quiet("Date R: %s\n", buffer);
1N/A }
1N/A
1N/A ntfs_log_quiet("Data Streams:\n");
1N/A list_for_each(item, &file->data) {
1N/A struct data *d = list_entry(item, struct data, list);
1N/A ntfs_log_quiet("Name: %s\n", (d->name) ? d->name : UNNAMED);
1N/A ntfs_log_quiet("Flags: ");
1N/A if (d->resident) ntfs_log_quiet("Resident\n");
1N/A if (d->compressed) ntfs_log_quiet("Compressed\n");
1N/A if (d->encrypted) ntfs_log_quiet("Encrypted\n");
1N/A if (!d->resident && !d->compressed && !d->encrypted)
1N/A ntfs_log_quiet("None\n");
1N/A else
1N/A ntfs_log_quiet("\n");
1N/A
1N/A ntfs_log_quiet("Size alloc: %lld\n", d->size_alloc);
1N/A ntfs_log_quiet("Size data: %lld\n", d->size_data);
1N/A ntfs_log_quiet("Size init: %lld\n", d->size_init);
1N/A ntfs_log_quiet("Size vcn: %lld\n", d->size_vcn);
1N/A
1N/A ntfs_log_quiet("Data runs:\n");
1N/A if ((!d->runlist) || (d->runlist[0].length <= 0)) {
1N/A ntfs_log_quiet(" None\n");
1N/A } else {
1N/A for (i = 0; d->runlist[i].length > 0; i++) {
1N/A ntfs_log_quiet(" %lld @ %lld\n",
1N/A (long long)d->runlist[i].length,
1N/A (long long)d->runlist[i].lcn);
1N/A }
1N/A }
1N/A
1N/A ntfs_log_quiet("Amount potentially recoverable %d%%\n",
1N/A d->percent);
1N/A }
1N/A
1N/A ntfs_log_quiet("________________________________________\n\n");
1N/A}
1N/A
1N/A/**
1N/A * list_record - Print a one line summary of the file
1N/A * @file: The file to work with
1N/A *
1N/A * Print a one line description of a file.
1N/A *
1N/A * Inode Flags %age Date Size Filename
1N/A *
1N/A * The output will contain the file's inode number (MFT Record), some flags,
1N/A * the percentage of the file that is recoverable, the last modification date,
1N/A * the size and the filename.
1N/A *
1N/A * The flags are F/D = File/Directory, N/R = Data is (Non-)Resident,
1N/A * C = Compressed, E = Encrypted, ! = Metadata may span multiple records.
1N/A *
1N/A * N.B. The file size is stored in many forms in several attributes. This
1N/A * display the largest it finds.
1N/A *
1N/A * N.B. If the filename is missing, or couldn't be converted to the current
1N/A * locale, "<none>" will be displayed.
1N/A *
1N/A * Return: none
1N/A */
1N/Astatic void list_record(struct ufile *file)
1N/A{
1N/A char buffer[20];
1N/A struct list_head *item;
1N/A const char *name = NULL;
1N/A long long size = 0;
1N/A int percent = 0;
1N/A
1N/A char flagd = '.', flagr = '.', flagc = '.', flagx = '.';
1N/A
1N/A strftime(buffer, sizeof(buffer), "%F", localtime(&file->date));
1N/A
1N/A if (file->attr_list)
1N/A flagx = '!';
1N/A
1N/A if (file->directory)
1N/A flagd = 'D';
1N/A else
1N/A flagd = 'F';
1N/A
1N/A list_for_each(item, &file->data) {
1N/A struct data *d = list_entry(item, struct data, list);
1N/A
1N/A if (!d->name) {
1N/A if (d->resident)
1N/A flagr = 'R';
1N/A else
1N/A flagr = 'N';
1N/A if (d->compressed)
1N/A flagc = 'C';
1N/A if (d->encrypted)
1N/A flagc = 'E';
1N/A
1N/A percent = max(percent, d->percent);
1N/A }
1N/A
1N/A size = max(size, d->size_data);
1N/A size = max(size, d->size_init);
1N/A }
1N/A
1N/A if (file->pref_name)
1N/A name = file->pref_name;
1N/A else
1N/A name = NONE;
1N/A
1N/A ntfs_log_quiet("%-8lld %c%c%c%c %3d%% %s %9lld %s\n",
1N/A file->inode, flagd, flagr, flagc, flagx,
1N/A percent, buffer, size, name);
1N/A
1N/A}
1N/A
1N/A/**
1N/A * name_match - Does a file have a name matching a regex
1N/A * @re: The regular expression object
1N/A * @file: The file to be tested
1N/A *
1N/A * Iterate through the file's $FILENAME attributes and compare them against the
1N/A * regular expression, created with regcomp.
1N/A *
1N/A * Return: 1 There is a matching filename.
1N/A * 0 There is no match.
1N/A */
1N/Astatic int name_match(regex_t *re, struct ufile *file)
1N/A{
1N/A struct list_head *item;
1N/A int result;
1N/A
1N/A if (!re || !file)
1N/A return 0;
1N/A
1N/A list_for_each(item, &file->name) {
1N/A struct filename *f = list_entry(item, struct filename, list);
1N/A
1N/A if (!f->name)
1N/A continue;
1N/A result = regexec(re, f->name, 0, NULL, 0);
1N/A if (result < 0) {
1N/A ntfs_log_perror("Couldn't compare filename with regex");
1N/A return 0;
1N/A } else if (result == REG_NOERROR) {
1N/A ntfs_log_debug("Found a matching filename.\n");
1N/A return 1;
1N/A }
1N/A }
1N/A
1N/A ntfs_log_debug("Filename '%s' doesn't match regex.\n", file->pref_name);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * write_data - Write out a block of data
1N/A * @fd: File descriptor to write to
1N/A * @buffer: Data to write
1N/A * @bufsize: Amount of data to write
1N/A *
1N/A * Write a block of data to a file descriptor.
1N/A *
1N/A * Return: -1 Error, something went wrong
1N/A * 0 Success, all the data was written
1N/A */
1N/Astatic unsigned int write_data(int fd, const char *buffer,
1N/A unsigned int bufsize)
1N/A{
1N/A ssize_t result1, result2;
1N/A
1N/A if (!buffer) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A
1N/A result1 = write(fd, buffer, bufsize);
1N/A if ((result1 == (ssize_t) bufsize) || (result1 < 0))
1N/A return result1;
1N/A
1N/A /* Try again with the rest of the buffer */
1N/A buffer += result1;
1N/A bufsize -= result1;
1N/A
1N/A result2 = write(fd, buffer, bufsize);
1N/A if (result2 < 0)
1N/A return result1;
1N/A
1N/A return result1 + result2;
1N/A}
1N/A
1N/A/**
1N/A * create_pathname - Create a path/file from some components
1N/A * @dir: Directory in which to create the file (optional)
1N/A * @name: Filename to give the file (optional)
1N/A * @stream: Name of the stream (optional)
1N/A * @buffer: Store the result here
1N/A * @bufsize: Size of buffer
1N/A *
1N/A * Create a filename from various pieces. The output will be of the form:
1N/A * dir/file
1N/A * dir/file:stream
1N/A * file
1N/A * file:stream
1N/A *
1N/A * All the components are optional. If the name is missing, "unknown" will be
1N/A * used. If the directory is missing the file will be created in the current
1N/A * directory. If the stream name is present it will be appended to the
1N/A * filename, delimited by a colon.
1N/A *
1N/A * N.B. If the buffer isn't large enough the name will be truncated.
1N/A *
1N/A * Return: n Length of the allocated name
1N/A */
1N/Astatic int create_pathname(const char *dir, const char *name,
1N/A const char *stream, char *buffer, int bufsize)
1N/A{
1N/A if (!name)
1N/A name = UNKNOWN;
1N/A
1N/A if (dir)
1N/A if (stream)
1N/A snprintf(buffer, bufsize, "%s/%s:%s", dir, name, stream);
1N/A else
1N/A snprintf(buffer, bufsize, "%s/%s", dir, name);
1N/A else
1N/A if (stream)
1N/A snprintf(buffer, bufsize, "%s:%s", name, stream);
1N/A else
1N/A snprintf(buffer, bufsize, "%s", name);
1N/A
1N/A return strlen(buffer);
1N/A}
1N/A
1N/A/**
1N/A * open_file - Open a file to write to
1N/A * @pathname: Path, name and stream of the file to open
1N/A *
1N/A * Create a file and return the file descriptor.
1N/A *
1N/A * N.B. If option force is given and existing file will be overwritten.
1N/A *
1N/A * Return: -1 Error, failed to create the file
1N/A * n Success, this is the file descriptor
1N/A */
1N/Astatic int open_file(const char *pathname)
1N/A{
1N/A int flags;
1N/A
1N/A ntfs_log_verbose("Creating file: %s\n", pathname);
1N/A
1N/A if (opts.force)
1N/A flags = O_RDWR | O_CREAT | O_TRUNC;
1N/A else
1N/A flags = O_RDWR | O_CREAT | O_EXCL;
1N/A
1N/A return open(pathname, flags, S_IRUSR | S_IWUSR);
1N/A}
1N/A
1N/A/**
1N/A * set_date - Set the file's date and time
1N/A * @pathname: Path and name of the file to alter
1N/A * @date: Date and time to set
1N/A *
1N/A * Give a file a particular date and time.
1N/A *
1N/A * Return: 1 Success, set the file's date and time
1N/A * 0 Error, failed to change the file's date and time
1N/A */
1N/Astatic int set_date(const char *pathname, time_t date)
1N/A{
1N/A struct utimbuf ut;
1N/A
1N/A if (!pathname)
1N/A return 0;
1N/A
1N/A ut.actime = date;
1N/A ut.modtime = date;
1N/A if (utime(pathname, &ut)) {
1N/A ntfs_log_error("ERROR: Couldn't set the file's date and time\n");
1N/A return 0;
1N/A }
1N/A return 1;
1N/A}
1N/A
1N/A/**
1N/A * undelete_file - Recover a deleted file from an NTFS volume
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A * @inode: MFT Record number to be recovered
1N/A *
1N/A * Read an MFT Record and try an recover any data associated with it. Some of
1N/A * the clusters may be in use; these will be filled with zeros or the fill byte
1N/A * supplied in the options.
1N/A *
1N/A * Each data stream will be recovered and saved to a file. The file's name will
1N/A * be the original filename and it will be written to the current directory.
1N/A * Any named data stream will be saved as filename:streamname.
1N/A *
1N/A * The output file's name and location can be altered by using the command line
1N/A * options.
1N/A *
1N/A * N.B. We cannot tell if someone has overwritten some of the data since the
1N/A * file was deleted.
1N/A *
1N/A * Return: 0 Error, something went wrong
1N/A * 1 Success, the data was recovered
1N/A */
1N/Astatic int undelete_file(ntfs_volume *vol, long long inode)
1N/A{
1N/A char pathname[256];
1N/A char *buffer = NULL;
1N/A unsigned int bufsize;
1N/A struct ufile *file;
1N/A int i, j;
1N/A long long start, end;
1N/A runlist_element *rl;
1N/A struct list_head *item;
1N/A int fd = -1;
1N/A long long k;
1N/A int result = 0;
1N/A char *name;
1N/A long long cluster_count; /* I'll need this variable (see below). +mabs */
1N/A
1N/A if (!vol)
1N/A return 0;
1N/A
1N/A /* try to get record */
1N/A file = read_record(vol, inode);
1N/A if (!file || !file->mft) {
1N/A ntfs_log_error("Can't read info from mft record %lld.\n", inode);
1N/A return 0;
1N/A }
1N/A
1N/A /* if flag was not set, print file informations */
1N/A if (avoid_duplicate_printing == 0) {
1N/A if (opts.verbose) {
1N/A dump_record(file);
1N/A } else {
1N/A list_record(file);
1N/A //ntfs_log_quiet("\n");
1N/A }
1N/A }
1N/A
1N/A bufsize = vol->cluster_size;
1N/A buffer = malloc(bufsize);
1N/A if (!buffer)
1N/A goto free;
1N/A
1N/A /* calc_percentage() must be called before dump_record() or
1N/A * list_record(). Otherwise, when undeleting, a file will always be
1N/A * listed as 0% recoverable even if successfully undeleted. +mabs
1N/A */
1N/A if (file->mft->flags & MFT_RECORD_IN_USE) {
1N/A ntfs_log_error("Record is in use by the mft\n");
1N/A if (!opts.force) {
1N/A free(buffer);
1N/A free_file(file);
1N/A return 0;
1N/A }
1N/A ntfs_log_verbose("Forced to continue.\n");
1N/A }
1N/A
1N/A if (calc_percentage(file, vol) == 0) {
1N/A ntfs_log_quiet("File has no recoverable data.\n");
1N/A goto free;
1N/A }
1N/A
1N/A if (list_empty(&file->data)) {
1N/A ntfs_log_quiet("File has no data. There is nothing to recover.\n");
1N/A goto free;
1N/A }
1N/A
1N/A list_for_each(item, &file->data) {
1N/A struct data *d = list_entry(item, struct data, list);
1N/A
1N/A if (opts.output)
1N/A name = opts.output;
1N/A else
1N/A name = file->pref_name;
1N/A
1N/A create_pathname(opts.dest, name, d->name, pathname, sizeof(pathname));
1N/A if (d->resident) {
1N/A fd = open_file(pathname);
1N/A if (fd < 0) {
1N/A ntfs_log_perror("Couldn't create file");
1N/A goto free;
1N/A }
1N/A
1N/A ntfs_log_verbose("File has resident data.\n");
1N/A if (write_data(fd, d->data, d->size_data) < d->size_data) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A
1N/A if (close(fd) < 0) {
1N/A ntfs_log_perror("Close failed");
1N/A }
1N/A fd = -1;
1N/A } else {
1N/A rl = d->runlist;
1N/A if (!rl) {
1N/A ntfs_log_verbose("File has no runlist, hence no data.\n");
1N/A continue;
1N/A }
1N/A
1N/A if (rl[0].length <= 0) {
1N/A ntfs_log_verbose("File has an empty runlist, hence no data.\n");
1N/A continue;
1N/A }
1N/A
1N/A fd = open_file(pathname);
1N/A if (fd < 0) {
1N/A ntfs_log_perror("Couldn't create output file");
1N/A goto free;
1N/A }
1N/A
1N/A if (rl[0].lcn == LCN_RL_NOT_MAPPED) { /* extended mft record */
1N/A ntfs_log_verbose("Missing segment at beginning, %lld "
1N/A "clusters.\n",
1N/A (long long)rl[0].length);
1N/A memset(buffer, opts.fillbyte, bufsize);
1N/A for (k = 0; k < rl[0].length * vol->cluster_size; k += bufsize) {
1N/A if (write_data(fd, buffer, bufsize) < bufsize) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A }
1N/A }
1N/A
1N/A cluster_count = 0LL;
1N/A for (i = 0; rl[i].length > 0; i++) {
1N/A
1N/A if (rl[i].lcn == LCN_RL_NOT_MAPPED) {
1N/A ntfs_log_verbose("Missing segment at end, "
1N/A "%lld clusters.\n",
1N/A (long long)rl[i].length);
1N/A memset(buffer, opts.fillbyte, bufsize);
1N/A for (k = 0; k < rl[k].length * vol->cluster_size; k += bufsize) {
1N/A if (write_data(fd, buffer, bufsize) < bufsize) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A cluster_count++;
1N/A }
1N/A continue;
1N/A }
1N/A
1N/A if (rl[i].lcn == LCN_HOLE) {
1N/A ntfs_log_verbose("File has a sparse section.\n");
1N/A memset(buffer, 0, bufsize);
1N/A for (k = 0; k < rl[k].length * vol->cluster_size; k += bufsize) {
1N/A if (write_data(fd, buffer, bufsize) < bufsize) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A }
1N/A continue;
1N/A }
1N/A
1N/A start = rl[i].lcn;
1N/A end = rl[i].lcn + rl[i].length;
1N/A
1N/A for (j = start; j < end; j++) {
1N/A if (utils_cluster_in_use(vol, j) && !opts.optimistic) {
1N/A memset(buffer, opts.fillbyte, bufsize);
1N/A if (write_data(fd, buffer, bufsize) < bufsize) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A } else {
1N/A if (ntfs_cluster_read(vol, j, 1, buffer) < 1) {
1N/A ntfs_log_perror("Read failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A if (write_data(fd, buffer, bufsize) < bufsize) {
1N/A ntfs_log_perror("Write failed");
1N/A close(fd);
1N/A goto free;
1N/A }
1N/A cluster_count++;
1N/A }
1N/A }
1N/A }
1N/A ntfs_log_quiet("\n");
1N/A
1N/A /*
1N/A * The following block of code implements the --truncate option.
1N/A * Its semantics are as follows:
1N/A * IF opts.truncate is set AND data stream currently being recovered is
1N/A * non-resident AND data stream has no holes (100% recoverability) AND
1N/A * 0 <= (data->size_alloc - data->size_data) <= vol->cluster_size AND
1N/A * cluster_count * vol->cluster_size == data->size_alloc THEN file
1N/A * currently being written is truncated to data->size_data bytes before
1N/A * it's closed.
1N/A * This multiple checks try to ensure that only files with consistent
1N/A * values of size/occupied clusters are eligible for truncation. Note
1N/A * that resident streams need not be truncated, since the original code
1N/A * already recovers their exact length. +mabs
1N/A */
1N/A if (opts.truncate) {
1N/A if (d->percent == 100 && d->size_alloc >= d->size_data &&
1N/A (d->size_alloc - d->size_data) <= (long long)vol->cluster_size &&
1N/A cluster_count * (long long)vol->cluster_size == d->size_alloc) {
1N/A if (ftruncate(fd, (off_t)d->size_data))
1N/A ntfs_log_perror("Truncation failed");
1N/A } else ntfs_log_quiet("Truncation not performed because file has an "
1N/A "inconsistent $MFT record.\n");
1N/A }
1N/A
1N/A if (close(fd) < 0) {
1N/A ntfs_log_perror("Close failed");
1N/A }
1N/A fd = -1;
1N/A
1N/A }
1N/A set_date(pathname, file->date);
1N/A if (d->name)
1N/A ntfs_log_quiet("Undeleted '%s:%s' successfully.\n", file->pref_name, d->name);
1N/A else
1N/A ntfs_log_quiet("Undeleted '%s' successfully.\n", file->pref_name);
1N/A }
1N/A result = 1;
1N/Afree:
1N/A if (buffer)
1N/A free(buffer);
1N/A free_file(file);
1N/A return result;
1N/A}
1N/A
1N/A/**
1N/A * scan_disk - Search an NTFS volume for files that could be undeleted
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A *
1N/A * Read through all the MFT entries looking for deleted files. For each one
1N/A * determine how much of the data lies in unused disk space.
1N/A *
1N/A * The list can be filtered by name, size and date, using command line options.
1N/A *
1N/A * Return: -1 Error, something went wrong
1N/A * n Success, the number of recoverable files
1N/A */
1N/Astatic int scan_disk(ntfs_volume *vol)
1N/A{
1N/A s64 nr_mft_records;
1N/A const int BUFSIZE = 8192;
1N/A char *buffer = NULL;
1N/A int results = 0;
1N/A ntfs_attr *attr;
1N/A long long size;
1N/A long long bmpsize;
1N/A int i, j, k, b;
1N/A int percent;
1N/A struct ufile *file;
1N/A regex_t re;
1N/A
1N/A if (!vol)
1N/A return -1;
1N/A
1N/A attr = ntfs_attr_open(vol->mft_ni, AT_BITMAP, AT_UNNAMED, 0);
1N/A if (!attr) {
1N/A ntfs_log_perror("ERROR: Couldn't open $MFT/$BITMAP");
1N/A return -1;
1N/A }
1N/A bmpsize = attr->initialized_size;
1N/A
1N/A buffer = malloc(BUFSIZE);
1N/A if (!buffer) {
1N/A ntfs_log_error("ERROR: Couldn't allocate memory in scan_disk()\n");
1N/A results = -1;
1N/A goto out;
1N/A }
1N/A
1N/A if (opts.match) {
1N/A int flags = REG_NOSUB;
1N/A
1N/A if (!opts.match_case)
1N/A flags |= REG_ICASE;
1N/A if (regcomp(&re, opts.match, flags)) {
1N/A ntfs_log_error("ERROR: Couldn't create a regex.\n");
1N/A goto out;
1N/A }
1N/A }
1N/A
1N/A nr_mft_records = vol->mft_na->initialized_size >>
1N/A vol->mft_record_size_bits;
1N/A
1N/A ntfs_log_quiet("Inode Flags %%age Date Size Filename\n");
1N/A ntfs_log_quiet("---------------------------------------------------------------\n");
1N/A for (i = 0; i < bmpsize; i += BUFSIZE) {
1N/A long long read_count = min((bmpsize - i), BUFSIZE);
1N/A size = ntfs_attr_pread(attr, i, read_count, buffer);
1N/A if (size < 0)
1N/A break;
1N/A
1N/A for (j = 0; j < size; j++) {
1N/A b = buffer[j];
1N/A for (k = 0; k < 8; k++, b>>=1) {
1N/A if (((i+j)*8+k) >= nr_mft_records)
1N/A goto done;
1N/A if (b & 1)
1N/A continue;
1N/A file = read_record(vol, (i+j)*8+k);
1N/A if (!file) {
1N/A ntfs_log_error("Couldn't read MFT Record %d.\n", (i+j)*8+k);
1N/A continue;
1N/A }
1N/A
1N/A if ((opts.since > 0) && (file->date <= opts.since))
1N/A goto skip;
1N/A if (opts.match && !name_match(&re, file))
1N/A goto skip;
1N/A if (opts.size_begin && (opts.size_begin > file->max_size))
1N/A goto skip;
1N/A if (opts.size_end && (opts.size_end < file->max_size))
1N/A goto skip;
1N/A
1N/A percent = calc_percentage(file, vol);
1N/A if ((opts.percent == -1) || (percent >= opts.percent)) {
1N/A if (opts.verbose)
1N/A dump_record(file);
1N/A else
1N/A list_record(file);
1N/A
1N/A /* Was -u specified with no inode
1N/A so undelete file by regex */
1N/A if (opts.mode == MODE_UNDELETE) {
1N/A if (!undelete_file(vol, file->inode))
1N/A ntfs_log_verbose("ERROR: Failed to undelete "
1N/A "inode %lli\n!",
1N/A file->inode);
1N/A ntfs_log_info("\n");
1N/A }
1N/A }
1N/A if (((opts.percent == -1) && (percent > 0)) ||
1N/A ((opts.percent > 0) && (percent >= opts.percent))) {
1N/A results++;
1N/A }
1N/Askip:
1N/A free_file(file);
1N/A }
1N/A }
1N/A }
1N/Adone:
1N/A ntfs_log_quiet("\nFiles with potentially recoverable content: %d\n",
1N/A results);
1N/Aout:
1N/A if (opts.match)
1N/A regfree(&re);
1N/A free(buffer);
1N/A if (attr)
1N/A ntfs_attr_close(attr);
1N/A return results;
1N/A}
1N/A
1N/A/**
1N/A * copy_mft - Write a range of MFT Records to a file
1N/A * @vol: An ntfs volume obtained from ntfs_mount
1N/A * @mft_begin: First MFT Record to save
1N/A * @mft_end: Last MFT Record to save
1N/A *
1N/A * Read a number of MFT Records and write them to a file.
1N/A *
1N/A * Return: 0 Success, all the records were written
1N/A * 1 Error, something went wrong
1N/A */
1N/Astatic int copy_mft(ntfs_volume *vol, long long mft_begin, long long mft_end)
1N/A{
1N/A s64 nr_mft_records;
1N/A char pathname[256];
1N/A ntfs_attr *mft;
1N/A char *buffer;
1N/A const char *name;
1N/A long long i;
1N/A int result = 1;
1N/A int fd;
1N/A
1N/A if (!vol)
1N/A return 1;
1N/A
1N/A if (mft_end < mft_begin) {
1N/A ntfs_log_error("Range to copy is backwards.\n");
1N/A return 1;
1N/A }
1N/A
1N/A buffer = malloc(vol->mft_record_size);
1N/A if (!buffer) {
1N/A ntfs_log_error("Couldn't allocate memory in copy_mft()\n");
1N/A return 1;
1N/A }
1N/A
1N/A mft = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0);
1N/A if (!mft) {
1N/A ntfs_log_perror("Couldn't open $MFT/$DATA");
1N/A goto free;
1N/A }
1N/A
1N/A name = opts.output;
1N/A if (!name) {
1N/A name = MFTFILE;
1N/A ntfs_log_debug("No output filename, defaulting to '%s'.\n",
1N/A name);
1N/A }
1N/A
1N/A create_pathname(opts.dest, name, NULL, pathname, sizeof(pathname));
1N/A fd = open_file(pathname);
1N/A if (fd < 0) {
1N/A ntfs_log_perror("Couldn't open output file '%s'", name);
1N/A goto attr;
1N/A }
1N/A
1N/A nr_mft_records = vol->mft_na->initialized_size >>
1N/A vol->mft_record_size_bits;
1N/A
1N/A mft_end = min(mft_end, nr_mft_records - 1);
1N/A
1N/A ntfs_log_debug("MFT records:\n");
1N/A ntfs_log_debug("\tTotal: %8lld\n", nr_mft_records);
1N/A ntfs_log_debug("\tBegin: %8lld\n", mft_begin);
1N/A ntfs_log_debug("\tEnd: %8lld\n", mft_end);
1N/A
1N/A for (i = mft_begin; i <= mft_end; i++) {
1N/A if (ntfs_attr_pread(mft, vol->mft_record_size * i,
1N/A vol->mft_record_size, buffer) < vol->mft_record_size) {
1N/A ntfs_log_perror("Couldn't read MFT Record %lld", i);
1N/A goto close;
1N/A }
1N/A
1N/A if (write_data(fd, buffer, vol->mft_record_size) < vol->mft_record_size) {
1N/A ntfs_log_perror("Write failed");
1N/A goto close;
1N/A }
1N/A }
1N/A
1N/A ntfs_log_verbose("Read %lld MFT Records\n", mft_end - mft_begin + 1);
1N/A result = 0;
1N/Aclose:
1N/A close(fd);
1N/Aattr:
1N/A ntfs_attr_close(mft);
1N/Afree:
1N/A free(buffer);
1N/A return result;
1N/A}
1N/A
1N/A/**
1N/A * handle_undelete
1N/A *
1N/A * Handles the undelete
1N/A */
1N/Astatic int handle_undelete(ntfs_volume *vol)
1N/A{
1N/A int result = 1;
1N/A int i;
1N/A unsigned long long inode;
1N/A
1N/A /* Check whether (an) inode(s) was specified or at least a regex! */
1N/A if (nr_entries == 0) {
1N/A if (with_regex == 0) {
1N/A ntfs_log_error("ERROR: NO inode(s) AND NO match-regex "
1N/A "specified!\n");
1N/A } else {
1N/A avoid_duplicate_printing= 1;
1N/A result = !scan_disk(vol);
1N/A if (result)
1N/A ntfs_log_verbose("ERROR: Failed to scan device "
1N/A "'%s'.\n", opts.device);
1N/A }
1N/A } else {
1N/A /* Normal undelete by specifying inode(s) */
1N/A ntfs_log_quiet("Inode Flags %%age Date Size Filename\n");
1N/A ntfs_log_quiet("---------------------------------------------------------------\n");
1N/A
1N/A /* loop all given inodes */
1N/A for (i = 0; i < nr_entries; i++) {
1N/A for (inode = ranges[i].begin; inode <= ranges[i].end; inode ++) {
1N/A /* Now undelete file */
1N/A result = !undelete_file(vol, inode);
1N/A if (result)
1N/A ntfs_log_verbose("ERROR: Failed to "
1N/A "undelete inode %lli\n!", inode);
1N/A }
1N/A }
1N/A }
1N/A return (result);
1N/A}
1N/A
1N/A/**
1N/A * main - Begin here
1N/A *
1N/A * Start from here.
1N/A *
1N/A * Return: 0 Success, the program worked
1N/A * 1 Error, something went wrong
1N/A */
1N/Aint main(int argc, char *argv[])
1N/A{
1N/A ntfs_volume *vol;
1N/A int result = 1;
1N/A
1N/A ntfs_log_set_handler(ntfs_log_handler_outerr);
1N/A
1N/A with_regex = 0;
1N/A avoid_duplicate_printing = 0;
1N/A
1N/A if (!parse_options(argc, argv))
1N/A goto free;
1N/A
1N/A utils_set_locale();
1N/A
1N/A vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY |
1N/A (opts.force ? NTFS_MNT_FORCE : 0));
1N/A if (!vol)
1N/A return 1;
1N/A
1N/A /* handling of the different modes */
1N/A switch (opts.mode) {
1N/A /* Scanning */
1N/A case MODE_SCAN:
1N/A result = !scan_disk(vol);
1N/A if (result)
1N/A ntfs_log_verbose("ERROR: Failed to scan device '%s'.\n",
1N/A opts.device);
1N/A break;
1N/A
1N/A /* Undelete-handling */
1N/A case MODE_UNDELETE:
1N/A result= handle_undelete(vol);
1N/A break;
1N/A
1N/A /* Handling of copy mft */
1N/A case MODE_COPY:
1N/A result = !copy_mft(vol, opts.mft_begin, opts.mft_end);
1N/A if (result)
1N/A ntfs_log_verbose("ERROR: Failed to read MFT blocks "
1N/A "%lld-%lld.\n", opts.mft_begin,
1N/A min((vol->mft_na->initialized_size >>
1N/A vol->mft_record_size_bits) , opts.mft_end));
1N/A break;
1N/A default:
1N/A ; /* Cannot happen */
1N/A }
1N/A
1N/A ntfs_umount(vol, FALSE);
1N/Afree:
1N/A if (opts.match)
1N/A free(opts.match);
1N/A
1N/A return result;
1N/A}
1N/A