2004-03-07 01:42:28 +08:00
|
|
|
/**
|
|
|
|
* ntfsmove - Part of the Linux-NTFS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Richard Russon
|
2005-10-07 22:10:56 +08:00
|
|
|
* Copyright (c) 2003-2005 Anton Altaparmakov
|
2004-03-07 01:42:28 +08:00
|
|
|
*
|
|
|
|
* This utility will move files on an NTFS volume.
|
|
|
|
*
|
|
|
|
* 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_GETOPT_H
|
2004-03-07 01:42:28 +08:00
|
|
|
#include <getopt.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
|
2016-02-17 03:43:00 +08:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "types.h"
|
|
|
|
#include "attrib.h"
|
2004-03-07 01:42:28 +08:00
|
|
|
#include "utils.h"
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "volume.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "bitmap.h"
|
2004-03-07 01:42:28 +08:00
|
|
|
#include "ntfsmove.h"
|
2010-11-22 22:26:27 +08:00
|
|
|
/* #include "version.h" */
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "logging.h"
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
static const char *EXEC_NAME = "ntfsmove";
|
|
|
|
static struct options opts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* version - Print version information about the program
|
|
|
|
*
|
|
|
|
* Print a copyright statement and a brief description of the program.
|
|
|
|
*
|
|
|
|
* Return: none
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static void version(void)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
2010-11-22 21:00:04 +08:00
|
|
|
ntfs_log_info("\n%s v%s (libntfs-3g) - Move files and directories on an "
|
|
|
|
"NTFS volume.\n\n", EXEC_NAME, VERSION);
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("Copyright (c) 2003 Richard Russon\n");
|
|
|
|
ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* usage - Print a list of the parameters to the program
|
|
|
|
*
|
|
|
|
* Print a list of the parameters and options for the program.
|
|
|
|
*
|
|
|
|
* Return: none
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static void usage(void)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("\nUsage: %s [options] device file\n"
|
2004-03-07 01:42:28 +08:00
|
|
|
"\n"
|
|
|
|
" -S --start Move to the start of the volume\n"
|
|
|
|
" -B --best Move to the best place on the volume\n"
|
|
|
|
" -E --end Move to the end of the volume\n"
|
|
|
|
" -C num --cluster num Move to this cluster offset\n"
|
|
|
|
"\n"
|
|
|
|
" -D --no-dirty Do not mark volume dirty (require chkdsk)\n"
|
|
|
|
" -n --no-action Do not write to disk\n"
|
|
|
|
" -f --force Use less caution\n"
|
|
|
|
" -h --help Print this help\n"
|
|
|
|
" -q --quiet Less output\n"
|
|
|
|
" -V --version Version information\n"
|
|
|
|
" -v --verbose More output\n\n",
|
|
|
|
EXEC_NAME);
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse_options - Read and validate the programs command line
|
|
|
|
*
|
|
|
|
* Read the command line, verify the syntax and parse the options.
|
|
|
|
* This function is very long, but quite simple.
|
|
|
|
*
|
|
|
|
* Return: 1 Success
|
|
|
|
* 0 Error, one or more problems
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int parse_options(int argc, char **argv)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
static const char *sopt = "-BC:DEfh?nqSVv";
|
|
|
|
static const struct option lopt[] = {
|
|
|
|
{ "best", no_argument, NULL, 'B' },
|
|
|
|
{ "cluster", required_argument, NULL, 'C' },
|
|
|
|
{ "end", no_argument, NULL, 'E' },
|
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "no-action", no_argument, NULL, 'n' },
|
|
|
|
{ "no-dirty", no_argument, NULL, 'D' },
|
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "start", no_argument, NULL, 'S' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2006-04-05 20:43:06 +08:00
|
|
|
int c = -1;
|
2004-03-07 01:42:28 +08:00
|
|
|
int err = 0;
|
|
|
|
int ver = 0;
|
|
|
|
int help = 0;
|
2005-10-29 02:39:52 +08:00
|
|
|
int levels = 0;
|
2004-03-07 01:42:28 +08:00
|
|
|
char *end = NULL;
|
|
|
|
|
|
|
|
opterr = 0; /* We'll handle the errors, thank you. */
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
|
2004-03-07 01:42:28 +08:00
|
|
|
switch (c) {
|
|
|
|
case 1: /* A non-option argument */
|
|
|
|
if (!opts.device) {
|
|
|
|
opts.device = argv[optind-1];
|
|
|
|
} else if (!opts.file) {
|
|
|
|
opts.file = argv[optind-1];
|
|
|
|
} else {
|
|
|
|
opts.device = NULL;
|
|
|
|
opts.file = NULL;
|
|
|
|
err++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
if (opts.location == 0)
|
|
|
|
opts.location = NTFS_MOVE_LOC_BEST;
|
|
|
|
else
|
|
|
|
opts.location = -1;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
if (opts.location == 0) {
|
2005-10-27 06:54:57 +08:00
|
|
|
opts.location = strtoll(optarg, &end, 0);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (end && *end)
|
|
|
|
err++;
|
|
|
|
} else {
|
|
|
|
opts.location = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
opts.nodirty++;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
if (opts.location == 0)
|
|
|
|
opts.location = NTFS_MOVE_LOC_END;
|
|
|
|
else
|
|
|
|
opts.location = -1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
opts.force++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2005-10-29 02:39:52 +08:00
|
|
|
if (strncmp (argv[optind-1], "--log-", 6) == 0) {
|
|
|
|
if (!ntfs_log_parse_option (argv[optind-1]))
|
|
|
|
err++;
|
|
|
|
break;
|
|
|
|
}
|
2004-03-07 01:42:28 +08:00
|
|
|
help++;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
opts.noaction++;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
opts.quiet++;
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET);
|
2004-03-07 01:42:28 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if (opts.location == 0)
|
|
|
|
opts.location = NTFS_MOVE_LOC_START;
|
|
|
|
else
|
|
|
|
opts.location = -1;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
ver++;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
opts.verbose++;
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
|
2004-03-07 01:42:28 +08:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]);
|
2004-03-07 01:42:28 +08:00
|
|
|
err++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
/* Make sure we're in sync with the log levels */
|
|
|
|
levels = ntfs_log_get_levels();
|
|
|
|
if (levels & NTFS_LOG_LEVEL_VERBOSE)
|
|
|
|
opts.verbose++;
|
|
|
|
if (!(levels & NTFS_LOG_LEVEL_QUIET))
|
|
|
|
opts.quiet++;
|
|
|
|
|
2004-03-07 01:42:28 +08:00
|
|
|
if (help || ver) {
|
|
|
|
opts.quiet = 0;
|
|
|
|
} else {
|
|
|
|
if ((opts.device == NULL) ||
|
|
|
|
(opts.file == NULL)) {
|
|
|
|
if (argc > 1)
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("You must specify one device and one file.\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
err++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.quiet && opts.verbose) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("You may not use --quiet and --verbose at the "
|
2004-03-07 01:42:28 +08:00
|
|
|
"same time.\n");
|
|
|
|
err++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.location == -1) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("You may only specify one location option: "
|
2004-03-07 01:42:28 +08:00
|
|
|
"--start, --best, --end or --cluster\n");
|
|
|
|
err++;
|
|
|
|
} else if (opts.location == 0) {
|
|
|
|
opts.location = NTFS_MOVE_LOC_BEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ver)
|
|
|
|
version();
|
|
|
|
if (help || err)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
return (!err && !help && !ver);
|
|
|
|
}
|
|
|
|
|
2004-08-20 20:04:47 +08:00
|
|
|
#if 0
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ntfs_debug_runlist_dump2 - Dump a runlist.
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int ntfs_debug_runlist_dump2(const runlist *rl, int abbr, char *prefix)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
//int abbr = 3; /* abbreviate long lists */
|
|
|
|
int len = 0;
|
|
|
|
int i;
|
|
|
|
int res = 0;
|
|
|
|
u64 total = 0;
|
|
|
|
const char *lcn_str[5] = { "HOLE", "NOTMAP", "ENOENT", "EINVAL", "XXXX" };
|
|
|
|
|
|
|
|
if (!rl) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info(" Run list not present.\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefix)
|
|
|
|
prefix = "";
|
|
|
|
|
|
|
|
if (abbr)
|
|
|
|
for (len = 0; rl[len].length; len++) ;
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s VCN LCN len\n", prefix);
|
2004-03-07 01:42:28 +08:00
|
|
|
for (i = 0; rl->length; i++, rl++) {
|
|
|
|
LCN lcn = rl->lcn;
|
|
|
|
|
|
|
|
total += rl->length;
|
|
|
|
if (abbr)
|
|
|
|
if (len > 20) {
|
|
|
|
if ((i == abbr) && (len > (abbr*2)))
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s ... ... ...\n", prefix);
|
2004-03-07 01:42:28 +08:00
|
|
|
if ((i > (abbr-1)) && (i < (len - (abbr-1))))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rl->vcn < -1)
|
|
|
|
res = -1;
|
|
|
|
|
|
|
|
if (lcn < (LCN)0) {
|
2004-08-20 20:30:34 +08:00
|
|
|
int j = -lcn - 1;
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2004-08-20 20:30:34 +08:00
|
|
|
if ((j < 0) || (j > 4)) {
|
|
|
|
j = 4;
|
2004-03-07 01:42:28 +08:00
|
|
|
res = -1;
|
|
|
|
}
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s%8lld %8s %8lld\n", prefix,
|
2004-08-20 20:30:34 +08:00
|
|
|
rl->vcn, lcn_str[j], rl->length);
|
2004-03-07 01:42:28 +08:00
|
|
|
} else
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s%8lld %8lld %8lld\n", prefix,
|
2004-03-07 01:42:28 +08:00
|
|
|
rl->vcn, rl->lcn, rl->length);
|
|
|
|
}
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("%s --------\n", prefix);
|
|
|
|
ntfs_log_info("%s %8lld\n", prefix, total);
|
|
|
|
ntfs_log_info("\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-08-20 20:04:47 +08:00
|
|
|
#endif /* if 0 */
|
|
|
|
|
2004-03-07 01:42:28 +08:00
|
|
|
/**
|
|
|
|
* resize_nonres_attr
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int resize_nonres_attr(MFT_RECORD *m, ATTR_RECORD *a, const u32 new_size)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int this_attr;
|
|
|
|
int next_attr;
|
|
|
|
int tail_size;
|
|
|
|
int file_size;
|
|
|
|
int old_size;
|
|
|
|
u8 *ptr;
|
|
|
|
|
2016-01-27 22:28:55 +08:00
|
|
|
old_size = le32_to_cpu(a->length);
|
|
|
|
file_size = le32_to_cpu(m->bytes_in_use);
|
2004-03-07 01:42:28 +08:00
|
|
|
this_attr = p2n(a)-p2n(m);
|
2016-01-27 22:28:55 +08:00
|
|
|
next_attr = this_attr + le32_to_cpu(a->length);
|
2004-03-07 01:42:28 +08:00
|
|
|
tail_size = file_size - next_attr;
|
|
|
|
ptr = (u8*) m;
|
|
|
|
|
|
|
|
/*
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("old_size = %d\n", old_size);
|
|
|
|
ntfs_log_info("new_size = %d\n", new_size);
|
|
|
|
ntfs_log_info("file_size = %d\n", file_size);
|
|
|
|
ntfs_log_info("this_attr = %d\n", this_attr);
|
|
|
|
ntfs_log_info("next_attr = %d\n", next_attr);
|
|
|
|
ntfs_log_info("tail_size = %d\n", tail_size);
|
2004-03-07 01:42:28 +08:00
|
|
|
*/
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
memmove(ptr + this_attr + new_size, ptr + next_attr, tail_size);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2016-01-27 22:28:55 +08:00
|
|
|
a->length = cpu_to_le32(new_size);
|
|
|
|
m->bytes_in_use = cpu_to_le32(le32_to_cpu(m->bytes_in_use) + (new_size - old_size));
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* calc_attr_length
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int calc_attr_length(ATTR_RECORD *rec, int runlength)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (!rec)
|
|
|
|
return -1;
|
|
|
|
if (!rec->non_resident)
|
|
|
|
return -1;
|
|
|
|
|
2016-01-27 22:28:55 +08:00
|
|
|
size = le16_to_cpu(rec->mapping_pairs_offset) + runlength + 7;
|
2004-03-07 01:42:28 +08:00
|
|
|
size &= 0xFFF8;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2004-08-20 20:04:47 +08:00
|
|
|
#if 0
|
|
|
|
|
2004-03-07 01:42:28 +08:00
|
|
|
/**
|
|
|
|
* dump_runs
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static void dump_runs(u8 *buffer, int len)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int i;
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("RUN: \e[01;31m");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info(" %02x", buffer[i]);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("\e[0m\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
2004-08-20 20:04:47 +08:00
|
|
|
#endif /* if 0 */
|
|
|
|
|
2004-03-07 01:42:28 +08:00
|
|
|
/**
|
|
|
|
* find_unused
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static runlist * find_unused(ntfs_volume *vol, s64 size, u64 loc
|
2005-06-20 05:09:40 +08:00
|
|
|
__attribute__((unused)), int flags __attribute__((unused)))
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
const int bufsize = 8192;
|
|
|
|
u8 *buffer;
|
|
|
|
int clus;
|
|
|
|
int i;
|
|
|
|
int curr = 0;
|
|
|
|
int count = 0;
|
|
|
|
s64 start = 0;
|
|
|
|
int bit = 0;
|
|
|
|
runlist *res = NULL;
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("find_unused\n");
|
2005-10-27 06:54:57 +08:00
|
|
|
buffer = malloc(bufsize);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!buffer) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!buffer\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("looking for space for %lld clusters\n", size);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
clus = vol->lcnbmp_na->allocated_size / bufsize;
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("clus = %d\n", clus);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
for (i = 0; i < clus; i++) {
|
2004-08-20 20:30:34 +08:00
|
|
|
int bytes_read, j;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
bytes_read = ntfs_attr_pread(vol->lcnbmp_na, i*bufsize,
|
2004-08-20 20:30:34 +08:00
|
|
|
bufsize, buffer);
|
|
|
|
if (bytes_read != bufsize) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!read\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (j = 0; j < bufsize*8; j++) {
|
2005-10-27 06:54:57 +08:00
|
|
|
bit = !!test_bit(j & 7, buffer[j>>3]);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (curr == bit) {
|
|
|
|
count++;
|
|
|
|
if ((!bit) && (count >= size)) {
|
2005-10-27 06:54:57 +08:00
|
|
|
//res = calloc(2, sizeof(*res));
|
|
|
|
res = calloc(1, 4096);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res) {
|
|
|
|
res[0].vcn = 0;
|
|
|
|
res[0].lcn = start;
|
|
|
|
res[0].length = size;
|
|
|
|
res[1].lcn = LCN_ENOENT;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("%d * %d\n", curr, count);
|
2004-03-07 01:42:28 +08:00
|
|
|
curr = bit;
|
|
|
|
count = 1;
|
|
|
|
start = i*bufsize*8 + j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("%d * %d\n", curr, count);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
free(buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
for (i = 0; i < size; i++) {
|
2005-10-27 06:54:57 +08:00
|
|
|
if (utils_cluster_in_use(vol, res->lcn + i)) {
|
2011-07-05 18:17:28 +08:00
|
|
|
ntfs_log_info("ERROR cluster %lld in use\n",
|
|
|
|
(long long)res->lcn + i);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("failed\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dont_move
|
|
|
|
*
|
|
|
|
* Don't let the user move:
|
|
|
|
* ANY metadata
|
|
|
|
* Any fragmented MFT records
|
|
|
|
* The boot file 'ntldr'
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int dont_move(ntfs_inode *ino)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
2004-05-14 21:02:33 +08:00
|
|
|
static const ntfschar ntldr[6] = {
|
2004-03-07 01:42:28 +08:00
|
|
|
const_cpu_to_le16('n'), const_cpu_to_le16('t'), const_cpu_to_le16('l'),
|
|
|
|
const_cpu_to_le16('d'), const_cpu_to_le16('r'), const_cpu_to_le16('\0')
|
|
|
|
};
|
|
|
|
|
|
|
|
ATTR_RECORD *rec;
|
|
|
|
FILE_NAME_ATTR *name;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (utils_is_metadata(ino)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("metadata\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
rec = find_first_attribute(AT_ATTRIBUTE_LIST, ino->mrec);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (rec) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("attribute list\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
rec = find_first_attribute(AT_FILE_NAME, ino->mrec);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!rec) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("extend inode\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-27 22:28:55 +08:00
|
|
|
name = (FILE_NAME_ATTR*) ((u8*)rec + le16_to_cpu(rec->value_offset));
|
2005-10-27 06:54:57 +08:00
|
|
|
if (ntfs_names_are_equal(ntldr, 5, name->file_name, name->file_name_length,
|
2004-03-07 01:42:28 +08:00
|
|
|
IGNORE_CASE, ino->vol->upcase, ino->vol->upcase_len)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("ntldr\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_alloc
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int bitmap_alloc(ntfs_volume *vol, runlist_element *rl)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!rl)
|
|
|
|
return -1;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
res = ntfs_bitmap_set_run(vol->lcnbmp_na, rl->lcn, rl->length);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("bitmap alloc returns %d\n", res);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bitmap_free
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int bitmap_free(ntfs_volume *vol, runlist_element *rl)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!rl)
|
|
|
|
return -1;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
res = ntfs_bitmap_clear_run(vol->lcnbmp_na, rl->lcn, rl->length);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("bitmap free returns %d\n", res);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data_copy
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static int data_copy(ntfs_volume *vol, runlist_element *from, runlist_element *to)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u8 *buffer;
|
|
|
|
s64 res = 0;
|
|
|
|
|
|
|
|
if (!vol || !from || !to)
|
|
|
|
return -1;
|
|
|
|
if ((from->length != to->length) || (from->lcn < 0) || (to->lcn < 0))
|
|
|
|
return -1;
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("data_copy: from 0x%llx to 0x%llx\n", from->lcn, to->lcn);
|
2005-10-27 06:54:57 +08:00
|
|
|
buffer = malloc(vol->cluster_size);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!buffer) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!buffer\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < from->length; i++) {
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("read cluster at %8lld\n", from->lcn+i);
|
|
|
|
res = ntfs_pread(vol->dev, (from->lcn+i) * vol->cluster_size,
|
|
|
|
vol->cluster_size, buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res != vol->cluster_size) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("!read\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info("write cluster to %8lld\n", to->lcn+i);
|
|
|
|
res = ntfs_pwrite(vol->dev, (to->lcn+i) * vol->cluster_size,
|
|
|
|
vol->cluster_size, buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res != vol->cluster_size) {
|
2011-07-05 18:17:28 +08:00
|
|
|
ntfs_log_error("!write %lld\n", (long long)res);
|
2004-03-07 01:42:28 +08:00
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
free(buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* move_runlist
|
|
|
|
*
|
|
|
|
* validate:
|
|
|
|
* runlists are the same size
|
|
|
|
* from in use
|
|
|
|
* to not in use
|
|
|
|
* allocate new space
|
|
|
|
* copy data
|
|
|
|
* deallocate old space
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static s64 move_runlist(ntfs_volume *vol, runlist_element *from,
|
2004-04-23 21:55:49 +08:00
|
|
|
runlist_element *to)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!vol || !from || !to)
|
|
|
|
return -1;
|
|
|
|
if (from->length != to->length) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("diffsizes\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((from->lcn < 0) || (to->lcn < 0)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("invalid runs\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < from->length; i++) {
|
2005-10-27 06:54:57 +08:00
|
|
|
if (!utils_cluster_in_use(vol, from->lcn+i)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("from not in use\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < to->length; i++) {
|
2005-10-27 06:54:57 +08:00
|
|
|
if (utils_cluster_in_use(vol, to->lcn+i)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("to is in use\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (bitmap_alloc(vol, to) < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("cannot bitmap_alloc\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (data_copy(vol, from, to) < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("cannot data_copy\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (bitmap_free(vol, from) < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("cannot bitmap_free\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**original
|
|
|
|
* move_datarun
|
|
|
|
* > 0 Bytes moved / size to be moved
|
|
|
|
* = 0 Nothing to do
|
|
|
|
* < 0 Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
// get size of runlist
|
|
|
|
// find somewhere to put data
|
|
|
|
// backup original runlist
|
|
|
|
// move the data
|
|
|
|
|
|
|
|
// got to get the runlist out of this function
|
|
|
|
// requires a mrec arg, not an ino (ino->mrec will do for now)
|
2005-07-07 06:47:01 +08:00
|
|
|
// check size of new runlist before allocating / moving
|
2004-03-07 01:42:28 +08:00
|
|
|
// replace one datarun with another (by hand)
|
2005-10-27 06:54:57 +08:00
|
|
|
static s64 move_datarun(ntfs_volume *vol, ntfs_inode *ino, ATTR_RECORD *rec,
|
2004-04-23 21:55:49 +08:00
|
|
|
runlist_element *run, u64 loc, int flags)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
runlist *from;
|
|
|
|
runlist *to;
|
|
|
|
int need_from;
|
|
|
|
int need_to;
|
|
|
|
int i;
|
|
|
|
s64 res = -1;
|
|
|
|
|
|
|
|
// find empty space
|
2005-10-27 06:54:57 +08:00
|
|
|
to = find_unused(vol, run->length, loc, flags);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!to) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("!to\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
to->vcn = run->vcn;
|
|
|
|
|
|
|
|
// copy original runlist
|
2005-10-27 06:54:57 +08:00
|
|
|
from = ntfs_mapping_pairs_decompress(vol, rec, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!from) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!from\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-05 18:17:28 +08:00
|
|
|
ntfs_log_info("move %lld,%lld,%lld to %lld,%lld,%lld\n",
|
|
|
|
(long long)run->vcn, (long long)run->lcn, (long long)run->length,
|
|
|
|
(long long)to->vcn, (long long)to->lcn, (long long)to->length);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2010-11-22 22:26:27 +08:00
|
|
|
need_from = ntfs_get_size_for_mapping_pairs(vol, from, 0, INT_MAX);
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("orig data run = %d bytes\n", need_from);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
//ntfs_debug_runlist_dump2(from, 5, "\t");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
for (i = 0; to[i].length > 0; i++) {
|
|
|
|
if (from[i].vcn == run->vcn) {
|
|
|
|
from[i].lcn = to->lcn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
//ntfs_debug_runlist_dump2(from, 5, "\t");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2010-11-22 22:26:27 +08:00
|
|
|
need_to = ntfs_get_size_for_mapping_pairs(vol, from, 0, INT_MAX);
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("new data run = %d bytes\n", need_to);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
need_from = calc_attr_length(rec, need_from);
|
|
|
|
need_to = calc_attr_length(rec, need_to);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("Before %d, after %d\n", need_from, need_to);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
if (need_from != need_to) {
|
2005-10-27 06:54:57 +08:00
|
|
|
if (resize_nonres_attr(ino->mrec, rec, need_to) < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!resize\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
res = move_runlist(vol, run, to);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("!move_runlist\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-03-07 01:42:28 +08:00
|
|
|
// wipe orig runs
|
2016-01-27 22:28:55 +08:00
|
|
|
memset(((u8*)rec) + le16_to_cpu(rec->mapping_pairs_offset), 0, need_to - le16_to_cpu(rec->mapping_pairs_offset));
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
// update data runs
|
2016-01-27 22:28:55 +08:00
|
|
|
ntfs_mapping_pairs_build(vol, ((u8*)rec) + le16_to_cpu(rec->mapping_pairs_offset),
|
2004-09-13 17:31:42 +08:00
|
|
|
need_to, from, 0, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
// commit
|
2005-10-27 06:54:57 +08:00
|
|
|
ntfs_inode_mark_dirty(ino);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (ntfs_inode_sync(ino) < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!sync\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
free(from);
|
|
|
|
free(to);
|
2004-03-07 01:42:28 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-26 00:58:38 +08:00
|
|
|
* move_attribute -
|
|
|
|
*
|
2004-03-07 01:42:28 +08:00
|
|
|
* > 0 Bytes moved / size to be moved
|
|
|
|
* = 0 Nothing to do
|
|
|
|
* < 0 Error
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static s64 move_attribute(ntfs_volume *vol, ntfs_inode *ino, ATTR_RECORD *rec,
|
2004-04-23 21:55:49 +08:00
|
|
|
u64 loc, int flags)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s64 res;
|
|
|
|
s64 count = 0;
|
|
|
|
runlist *runs;
|
|
|
|
|
|
|
|
// NTFS_MOVE_LOC_BEST : assess how much space this attribute will need,
|
|
|
|
// find that space and pass the location to our children.
|
|
|
|
// Anything else we pass directly to move_datarun.
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
runs = ntfs_mapping_pairs_decompress(vol, rec, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!runs) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("!runs\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
//ntfs_debug_runlist_dump2(runs, 5, "\t");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info(" VCN LCN Length\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
for (i = 0; runs[i].length > 0; i++) {
|
|
|
|
if (runs[i].lcn == LCN_RL_NOT_MAPPED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
res = move_datarun(vol, ino, rec, runs+i, loc, flags);
|
2005-10-29 02:39:52 +08:00
|
|
|
//ntfs_log_info(" %8lld %8lld %8lld\n", runs[i].vcn, runs[i].lcn, runs[i].length);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res < 0) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("!move_datarun\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
count = res;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-26 00:58:38 +08:00
|
|
|
* move_file -
|
|
|
|
*
|
2004-03-07 01:42:28 +08:00
|
|
|
* > 0 Bytes moved / size to be moved
|
|
|
|
* = 0 Nothing to do
|
|
|
|
* < 0 Error
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
static s64 move_file(ntfs_volume *vol, ntfs_inode *ino, u64 loc, int flags)
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
ntfs_attr_search_ctx *ctx;
|
|
|
|
ATTR_RECORD *rec;
|
|
|
|
s64 res;
|
|
|
|
s64 count = 0;
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
buffer = malloc(MAX_PATH);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!buffer) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("Out of memory\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
utils_inode_get_name(ino, buffer, MAX_PATH);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (dont_move(ino)) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_error("can't move\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("Moving %s\n", buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
// NTFS_MOVE_LOC_BEST : assess how much space all the attributes will need,
|
|
|
|
// find that space and pass the location to our children.
|
|
|
|
// Anything else we pass directly to move_attribute.
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
ctx = ntfs_attr_get_search_ctx(ino, NULL);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
while ((rec = find_attribute(AT_UNUSED, ctx))) {
|
|
|
|
utils_attr_get_name(vol, rec, buffer, MAX_PATH);
|
2015-12-22 06:31:09 +08:00
|
|
|
ntfs_log_info("\tAttribute 0x%02x %s is ", le32_to_cpu(rec->type), buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
|
|
|
|
if (rec->non_resident) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("non-resident. Moving it.\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
res = move_attribute(vol, ino, rec, loc, flags);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (res < 0) {
|
|
|
|
count = res;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count += res;
|
|
|
|
} else {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("resident.\n\t\tSkipping it.\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
ntfs_attr_put_search_ctx(ctx);
|
|
|
|
free(buffer);
|
2004-03-07 01:42:28 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* main - Begin here
|
|
|
|
*
|
|
|
|
* Start from here.
|
|
|
|
*
|
|
|
|
* Return: 0 Success, the program worked
|
|
|
|
* 1 Error, something went wrong
|
|
|
|
*/
|
2005-10-27 06:54:57 +08:00
|
|
|
int main(int argc, char *argv[])
|
2004-03-07 01:42:28 +08:00
|
|
|
{
|
|
|
|
ntfs_volume *vol;
|
|
|
|
ntfs_inode *inode;
|
|
|
|
int flags = 0;
|
|
|
|
int result = 1;
|
|
|
|
s64 count;
|
|
|
|
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_set_handler(ntfs_log_handler_outerr);
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (!parse_options(argc, argv))
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
utils_set_locale();
|
|
|
|
|
|
|
|
if (opts.noaction)
|
2012-11-07 23:29:48 +08:00
|
|
|
flags |= NTFS_MNT_RDONLY;
|
2007-09-15 23:12:32 +08:00
|
|
|
if (opts.force)
|
2012-11-07 23:29:48 +08:00
|
|
|
flags |= NTFS_MNT_RECOVER;
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2007-09-15 23:12:32 +08:00
|
|
|
vol = utils_mount_volume(opts.device, flags);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!vol) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!vol\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-06-13 02:12:55 +08:00
|
|
|
inode = ntfs_pathname_to_inode(vol, NULL, opts.file);
|
2004-03-07 01:42:28 +08:00
|
|
|
if (!inode) {
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("!inode\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
count = move_file(vol, inode, opts.location, 0);
|
2004-03-07 01:42:28 +08:00
|
|
|
if ((count > 0) && (!opts.nodirty)) {
|
2010-12-02 16:26:00 +08:00
|
|
|
|
|
|
|
/* Porting note: libntfs-3g does not automatically set or clear
|
|
|
|
* dirty flags on mount/unmount. It always preserves them until
|
|
|
|
* they are explicitly changed with ntfs_volume_write_flags.
|
|
|
|
* This means that the dirty flag is possibly not set, but
|
|
|
|
* should be set. So we explicitly set it with a call to
|
|
|
|
* ntfs_volume_write_flags. */
|
|
|
|
if(!(vol->flags & VOLUME_IS_DIRTY) && ntfs_volume_write_flags(
|
|
|
|
vol, vol->flags | VOLUME_IS_DIRTY)) {
|
|
|
|
ntfs_log_error("Error: Failed to set volume dirty "
|
|
|
|
"flag (%d (%s))!\n", errno, strerror(errno));
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
2010-12-02 16:26:00 +08:00
|
|
|
|
2011-07-05 18:17:28 +08:00
|
|
|
ntfs_log_info("Relocated %lld bytes\n", (long long)count);
|
2004-03-07 01:42:28 +08:00
|
|
|
}
|
|
|
|
if (count >= 0)
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
if (result)
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("failed\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
else
|
2005-10-29 02:39:52 +08:00
|
|
|
ntfs_log_info("success\n");
|
2004-03-07 01:42:28 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
ntfs_inode_close(inode);
|
|
|
|
ntfs_umount(vol, FALSE);
|
2004-03-07 01:42:28 +08:00
|
|
|
return result;
|
|
|
|
}
|