2003-06-03 19:10:57 +08:00
|
|
|
/**
|
|
|
|
* ntfsclone - Part of the Linux-NTFS project.
|
|
|
|
*
|
2006-03-28 05:40:26 +08:00
|
|
|
* Copyright (c) 2003-2006 Szabolcs Szakacsits
|
2006-08-23 18:18:34 +08:00
|
|
|
* Copyright (c) 2004-2006 Anton Altaparmakov
|
2017-05-25 16:28:05 +08:00
|
|
|
* Copyright (c) 2010-2017 Jean-Pierre Andre
|
2004-07-11 04:01:17 +08:00
|
|
|
* Special image format support copyright (c) 2004 Per Olofsson
|
2003-06-03 19:10:57 +08:00
|
|
|
*
|
2004-07-11 04:01:17 +08:00
|
|
|
* Clone NTFS data and/or metadata to a sparse file, image, device or stdout.
|
2003-06-03 19:10:57 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <unistd.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <stdlib.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDIO_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <stdio.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <sys/types.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <sys/stat.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2012-01-24 01:19:17 +08:00
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2004-03-17 20:22:03 +08:00
|
|
|
#include <sys/ioctl.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2004-01-07 18:22:51 +08:00
|
|
|
#ifdef HAVE_SYS_VFS_H
|
2005-11-08 04:49:18 +08:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#include <sys/statvfs.h>
|
2004-01-07 18:22:51 +08:00
|
|
|
#endif
|
2005-09-28 21:47:47 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <fcntl.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDARG_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <stdarg.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <string.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <errno.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GETOPT_H
|
2003-06-03 19:10:57 +08:00
|
|
|
#include <getopt.h>
|
2005-09-28 21:47:47 +08:00
|
|
|
#endif
|
2012-01-24 01:19:17 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2015-03-13 15:59:20 +08:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2007-08-23 22:58:52 +08:00
|
|
|
/*
|
|
|
|
* FIXME: ntfsclone do bad things about endians handling. Fix it and remove
|
|
|
|
* this note and define.
|
|
|
|
*/
|
|
|
|
#define NTFS_DO_NOT_CHECK_ENDIANS
|
|
|
|
|
2010-12-15 19:03:51 +08:00
|
|
|
#include "debug.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "support.h"
|
|
|
|
#include "endians.h"
|
|
|
|
#include "bootsect.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "attrib.h"
|
|
|
|
#include "mst.h"
|
|
|
|
#include "volume.h"
|
|
|
|
#include "mft.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
#include "inode.h"
|
|
|
|
#include "index.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "runlist.h"
|
|
|
|
#include "ntfstime.h"
|
2003-06-03 19:10:57 +08:00
|
|
|
#include "utils.h"
|
2010-01-15 16:33:54 +08:00
|
|
|
/* #include "version.h" */
|
2010-12-17 16:04:01 +08:00
|
|
|
#include "misc.h"
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-03-17 20:22:03 +08:00
|
|
|
#if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
|
|
|
|
#define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */
|
|
|
|
#endif
|
|
|
|
#if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64)
|
|
|
|
#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */
|
|
|
|
#endif
|
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
#if defined(linux) || defined(__uClinux__) || defined(__sun) \
|
|
|
|
|| defined(__APPLE__) || defined(__DARWIN__)
|
|
|
|
/* Make sure the presence of <windows.h> means compiling for Windows */
|
|
|
|
#undef HAVE_WINDOWS_H
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__sun) | defined(HAVE_WINDOWS_H)
|
2011-07-05 18:17:11 +08:00
|
|
|
#define NO_STATFS 1 /* statfs(2) and f_type are not universal */
|
|
|
|
#endif
|
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
/*
|
|
|
|
* Replacements for functions which do not exist on Windows
|
|
|
|
*/
|
|
|
|
int setmode(int, int); /* from msvcrt.dll */
|
|
|
|
|
|
|
|
#define getpid() (0)
|
|
|
|
#define srandom(seed) srand(seed)
|
|
|
|
#define random() rand()
|
|
|
|
#define fsync(fd) (0)
|
2014-04-12 15:14:08 +08:00
|
|
|
#define ioctl(fd,code,buf) (-1)
|
2013-02-09 19:54:44 +08:00
|
|
|
#define ftruncate(fd, size) ntfs_device_win32_ftruncate(dev_out, size)
|
|
|
|
#define BINWMODE "wb"
|
|
|
|
#else
|
|
|
|
#define BINWMODE "w"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
static const char *EXEC_NAME = "ntfsclone";
|
|
|
|
|
2005-09-25 05:36:51 +08:00
|
|
|
static const char *bad_sectors_warning_msg =
|
|
|
|
"*************************************************************************\n"
|
2012-11-06 15:23:04 +08:00
|
|
|
"* WARNING: The disk has one or more bad sectors. This means that damage *\n"
|
|
|
|
"* has occurred on the disk surface, possibly caused by deterioration of *\n"
|
|
|
|
"* the physical media, manufacturing faults or other reasons. The *\n"
|
|
|
|
"* reliability of the disk may stay stable or degrade fast. *\n"
|
2005-09-25 05:36:51 +08:00
|
|
|
"* Use the --rescue option to efficiently save as much data as possible! *\n"
|
|
|
|
"*************************************************************************\n";
|
|
|
|
|
2006-02-06 06:10:01 +08:00
|
|
|
static const char *dirty_volume_msg =
|
|
|
|
"Volume '%s' is scheduled for a check or it was shutdown \n"
|
|
|
|
"uncleanly. Please boot Windows or use the --force option to progress.\n";
|
|
|
|
|
2006-12-13 03:00:00 +08:00
|
|
|
static struct {
|
2003-06-03 19:10:57 +08:00
|
|
|
int verbose;
|
|
|
|
int quiet;
|
|
|
|
int debug;
|
|
|
|
int force;
|
|
|
|
int overwrite;
|
2004-03-05 19:27:01 +08:00
|
|
|
int std_out;
|
2005-06-20 05:09:40 +08:00
|
|
|
int blkdev_out; /* output file is block device */
|
2005-11-08 05:13:31 +08:00
|
|
|
int metadata; /* metadata only cloning */
|
2013-02-09 18:29:21 +08:00
|
|
|
int no_action; /* do not really restore */
|
2005-03-01 03:23:20 +08:00
|
|
|
int ignore_fs_check;
|
2005-03-02 05:48:00 +08:00
|
|
|
int rescue;
|
2004-07-11 04:01:17 +08:00
|
|
|
int save_image;
|
2012-01-24 01:19:17 +08:00
|
|
|
int new_serial;
|
2011-10-07 17:42:08 +08:00
|
|
|
int metadata_image;
|
2010-12-21 22:51:07 +08:00
|
|
|
int preserve_timestamps;
|
2016-04-06 14:51:36 +08:00
|
|
|
int full_logfile;
|
2004-07-11 04:01:17 +08:00
|
|
|
int restore_image;
|
2003-06-03 19:10:57 +08:00
|
|
|
char *output;
|
|
|
|
char *volume;
|
2011-07-05 18:17:11 +08:00
|
|
|
#ifndef NO_STATFS
|
2004-04-02 05:30:42 +08:00
|
|
|
struct statfs stfs;
|
2011-07-05 18:17:11 +08:00
|
|
|
#endif
|
2003-06-03 19:10:57 +08:00
|
|
|
} opt;
|
|
|
|
|
|
|
|
struct bitmap {
|
|
|
|
s64 size;
|
2004-08-20 20:01:17 +08:00
|
|
|
u8 *bm;
|
2003-06-03 19:10:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct progress_bar {
|
|
|
|
u64 start;
|
|
|
|
u64 stop;
|
|
|
|
int resolution;
|
|
|
|
float unit;
|
|
|
|
};
|
|
|
|
|
2004-04-15 07:31:11 +08:00
|
|
|
typedef struct {
|
2003-06-03 19:10:57 +08:00
|
|
|
ntfs_inode *ni; /* inode being processed */
|
|
|
|
ntfs_attr_search_ctx *ctx; /* inode attribute being processed */
|
2003-11-19 08:05:30 +08:00
|
|
|
s64 inuse; /* number of clusters in use */
|
2013-02-09 18:41:39 +08:00
|
|
|
int more_use; /* possibly allocated clusters */
|
2011-10-07 17:42:08 +08:00
|
|
|
LCN current_lcn;
|
2004-04-15 07:31:11 +08:00
|
|
|
} ntfs_walk_clusters_ctx;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
typedef int (ntfs_walk_op)(ntfs_inode *ni, void *data);
|
|
|
|
|
|
|
|
struct ntfs_walk_cluster {
|
|
|
|
ntfs_walk_op *inode_op; /* not implemented yet */
|
|
|
|
ntfs_walk_clusters_ctx *image;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-12-13 03:00:00 +08:00
|
|
|
static ntfs_volume *vol = NULL;
|
|
|
|
static struct bitmap lcn_bitmap;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2006-12-13 03:00:00 +08:00
|
|
|
static int fd_in;
|
|
|
|
static int fd_out;
|
2011-03-23 16:35:24 +08:00
|
|
|
static FILE *stream_out = (FILE*)NULL;
|
2013-02-09 19:54:44 +08:00
|
|
|
struct ntfs_device *dev_out = (struct ntfs_device*)NULL;
|
2006-12-13 03:00:00 +08:00
|
|
|
static FILE *msg_out = NULL;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2006-12-13 03:00:00 +08:00
|
|
|
static int wipe = 0;
|
|
|
|
static unsigned int nr_used_mft_records = 0;
|
|
|
|
static unsigned int wiped_unused_mft_data = 0;
|
|
|
|
static unsigned int wiped_unused_mft = 0;
|
|
|
|
static unsigned int wiped_resident_data = 0;
|
|
|
|
static unsigned int wiped_timestamp_data = 0;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2012-01-24 01:19:17 +08:00
|
|
|
static le64 volume_serial_number; /* new random serial number */
|
2011-02-17 23:11:26 +08:00
|
|
|
static u64 full_device_size; /* full size, including the backup boot sector */
|
|
|
|
|
2007-08-23 22:58:52 +08:00
|
|
|
static BOOL image_is_host_endian = FALSE;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
#define IMAGE_MAGIC "\0ntfsclone-image"
|
|
|
|
#define IMAGE_MAGIC_SIZE 16
|
2010-12-21 22:51:07 +08:00
|
|
|
#define IMAGE_OFFSET_OFFSET 46 /* must be the same for all versions ! */
|
|
|
|
#define IMAGE_HDR_ALIGN 8 /* alignment wanted after header */
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2006-08-23 18:18:34 +08:00
|
|
|
/* This is the first endianness safe format version. */
|
|
|
|
#define NTFSCLONE_IMG_VER_MAJOR_ENDIANNESS_SAFE 10
|
|
|
|
#define NTFSCLONE_IMG_VER_MINOR_ENDIANNESS_SAFE 0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the version to 10.0 to avoid colisions with old ntfsclone which
|
|
|
|
* stupidly used the volume version as the image version... )-: I hope NTFS
|
|
|
|
* never reaches version 10.0 and if it does one day I hope no-one is using
|
|
|
|
* such an old ntfsclone by then...
|
2006-08-23 19:22:07 +08:00
|
|
|
*
|
|
|
|
* NOTE: Only bump the minor version if the image format and header are still
|
|
|
|
* backwards compatible. Otherwise always bump the major version. If in
|
|
|
|
* doubt, bump the major version.
|
2011-02-16 19:18:56 +08:00
|
|
|
*
|
|
|
|
* Moved to 10.1 : Alternate boot sector now saved. Still compatible.
|
2006-08-23 18:18:34 +08:00
|
|
|
*/
|
|
|
|
#define NTFSCLONE_IMG_VER_MAJOR 10
|
2011-02-16 19:18:56 +08:00
|
|
|
#define NTFSCLONE_IMG_VER_MINOR 1
|
2006-08-23 18:18:34 +08:00
|
|
|
|
2013-02-09 18:48:54 +08:00
|
|
|
enum { CMD_GAP, CMD_NEXT } ;
|
|
|
|
|
2006-08-23 18:18:34 +08:00
|
|
|
/* All values are in little endian. */
|
2010-12-21 22:51:07 +08:00
|
|
|
static struct image_hdr {
|
2004-07-11 04:01:17 +08:00
|
|
|
char magic[IMAGE_MAGIC_SIZE];
|
|
|
|
u8 major_ver;
|
|
|
|
u8 minor_ver;
|
2010-12-21 22:51:07 +08:00
|
|
|
/* the following is aligned dangerously (too late...) */
|
|
|
|
le32 cluster_size;
|
|
|
|
le64 device_size;
|
|
|
|
sle64 nr_clusters;
|
|
|
|
le64 inuse;
|
|
|
|
le32 offset_to_image_data; /* From start of image_hdr. */
|
2005-10-27 06:54:57 +08:00
|
|
|
} __attribute__((__packed__)) image_hdr;
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
static int compare_bitmaps(struct bitmap *a, BOOL copy);
|
|
|
|
|
2006-08-23 19:22:07 +08:00
|
|
|
#define NTFSCLONE_IMG_HEADER_SIZE_OLD \
|
2010-12-21 22:51:07 +08:00
|
|
|
(offsetof(struct image_hdr, offset_to_image_data))
|
2006-08-23 19:22:07 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
#define NTFS_MBYTE (1000 * 1000)
|
|
|
|
|
|
|
|
#define ERR_PREFIX "ERROR"
|
|
|
|
#define PERR_PREFIX ERR_PREFIX "(%d): "
|
|
|
|
#define NERR_PREFIX ERR_PREFIX ": "
|
|
|
|
|
2005-10-22 02:05:13 +08:00
|
|
|
#define LAST_METADATA_INODE 11
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-10-22 02:05:13 +08:00
|
|
|
#define NTFS_MAX_CLUSTER_SIZE 65536
|
|
|
|
#define NTFS_SECTOR_SIZE 512
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
#define rounded_up_division(a, b) (((a) + (b - 1)) / (b))
|
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
#define read_all(f, p, n) io_all((f), (p), (n), 0)
|
|
|
|
#define write_all(f, p, n) io_all((f), (p), (n), 1)
|
|
|
|
|
2005-11-27 20:31:39 +08:00
|
|
|
__attribute__((format(printf, 1, 2)))
|
|
|
|
static void Printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(msg_out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fflush(msg_out);
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((format(printf, 1, 2)))
|
2004-01-16 01:07:57 +08:00
|
|
|
static void perr_printf(const char *fmt, ...)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int eo = errno;
|
|
|
|
|
|
|
|
Printf(PERR_PREFIX, eo);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(msg_out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
Printf(": %s\n", strerror(eo));
|
|
|
|
fflush(msg_out);
|
|
|
|
}
|
|
|
|
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((format(printf, 1, 2)))
|
2004-01-16 01:07:57 +08:00
|
|
|
static void err_printf(const char *fmt, ...)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
Printf(NERR_PREFIX);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(msg_out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fflush(msg_out);
|
|
|
|
}
|
|
|
|
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
__attribute__((format(printf, 1, 2)))
|
2010-12-21 22:51:07 +08:00
|
|
|
static void err_exit(const char *fmt, ...)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
Printf(NERR_PREFIX);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(msg_out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fflush(msg_out);
|
2010-12-21 22:51:07 +08:00
|
|
|
if (vol)
|
|
|
|
ntfs_umount(vol,FALSE);
|
2003-06-03 19:10:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((noreturn))
|
|
|
|
__attribute__((format(printf, 1, 2)))
|
2010-12-21 22:51:07 +08:00
|
|
|
static void perr_exit(const char *fmt, ...)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int eo = errno;
|
|
|
|
|
|
|
|
Printf(PERR_PREFIX, eo);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(msg_out, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
Printf(": %s\n", strerror(eo));
|
|
|
|
fflush(msg_out);
|
2010-12-21 22:51:07 +08:00
|
|
|
if (vol)
|
|
|
|
ntfs_umount(vol,FALSE);
|
2003-06-03 19:10:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-12 19:52:40 +08:00
|
|
|
__attribute__((noreturn))
|
2014-04-23 16:13:27 +08:00
|
|
|
static void usage(int ret)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2005-11-27 20:31:39 +08:00
|
|
|
fprintf(stderr, "\nUsage: %s [OPTIONS] SOURCE\n"
|
2004-07-11 04:01:17 +08:00
|
|
|
" Efficiently clone NTFS to a sparse file, image, device or standard output.\n"
|
2003-06-03 19:10:57 +08:00
|
|
|
"\n"
|
2004-04-02 05:30:42 +08:00
|
|
|
" -o, --output FILE Clone NTFS to the non-existent FILE\n"
|
|
|
|
" -O, --overwrite FILE Clone NTFS to FILE, overwriting if exists\n"
|
2004-07-11 04:01:17 +08:00
|
|
|
" -s, --save-image Save to the special image format\n"
|
|
|
|
" -r, --restore-image Restore from the special image format\n"
|
2005-03-02 05:48:00 +08:00
|
|
|
" --rescue Continue after disk read errors\n"
|
2005-06-12 17:55:45 +08:00
|
|
|
" -m, --metadata Clone *only* metadata (for NTFS experts)\n"
|
2013-02-09 18:29:21 +08:00
|
|
|
" -n, --no-action Test restoring, without outputting anything\n"
|
2005-06-12 17:55:45 +08:00
|
|
|
" --ignore-fs-check Ignore the filesystem check result\n"
|
2012-01-24 01:19:17 +08:00
|
|
|
" --new-serial Set a new serial number\n"
|
|
|
|
" --new-half-serial Set a partial new serial number\n"
|
2010-12-21 22:51:07 +08:00
|
|
|
" -t, --preserve-timestamps Do not clear the timestamps\n"
|
2012-01-24 01:10:26 +08:00
|
|
|
" -q, --quiet Do not display any progress bars\n"
|
2004-04-02 05:30:42 +08:00
|
|
|
" -f, --force Force to progress (DANGEROUS)\n"
|
2016-04-06 14:51:36 +08:00
|
|
|
" --full-logfile Include the full logfile in metadata output\n"
|
2004-04-02 05:30:42 +08:00
|
|
|
" -h, --help Display this help\n"
|
2003-06-03 19:10:57 +08:00
|
|
|
#ifdef DEBUG
|
2004-04-02 05:30:42 +08:00
|
|
|
" -d, --debug Show debug information\n"
|
2003-06-03 19:10:57 +08:00
|
|
|
#endif
|
2016-04-06 17:09:35 +08:00
|
|
|
" -V, --version Display version information\n"
|
2003-06-03 19:10:57 +08:00
|
|
|
"\n"
|
2005-06-12 17:55:45 +08:00
|
|
|
" If FILE is '-' then send the image to the standard output. If SOURCE is '-'\n"
|
|
|
|
" and --restore-image is used then read the image from the standard input.\n"
|
2003-06-03 19:10:57 +08:00
|
|
|
"\n", EXEC_NAME);
|
2005-11-27 20:31:39 +08:00
|
|
|
fprintf(stderr, "%s%s", ntfs_bugs, ntfs_home);
|
2014-04-23 16:13:27 +08:00
|
|
|
exit(ret);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2014-02-17 01:25:09 +08:00
|
|
|
/**
|
|
|
|
* version
|
|
|
|
*/
|
|
|
|
__attribute__((noreturn))
|
|
|
|
static void version(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Efficiently clone, image, restore or rescue an NTFS Volume.\n\n"
|
|
|
|
"Copyright (c) 2003-2006 Szabolcs Szakacsits\n"
|
|
|
|
"Copyright (c) 2004-2006 Anton Altaparmakov\n"
|
2016-04-06 14:51:36 +08:00
|
|
|
"Copyright (c) 2010-2016 Jean-Pierre Andre\n\n");
|
2014-02-17 01:25:09 +08:00
|
|
|
fprintf(stderr, "%s\n%s%s", ntfs_gpl, ntfs_bugs, ntfs_home);
|
2014-04-23 16:13:27 +08:00
|
|
|
exit(0);
|
2014-02-17 01:25:09 +08:00
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void parse_options(int argc, char **argv)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2014-02-17 01:25:09 +08:00
|
|
|
static const char *sopt = "-dfhmno:O:qrstV";
|
2003-06-03 19:10:57 +08:00
|
|
|
static const struct option lopt[] = {
|
|
|
|
#ifdef DEBUG
|
2005-03-01 03:23:20 +08:00
|
|
|
{ "debug", no_argument, NULL, 'd' },
|
2003-06-03 19:10:57 +08:00
|
|
|
#endif
|
2012-01-24 01:10:26 +08:00
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
2005-03-01 03:23:20 +08:00
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "metadata", no_argument, NULL, 'm' },
|
2013-02-09 18:29:21 +08:00
|
|
|
{ "no-action", no_argument, NULL, 'n' },
|
2005-03-01 03:23:20 +08:00
|
|
|
{ "output", required_argument, NULL, 'o' },
|
|
|
|
{ "overwrite", required_argument, NULL, 'O' },
|
|
|
|
{ "restore-image", no_argument, NULL, 'r' },
|
|
|
|
{ "ignore-fs-check", no_argument, NULL, 'C' },
|
2005-03-02 05:48:00 +08:00
|
|
|
{ "rescue", no_argument, NULL, 'R' },
|
2012-01-24 01:19:17 +08:00
|
|
|
{ "new-serial", no_argument, NULL, 'I' },
|
|
|
|
{ "new-half-serial", no_argument, NULL, 'i' },
|
2016-04-06 14:51:36 +08:00
|
|
|
{ "full-logfile", no_argument, NULL, 'l' },
|
2005-03-01 03:23:20 +08:00
|
|
|
{ "save-image", no_argument, NULL, 's' },
|
2010-12-21 22:51:07 +08:00
|
|
|
{ "preserve-timestamps", no_argument, NULL, 't' },
|
2014-02-17 01:25:09 +08:00
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2003-06-03 19:10:57 +08:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2006-04-05 20:43:06 +08:00
|
|
|
int c;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
memset(&opt, 0, sizeof(opt));
|
|
|
|
|
2006-04-05 20:43:06 +08:00
|
|
|
while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
|
2003-06-03 19:10:57 +08:00
|
|
|
switch (c) {
|
|
|
|
case 1: /* A non-option argument */
|
|
|
|
if (opt.volume)
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2003-06-03 19:10:57 +08:00
|
|
|
opt.volume = argv[optind-1];
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
opt.debug++;
|
|
|
|
break;
|
2012-01-24 01:10:26 +08:00
|
|
|
case 'q':
|
|
|
|
opt.quiet++;
|
|
|
|
break;
|
2003-06-03 19:10:57 +08:00
|
|
|
case 'f':
|
|
|
|
opt.force++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(0);
|
2003-06-03 19:10:57 +08:00
|
|
|
case '?':
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2012-01-24 01:19:17 +08:00
|
|
|
case 'i': /* not proposed as a short option */
|
|
|
|
opt.new_serial |= 1;
|
|
|
|
break;
|
|
|
|
case 'I': /* not proposed as a short option */
|
|
|
|
opt.new_serial |= 2;
|
|
|
|
break;
|
2016-04-06 14:51:36 +08:00
|
|
|
case 'l':
|
|
|
|
opt.full_logfile++;
|
|
|
|
break;
|
2003-06-03 19:10:57 +08:00
|
|
|
case 'm':
|
2005-11-08 05:13:31 +08:00
|
|
|
opt.metadata++;
|
2003-06-03 19:10:57 +08:00
|
|
|
break;
|
2013-02-09 18:29:21 +08:00
|
|
|
case 'n':
|
|
|
|
opt.no_action++;
|
|
|
|
break;
|
2003-06-03 19:10:57 +08:00
|
|
|
case 'O':
|
|
|
|
opt.overwrite++;
|
|
|
|
case 'o':
|
|
|
|
if (opt.output)
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2004-04-15 08:42:33 +08:00
|
|
|
opt.output = optarg;
|
2003-06-03 19:10:57 +08:00
|
|
|
break;
|
2004-07-11 04:01:17 +08:00
|
|
|
case 'r':
|
|
|
|
opt.restore_image++;
|
|
|
|
break;
|
2005-03-01 03:23:20 +08:00
|
|
|
case 'C':
|
|
|
|
opt.ignore_fs_check++;
|
|
|
|
break;
|
2005-03-02 05:48:00 +08:00
|
|
|
case 'R':
|
|
|
|
opt.rescue++;
|
|
|
|
break;
|
2004-07-11 04:01:17 +08:00
|
|
|
case 's':
|
|
|
|
opt.save_image++;
|
|
|
|
break;
|
2010-12-21 22:51:07 +08:00
|
|
|
case 't':
|
|
|
|
opt.preserve_timestamps++;
|
|
|
|
break;
|
2014-02-17 01:25:09 +08:00
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
break;
|
2003-06-03 19:10:57 +08:00
|
|
|
default:
|
|
|
|
err_printf("Unknown option '%s'.\n", argv[optind-1]);
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action && (opt.output == NULL)) {
|
2003-06-03 19:10:57 +08:00
|
|
|
err_printf("You must specify an output file.\n");
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action && (strcmp(opt.output, "-") == 0))
|
2004-03-05 19:27:01 +08:00
|
|
|
opt.std_out++;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
if (opt.volume == NULL) {
|
|
|
|
err_printf("You must specify a device file.\n");
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 22:01:36 +08:00
|
|
|
if (!opt.restore_image && !strcmp(opt.volume, "-")) {
|
|
|
|
err_printf("Only special images can be read from standard input\n");
|
2014-04-23 16:13:27 +08:00
|
|
|
usage(1);
|
2013-02-09 22:01:36 +08:00
|
|
|
}
|
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.metadata && opt.save_image) {
|
|
|
|
opt.metadata_image++;
|
|
|
|
opt.save_image = 0;
|
|
|
|
}
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2005-11-08 05:13:31 +08:00
|
|
|
if (opt.metadata && opt.restore_image)
|
2004-07-11 04:01:17 +08:00
|
|
|
err_exit("Restoring only metadata from an image is not "
|
|
|
|
"supported!\n");
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.metadata && !opt.metadata_image && opt.std_out)
|
2003-07-11 20:35:05 +08:00
|
|
|
err_exit("Cloning only metadata to stdout isn't supported!\n");
|
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
if (opt.ignore_fs_check && !opt.metadata && !opt.rescue)
|
2005-03-01 03:23:20 +08:00
|
|
|
err_exit("Filesystem check can be ignored only for metadata "
|
2013-02-09 18:41:39 +08:00
|
|
|
"cloning or rescue situations!\n");
|
2005-03-01 03:23:20 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (opt.save_image && opt.restore_image)
|
|
|
|
err_exit("Saving and restoring an image at the same time "
|
|
|
|
"is not supported!\n");
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
if (opt.no_action && !opt.restore_image)
|
|
|
|
err_exit("A restoring test requires the restore option!\n");
|
|
|
|
|
|
|
|
if (opt.no_action && opt.output)
|
|
|
|
err_exit("A restoring test requires not defining any output!\n");
|
|
|
|
|
|
|
|
if (!opt.no_action && !opt.std_out) {
|
2003-07-11 20:35:05 +08:00
|
|
|
struct stat st;
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
BOOL blkdev = opt.output[0] && (opt.output[1] == ':')
|
|
|
|
&& !opt.output[2];
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
if (!blkdev && (stat(opt.output, &st) == -1)) {
|
|
|
|
#else
|
2003-07-11 20:35:05 +08:00
|
|
|
if (stat(opt.output, &st) == -1) {
|
2013-02-09 19:54:44 +08:00
|
|
|
#endif
|
2003-07-11 20:35:05 +08:00
|
|
|
if (errno != ENOENT)
|
2005-06-20 05:09:40 +08:00
|
|
|
perr_exit("Couldn't access '%s'", opt.output);
|
2003-07-11 20:35:05 +08:00
|
|
|
} else {
|
|
|
|
if (!opt.overwrite)
|
|
|
|
err_exit("Output file '%s' already exists.\n"
|
|
|
|
"Use option --overwrite if you want to"
|
|
|
|
" replace its content.\n", opt.output);
|
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
if (blkdev) {
|
|
|
|
#else
|
2003-07-11 20:35:05 +08:00
|
|
|
if (S_ISBLK(st.st_mode)) {
|
2013-02-09 19:54:44 +08:00
|
|
|
#endif
|
2003-07-11 20:35:05 +08:00
|
|
|
opt.blkdev_out = 1;
|
2007-10-26 18:28:51 +08:00
|
|
|
if (opt.metadata && !opt.force)
|
2003-07-11 20:35:05 +08:00
|
|
|
err_exit("Cloning only metadata to a "
|
2007-10-26 18:28:51 +08:00
|
|
|
"block device does not usually "
|
|
|
|
"make sense, aborting...\n"
|
|
|
|
"If you were instructed to do "
|
|
|
|
"this by a developer and/or are "
|
|
|
|
"sure that this is what you want "
|
|
|
|
"to do, run this utility again "
|
|
|
|
"but this time add the force "
|
|
|
|
"option, i.e. add '--force' to "
|
|
|
|
"the command line arguments.");
|
2003-07-11 20:35:05 +08:00
|
|
|
}
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 19:12:02 +08:00
|
|
|
/*
|
|
|
|
* Send messages, debug information and library messages to stdout,
|
|
|
|
* but, if outputing to stdout send them to stderr
|
|
|
|
*/
|
|
|
|
if (opt.std_out) {
|
2003-06-03 19:10:57 +08:00
|
|
|
msg_out = stderr;
|
2013-02-09 19:12:02 +08:00
|
|
|
ntfs_log_set_handler(ntfs_log_handler_stderr);
|
2005-10-16 05:44:17 +08:00
|
|
|
} else {
|
2013-02-09 19:12:02 +08:00
|
|
|
msg_out = stdout;
|
|
|
|
ntfs_log_set_handler(ntfs_log_handler_outerr);
|
2005-10-16 05:44:17 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2012-01-24 01:19:17 +08:00
|
|
|
/*
|
|
|
|
* Initialize the random number generator with the current
|
|
|
|
* time, and generate a 64-bit random number for the serial
|
|
|
|
* number
|
|
|
|
*/
|
|
|
|
static void generate_serial_number(void) {
|
|
|
|
u64 sn;
|
|
|
|
|
|
|
|
/* different values for parallel processes */
|
|
|
|
srandom(time((time_t*)NULL) ^ (getpid() << 16));
|
|
|
|
sn = ((u64)random() << 32) | ((u64)random() & 0xffffffff);
|
|
|
|
volume_serial_number = cpu_to_le64(sn);
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void progress_init(struct progress_bar *p, u64 start, u64 stop, int res)
|
2003-07-11 20:35:05 +08:00
|
|
|
{
|
|
|
|
p->start = start;
|
|
|
|
p->stop = stop;
|
|
|
|
p->unit = 100.0 / (stop - start);
|
|
|
|
p->resolution = res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void progress_update(struct progress_bar *p, u64 current)
|
2003-07-11 20:35:05 +08:00
|
|
|
{
|
|
|
|
float percent = p->unit * current;
|
|
|
|
|
2012-01-24 01:10:26 +08:00
|
|
|
if (opt.quiet)
|
|
|
|
return;
|
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
if (current != p->stop) {
|
|
|
|
if ((current - p->start) % p->resolution)
|
|
|
|
return;
|
|
|
|
Printf("%6.2f percent completed\r", percent);
|
|
|
|
} else
|
|
|
|
Printf("100.00 percent completed\n");
|
|
|
|
fflush(msg_out);
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-04-15 07:31:11 +08:00
|
|
|
static s64 is_critical_metadata(ntfs_walk_clusters_ctx *image, runlist *rl)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2004-04-15 07:31:11 +08:00
|
|
|
s64 inode = image->ni->mft_no;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-04-15 07:31:11 +08:00
|
|
|
if (inode <= LAST_METADATA_INODE) {
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-10-01 02:16:48 +08:00
|
|
|
/* Don't save bad sectors (both $Bad and unnamed are ignored */
|
|
|
|
if (inode == FILE_BadClus && image->ctx->attr->type == AT_DATA)
|
|
|
|
return 0;
|
|
|
|
|
2016-04-06 14:51:36 +08:00
|
|
|
if ((inode != FILE_LogFile) || opt.full_logfile)
|
2004-04-15 07:31:11 +08:00
|
|
|
return rl->length;
|
|
|
|
|
|
|
|
if (image->ctx->attr->type == AT_DATA) {
|
2005-03-01 03:23:20 +08:00
|
|
|
|
2005-03-06 03:08:57 +08:00
|
|
|
/* Save at least the first 16 KiB of FILE_LogFile */
|
|
|
|
s64 s = (s64)16384 - rl->vcn * vol->cluster_size;
|
2004-04-15 07:31:11 +08:00
|
|
|
if (s > 0) {
|
|
|
|
s = rounded_up_division(s, vol->cluster_size);
|
|
|
|
if (rl->length < s)
|
|
|
|
s = rl->length;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (image->ctx->attr->type != AT_DATA)
|
2004-04-15 07:31:11 +08:00
|
|
|
return rl->length;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-09 18:37:31 +08:00
|
|
|
static off_t tellin(int in)
|
|
|
|
{
|
|
|
|
return (lseek(in, 0, SEEK_CUR));
|
|
|
|
}
|
2003-07-11 20:35:05 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static int io_all(void *fd, void *buf, int count, int do_write)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2003-07-11 20:35:05 +08:00
|
|
|
int i;
|
2005-04-25 15:26:08 +08:00
|
|
|
struct ntfs_device *dev = fd;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
while (count > 0) {
|
2011-03-23 16:35:24 +08:00
|
|
|
if (do_write) {
|
2013-02-09 18:29:21 +08:00
|
|
|
if (opt.no_action) {
|
|
|
|
i = count;
|
|
|
|
} else {
|
|
|
|
if (opt.save_image || opt.metadata_image)
|
|
|
|
i = fwrite(buf, 1, count, stream_out);
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
else if (dev_out)
|
|
|
|
i = dev_out->d_ops->write(dev_out,
|
|
|
|
buf, count);
|
|
|
|
#endif
|
2013-02-09 18:29:21 +08:00
|
|
|
else
|
|
|
|
i = write(*(int *)fd, buf, count);
|
|
|
|
}
|
2011-03-23 16:35:24 +08:00
|
|
|
} else if (opt.restore_image)
|
2004-07-11 04:01:17 +08:00
|
|
|
i = read(*(int *)fd, buf, count);
|
2003-07-11 20:35:05 +08:00
|
|
|
else
|
|
|
|
i = dev->d_ops->read(dev, buf, count);
|
|
|
|
if (i < 0) {
|
|
|
|
if (errno != EAGAIN && errno != EINTR)
|
|
|
|
return -1;
|
2009-03-24 00:21:17 +08:00
|
|
|
} else if (i == 0 && !do_write && opt.restore_image) {
|
|
|
|
return -1;
|
2003-07-11 20:35:05 +08:00
|
|
|
} else {
|
|
|
|
count -= i;
|
|
|
|
buf = i + (char *) buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
|
2013-02-09 22:07:06 +08:00
|
|
|
static void rescue_sector(void *fd, u32 bytes_per_sector, off_t pos, void *buff)
|
2005-04-25 15:26:08 +08:00
|
|
|
{
|
2013-02-09 22:04:41 +08:00
|
|
|
const char badsector_magic[] = "BadSectoR";
|
2005-04-25 15:26:08 +08:00
|
|
|
struct ntfs_device *dev = fd;
|
|
|
|
|
|
|
|
if (opt.restore_image) {
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action
|
|
|
|
&& (lseek(*(int *)fd, pos, SEEK_SET) == (off_t)-1))
|
2005-04-25 15:26:08 +08:00
|
|
|
perr_exit("lseek");
|
|
|
|
} else {
|
|
|
|
if (vol->dev->d_ops->seek(dev, pos, SEEK_SET) == (off_t)-1)
|
|
|
|
perr_exit("seek input");
|
|
|
|
}
|
|
|
|
|
2013-02-09 22:07:06 +08:00
|
|
|
if (read_all(fd, buff, bytes_per_sector) == -1) {
|
2005-04-25 15:26:08 +08:00
|
|
|
Printf("WARNING: Can't read sector at %llu, lost data.\n",
|
2005-10-27 06:54:57 +08:00
|
|
|
(unsigned long long)pos);
|
2013-02-09 22:07:06 +08:00
|
|
|
memset(buff, '?', bytes_per_sector);
|
2005-04-25 15:26:08 +08:00
|
|
|
memmove(buff, badsector_magic, sizeof(badsector_magic));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
/*
|
|
|
|
* Read a cluster, try to rescue if cannot read
|
|
|
|
*/
|
|
|
|
|
2013-02-09 22:07:06 +08:00
|
|
|
static void read_rescue(void *fd, char *buff, u32 csize, u32 bytes_per_sector,
|
|
|
|
u64 rescue_lcn)
|
2012-01-24 01:13:59 +08:00
|
|
|
{
|
|
|
|
off_t rescue_pos;
|
|
|
|
|
|
|
|
if (read_all(fd, buff, csize) == -1) {
|
|
|
|
|
|
|
|
if (errno != EIO)
|
|
|
|
perr_exit("read_all");
|
|
|
|
else if (opt.rescue){
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
rescue_pos = (off_t)(rescue_lcn * csize);
|
2013-02-09 22:07:06 +08:00
|
|
|
for (i = 0; i < csize; i += bytes_per_sector)
|
|
|
|
rescue_sector(fd, bytes_per_sector,
|
|
|
|
rescue_pos + i, buff + i);
|
2012-01-24 01:13:59 +08:00
|
|
|
} else {
|
|
|
|
Printf("%s", bad_sectors_warning_msg);
|
|
|
|
err_exit("Disk is faulty, can't make full backup!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-25 15:26:08 +08:00
|
|
|
|
2011-02-17 23:11:26 +08:00
|
|
|
static void copy_cluster(int rescue, u64 rescue_lcn, u64 lcn)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
char buff[NTFS_MAX_CLUSTER_SIZE]; /* overflow checked at mount time */
|
2005-04-25 15:26:08 +08:00
|
|
|
/* vol is NULL if opt.restore_image is set */
|
2011-02-17 23:11:26 +08:00
|
|
|
s32 csize = le32_to_cpu(image_hdr.cluster_size);
|
2012-01-24 01:19:17 +08:00
|
|
|
BOOL backup_bootsector;
|
2005-04-25 15:26:08 +08:00
|
|
|
void *fd = (void *)&fd_in;
|
2005-09-26 05:42:47 +08:00
|
|
|
off_t rescue_pos;
|
2012-01-24 01:19:17 +08:00
|
|
|
NTFS_BOOT_SECTOR *bs;
|
|
|
|
le64 mask;
|
2013-02-09 22:07:06 +08:00
|
|
|
static u16 bytes_per_sector = NTFS_SECTOR_SIZE;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2005-04-25 15:26:08 +08:00
|
|
|
if (!opt.restore_image) {
|
|
|
|
csize = vol->cluster_size;
|
2013-02-09 22:07:06 +08:00
|
|
|
bytes_per_sector = vol->sector_size;
|
2005-04-25 15:26:08 +08:00
|
|
|
fd = vol->dev;
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-09-26 05:42:47 +08:00
|
|
|
rescue_pos = (off_t)(rescue_lcn * csize);
|
|
|
|
|
2011-02-17 23:11:26 +08:00
|
|
|
/* possible partial cluster holding the backup boot sector */
|
2012-01-24 01:19:17 +08:00
|
|
|
backup_bootsector = (lcn + 1)*csize >= full_device_size;
|
|
|
|
if (backup_bootsector) {
|
2011-02-17 23:11:26 +08:00
|
|
|
csize = full_device_size - lcn*csize;
|
|
|
|
if (csize < 0) {
|
|
|
|
err_exit("Corrupted input, copy aborted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
// need reading when not about to write ?
|
2005-04-25 15:26:08 +08:00
|
|
|
if (read_all(fd, buff, csize) == -1) {
|
2005-03-02 05:48:00 +08:00
|
|
|
|
2013-02-09 18:37:31 +08:00
|
|
|
if (errno != EIO) {
|
|
|
|
if (!errno && opt.restore_image)
|
|
|
|
err_exit("Short image file...\n");
|
|
|
|
else
|
|
|
|
perr_exit("read_all");
|
|
|
|
}
|
2005-09-25 05:36:51 +08:00
|
|
|
else if (rescue){
|
2011-02-17 23:11:26 +08:00
|
|
|
s32 i;
|
2013-02-09 22:07:06 +08:00
|
|
|
for (i = 0; i < csize; i += bytes_per_sector)
|
|
|
|
rescue_sector(fd, bytes_per_sector,
|
|
|
|
rescue_pos + i, buff + i);
|
2005-09-25 05:36:51 +08:00
|
|
|
} else {
|
2005-10-05 04:36:42 +08:00
|
|
|
Printf("%s", bad_sectors_warning_msg);
|
2005-09-25 05:36:51 +08:00
|
|
|
err_exit("Disk is faulty, can't make full backup!");
|
|
|
|
}
|
2005-03-02 05:48:00 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2012-01-24 01:19:17 +08:00
|
|
|
/* Set the new serial number if requested */
|
|
|
|
if (opt.new_serial
|
|
|
|
&& !opt.save_image
|
|
|
|
&& (!lcn || backup_bootsector)) {
|
|
|
|
/*
|
|
|
|
* For updating the backup boot sector, we need to
|
|
|
|
* know the sector size, but this is not recorded
|
|
|
|
* in the image header, so we collect it on the fly
|
|
|
|
* while reading the first boot sector.
|
|
|
|
*/
|
|
|
|
if (!lcn) {
|
|
|
|
bs = (NTFS_BOOT_SECTOR*)buff;
|
|
|
|
bytes_per_sector = le16_to_cpu(bs->bpb.bytes_per_sector);
|
|
|
|
if ((bytes_per_sector > csize)
|
|
|
|
|| (bytes_per_sector < NTFS_SECTOR_SIZE))
|
|
|
|
bytes_per_sector = NTFS_SECTOR_SIZE;
|
|
|
|
} else
|
|
|
|
bs = (NTFS_BOOT_SECTOR*)(buff
|
|
|
|
+ csize - bytes_per_sector);
|
|
|
|
if (opt.new_serial & 2)
|
|
|
|
bs->volume_serial_number = volume_serial_number;
|
|
|
|
else {
|
|
|
|
mask = const_cpu_to_le64(~0x0ffffffffULL);
|
|
|
|
bs->volume_serial_number
|
|
|
|
= (volume_serial_number & mask)
|
|
|
|
| (bs->volume_serial_number & ~mask);
|
|
|
|
}
|
|
|
|
/* Show the new full serial after merging */
|
|
|
|
if (!lcn)
|
|
|
|
Printf("New serial number : 0x%llx\n",
|
|
|
|
(long long)le64_to_cpu(
|
|
|
|
bs->volume_serial_number));
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
if (opt.save_image || (opt.metadata_image && wipe)) {
|
2013-02-09 18:48:54 +08:00
|
|
|
char cmd = CMD_NEXT;
|
2004-07-11 04:01:17 +08:00
|
|
|
if (write_all(&fd_out, &cmd, sizeof(cmd)) == -1)
|
|
|
|
perr_exit("write_all");
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
if ((!opt.metadata_image || wipe)
|
2011-10-07 17:42:08 +08:00
|
|
|
&& (write_all(&fd_out, buff, csize) == -1)) {
|
2011-07-05 18:17:11 +08:00
|
|
|
#ifndef NO_STATFS
|
2004-04-02 05:30:42 +08:00
|
|
|
int err = errno;
|
|
|
|
perr_printf("Write failed");
|
|
|
|
if (err == EIO && opt.stfs.f_type == 0x517b)
|
|
|
|
Printf("Apparently you tried to clone to a remote "
|
|
|
|
"Windows computer but they don't\nhave "
|
|
|
|
"efficient sparse file handling by default. "
|
|
|
|
"Please try a different method.\n");
|
|
|
|
exit(1);
|
2011-07-05 18:17:11 +08:00
|
|
|
#else
|
|
|
|
perr_printf("Write failed");
|
|
|
|
#endif
|
2004-04-02 05:30:42 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
static s64 lseek_out(int fd, s64 pos, int mode)
|
|
|
|
{
|
|
|
|
s64 ret;
|
|
|
|
|
|
|
|
if (dev_out)
|
|
|
|
ret = (dev_out->d_ops->seek)(dev_out, pos, mode);
|
|
|
|
else
|
|
|
|
ret = lseek(fd, pos, mode);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void lseek_to_cluster(s64 lcn)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
off_t pos;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
pos = (off_t)(lcn * vol->cluster_size);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (vol->dev->d_ops->seek(vol->dev, pos, SEEK_SET) == (off_t)-1)
|
|
|
|
perr_exit("lseek input");
|
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.std_out || opt.save_image || opt.metadata_image)
|
2003-06-03 19:10:57 +08:00
|
|
|
return;
|
2005-03-01 03:23:20 +08:00
|
|
|
|
2013-02-09 19:54:44 +08:00
|
|
|
if (lseek_out(fd_out, pos, SEEK_SET) == (off_t)-1)
|
|
|
|
perr_exit("lseek output");
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
static void gap_to_cluster(s64 gap)
|
|
|
|
{
|
|
|
|
sle64 count;
|
|
|
|
char buf[1 + sizeof(count)];
|
|
|
|
|
|
|
|
if (gap) {
|
|
|
|
count = cpu_to_sle64(gap);
|
2013-02-09 18:48:54 +08:00
|
|
|
buf[0] = CMD_GAP;
|
2011-10-07 17:42:08 +08:00
|
|
|
memcpy(&buf[1], &count, sizeof(count));
|
|
|
|
if (write_all(&fd_out, buf, sizeof(buf)) == -1)
|
|
|
|
perr_exit("write_all");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
static void image_skip_clusters(s64 count)
|
|
|
|
{
|
|
|
|
if (opt.save_image && count > 0) {
|
2015-12-22 06:55:31 +08:00
|
|
|
sle64 count_buf;
|
2004-07-11 04:01:17 +08:00
|
|
|
char buff[1 + sizeof(count)];
|
|
|
|
|
2013-02-09 18:48:54 +08:00
|
|
|
buff[0] = CMD_GAP;
|
2006-08-23 19:58:55 +08:00
|
|
|
count_buf = cpu_to_sle64(count);
|
|
|
|
memcpy(buff + 1, &count_buf, sizeof(count_buf));
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (write_all(&fd_out, buff, sizeof(buff)) == -1)
|
|
|
|
perr_exit("write_all");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
static void write_image_hdr(void)
|
|
|
|
{
|
|
|
|
char alignment[IMAGE_HDR_ALIGN];
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.save_image || opt.metadata_image) {
|
|
|
|
int alignsize = le32_to_cpu(image_hdr.offset_to_image_data)
|
|
|
|
- sizeof(image_hdr);
|
|
|
|
memset(alignment,0,IMAGE_HDR_ALIGN);
|
|
|
|
if ((alignsize < 0)
|
|
|
|
|| write_all(&fd_out, &image_hdr, sizeof(image_hdr))
|
|
|
|
|| write_all(&fd_out, alignment, alignsize))
|
|
|
|
perr_exit("write_all");
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
static void clone_ntfs(u64 nr_clusters, int more_use)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2004-09-29 07:43:15 +08:00
|
|
|
u64 cl, last_cl; /* current and last used cluster */
|
2003-07-11 20:35:05 +08:00
|
|
|
void *buf;
|
|
|
|
u32 csize = vol->cluster_size;
|
|
|
|
u64 p_counter = 0;
|
2010-12-21 22:51:07 +08:00
|
|
|
char alignment[IMAGE_HDR_ALIGN];
|
2003-07-11 20:35:05 +08:00
|
|
|
struct progress_bar progress;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (opt.save_image)
|
|
|
|
Printf("Saving NTFS to image ...\n");
|
|
|
|
else
|
|
|
|
Printf("Cloning NTFS ...\n");
|
|
|
|
|
2012-01-24 01:19:17 +08:00
|
|
|
if (opt.new_serial)
|
|
|
|
generate_serial_number();
|
|
|
|
|
2006-08-13 22:49:35 +08:00
|
|
|
buf = ntfs_calloc(csize);
|
|
|
|
if (!buf)
|
2004-09-29 07:43:15 +08:00
|
|
|
perr_exit("clone_ntfs");
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
progress_init(&progress, p_counter, nr_clusters, 100);
|
2004-07-11 04:01:17 +08:00
|
|
|
|
|
|
|
if (opt.save_image) {
|
2010-12-21 22:51:07 +08:00
|
|
|
int alignsize = le32_to_cpu(image_hdr.offset_to_image_data)
|
|
|
|
- sizeof(image_hdr);
|
|
|
|
memset(alignment,0,IMAGE_HDR_ALIGN);
|
|
|
|
if ((alignsize < 0)
|
|
|
|
|| write_all(&fd_out, &image_hdr, sizeof(image_hdr))
|
|
|
|
|| write_all(&fd_out, alignment, alignsize))
|
2004-07-11 04:01:17 +08:00
|
|
|
perr_exit("write_all");
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
/* save suspicious clusters if required */
|
|
|
|
if (more_use && opt.ignore_fs_check) {
|
|
|
|
compare_bitmaps(&lcn_bitmap, TRUE);
|
|
|
|
}
|
2011-02-16 19:18:56 +08:00
|
|
|
/* Examine up to the alternate boot sector */
|
|
|
|
for (last_cl = cl = 0; cl <= (u64)vol->nr_clusters; cl++) {
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-09-29 07:43:15 +08:00
|
|
|
if (ntfs_bit_get(lcn_bitmap.bm, cl)) {
|
|
|
|
progress_update(&progress, ++p_counter);
|
|
|
|
lseek_to_cluster(cl);
|
|
|
|
image_skip_clusters(cl - last_cl - 1);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2011-02-17 23:11:26 +08:00
|
|
|
copy_cluster(opt.rescue, cl, cl);
|
2004-09-29 07:43:15 +08:00
|
|
|
last_cl = cl;
|
|
|
|
continue;
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-09-29 07:43:15 +08:00
|
|
|
if (opt.std_out && !opt.save_image) {
|
|
|
|
progress_update(&progress, ++p_counter);
|
|
|
|
if (write_all(&fd_out, buf, csize) == -1)
|
|
|
|
perr_exit("write_all");
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
}
|
2004-09-29 07:43:15 +08:00
|
|
|
image_skip_clusters(cl - last_cl - 1);
|
2010-12-21 22:51:07 +08:00
|
|
|
free(buf);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
static void write_empty_clusters(s32 csize, s64 count,
|
|
|
|
struct progress_bar *progress, u64 *p_counter)
|
|
|
|
{
|
|
|
|
s64 i;
|
|
|
|
char buff[NTFS_MAX_CLUSTER_SIZE];
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
memset(buff, 0, csize);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (write_all(&fd_out, buff, csize) == -1)
|
|
|
|
perr_exit("write_all");
|
|
|
|
progress_update(progress, ++(*p_counter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_image(void)
|
|
|
|
{
|
|
|
|
s64 pos = 0, count;
|
2006-08-23 18:18:34 +08:00
|
|
|
s32 csize = le32_to_cpu(image_hdr.cluster_size);
|
2004-07-11 04:01:17 +08:00
|
|
|
char cmd;
|
|
|
|
u64 p_counter = 0;
|
|
|
|
struct progress_bar progress;
|
|
|
|
|
|
|
|
Printf("Restoring NTFS from image ...\n");
|
|
|
|
|
|
|
|
progress_init(&progress, p_counter, opt.std_out ?
|
2016-02-15 19:02:10 +08:00
|
|
|
(u64)sle64_to_cpu(image_hdr.nr_clusters) + 1 :
|
2015-12-22 06:55:31 +08:00
|
|
|
le64_to_cpu(image_hdr.inuse) + 1,
|
2006-08-23 18:18:34 +08:00
|
|
|
100);
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2012-01-24 01:19:17 +08:00
|
|
|
if (opt.new_serial)
|
|
|
|
generate_serial_number();
|
|
|
|
|
2011-02-16 19:18:56 +08:00
|
|
|
/* Restore up to the alternate boot sector */
|
|
|
|
while (pos <= sle64_to_cpu(image_hdr.nr_clusters)) {
|
|
|
|
if (read_all(&fd_in, &cmd, sizeof(cmd)) == -1) {
|
|
|
|
if (pos == sle64_to_cpu(image_hdr.nr_clusters)) {
|
|
|
|
/* alternate boot sector no present in old images */
|
|
|
|
Printf("Warning : no alternate boot"
|
|
|
|
" sector in image\n");
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
perr_exit("read_all");
|
|
|
|
}
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2013-02-09 18:48:54 +08:00
|
|
|
if (cmd == CMD_GAP) {
|
2010-12-21 22:51:07 +08:00
|
|
|
if (!image_is_host_endian) {
|
2015-12-22 06:55:31 +08:00
|
|
|
sle64 lecount;
|
2010-12-21 22:51:07 +08:00
|
|
|
|
|
|
|
/* little endian image, on any computer */
|
|
|
|
if (read_all(&fd_in, &lecount,
|
|
|
|
sizeof(lecount)) == -1)
|
|
|
|
perr_exit("read_all");
|
|
|
|
count = sle64_to_cpu(lecount);
|
|
|
|
} else {
|
|
|
|
/* big endian image on big endian computer */
|
|
|
|
if (read_all(&fd_in, &count,
|
|
|
|
sizeof(count)) == -1)
|
|
|
|
perr_exit("read_all");
|
|
|
|
}
|
2013-02-09 18:37:31 +08:00
|
|
|
if (!count)
|
|
|
|
err_exit("Bad offset at input location 0x%llx\n",
|
|
|
|
(long long)tellin(fd_in) - 9);
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.std_out) {
|
|
|
|
if ((!p_counter && count) || (count < 0))
|
|
|
|
err_exit("Cannot restore a metadata"
|
|
|
|
" image to stdout\n");
|
|
|
|
else
|
|
|
|
write_empty_clusters(csize, count,
|
2004-07-11 04:01:17 +08:00
|
|
|
&progress, &p_counter);
|
2011-10-07 17:42:08 +08:00
|
|
|
} else {
|
|
|
|
if (((pos + count) < 0)
|
|
|
|
|| ((pos + count)
|
|
|
|
> sle64_to_cpu(image_hdr.nr_clusters)))
|
2013-02-09 18:37:31 +08:00
|
|
|
err_exit("restore_image: corrupt image "
|
|
|
|
"at input offset %lld\n",
|
|
|
|
(long long)tellin(fd_in) - 9);
|
2013-02-09 19:54:44 +08:00
|
|
|
else {
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action
|
2013-02-09 19:54:44 +08:00
|
|
|
&& (lseek_out(fd_out, count * csize,
|
2013-02-09 18:29:21 +08:00
|
|
|
SEEK_CUR) == (off_t)-1))
|
2011-10-07 17:42:08 +08:00
|
|
|
perr_exit("restore_image: lseek");
|
2013-02-09 19:54:44 +08:00
|
|
|
}
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
|
|
|
pos += count;
|
2013-02-09 18:48:54 +08:00
|
|
|
} else if (cmd == CMD_NEXT) {
|
2011-02-17 23:11:26 +08:00
|
|
|
copy_cluster(0, 0, pos);
|
2004-07-11 04:01:17 +08:00
|
|
|
pos++;
|
|
|
|
progress_update(&progress, ++p_counter);
|
|
|
|
} else
|
2013-02-09 18:37:31 +08:00
|
|
|
err_exit("Invalid command code %d at input offset 0x%llx\n",
|
|
|
|
cmd, (long long)tellin(fd_in) - 1);
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-28 05:40:26 +08:00
|
|
|
static void wipe_index_entry_timestams(INDEX_ENTRY *e)
|
|
|
|
{
|
2010-07-02 01:16:26 +08:00
|
|
|
static const struct timespec zero_time = { .tv_sec = 0, .tv_nsec = 0 };
|
2015-12-22 06:55:31 +08:00
|
|
|
sle64 timestamp = timespec2ntfs(zero_time);
|
2006-03-28 05:40:26 +08:00
|
|
|
|
2006-08-13 22:34:07 +08:00
|
|
|
/* FIXME: can fall into infinite loop if corrupted */
|
2010-01-15 16:33:54 +08:00
|
|
|
while (!(e->ie_flags & INDEX_ENTRY_END)) {
|
2006-03-28 05:40:26 +08:00
|
|
|
|
|
|
|
e->key.file_name.creation_time = timestamp;
|
|
|
|
e->key.file_name.last_data_change_time = timestamp;
|
|
|
|
e->key.file_name.last_mft_change_time = timestamp;
|
|
|
|
e->key.file_name.last_access_time = timestamp;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-03-28 05:40:26 +08:00
|
|
|
wiped_timestamp_data += 32;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-03-28 05:40:26 +08:00
|
|
|
e = (INDEX_ENTRY *)((u8 *)e + le16_to_cpu(e->length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wipe_index_allocation_timestamps(ntfs_inode *ni, ATTR_RECORD *attr)
|
|
|
|
{
|
|
|
|
INDEX_ALLOCATION *indexa, *tmp_indexa;
|
|
|
|
INDEX_ENTRY *entry;
|
|
|
|
INDEX_ROOT *indexr;
|
|
|
|
u8 *bitmap, *byte;
|
|
|
|
int bit;
|
|
|
|
ntfs_attr *na;
|
|
|
|
ntfschar *name;
|
|
|
|
u32 name_len;
|
|
|
|
|
|
|
|
indexr = ntfs_index_root_get(ni, attr);
|
|
|
|
if (!indexr) {
|
2006-08-13 22:52:19 +08:00
|
|
|
perr_printf("Failed to read $INDEX_ROOT attribute of inode "
|
2010-12-21 22:51:07 +08:00
|
|
|
"%lld", (long long)ni->mft_no);
|
2006-03-28 05:40:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indexr->type != AT_FILE_NAME)
|
|
|
|
goto out_indexr;
|
|
|
|
|
|
|
|
name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset));
|
|
|
|
name_len = attr->name_length;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2010-12-21 22:51:07 +08:00
|
|
|
byte = bitmap = ntfs_attr_readall(ni, AT_BITMAP, name, name_len,
|
|
|
|
NULL);
|
2006-03-28 05:40:26 +08:00
|
|
|
if (!byte) {
|
2006-08-13 22:34:07 +08:00
|
|
|
perr_printf("Failed to read $BITMAP attribute");
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_indexr;
|
|
|
|
}
|
|
|
|
|
|
|
|
na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, name, name_len);
|
|
|
|
if (!na) {
|
2006-08-13 22:34:07 +08:00
|
|
|
perr_printf("Failed to open $INDEX_ALLOCATION attribute");
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_bitmap;
|
|
|
|
}
|
2006-08-13 23:15:08 +08:00
|
|
|
|
|
|
|
if (!na->data_size)
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_na;
|
2006-08-13 23:15:08 +08:00
|
|
|
|
2006-08-13 22:49:35 +08:00
|
|
|
tmp_indexa = indexa = ntfs_malloc(na->data_size);
|
|
|
|
if (!tmp_indexa)
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_na;
|
2006-08-13 22:49:35 +08:00
|
|
|
|
2006-03-28 05:40:26 +08:00
|
|
|
if (ntfs_attr_pread(na, 0, na->data_size, indexa) != na->data_size) {
|
2006-08-13 22:34:07 +08:00
|
|
|
perr_printf("Failed to read $INDEX_ALLOCATION attribute");
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_indexa;
|
|
|
|
}
|
|
|
|
|
|
|
|
bit = 0;
|
|
|
|
while ((u8 *)tmp_indexa < (u8 *)indexa + na->data_size) {
|
2006-10-27 20:24:29 +08:00
|
|
|
if (*byte & (1 << bit)) {
|
2006-03-28 05:40:26 +08:00
|
|
|
if (ntfs_mst_post_read_fixup((NTFS_RECORD *)tmp_indexa,
|
2006-08-23 19:58:55 +08:00
|
|
|
le32_to_cpu(
|
|
|
|
indexr->index_block_size))) {
|
2006-08-13 22:34:07 +08:00
|
|
|
perr_printf("Damaged INDX record");
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_indexa;
|
|
|
|
}
|
|
|
|
entry = (INDEX_ENTRY *)((u8 *)tmp_indexa + le32_to_cpu(
|
|
|
|
tmp_indexa->index.entries_offset) + 0x18);
|
|
|
|
|
|
|
|
wipe_index_entry_timestams(entry);
|
|
|
|
|
2006-04-05 10:50:44 +08:00
|
|
|
if (ntfs_mft_usn_dec((MFT_RECORD *)tmp_indexa))
|
|
|
|
perr_exit("ntfs_mft_usn_dec");
|
|
|
|
|
2006-03-28 05:40:26 +08:00
|
|
|
if (ntfs_mst_pre_write_fixup((NTFS_RECORD *)tmp_indexa,
|
2006-08-23 19:58:55 +08:00
|
|
|
le32_to_cpu(
|
|
|
|
indexr->index_block_size))) {
|
2006-08-13 22:34:07 +08:00
|
|
|
perr_printf("INDX write fixup failed");
|
2006-03-28 05:40:26 +08:00
|
|
|
goto out_indexa;
|
|
|
|
}
|
|
|
|
}
|
2006-10-27 20:24:29 +08:00
|
|
|
tmp_indexa = (INDEX_ALLOCATION *)((u8 *)tmp_indexa +
|
2006-08-23 19:58:55 +08:00
|
|
|
le32_to_cpu(indexr->index_block_size));
|
2006-03-28 05:40:26 +08:00
|
|
|
bit++;
|
|
|
|
if (bit > 7) {
|
|
|
|
bit = 0;
|
|
|
|
byte++;
|
|
|
|
}
|
|
|
|
}
|
2010-07-02 01:16:26 +08:00
|
|
|
if (ntfs_rl_pwrite(vol, na->rl, 0, 0, na->data_size, indexa) != na->data_size)
|
2010-12-21 22:51:07 +08:00
|
|
|
perr_printf("ntfs_rl_pwrite failed for inode %lld",
|
|
|
|
(long long)ni->mft_no);
|
2006-03-28 05:40:26 +08:00
|
|
|
out_indexa:
|
|
|
|
free(indexa);
|
|
|
|
out_na:
|
|
|
|
ntfs_attr_close(na);
|
|
|
|
out_bitmap:
|
|
|
|
free(bitmap);
|
|
|
|
out_indexr:
|
|
|
|
free(indexr);
|
|
|
|
}
|
|
|
|
|
2015-12-22 06:55:31 +08:00
|
|
|
static void wipe_index_root_timestamps(ATTR_RECORD *attr, sle64 timestamp)
|
2005-11-18 08:24:45 +08:00
|
|
|
{
|
|
|
|
INDEX_ENTRY *entry;
|
|
|
|
INDEX_ROOT *iroot;
|
|
|
|
|
|
|
|
iroot = (INDEX_ROOT *)((u8 *)attr + le16_to_cpu(attr->value_offset));
|
|
|
|
entry = (INDEX_ENTRY *)((u8 *)iroot +
|
|
|
|
le32_to_cpu(iroot->index.entries_offset) + 0x10);
|
|
|
|
|
2010-01-15 16:33:54 +08:00
|
|
|
while (!(entry->ie_flags & INDEX_ENTRY_END)) {
|
2005-11-18 08:24:45 +08:00
|
|
|
|
|
|
|
if (iroot->type == AT_FILE_NAME) {
|
|
|
|
|
2006-02-07 04:51:17 +08:00
|
|
|
entry->key.file_name.creation_time = timestamp;
|
|
|
|
entry->key.file_name.last_access_time = timestamp;
|
|
|
|
entry->key.file_name.last_data_change_time = timestamp;
|
|
|
|
entry->key.file_name.last_mft_change_time = timestamp;
|
2005-11-18 08:24:45 +08:00
|
|
|
|
2006-02-07 06:17:24 +08:00
|
|
|
wiped_timestamp_data += 32;
|
|
|
|
|
2005-11-18 08:24:45 +08:00
|
|
|
} else if (ntfs_names_are_equal(NTFS_INDEX_Q,
|
|
|
|
sizeof(NTFS_INDEX_Q) / 2 - 1,
|
|
|
|
(ntfschar *)((char *)attr +
|
|
|
|
le16_to_cpu(attr->name_offset)),
|
2010-12-21 22:51:07 +08:00
|
|
|
attr->name_length, CASE_SENSITIVE, NULL, 0)) {
|
2005-11-18 08:24:45 +08:00
|
|
|
|
|
|
|
QUOTA_CONTROL_ENTRY *quota_q;
|
|
|
|
|
|
|
|
quota_q = (QUOTA_CONTROL_ENTRY *)((u8 *)entry +
|
2006-08-23 19:58:55 +08:00
|
|
|
le16_to_cpu(entry->data_offset));
|
2005-11-18 08:24:45 +08:00
|
|
|
/*
|
2006-02-07 06:17:24 +08:00
|
|
|
* FIXME: no guarantee it's indeed /$Extend/$Quota:$Q.
|
|
|
|
* For now, as a minimal safeguard, we check only for
|
|
|
|
* quota version 2 ...
|
2005-11-18 08:24:45 +08:00
|
|
|
*/
|
2006-02-07 06:17:24 +08:00
|
|
|
if (le32_to_cpu(quota_q->version) == 2) {
|
2006-02-07 04:51:17 +08:00
|
|
|
quota_q->change_time = timestamp;
|
2006-02-07 06:17:24 +08:00
|
|
|
wiped_timestamp_data += 4;
|
|
|
|
}
|
2005-11-18 08:24:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = (INDEX_ENTRY*)((u8*)entry + le16_to_cpu(entry->length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-07 04:51:17 +08:00
|
|
|
#define WIPE_TIMESTAMPS(atype, attr, timestamp) \
|
2003-06-03 19:10:57 +08:00
|
|
|
do { \
|
|
|
|
atype *ats; \
|
2006-08-23 19:58:55 +08:00
|
|
|
ats = (atype *)((char *)(attr) + le16_to_cpu((attr)->value_offset)); \
|
2003-06-03 19:10:57 +08:00
|
|
|
\
|
2006-02-07 04:51:17 +08:00
|
|
|
ats->creation_time = (timestamp); \
|
|
|
|
ats->last_data_change_time = (timestamp); \
|
|
|
|
ats->last_mft_change_time= (timestamp); \
|
|
|
|
ats->last_access_time = (timestamp); \
|
2003-06-03 19:10:57 +08:00
|
|
|
\
|
|
|
|
wiped_timestamp_data += 32; \
|
|
|
|
\
|
2005-10-22 02:05:13 +08:00
|
|
|
} while (0)
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void wipe_timestamps(ntfs_walk_clusters_ctx *image)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2010-07-02 01:16:26 +08:00
|
|
|
static const struct timespec zero_time = { .tv_sec = 0, .tv_nsec = 0 };
|
2003-06-03 19:10:57 +08:00
|
|
|
ATTR_RECORD *a = image->ctx->attr;
|
2015-12-22 06:55:31 +08:00
|
|
|
sle64 timestamp = timespec2ntfs(zero_time);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
|
|
|
if (a->type == AT_FILE_NAME)
|
2006-02-07 04:51:17 +08:00
|
|
|
WIPE_TIMESTAMPS(FILE_NAME_ATTR, a, timestamp);
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-06-20 05:09:40 +08:00
|
|
|
else if (a->type == AT_STANDARD_INFORMATION)
|
2006-02-07 04:51:17 +08:00
|
|
|
WIPE_TIMESTAMPS(STANDARD_INFORMATION, a, timestamp);
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2005-11-18 08:24:45 +08:00
|
|
|
else if (a->type == AT_INDEX_ROOT)
|
2006-02-07 04:51:17 +08:00
|
|
|
wipe_index_root_timestamps(a, timestamp);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void wipe_resident_data(ntfs_walk_clusters_ctx *image)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
ATTR_RECORD *a;
|
2003-11-19 08:05:30 +08:00
|
|
|
u32 i;
|
|
|
|
int n = 0;
|
2004-04-23 18:12:49 +08:00
|
|
|
u8 *p;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
a = image->ctx->attr;
|
2004-04-23 18:12:49 +08:00
|
|
|
p = (u8*)a + le16_to_cpu(a->value_offset);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (image->ni->mft_no <= LAST_METADATA_INODE)
|
|
|
|
return;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (a->type != AT_DATA)
|
|
|
|
return;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-04-23 18:12:49 +08:00
|
|
|
for (i = 0; i < le32_to_cpu(a->value_length); i++) {
|
2003-06-03 19:10:57 +08:00
|
|
|
if (p[i]) {
|
|
|
|
p[i] = 0;
|
|
|
|
n++;
|
2005-06-20 05:09:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
wiped_resident_data += n;
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
static int wipe_data(char *p, int pos, int len)
|
|
|
|
{
|
|
|
|
int wiped = 0;
|
|
|
|
|
|
|
|
for (p += pos; --len >= 0;) {
|
|
|
|
if (p[len]) {
|
|
|
|
p[len] = 0;
|
|
|
|
wiped++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wiped;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wipe_unused_mft_data(ntfs_inode *ni)
|
|
|
|
{
|
|
|
|
int unused;
|
|
|
|
MFT_RECORD *m = ni->mrec;
|
|
|
|
|
|
|
|
/* FIXME: broken MFTMirr update was fixed in libntfs, check if OK now */
|
|
|
|
if (ni->mft_no <= LAST_METADATA_INODE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unused = le32_to_cpu(m->bytes_allocated) - le32_to_cpu(m->bytes_in_use);
|
|
|
|
wiped_unused_mft_data += wipe_data((char *)m,
|
|
|
|
le32_to_cpu(m->bytes_in_use), unused);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wipe_unused_mft(ntfs_inode *ni)
|
|
|
|
{
|
|
|
|
int unused;
|
|
|
|
MFT_RECORD *m = ni->mrec;
|
|
|
|
|
|
|
|
/* FIXME: broken MFTMirr update was fixed in libntfs, check if OK now */
|
|
|
|
if (ni->mft_no <= LAST_METADATA_INODE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unused = le32_to_cpu(m->bytes_in_use) - sizeof(MFT_RECORD);
|
|
|
|
wiped_unused_mft += wipe_data((char *)m, sizeof(MFT_RECORD), unused);
|
|
|
|
}
|
|
|
|
|
2005-03-06 03:08:57 +08:00
|
|
|
static void clone_logfile_parts(ntfs_walk_clusters_ctx *image, runlist *rl)
|
|
|
|
{
|
|
|
|
s64 offset = 0, lcn, vcn;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2005-03-06 03:08:57 +08:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
vcn = offset / image->ni->vol->cluster_size;
|
|
|
|
lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
|
|
|
|
if (lcn < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
lseek_to_cluster(lcn);
|
2011-10-07 17:42:08 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
if ((lcn + 1) != image->current_lcn) {
|
|
|
|
/* do not duplicate a cluster */
|
|
|
|
if (opt.metadata_image && wipe)
|
|
|
|
gap_to_cluster(lcn - image->current_lcn);
|
2011-10-07 17:42:08 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
copy_cluster(opt.rescue, lcn, lcn);
|
|
|
|
}
|
2011-10-07 17:42:08 +08:00
|
|
|
image->current_lcn = lcn + 1;
|
2012-08-20 19:44:33 +08:00
|
|
|
if (opt.metadata_image && !wipe)
|
2011-10-07 17:42:08 +08:00
|
|
|
image->inuse++;
|
2005-03-06 03:08:57 +08:00
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
offset = NTFS_BLOCK_SIZE >> 1;
|
|
|
|
else
|
|
|
|
offset <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
/*
|
|
|
|
* In-memory wiping of MFT record or MFTMirr record
|
|
|
|
* (only for metadata images)
|
|
|
|
*
|
|
|
|
* The resident data and (optionally) the timestamps are wiped.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void wipe_mft(char *mrec, u32 mrecsz, u64 mft_no)
|
|
|
|
{
|
|
|
|
ntfs_walk_clusters_ctx image;
|
|
|
|
ntfs_attr_search_ctx *ctx;
|
|
|
|
ntfs_inode ni;
|
|
|
|
|
|
|
|
ni.mft_no = mft_no;
|
|
|
|
ni.mrec = (MFT_RECORD*)mrec;
|
|
|
|
ni.vol = vol; /* Hmm */
|
|
|
|
image.ni = ∋
|
|
|
|
ntfs_mst_post_read_fixup_warn((NTFS_RECORD*)mrec,mrecsz,FALSE);
|
|
|
|
wipe_unused_mft_data(&ni);
|
|
|
|
if (!(((MFT_RECORD*)mrec)->flags & MFT_RECORD_IN_USE)) {
|
|
|
|
wipe_unused_mft(&ni);
|
|
|
|
} else {
|
|
|
|
/* ctx with no ntfs_inode prevents from searching external attrs */
|
|
|
|
if (!(ctx = ntfs_attr_get_search_ctx((ntfs_inode*)NULL, (MFT_RECORD*)mrec)))
|
|
|
|
perr_exit("ntfs_get_attr_search_ctx");
|
|
|
|
|
|
|
|
while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0,
|
|
|
|
NULL, 0, ctx)) {
|
|
|
|
if (ctx->attr->type == AT_END)
|
|
|
|
break;
|
|
|
|
|
|
|
|
image.ctx = ctx;
|
|
|
|
if (!ctx->attr->non_resident
|
|
|
|
&& (mft_no > LAST_METADATA_INODE))
|
|
|
|
wipe_resident_data(&image);
|
|
|
|
if (!opt.preserve_timestamps)
|
|
|
|
wipe_timestamps(&image);
|
|
|
|
}
|
|
|
|
ntfs_attr_put_search_ctx(ctx);
|
|
|
|
}
|
|
|
|
ntfs_mft_usn_dec((MFT_RECORD*)mrec);
|
|
|
|
ntfs_mst_pre_write_fixup((NTFS_RECORD*)mrec,mrecsz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In-memory wiping of a directory record (I30)
|
|
|
|
* (only for metadata images)
|
|
|
|
*
|
|
|
|
* The timestamps are (optionally) wiped
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void wipe_indx(char *mrec, u32 mrecsz)
|
|
|
|
{
|
|
|
|
INDEX_ENTRY *entry;
|
|
|
|
INDEX_ALLOCATION *indexa;
|
|
|
|
|
|
|
|
if (ntfs_mst_post_read_fixup((NTFS_RECORD *)mrec, mrecsz)) {
|
|
|
|
perr_printf("Damaged INDX record");
|
|
|
|
goto out_indexa;
|
|
|
|
}
|
|
|
|
indexa = (INDEX_ALLOCATION*)mrec;
|
|
|
|
/*
|
|
|
|
* The index bitmap is not checked, obsoleted records are
|
|
|
|
* wiped if they pass the safety checks
|
|
|
|
*/
|
|
|
|
if ((indexa->magic == magic_INDX)
|
|
|
|
&& (le32_to_cpu(indexa->index.entries_offset) >= sizeof(INDEX_HEADER))
|
|
|
|
&& (le32_to_cpu(indexa->index.allocated_size) <= mrecsz)) {
|
|
|
|
entry = (INDEX_ENTRY *)((u8 *)mrec + le32_to_cpu(
|
|
|
|
indexa->index.entries_offset) + 0x18);
|
|
|
|
wipe_index_entry_timestams(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ntfs_mft_usn_dec((MFT_RECORD *)mrec))
|
|
|
|
perr_exit("ntfs_mft_usn_dec");
|
|
|
|
|
|
|
|
if (ntfs_mst_pre_write_fixup((NTFS_RECORD *)mrec, mrecsz)) {
|
|
|
|
perr_printf("INDX write fixup failed");
|
|
|
|
goto out_indexa;
|
|
|
|
}
|
|
|
|
out_indexa : ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a set of related clusters (MFT record or index block)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void write_set(char *buff, u32 csize, s64 *current_lcn,
|
|
|
|
runlist_element *rl, u32 wi, u32 wj, u32 cnt)
|
|
|
|
{
|
|
|
|
u32 k;
|
|
|
|
s64 target_lcn;
|
2013-02-09 18:48:54 +08:00
|
|
|
char cmd = CMD_NEXT;
|
2012-01-24 01:13:59 +08:00
|
|
|
|
|
|
|
for (k=0; k<cnt; k++) {
|
|
|
|
target_lcn = rl[wi].lcn + wj;
|
|
|
|
if (target_lcn != *current_lcn)
|
|
|
|
gap_to_cluster(target_lcn - *current_lcn);
|
|
|
|
if ((write_all(&fd_out, &cmd, sizeof(cmd)) == -1)
|
|
|
|
|| (write_all(&fd_out, &buff[k*csize], csize) == -1))
|
|
|
|
perr_exit("Failed to write_all");
|
|
|
|
*current_lcn = target_lcn + 1;
|
|
|
|
|
|
|
|
if (++wj >= rl[wi].length) {
|
|
|
|
wj = 0;
|
|
|
|
wi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy and wipe the full MFT or MFTMirr data.
|
|
|
|
* (only for metadata images)
|
|
|
|
*
|
|
|
|
* Data are read and written by full clusters, but the wiping is done
|
|
|
|
* per MFT record.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void copy_wipe_mft(ntfs_walk_clusters_ctx *image, runlist *rl)
|
|
|
|
{
|
|
|
|
char buff[NTFS_MAX_CLUSTER_SIZE]; /* overflow checked at mount time */
|
|
|
|
void *fd;
|
|
|
|
s64 mft_no;
|
|
|
|
u32 mft_record_size;
|
|
|
|
u32 csize;
|
2013-02-09 22:07:06 +08:00
|
|
|
u32 bytes_per_sector;
|
2012-01-24 01:13:59 +08:00
|
|
|
u32 records_per_set;
|
|
|
|
u32 clusters_per_set;
|
|
|
|
u32 wi,wj; /* indexes for reading */
|
|
|
|
u32 ri,rj; /* indexes for writing */
|
|
|
|
u32 k; /* lcn within run */
|
|
|
|
u32 r; /* mft_record within set */
|
|
|
|
s64 current_lcn;
|
|
|
|
|
|
|
|
current_lcn = image->current_lcn;
|
|
|
|
mft_record_size = image->ni->vol->mft_record_size;
|
|
|
|
csize = image->ni->vol->cluster_size;
|
2013-02-09 22:07:06 +08:00
|
|
|
bytes_per_sector = image->ni->vol->sector_size;
|
2012-01-24 01:13:59 +08:00
|
|
|
fd = image->ni->vol->dev;
|
|
|
|
/*
|
|
|
|
* Depending on the sizes, there may be several records
|
|
|
|
* per cluster, or several clusters per record.
|
|
|
|
*/
|
|
|
|
if (csize >= mft_record_size) {
|
|
|
|
records_per_set = csize/mft_record_size;
|
|
|
|
clusters_per_set = 1;
|
|
|
|
} else {
|
|
|
|
clusters_per_set = mft_record_size/csize;
|
|
|
|
records_per_set = 1;
|
|
|
|
}
|
|
|
|
mft_no = 0;
|
|
|
|
ri = rj = 0;
|
|
|
|
wi = wj = 0;
|
2013-02-09 18:12:45 +08:00
|
|
|
if (rl[ri].length)
|
|
|
|
lseek_to_cluster(rl[ri].lcn);
|
2012-01-24 01:13:59 +08:00
|
|
|
while (rl[ri].length) {
|
|
|
|
for (k=0; (k<clusters_per_set) && rl[ri].length; k++) {
|
2013-02-09 22:07:06 +08:00
|
|
|
read_rescue(fd, &buff[k*csize], csize, bytes_per_sector,
|
|
|
|
rl[ri].lcn + rj);
|
2012-01-24 01:13:59 +08:00
|
|
|
if (++rj >= rl[ri].length) {
|
|
|
|
rj = 0;
|
|
|
|
if (rl[++ri].length)
|
|
|
|
lseek_to_cluster(rl[ri].lcn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k == clusters_per_set) {
|
|
|
|
for (r=0; r<records_per_set; r++) {
|
|
|
|
if (!strncmp(&buff[r*mft_record_size],"FILE",4))
|
|
|
|
wipe_mft(&buff[r*mft_record_size],
|
|
|
|
mft_record_size, mft_no);
|
|
|
|
mft_no++;
|
|
|
|
}
|
|
|
|
write_set(buff, csize, ¤t_lcn,
|
|
|
|
rl, wi, wj, clusters_per_set);
|
|
|
|
wj += clusters_per_set;
|
|
|
|
while (rl[wi].length && (wj >= rl[wi].length))
|
|
|
|
wj -= rl[wi++].length;
|
|
|
|
} else {
|
|
|
|
err_exit("Short last MFT record\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image->current_lcn = current_lcn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy and wipe the non-resident part of a directory index
|
|
|
|
* (only for metadata images)
|
|
|
|
*
|
|
|
|
* Data are read and written by full clusters, but the wiping is done
|
|
|
|
* per index record.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void copy_wipe_i30(ntfs_walk_clusters_ctx *image, runlist *rl)
|
|
|
|
{
|
|
|
|
char buff[NTFS_MAX_CLUSTER_SIZE]; /* overflow checked at mount time */
|
|
|
|
void *fd;
|
|
|
|
u32 indx_record_size;
|
|
|
|
u32 csize;
|
2013-02-09 22:07:06 +08:00
|
|
|
u32 bytes_per_sector;
|
2012-01-24 01:13:59 +08:00
|
|
|
u32 records_per_set;
|
|
|
|
u32 clusters_per_set;
|
|
|
|
u32 wi,wj; /* indexes for reading */
|
|
|
|
u32 ri,rj; /* indexes for writing */
|
|
|
|
u32 k; /* lcn within run */
|
|
|
|
u32 r; /* mft_record within set */
|
|
|
|
s64 current_lcn;
|
|
|
|
|
|
|
|
current_lcn = image->current_lcn;
|
|
|
|
csize = image->ni->vol->cluster_size;
|
2013-02-09 22:07:06 +08:00
|
|
|
bytes_per_sector = image->ni->vol->sector_size;
|
2012-01-24 01:13:59 +08:00
|
|
|
fd = image->ni->vol->dev;
|
|
|
|
/*
|
|
|
|
* Depending on the sizes, there may be several records
|
|
|
|
* per cluster, or several clusters per record.
|
|
|
|
*/
|
|
|
|
indx_record_size = image->ni->vol->indx_record_size;
|
|
|
|
if (csize >= indx_record_size) {
|
|
|
|
records_per_set = csize/indx_record_size;
|
|
|
|
clusters_per_set = 1;
|
|
|
|
} else {
|
|
|
|
clusters_per_set = indx_record_size/csize;
|
|
|
|
records_per_set = 1;
|
|
|
|
}
|
|
|
|
ri = rj = 0;
|
|
|
|
wi = wj = 0;
|
2013-02-09 18:12:45 +08:00
|
|
|
if (rl[ri].length)
|
|
|
|
lseek_to_cluster(rl[ri].lcn);
|
2012-01-24 01:13:59 +08:00
|
|
|
while (rl[ri].length) {
|
|
|
|
for (k=0; (k<clusters_per_set) && rl[ri].length; k++) {
|
2013-02-09 22:07:06 +08:00
|
|
|
read_rescue(fd, &buff[k*csize], csize, bytes_per_sector,
|
|
|
|
rl[ri].lcn + rj);
|
2012-01-24 01:13:59 +08:00
|
|
|
if (++rj >= rl[ri].length) {
|
|
|
|
rj = 0;
|
|
|
|
if (rl[++ri].length)
|
|
|
|
lseek_to_cluster(rl[ri].lcn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k == clusters_per_set) {
|
|
|
|
/* wipe records_per_set records */
|
|
|
|
if (!opt.preserve_timestamps)
|
|
|
|
for (r=0; r<records_per_set; r++) {
|
|
|
|
if (!strncmp(&buff[r*indx_record_size],"INDX",4))
|
|
|
|
wipe_indx(&buff[r*indx_record_size],
|
|
|
|
indx_record_size);
|
|
|
|
}
|
|
|
|
write_set(buff, csize, ¤t_lcn,
|
|
|
|
rl, wi, wj, clusters_per_set);
|
|
|
|
wj += clusters_per_set;
|
|
|
|
while (rl[wi].length && (wj >= rl[wi].length))
|
|
|
|
wj -= rl[wi++].length;
|
|
|
|
} else {
|
|
|
|
err_exit("Short last directory index record\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image->current_lcn = current_lcn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_clusters(ntfs_walk_clusters_ctx *image, runlist *rl)
|
|
|
|
{
|
|
|
|
s64 i, len; /* number of clusters to copy */
|
|
|
|
|
|
|
|
if (opt.restore_image)
|
|
|
|
err_exit("Bug : invalid dump_clusters()\n");
|
|
|
|
|
|
|
|
if ((opt.std_out && !opt.metadata_image) || !opt.metadata)
|
|
|
|
return;
|
|
|
|
if (!(len = is_critical_metadata(image, rl)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
lseek_to_cluster(rl->lcn);
|
|
|
|
if (opt.metadata_image ? wipe : !wipe) {
|
2013-02-09 19:53:38 +08:00
|
|
|
if (opt.metadata_image)
|
|
|
|
gap_to_cluster(rl->lcn - image->current_lcn);
|
2012-01-24 01:13:59 +08:00
|
|
|
/* FIXME: this could give pretty suboptimal performance */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
copy_cluster(opt.rescue, rl->lcn + i, rl->lcn + i);
|
2013-02-09 19:53:38 +08:00
|
|
|
if (opt.metadata_image)
|
|
|
|
image->current_lcn = rl->lcn + len;
|
2012-01-24 01:13:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void walk_runs(struct ntfs_walk_cluster *walk)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
runlist *rl;
|
|
|
|
ATTR_RECORD *a;
|
|
|
|
ntfs_attr_search_ctx *ctx;
|
2012-01-24 01:13:59 +08:00
|
|
|
BOOL mft_data;
|
|
|
|
BOOL index_i30;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
ctx = walk->image->ctx;
|
|
|
|
a = ctx->attr;
|
|
|
|
|
|
|
|
if (!a->non_resident) {
|
|
|
|
if (wipe) {
|
|
|
|
wipe_resident_data(walk->image);
|
2010-12-21 22:51:07 +08:00
|
|
|
if (!opt.preserve_timestamps)
|
|
|
|
wipe_timestamps(walk->image);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-21 22:51:07 +08:00
|
|
|
if (wipe
|
|
|
|
&& !opt.preserve_timestamps
|
|
|
|
&& walk->image->ctx->attr->type == AT_INDEX_ALLOCATION)
|
2006-03-28 05:40:26 +08:00
|
|
|
wipe_index_allocation_timestamps(walk->image->ni, a);
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (!(rl = ntfs_mapping_pairs_decompress(vol, a, NULL)))
|
|
|
|
perr_exit("ntfs_decompress_mapping_pairs");
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
/* special wipings for MFT records and directory indexes */
|
|
|
|
mft_data = ((walk->image->ni->mft_no == FILE_MFT)
|
|
|
|
|| (walk->image->ni->mft_no == FILE_MFTMirr))
|
|
|
|
&& (a->type == AT_DATA);
|
|
|
|
index_i30 = (walk->image->ctx->attr->type == AT_INDEX_ALLOCATION)
|
|
|
|
&& (a->name_length == 4)
|
|
|
|
&& !memcmp((char*)a + le16_to_cpu(a->name_offset),
|
|
|
|
NTFS_INDEX_I30,8);
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
for (i = 0; rl[i].length; i++) {
|
|
|
|
s64 lcn = rl[i].lcn;
|
|
|
|
s64 lcn_length = rl[i].length;
|
|
|
|
|
|
|
|
if (lcn == LCN_HOLE || lcn == LCN_RL_NOT_MAPPED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* FIXME: ntfs_mapping_pairs_decompress should return error */
|
|
|
|
if (lcn < 0 || lcn_length < 0)
|
|
|
|
err_exit("Corrupt runlist in inode %lld attr %x LCN "
|
2010-12-21 22:51:07 +08:00
|
|
|
"%llx length %llx\n",
|
|
|
|
(long long)ctx->ntfs_ino->mft_no,
|
|
|
|
(unsigned int)le32_to_cpu(a->type),
|
|
|
|
(long long)lcn, (long long)lcn_length);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
if (opt.metadata_image ? wipe && !mft_data && !index_i30 : !wipe)
|
2003-06-03 19:10:57 +08:00
|
|
|
dump_clusters(walk->image, rl + i);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
for (j = 0; j < lcn_length; j++) {
|
|
|
|
u64 k = (u64)lcn + j;
|
2017-05-25 16:28:05 +08:00
|
|
|
if (ntfs_bit_get_and_set(lcn_bitmap.bm, k, 1)) {
|
|
|
|
if (opt.ignore_fs_check)
|
|
|
|
Printf("Cluster %llu is referenced"
|
|
|
|
" twice!\n",
|
|
|
|
(unsigned long long)k);
|
|
|
|
else
|
|
|
|
err_exit("Cluster %llu referenced"
|
|
|
|
" twice!\nYou didn't shutdown"
|
|
|
|
" your Windows properly?\n",
|
|
|
|
(unsigned long long)k);
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (!opt.metadata_image)
|
|
|
|
walk->image->inuse += lcn_length;
|
2012-08-20 19:44:33 +08:00
|
|
|
/*
|
|
|
|
* For a metadata image, we have to compute the
|
|
|
|
* number of metadata clusters for the percentages
|
|
|
|
* to be displayed correctly while restoring.
|
|
|
|
*/
|
|
|
|
if (!wipe && opt.metadata_image) {
|
|
|
|
if ((walk->image->ni->mft_no == FILE_LogFile)
|
|
|
|
&& (walk->image->ctx->attr->type == AT_DATA)) {
|
|
|
|
/* 16 KiB of FILE_LogFile */
|
|
|
|
walk->image->inuse
|
|
|
|
+= is_critical_metadata(walk->image,rl);
|
|
|
|
} else {
|
|
|
|
if ((walk->image->ni->mft_no
|
|
|
|
<= LAST_METADATA_INODE)
|
|
|
|
|| (walk->image->ctx->attr->type != AT_DATA))
|
|
|
|
walk->image->inuse += lcn_length;
|
|
|
|
}
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
2012-01-24 01:13:59 +08:00
|
|
|
if (wipe && opt.metadata_image) {
|
2013-02-09 18:12:45 +08:00
|
|
|
ntfs_attr *na;
|
|
|
|
/*
|
|
|
|
* Non-resident metadata has to be wiped globally,
|
|
|
|
* because its logical blocks may be larger than
|
|
|
|
* a cluster and split over two extents.
|
|
|
|
*/
|
|
|
|
if (mft_data && !a->lowest_vcn) {
|
|
|
|
na = ntfs_attr_open(walk->image->ni,
|
|
|
|
AT_DATA, NULL, 0);
|
|
|
|
if (na) {
|
|
|
|
na->rl = rl;
|
|
|
|
rl = (runlist_element*)NULL;
|
|
|
|
if (!ntfs_attr_map_whole_runlist(na)) {
|
|
|
|
copy_wipe_mft(walk->image,na->rl);
|
|
|
|
} else
|
|
|
|
perr_exit("Failed to map data of inode %lld",
|
|
|
|
(long long)walk->image->ni->mft_no);
|
|
|
|
ntfs_attr_close(na);
|
|
|
|
} else
|
|
|
|
perr_exit("Failed to open data of inode %lld",
|
|
|
|
(long long)walk->image->ni->mft_no);
|
|
|
|
}
|
|
|
|
if (index_i30 && !a->lowest_vcn) {
|
|
|
|
na = ntfs_attr_open(walk->image->ni,
|
|
|
|
AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4);
|
|
|
|
if (na) {
|
|
|
|
na->rl = rl;
|
|
|
|
rl = (runlist_element*)NULL;
|
|
|
|
if (!ntfs_attr_map_whole_runlist(na)) {
|
|
|
|
copy_wipe_i30(walk->image,na->rl);
|
|
|
|
} else
|
|
|
|
perr_exit("Failed to map index of inode %lld",
|
|
|
|
(long long)walk->image->ni->mft_no);
|
|
|
|
ntfs_attr_close(na);
|
|
|
|
} else
|
|
|
|
perr_exit("Failed to open index of inode %lld",
|
|
|
|
(long long)walk->image->ni->mft_no);
|
|
|
|
}
|
2012-01-24 01:13:59 +08:00
|
|
|
}
|
|
|
|
if (opt.metadata
|
|
|
|
&& (opt.metadata_image || !wipe)
|
|
|
|
&& (walk->image->ni->mft_no == FILE_LogFile)
|
|
|
|
&& (walk->image->ctx->attr->type == AT_DATA))
|
2005-03-06 03:08:57 +08:00
|
|
|
clone_logfile_parts(walk->image, rl);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
free(rl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void walk_attributes(struct ntfs_walk_cluster *walk)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
ntfs_attr_search_ctx *ctx;
|
|
|
|
|
|
|
|
if (!(ctx = ntfs_attr_get_search_ctx(walk->image->ni, NULL)))
|
|
|
|
perr_exit("ntfs_get_attr_search_ctx");
|
|
|
|
|
|
|
|
while (!ntfs_attrs_walk(ctx)) {
|
|
|
|
if (ctx->attr->type == AT_END)
|
|
|
|
break;
|
|
|
|
|
|
|
|
walk->image->ctx = ctx;
|
|
|
|
walk_runs(walk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ntfs_attr_put_search_ctx(ctx);
|
|
|
|
}
|
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
/*
|
|
|
|
* Compare the actual bitmap to the list of clusters
|
|
|
|
* allocated to identified files.
|
|
|
|
*
|
|
|
|
* Clusters found in use, though not marked in the bitmap are copied
|
|
|
|
* if the option --ignore-fs-checks is set.
|
|
|
|
*/
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
static int compare_bitmaps(struct bitmap *a, BOOL copy)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
s64 i, pos, count;
|
|
|
|
int mismatch = 0;
|
2013-02-09 18:41:39 +08:00
|
|
|
int more_use = 0;
|
|
|
|
s64 new_cl;
|
2003-06-03 19:10:57 +08:00
|
|
|
u8 bm[NTFS_BUF_SIZE];
|
|
|
|
|
|
|
|
Printf("Accounting clusters ...\n");
|
|
|
|
|
|
|
|
pos = 0;
|
2013-02-09 18:41:39 +08:00
|
|
|
new_cl = 0;
|
2003-06-03 19:10:57 +08:00
|
|
|
while (1) {
|
|
|
|
count = ntfs_attr_pread(vol->lcnbmp_na, pos, NTFS_BUF_SIZE, bm);
|
|
|
|
if (count == -1)
|
|
|
|
perr_exit("Couldn't get $Bitmap $DATA");
|
|
|
|
|
|
|
|
if (count == 0) {
|
2011-07-05 18:17:29 +08:00
|
|
|
/* the backup bootsector need not be accounted for */
|
|
|
|
if (((vol->nr_clusters + 7) >> 3) > pos)
|
2005-09-30 07:30:15 +08:00
|
|
|
err_exit("$Bitmap size is smaller than expected"
|
2011-07-05 18:17:29 +08:00
|
|
|
" (%lld < %lld)\n",
|
|
|
|
(long long)pos, (long long)a->size);
|
2003-06-03 19:10:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++, pos++) {
|
2005-06-20 05:09:40 +08:00
|
|
|
s64 cl; /* current cluster */
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-09-30 07:30:15 +08:00
|
|
|
if (a->size <= pos)
|
|
|
|
goto done;
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (a->bm[pos] == bm[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (cl = pos * 8; cl < (pos + 1) * 8; cl++) {
|
|
|
|
char bit;
|
|
|
|
|
|
|
|
bit = ntfs_bit_get(a->bm, cl);
|
|
|
|
if (bit == ntfs_bit_get(bm, i * 8 + cl % 8))
|
|
|
|
continue;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
if (!bit)
|
|
|
|
more_use++;
|
|
|
|
if (opt.ignore_fs_check && !bit && copy) {
|
2005-03-01 03:23:20 +08:00
|
|
|
lseek_to_cluster(cl);
|
2013-02-09 18:41:39 +08:00
|
|
|
if (opt.save_image
|
|
|
|
|| (opt.metadata
|
|
|
|
&& opt.metadata_image)) {
|
|
|
|
gap_to_cluster(cl - new_cl);
|
|
|
|
new_cl = cl + 1;
|
|
|
|
}
|
2011-02-17 23:11:26 +08:00
|
|
|
copy_cluster(opt.rescue, cl, cl);
|
2005-03-01 03:23:20 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
if (++mismatch > 10)
|
|
|
|
continue;
|
|
|
|
|
2004-03-09 22:47:34 +08:00
|
|
|
Printf("Cluster accounting failed at %lld "
|
2005-04-25 15:26:08 +08:00
|
|
|
"(0x%llx): %s cluster in $Bitmap\n",
|
|
|
|
(long long)cl, (unsigned long long)cl,
|
|
|
|
bit ? "missing" : "extra");
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-09-30 07:30:15 +08:00
|
|
|
done:
|
2003-06-03 19:10:57 +08:00
|
|
|
if (mismatch) {
|
2005-03-01 03:23:20 +08:00
|
|
|
Printf("Totally %d cluster accounting mismatches.\n", mismatch);
|
|
|
|
if (opt.ignore_fs_check) {
|
|
|
|
Printf("WARNING: The NTFS inconsistency was overruled "
|
|
|
|
"by the --ignore-fs-check option.\n");
|
2013-02-09 18:41:39 +08:00
|
|
|
if (new_cl) {
|
|
|
|
gap_to_cluster(-new_cl);
|
|
|
|
}
|
|
|
|
return (more_use);
|
2005-03-01 03:23:20 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
err_exit("Filesystem check failed! Windows wasn't shutdown "
|
|
|
|
"properly or inconsistent\nfilesystem. Please run "
|
2005-10-01 04:29:58 +08:00
|
|
|
"chkdsk /f on Windows then reboot it TWICE.\n");
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
2013-02-09 18:41:39 +08:00
|
|
|
return (more_use);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-06 03:35:20 +08:00
|
|
|
static void mft_record_write_with_same_usn(ntfs_volume *volume, ntfs_inode *ni)
|
|
|
|
{
|
2006-04-05 10:49:24 +08:00
|
|
|
if (ntfs_mft_usn_dec(ni->mrec))
|
|
|
|
perr_exit("ntfs_mft_usn_dec");
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-02-06 03:35:20 +08:00
|
|
|
if (ntfs_mft_record_write(volume, ni->mft_no, ni->mrec))
|
|
|
|
perr_exit("ntfs_mft_record_write");
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2006-08-13 23:00:27 +08:00
|
|
|
static void mft_inode_write_with_same_usn(ntfs_volume *volume, ntfs_inode *ni)
|
|
|
|
{
|
|
|
|
s32 i;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-08-13 23:00:27 +08:00
|
|
|
mft_record_write_with_same_usn(volume, ni);
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-08-13 23:00:27 +08:00
|
|
|
if (ni->nr_extents <= 0)
|
|
|
|
return;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2006-08-13 23:00:27 +08:00
|
|
|
for (i = 0; i < ni->nr_extents; ++i) {
|
|
|
|
ntfs_inode *eni = ni->extent_nis[i];
|
|
|
|
mft_record_write_with_same_usn(volume, eni);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static int walk_clusters(ntfs_volume *volume, struct ntfs_walk_cluster *walk)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
s64 inode = 0;
|
|
|
|
s64 last_mft_rec;
|
2011-10-07 17:42:08 +08:00
|
|
|
u64 nr_clusters;
|
2003-06-03 19:10:57 +08:00
|
|
|
ntfs_inode *ni;
|
|
|
|
struct progress_bar progress;
|
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
if (opt.restore_image || (!opt.metadata && wipe))
|
|
|
|
err_exit("Bug : invalid walk_clusters()\n");
|
2003-06-03 19:10:57 +08:00
|
|
|
Printf("Scanning volume ...\n");
|
|
|
|
|
2004-09-13 18:28:54 +08:00
|
|
|
last_mft_rec = (volume->mft_na->initialized_size >>
|
|
|
|
volume->mft_record_size_bits) - 1;
|
2011-10-07 17:42:08 +08:00
|
|
|
walk->image->current_lcn = 0;
|
2003-06-03 19:10:57 +08:00
|
|
|
progress_init(&progress, inode, last_mft_rec, 100);
|
|
|
|
|
2011-09-23 17:28:42 +08:00
|
|
|
NVolSetNoFixupWarn(volume);
|
2003-06-03 19:10:57 +08:00
|
|
|
for (; inode <= last_mft_rec; inode++) {
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
int err, deleted_inode;
|
|
|
|
MFT_REF mref = (MFT_REF)inode;
|
|
|
|
|
|
|
|
progress_update(&progress, inode);
|
|
|
|
|
2003-07-11 20:35:05 +08:00
|
|
|
/* FIXME: Terrible kludge for libntfs not being able to return
|
2003-06-03 19:10:57 +08:00
|
|
|
a deleted MFT record as inode */
|
2006-08-13 22:49:35 +08:00
|
|
|
ni = ntfs_calloc(sizeof(ntfs_inode));
|
2003-06-03 19:10:57 +08:00
|
|
|
if (!ni)
|
|
|
|
perr_exit("walk_clusters");
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
ni->vol = volume;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
err = ntfs_file_record_read(volume, mref, &ni->mrec, NULL);
|
2003-06-03 19:10:57 +08:00
|
|
|
if (err == -1) {
|
|
|
|
free(ni);
|
|
|
|
continue;
|
2005-06-20 05:09:40 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
deleted_inode = !(ni->mrec->flags & MFT_RECORD_IN_USE);
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
if (deleted_inode && !opt.metadata_image) {
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
ni->mft_no = MREF(mref);
|
|
|
|
if (wipe) {
|
|
|
|
wipe_unused_mft(ni);
|
|
|
|
wipe_unused_mft_data(ni);
|
2006-02-06 03:35:20 +08:00
|
|
|
mft_record_write_with_same_usn(volume, ni);
|
2005-06-20 05:09:40 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2005-10-22 02:05:13 +08:00
|
|
|
free(ni->mrec);
|
2003-06-03 19:10:57 +08:00
|
|
|
free(ni);
|
|
|
|
|
2005-10-27 06:54:57 +08:00
|
|
|
if (deleted_inode)
|
2003-06-03 19:10:57 +08:00
|
|
|
continue;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
if ((ni = ntfs_inode_open(volume, mref)) == NULL) {
|
2003-06-03 19:10:57 +08:00
|
|
|
/* FIXME: continue only if it make sense, e.g.
|
|
|
|
MFT record not in use based on $MFT bitmap */
|
|
|
|
if (errno == EIO || errno == ENOENT)
|
|
|
|
continue;
|
2010-12-21 22:51:07 +08:00
|
|
|
perr_exit("Reading inode %lld failed",
|
|
|
|
(long long)inode);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wipe)
|
|
|
|
nr_used_mft_records++;
|
|
|
|
|
2004-04-23 18:12:49 +08:00
|
|
|
if (ni->mrec->base_mft_record)
|
2003-06-03 19:10:57 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
walk->image->ni = ni;
|
|
|
|
walk_attributes(walk);
|
2005-06-20 05:09:40 +08:00
|
|
|
out:
|
2012-01-24 00:11:46 +08:00
|
|
|
if (wipe && !opt.metadata_image) {
|
|
|
|
int i;
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
wipe_unused_mft_data(ni);
|
2012-01-24 00:11:46 +08:00
|
|
|
for (i = 0; i < ni->nr_extents; ++i) {
|
|
|
|
wipe_unused_mft_data(ni->extent_nis[i]);
|
|
|
|
}
|
2006-08-13 23:00:27 +08:00
|
|
|
mft_inode_write_with_same_usn(volume, ni);
|
2005-06-20 05:09:40 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
if (ntfs_inode_close(ni))
|
2010-12-21 22:51:07 +08:00
|
|
|
perr_exit("ntfs_inode_close for inode %lld",
|
|
|
|
(long long)inode);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.metadata) {
|
2013-02-09 18:41:39 +08:00
|
|
|
if (opt.metadata_image && wipe && opt.ignore_fs_check) {
|
|
|
|
gap_to_cluster(-walk->image->current_lcn);
|
|
|
|
compare_bitmaps(&lcn_bitmap, TRUE);
|
|
|
|
walk->image->current_lcn = 0;
|
|
|
|
}
|
2013-02-09 19:53:38 +08:00
|
|
|
if (opt.metadata_image ? wipe : !wipe) {
|
2011-10-07 17:42:08 +08:00
|
|
|
/* also get the backup bootsector */
|
2013-02-09 19:53:38 +08:00
|
|
|
nr_clusters = vol->nr_clusters;
|
|
|
|
lseek_to_cluster(nr_clusters);
|
|
|
|
if (opt.metadata_image && wipe)
|
|
|
|
gap_to_cluster(nr_clusters
|
|
|
|
- walk->image->current_lcn);
|
2011-10-07 17:42:08 +08:00
|
|
|
copy_cluster(opt.rescue, nr_clusters, nr_clusters);
|
2013-02-09 19:53:38 +08:00
|
|
|
walk->image->current_lcn = nr_clusters;
|
|
|
|
}
|
2012-08-20 19:44:33 +08:00
|
|
|
/* Not counted, for compatibility with older versions */
|
|
|
|
if (!opt.metadata_image)
|
|
|
|
walk->image->inuse++;
|
2011-10-07 17:42:08 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $Bitmap can overlap the end of the volume. Any bits in this region
|
|
|
|
* must be set. This region also encompasses the backup boot sector.
|
|
|
|
*/
|
2004-01-16 01:07:57 +08:00
|
|
|
static void bitmap_file_data_fixup(s64 cluster, struct bitmap *bm)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
for (; cluster < bm->size << 3; cluster++)
|
|
|
|
ntfs_bit_set(bm->bm, (u64)cluster, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a block of memory with one bit for each cluster of the disk.
|
|
|
|
* All the bits are set to 0, except those representing the region beyond the
|
|
|
|
* end of the disk.
|
|
|
|
*/
|
2004-01-16 01:07:57 +08:00
|
|
|
static void setup_lcn_bitmap(void)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
/* Determine lcn bitmap byte size and allocate it. */
|
2011-02-16 19:18:56 +08:00
|
|
|
/* include the alternate boot sector in the bitmap count */
|
|
|
|
lcn_bitmap.size = rounded_up_division(vol->nr_clusters + 1, 8);
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2006-08-13 22:49:35 +08:00
|
|
|
lcn_bitmap.bm = ntfs_calloc(lcn_bitmap.size);
|
|
|
|
if (!lcn_bitmap.bm)
|
2003-06-03 19:10:57 +08:00
|
|
|
perr_exit("Failed to allocate internal buffer");
|
|
|
|
|
|
|
|
bitmap_file_data_fixup(vol->nr_clusters, &lcn_bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static s64 volume_size(ntfs_volume *volume, s64 nr_clusters)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2004-01-16 01:07:57 +08:00
|
|
|
return nr_clusters * volume->cluster_size;
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void print_volume_size(const char *str, s64 bytes)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2004-03-09 22:47:34 +08:00
|
|
|
Printf("%s: %lld bytes (%lld MB)\n", str, (long long)bytes,
|
|
|
|
(long long)rounded_up_division(bytes, NTFS_MBYTE));
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-29 07:41:41 +08:00
|
|
|
static void print_disk_usage(const char *spacer, u32 cluster_size,
|
|
|
|
s64 nr_clusters, s64 inuse)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
2003-07-11 20:35:05 +08:00
|
|
|
s64 total, used;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
total = nr_clusters * cluster_size;
|
|
|
|
used = inuse * cluster_size;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2006-08-23 19:22:07 +08:00
|
|
|
Printf("Space in use %s: %lld MB (%.1f%%) ", spacer,
|
2004-03-09 22:47:34 +08:00
|
|
|
(long long)rounded_up_division(used, NTFS_MBYTE),
|
|
|
|
100.0 * ((float)used / total));
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
static void print_image_info(void)
|
|
|
|
{
|
2006-08-23 19:22:07 +08:00
|
|
|
Printf("Ntfsclone image version: %d.%d\n",
|
2006-08-23 18:18:34 +08:00
|
|
|
image_hdr.major_ver, image_hdr.minor_ver);
|
2006-08-23 19:22:07 +08:00
|
|
|
Printf("Cluster size : %u bytes\n",
|
2006-08-23 18:18:34 +08:00
|
|
|
(unsigned)le32_to_cpu(image_hdr.cluster_size));
|
2006-08-23 19:22:07 +08:00
|
|
|
print_volume_size("Image volume size ",
|
2006-08-23 18:18:34 +08:00
|
|
|
sle64_to_cpu(image_hdr.nr_clusters) *
|
|
|
|
le32_to_cpu(image_hdr.cluster_size));
|
2006-08-23 19:22:07 +08:00
|
|
|
Printf("Image device size : %lld bytes\n",
|
2015-12-22 06:55:31 +08:00
|
|
|
(long long)le64_to_cpu(image_hdr.device_size));
|
2006-08-23 19:22:07 +08:00
|
|
|
print_disk_usage(" ", le32_to_cpu(image_hdr.cluster_size),
|
2006-08-23 18:18:34 +08:00
|
|
|
sle64_to_cpu(image_hdr.nr_clusters),
|
2015-12-22 06:55:31 +08:00
|
|
|
le64_to_cpu(image_hdr.inuse));
|
2006-08-23 19:22:07 +08:00
|
|
|
Printf("Offset to image data : %u (0x%x) bytes\n",
|
|
|
|
(unsigned)le32_to_cpu(image_hdr.offset_to_image_data),
|
|
|
|
(unsigned)le32_to_cpu(image_hdr.offset_to_image_data));
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
|
|
|
|
2004-03-23 10:00:56 +08:00
|
|
|
static void check_if_mounted(const char *device, unsigned long new_mntflag)
|
2003-06-03 19:10:57 +08:00
|
|
|
{
|
|
|
|
unsigned long mntflag;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-03-23 10:00:56 +08:00
|
|
|
if (ntfs_check_if_mounted(device, &mntflag))
|
|
|
|
perr_exit("Failed to check '%s' mount state", device);
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
if (mntflag & NTFS_MF_MOUNTED) {
|
|
|
|
if (!(mntflag & NTFS_MF_READONLY))
|
2005-06-12 18:15:21 +08:00
|
|
|
err_exit("Device '%s' is mounted read-write. "
|
2004-03-23 10:00:56 +08:00
|
|
|
"You must 'umount' it first.\n", device);
|
2003-06-03 19:10:57 +08:00
|
|
|
if (!new_mntflag)
|
2005-06-12 18:15:21 +08:00
|
|
|
err_exit("Device '%s' is mounted. "
|
2004-03-23 10:00:56 +08:00
|
|
|
"You must 'umount' it first.\n", device);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
2004-03-23 10:00:56 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-03-23 10:00:56 +08:00
|
|
|
/**
|
2005-10-28 20:55:41 +08:00
|
|
|
* mount_volume -
|
|
|
|
*
|
2004-03-23 10:00:56 +08:00
|
|
|
* First perform some checks to determine if the volume is already mounted, or
|
|
|
|
* is dirty (Windows wasn't shutdown properly). If everything is OK, then mount
|
|
|
|
* the volume (load the metadata into memory).
|
|
|
|
*/
|
|
|
|
static void mount_volume(unsigned long new_mntflag)
|
|
|
|
{
|
|
|
|
check_if_mounted(opt.volume, new_mntflag);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (!(vol = ntfs_mount(opt.volume, new_mntflag))) {
|
|
|
|
|
|
|
|
int err = errno;
|
|
|
|
|
2005-06-12 18:15:21 +08:00
|
|
|
perr_printf("Opening '%s' as NTFS failed", opt.volume);
|
2003-06-03 19:10:57 +08:00
|
|
|
if (err == EINVAL) {
|
|
|
|
Printf("Apparently device '%s' doesn't have a "
|
|
|
|
"valid NTFS. Maybe you selected\nthe whole "
|
|
|
|
"disk instead of a partition (e.g. /dev/hda, "
|
|
|
|
"not /dev/hda1)?\n", opt.volume);
|
|
|
|
}
|
2013-02-09 18:41:39 +08:00
|
|
|
/*
|
|
|
|
* Retry with recovering the log file enabled.
|
|
|
|
* Normally avoided in order to get the original log file
|
|
|
|
* data, but needed when remounting the metadata of a
|
|
|
|
* volume improperly unmounted from Windows.
|
2016-04-06 14:51:36 +08:00
|
|
|
* If the full log file was requested, it must be kept
|
|
|
|
* as is, so we just remount read-only.
|
2013-02-09 18:41:39 +08:00
|
|
|
*/
|
|
|
|
if (!(new_mntflag & (NTFS_MNT_RDONLY | NTFS_MNT_RECOVER))) {
|
2016-04-06 14:51:36 +08:00
|
|
|
if (opt.full_logfile) {
|
|
|
|
Printf("Retrying read-only to ignore"
|
|
|
|
" the log file...\n");
|
|
|
|
vol = ntfs_mount(opt.volume,
|
|
|
|
new_mntflag | NTFS_MNT_RDONLY);
|
|
|
|
} else {
|
|
|
|
Printf("Trying to recover...\n");
|
|
|
|
vol = ntfs_mount(opt.volume,
|
2013-02-09 18:41:39 +08:00
|
|
|
new_mntflag | NTFS_MNT_RECOVER);
|
2016-04-06 14:51:36 +08:00
|
|
|
}
|
2013-02-09 18:41:39 +08:00
|
|
|
Printf("... %s\n",(vol ? "Successful" : "Failed"));
|
|
|
|
}
|
|
|
|
if (!vol)
|
|
|
|
exit(1);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vol->flags & VOLUME_IS_DIRTY)
|
|
|
|
if (opt.force-- <= 0)
|
2006-02-06 06:10:01 +08:00
|
|
|
err_exit(dirty_volume_msg, opt.volume);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
if (NTFS_MAX_CLUSTER_SIZE < vol->cluster_size)
|
2004-08-20 20:30:34 +08:00
|
|
|
err_exit("Cluster size %u is too large!\n",
|
|
|
|
(unsigned int)vol->cluster_size);
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
Printf("NTFS volume version: %d.%d\n", vol->major_ver, vol->minor_ver);
|
|
|
|
if (ntfs_version_is_supported(vol))
|
|
|
|
perr_exit("Unknown NTFS version");
|
|
|
|
|
2004-04-23 18:12:49 +08:00
|
|
|
Printf("Cluster size : %u bytes\n",
|
|
|
|
(unsigned int)vol->cluster_size);
|
2003-06-03 19:10:57 +08:00
|
|
|
print_volume_size("Current volume size",
|
|
|
|
volume_size(vol, vol->nr_clusters));
|
|
|
|
}
|
|
|
|
|
2006-12-13 03:00:00 +08:00
|
|
|
static struct ntfs_walk_cluster backup_clusters = { NULL, NULL };
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static int device_offset_valid(int fd, s64 ofs)
|
2003-07-11 20:35:05 +08:00
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (lseek(fd, ofs, SEEK_SET) >= 0 && read(fd, &ch, 1) == 1)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static s64 device_size_get(int fd)
|
2003-07-11 20:35:05 +08:00
|
|
|
{
|
|
|
|
s64 high, low;
|
2004-02-17 22:01:22 +08:00
|
|
|
#ifdef BLKGETSIZE64
|
|
|
|
{ u64 size;
|
|
|
|
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
|
2005-10-25 09:28:10 +08:00
|
|
|
ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu "
|
|
|
|
"(0x%llx).\n", (unsigned long long)size,
|
2005-04-25 15:26:08 +08:00
|
|
|
(unsigned long long)size);
|
2004-03-17 20:22:03 +08:00
|
|
|
return (s64)size;
|
2004-02-17 22:01:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2003-07-11 20:35:05 +08:00
|
|
|
#ifdef BLKGETSIZE
|
2004-02-17 22:01:22 +08:00
|
|
|
{ unsigned long size;
|
2003-07-11 20:35:05 +08:00
|
|
|
|
2004-02-17 22:01:22 +08:00
|
|
|
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
|
2005-10-25 09:28:10 +08:00
|
|
|
ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu "
|
|
|
|
"(0x%lx).\n", size, size);
|
2004-02-17 22:01:22 +08:00
|
|
|
return (s64)size * 512;
|
|
|
|
}
|
2003-07-11 20:35:05 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FDGETPRM
|
|
|
|
{ struct floppy_struct this_floppy;
|
|
|
|
|
|
|
|
if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
|
2005-10-25 09:28:10 +08:00
|
|
|
ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu "
|
|
|
|
"(0x%lx).\n", this_floppy.size,
|
|
|
|
this_floppy.size);
|
2003-07-11 20:35:05 +08:00
|
|
|
return (s64)this_floppy.size * 512;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* We couldn't figure it out by using a specialized ioctl,
|
|
|
|
* so do binary search to find the size of the device.
|
|
|
|
*/
|
|
|
|
low = 0LL;
|
|
|
|
for (high = 1024LL; !device_offset_valid(fd, high); high <<= 1)
|
|
|
|
low = high;
|
|
|
|
while (low < high - 1LL) {
|
|
|
|
const s64 mid = (low + high) / 2;
|
|
|
|
|
|
|
|
if (!device_offset_valid(fd, mid))
|
|
|
|
low = mid;
|
|
|
|
else
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
lseek(fd, 0LL, SEEK_SET);
|
|
|
|
return (low + 1LL);
|
|
|
|
}
|
|
|
|
|
2004-01-16 01:07:57 +08:00
|
|
|
static void fsync_clone(int fd)
|
|
|
|
{
|
|
|
|
Printf("Syncing ...\n");
|
2011-03-23 16:35:24 +08:00
|
|
|
if (opt.save_image && stream_out && fflush(stream_out))
|
|
|
|
perr_exit("fflush");
|
2004-01-16 01:07:57 +08:00
|
|
|
if (fsync(fd) && errno != EINVAL)
|
|
|
|
perr_exit("fsync");
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-04-02 05:30:42 +08:00
|
|
|
static void set_filesize(s64 filesize)
|
|
|
|
{
|
2011-07-05 18:17:11 +08:00
|
|
|
#ifndef NO_STATFS
|
2005-06-28 05:03:06 +08:00
|
|
|
long fs_type = 0; /* Unknown filesystem type */
|
|
|
|
|
2004-04-02 05:30:42 +08:00
|
|
|
if (fstatfs(fd_out, &opt.stfs) == -1)
|
|
|
|
Printf("WARNING: Couldn't get filesystem type: "
|
|
|
|
"%s\n", strerror(errno));
|
2005-10-22 02:05:13 +08:00
|
|
|
else
|
2005-06-28 05:03:06 +08:00
|
|
|
fs_type = opt.stfs.f_type;
|
2005-10-22 02:05:13 +08:00
|
|
|
|
2005-06-28 05:03:06 +08:00
|
|
|
if (fs_type == 0x52654973)
|
2004-04-02 05:30:42 +08:00
|
|
|
Printf("WARNING: You're using ReiserFS, it has very poor "
|
|
|
|
"performance creating\nlarge sparse files. The next "
|
|
|
|
"operation might take a very long time!\n"
|
|
|
|
"Creating sparse output file ...\n");
|
2005-06-28 05:03:06 +08:00
|
|
|
else if (fs_type == 0x517b)
|
2004-04-02 05:30:42 +08:00
|
|
|
Printf("WARNING: You're using SMBFS and if the remote share "
|
|
|
|
"isn't Samba but a Windows\ncomputer then the clone "
|
|
|
|
"operation will be very inefficient and may fail!\n");
|
2011-07-05 18:17:11 +08:00
|
|
|
#endif
|
2004-04-02 05:30:42 +08:00
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action && (ftruncate(fd_out, filesize) == -1)) {
|
2004-04-02 05:30:42 +08:00
|
|
|
int err = errno;
|
|
|
|
perr_printf("ftruncate failed for file '%s'", opt.output);
|
2011-07-05 18:17:11 +08:00
|
|
|
#ifndef NO_STATFS
|
2005-06-28 05:03:06 +08:00
|
|
|
if (fs_type)
|
|
|
|
Printf("Destination filesystem type is 0x%lx.\n",
|
|
|
|
(unsigned long)fs_type);
|
2011-07-05 18:17:11 +08:00
|
|
|
#endif
|
2004-04-02 05:30:42 +08:00
|
|
|
if (err == E2BIG) {
|
|
|
|
Printf("Your system or the destination filesystem "
|
|
|
|
"doesn't support large files.\n");
|
2011-07-05 18:17:11 +08:00
|
|
|
#ifndef NO_STATFS
|
2005-06-28 05:03:06 +08:00
|
|
|
if (fs_type == 0x517b) {
|
2004-04-02 05:30:42 +08:00
|
|
|
Printf("SMBFS needs minimum Linux kernel "
|
|
|
|
"version 2.4.25 and\n the 'lfs' option"
|
2004-06-04 16:59:26 +08:00
|
|
|
"\nfor smbmount to have large "
|
2004-04-02 05:30:42 +08:00
|
|
|
"file support.\n");
|
|
|
|
}
|
2011-07-05 18:17:11 +08:00
|
|
|
#endif
|
2005-06-28 05:03:06 +08:00
|
|
|
} else if (err == EPERM) {
|
|
|
|
Printf("Apparently the destination filesystem doesn't "
|
|
|
|
"support sparse files.\nYou can overcome this "
|
|
|
|
"by using the more efficient --save-image "
|
|
|
|
"option\nof ntfsclone. Use the --restore-image "
|
|
|
|
"option to restore the image.\n");
|
2004-04-02 05:30:42 +08:00
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-03-11 16:54:53 +08:00
|
|
|
/*
|
|
|
|
* If truncate just created a sparse file, the ability
|
|
|
|
* to generically store big files has been checked, but no
|
|
|
|
* space has been reserved and available space has probably
|
|
|
|
* not been checked. Better reset the file so that we write
|
|
|
|
* sequentially to the end.
|
|
|
|
*/
|
|
|
|
if (!opt.no_action) {
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
if (ftruncate(fd_out, 0))
|
|
|
|
Printf("Failed to reset the output file.\n");
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = fstat(fd_out, &st);
|
|
|
|
if (s || (!st.st_blocks && ftruncate(fd_out, 0)))
|
|
|
|
Printf("Failed to reset the output file.\n");
|
|
|
|
#endif
|
|
|
|
/* Proceed even if ftruncate failed */
|
|
|
|
}
|
2004-04-02 05:30:42 +08:00
|
|
|
}
|
2004-07-11 04:01:17 +08:00
|
|
|
|
|
|
|
static s64 open_image(void)
|
|
|
|
{
|
|
|
|
if (strcmp(opt.volume, "-") == 0) {
|
2005-06-20 05:09:40 +08:00
|
|
|
if ((fd_in = fileno(stdin)) == -1)
|
2013-02-09 19:54:44 +08:00
|
|
|
perr_exit("fileno for stdin failed");
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
if (setmode(fd_in,O_BINARY) == -1)
|
|
|
|
perr_exit("setting binary stdin failed");
|
|
|
|
#endif
|
2004-07-11 04:01:17 +08:00
|
|
|
} else {
|
2013-02-09 19:54:44 +08:00
|
|
|
if ((fd_in = open(opt.volume, O_RDONLY | O_BINARY)) == -1)
|
2004-07-11 04:01:17 +08:00
|
|
|
perr_exit("failed to open image");
|
|
|
|
}
|
2006-08-23 19:22:07 +08:00
|
|
|
if (read_all(&fd_in, &image_hdr, NTFSCLONE_IMG_HEADER_SIZE_OLD) == -1)
|
2004-07-11 04:01:17 +08:00
|
|
|
perr_exit("read_all");
|
|
|
|
if (memcmp(image_hdr.magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE) != 0)
|
|
|
|
err_exit("Input file is not an image! (invalid magic)\n");
|
2006-08-23 18:18:34 +08:00
|
|
|
if (image_hdr.major_ver < NTFSCLONE_IMG_VER_MAJOR_ENDIANNESS_SAFE) {
|
|
|
|
image_hdr.major_ver = NTFSCLONE_IMG_VER_MAJOR;
|
|
|
|
image_hdr.minor_ver = NTFSCLONE_IMG_VER_MINOR;
|
2006-11-28 18:09:56 +08:00
|
|
|
#if (__BYTE_ORDER == __BIG_ENDIAN)
|
2010-12-21 22:51:07 +08:00
|
|
|
/*
|
|
|
|
* old image read on a big endian computer,
|
|
|
|
* assuming it was created big endian and read cpu-wise,
|
|
|
|
* so we should translate to little endian
|
|
|
|
*/
|
2006-11-28 18:09:56 +08:00
|
|
|
Printf("Old image format detected. If the image was created "
|
|
|
|
"on a little endian architecture it will not "
|
|
|
|
"work. Use a more recent version of "
|
|
|
|
"ntfsclone to recreate the image.\n");
|
2006-08-23 18:18:34 +08:00
|
|
|
image_hdr.cluster_size = cpu_to_le32(image_hdr.cluster_size);
|
|
|
|
image_hdr.device_size = cpu_to_sle64(image_hdr.device_size);
|
|
|
|
image_hdr.nr_clusters = cpu_to_sle64(image_hdr.nr_clusters);
|
|
|
|
image_hdr.inuse = cpu_to_sle64(image_hdr.inuse);
|
2006-11-28 18:09:56 +08:00
|
|
|
#endif
|
2006-08-23 19:22:07 +08:00
|
|
|
image_hdr.offset_to_image_data =
|
2010-12-21 22:51:07 +08:00
|
|
|
const_cpu_to_le32((sizeof(image_hdr)
|
|
|
|
+ IMAGE_HDR_ALIGN - 1) & -IMAGE_HDR_ALIGN);
|
2007-08-23 22:58:52 +08:00
|
|
|
image_is_host_endian = TRUE;
|
2006-08-23 19:22:07 +08:00
|
|
|
} else {
|
2010-12-21 22:51:07 +08:00
|
|
|
/* safe image : little endian data */
|
|
|
|
le32 offset_to_image_data;
|
2006-08-23 19:22:07 +08:00
|
|
|
int delta;
|
|
|
|
|
|
|
|
if (image_hdr.major_ver > NTFSCLONE_IMG_VER_MAJOR)
|
|
|
|
err_exit("Do not know how to handle image format "
|
|
|
|
"version %d.%d. Please obtain a "
|
|
|
|
"newer version of ntfsclone.\n",
|
|
|
|
image_hdr.major_ver,
|
|
|
|
image_hdr.minor_ver);
|
|
|
|
/* Read the image header data offset. */
|
|
|
|
if (read_all(&fd_in, &offset_to_image_data,
|
|
|
|
sizeof(offset_to_image_data)) == -1)
|
|
|
|
perr_exit("read_all");
|
2010-12-21 22:51:07 +08:00
|
|
|
/* do not translate little endian data */
|
|
|
|
image_hdr.offset_to_image_data = offset_to_image_data;
|
2006-08-23 19:22:07 +08:00
|
|
|
/*
|
|
|
|
* Read any fields from the header that we have not read yet so
|
|
|
|
* that the input stream is positioned correctly. This means
|
|
|
|
* we can support future minor versions that just extend the
|
|
|
|
* header in a backwards compatible way.
|
|
|
|
*/
|
2010-12-21 22:51:07 +08:00
|
|
|
delta = le32_to_cpu(offset_to_image_data)
|
|
|
|
- (NTFSCLONE_IMG_HEADER_SIZE_OLD +
|
2007-08-23 22:58:52 +08:00
|
|
|
sizeof(image_hdr.offset_to_image_data));
|
2006-08-23 19:22:07 +08:00
|
|
|
if (delta > 0) {
|
|
|
|
char *dummy_buf;
|
|
|
|
|
|
|
|
dummy_buf = malloc(delta);
|
|
|
|
if (!dummy_buf)
|
|
|
|
perr_exit("malloc dummy_buffer");
|
|
|
|
if (read_all(&fd_in, dummy_buf, delta) == -1)
|
|
|
|
perr_exit("read_all");
|
2010-12-21 22:51:07 +08:00
|
|
|
free(dummy_buf);
|
2006-08-23 19:22:07 +08:00
|
|
|
}
|
2006-08-23 18:18:34 +08:00
|
|
|
}
|
2015-12-22 06:55:31 +08:00
|
|
|
return le64_to_cpu(image_hdr.device_size);
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static s64 open_volume(void)
|
|
|
|
{
|
|
|
|
s64 device_size;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2012-11-07 23:29:48 +08:00
|
|
|
mount_volume(NTFS_MNT_RDONLY);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
device_size = ntfs_device_size_get(vol->dev, 1);
|
|
|
|
if (device_size <= 0)
|
2010-12-21 22:51:07 +08:00
|
|
|
err_exit("Couldn't get device size (%lld)!\n",
|
|
|
|
(long long)device_size);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
print_volume_size("Current device size", device_size);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (device_size < vol->nr_clusters * vol->cluster_size)
|
|
|
|
err_exit("Current NTFS volume size is bigger than the device "
|
|
|
|
"size (%lld)!\nCorrupt partition table or incorrect "
|
2010-12-21 22:51:07 +08:00
|
|
|
"device partitioning?\n", (long long)device_size);
|
2004-07-11 04:01:17 +08:00
|
|
|
|
|
|
|
return device_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initialise_image_hdr(s64 device_size, s64 inuse)
|
|
|
|
{
|
|
|
|
memcpy(image_hdr.magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE);
|
2006-08-23 18:18:34 +08:00
|
|
|
image_hdr.major_ver = NTFSCLONE_IMG_VER_MAJOR;
|
|
|
|
image_hdr.minor_ver = NTFSCLONE_IMG_VER_MINOR;
|
|
|
|
image_hdr.cluster_size = cpu_to_le32(vol->cluster_size);
|
2015-12-22 06:55:31 +08:00
|
|
|
image_hdr.device_size = cpu_to_le64(device_size);
|
2006-08-23 18:18:34 +08:00
|
|
|
image_hdr.nr_clusters = cpu_to_sle64(vol->nr_clusters);
|
2015-12-22 06:55:31 +08:00
|
|
|
image_hdr.inuse = cpu_to_le64(inuse);
|
2010-12-21 22:51:07 +08:00
|
|
|
image_hdr.offset_to_image_data = cpu_to_le32((sizeof(image_hdr)
|
|
|
|
+ IMAGE_HDR_ALIGN - 1) & -IMAGE_HDR_ALIGN);
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
|
|
|
|
2004-09-04 04:00:47 +08:00
|
|
|
static void check_output_device(s64 input_size)
|
2004-07-11 04:01:17 +08:00
|
|
|
{
|
|
|
|
if (opt.blkdev_out) {
|
2013-02-09 19:54:44 +08:00
|
|
|
s64 dest_size;
|
|
|
|
|
|
|
|
if (dev_out)
|
|
|
|
dest_size = ntfs_device_size_get(dev_out, 1);
|
|
|
|
else
|
|
|
|
dest_size = device_size_get(fd_out);
|
2004-09-04 04:00:47 +08:00
|
|
|
if (dest_size < input_size)
|
|
|
|
err_exit("Output device is too small (%lld) to fit the "
|
2010-12-21 22:51:07 +08:00
|
|
|
"NTFS image (%lld).\n",
|
|
|
|
(long long)dest_size, (long long)input_size);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
check_if_mounted(opt.output, 0);
|
|
|
|
} else
|
2004-09-04 04:00:47 +08:00
|
|
|
set_filesize(input_size);
|
2004-07-11 04:01:17 +08:00
|
|
|
}
|
2004-04-02 05:30:42 +08:00
|
|
|
|
2004-10-01 02:16:48 +08:00
|
|
|
static void ignore_bad_clusters(ntfs_walk_clusters_ctx *image)
|
|
|
|
{
|
|
|
|
ntfs_inode *ni;
|
2015-11-09 23:16:39 +08:00
|
|
|
ntfs_attr *na;
|
|
|
|
runlist *rl;
|
2004-10-01 02:16:48 +08:00
|
|
|
s64 nr_bad_clusters = 0;
|
2015-11-09 23:16:39 +08:00
|
|
|
static le16 Bad[4] = {
|
|
|
|
const_cpu_to_le16('$'), const_cpu_to_le16('B'),
|
|
|
|
const_cpu_to_le16('a'), const_cpu_to_le16('d')
|
|
|
|
} ;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-10-01 02:16:48 +08:00
|
|
|
if (!(ni = ntfs_inode_open(vol, FILE_BadClus)))
|
|
|
|
perr_exit("ntfs_open_inode");
|
|
|
|
|
2015-11-09 23:16:39 +08:00
|
|
|
na = ntfs_attr_open(ni, AT_DATA, Bad, 4);
|
|
|
|
if (!na)
|
|
|
|
perr_exit("ntfs_attr_open");
|
|
|
|
if (ntfs_attr_map_whole_runlist(na))
|
|
|
|
perr_exit("ntfs_attr_map_whole_runlist");
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2015-11-09 23:16:39 +08:00
|
|
|
for (rl = na->rl; rl->length; rl++) {
|
2004-10-01 02:16:48 +08:00
|
|
|
s64 lcn = rl->lcn;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2004-10-01 02:16:48 +08:00
|
|
|
if (lcn == LCN_HOLE || lcn < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (; lcn < rl->lcn + rl->length; lcn++, nr_bad_clusters++) {
|
|
|
|
if (ntfs_bit_get_and_set(lcn_bitmap.bm, lcn, 0))
|
|
|
|
image->inuse--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nr_bad_clusters)
|
|
|
|
Printf("WARNING: The disk has %lld or more bad sectors"
|
2010-12-21 22:51:07 +08:00
|
|
|
" (hardware faults).\n", (long long)nr_bad_clusters);
|
2015-11-09 23:16:39 +08:00
|
|
|
ntfs_attr_close(na);
|
2004-10-01 02:16:48 +08:00
|
|
|
if (ntfs_inode_close(ni))
|
|
|
|
perr_exit("ntfs_inode_close failed for $BadClus");
|
|
|
|
}
|
|
|
|
|
2005-11-08 04:49:18 +08:00
|
|
|
static void check_dest_free_space(u64 src_bytes)
|
|
|
|
{
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifndef HAVE_WINDOWS_H
|
2005-11-08 04:49:18 +08:00
|
|
|
u64 dest_bytes;
|
|
|
|
struct statvfs stvfs;
|
2006-12-13 08:11:50 +08:00
|
|
|
struct stat st;
|
2005-11-08 04:49:18 +08:00
|
|
|
|
2005-11-14 05:48:35 +08:00
|
|
|
if (opt.metadata || opt.blkdev_out || opt.std_out)
|
2005-11-08 04:49:18 +08:00
|
|
|
return;
|
2006-10-27 20:24:29 +08:00
|
|
|
/*
|
2005-11-14 05:48:35 +08:00
|
|
|
* TODO: save_image needs a bit more space than src_bytes
|
|
|
|
* due to the free space encoding overhead.
|
|
|
|
*/
|
2005-11-08 04:49:18 +08:00
|
|
|
if (fstatvfs(fd_out, &stvfs) == -1) {
|
|
|
|
Printf("WARNING: Unknown free space on the destination: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2006-12-04 02:26:58 +08:00
|
|
|
|
|
|
|
/* If file is a FIFO then there is no point in checking the size. */
|
2006-12-13 08:11:50 +08:00
|
|
|
if (!fstat(fd_out, &st)) {
|
|
|
|
if (S_ISFIFO(st.st_mode))
|
2006-12-04 02:26:58 +08:00
|
|
|
return;
|
|
|
|
} else
|
|
|
|
Printf("WARNING: fstat failed: %s\n", strerror(errno));
|
2005-11-08 04:49:18 +08:00
|
|
|
|
2006-06-16 02:43:24 +08:00
|
|
|
dest_bytes = (u64)stvfs.f_frsize * stvfs.f_bfree;
|
|
|
|
if (!dest_bytes)
|
|
|
|
dest_bytes = (u64)stvfs.f_bsize * stvfs.f_bfree;
|
2006-10-27 20:24:29 +08:00
|
|
|
|
2005-11-08 04:49:18 +08:00
|
|
|
if (dest_bytes < src_bytes)
|
2006-06-16 02:43:24 +08:00
|
|
|
err_exit("Destination doesn't have enough free space: "
|
|
|
|
"%llu MB < %llu MB\n",
|
2010-12-21 22:51:07 +08:00
|
|
|
(unsigned long long)rounded_up_division(dest_bytes, NTFS_MBYTE),
|
|
|
|
(unsigned long long)rounded_up_division(src_bytes, NTFS_MBYTE));
|
2013-02-09 19:54:44 +08:00
|
|
|
#endif
|
2005-11-08 04:49:18 +08:00
|
|
|
}
|
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ntfs_walk_clusters_ctx image;
|
2004-09-04 04:00:47 +08:00
|
|
|
s64 device_size; /* input device size in bytes */
|
|
|
|
s64 ntfs_size;
|
2005-06-12 22:35:57 +08:00
|
|
|
unsigned int wiped_total = 0;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2010-12-21 22:51:07 +08:00
|
|
|
/* make sure the layout of header is not affected by alignments */
|
|
|
|
if (offsetof(struct image_hdr, offset_to_image_data)
|
|
|
|
!= IMAGE_OFFSET_OFFSET) {
|
|
|
|
fprintf(stderr,"ntfsclone is not compiled properly. "
|
|
|
|
"Please fix\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
/* print to stderr, stdout can be an NTFS image ... */
|
2010-11-22 21:00:04 +08:00
|
|
|
fprintf(stderr, "%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION);
|
2003-06-03 19:10:57 +08:00
|
|
|
msg_out = stderr;
|
|
|
|
|
|
|
|
parse_options(argc, argv);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2005-10-27 04:48:38 +08:00
|
|
|
utils_set_locale();
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-09-04 04:00:47 +08:00
|
|
|
if (opt.restore_image) {
|
2004-07-11 04:01:17 +08:00
|
|
|
device_size = open_image();
|
2006-08-23 18:18:34 +08:00
|
|
|
ntfs_size = sle64_to_cpu(image_hdr.nr_clusters) *
|
|
|
|
le32_to_cpu(image_hdr.cluster_size);
|
2004-09-04 04:00:47 +08:00
|
|
|
} else {
|
2004-07-11 04:01:17 +08:00
|
|
|
device_size = open_volume();
|
2004-09-04 04:00:47 +08:00
|
|
|
ntfs_size = vol->nr_clusters * vol->cluster_size;
|
|
|
|
}
|
2006-11-28 18:09:56 +08:00
|
|
|
// FIXME: This needs to be the cluster size...
|
2004-09-04 04:00:47 +08:00
|
|
|
ntfs_size += 512; /* add backup boot sector */
|
2011-02-17 23:11:26 +08:00
|
|
|
full_device_size = device_size;
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-03-05 19:27:01 +08:00
|
|
|
if (opt.std_out) {
|
2005-10-27 06:54:57 +08:00
|
|
|
if ((fd_out = fileno(stdout)) == -1)
|
|
|
|
perr_exit("fileno for stdout failed");
|
2011-03-23 16:35:24 +08:00
|
|
|
stream_out = stdout;
|
2013-02-09 19:54:44 +08:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
if (setmode(fileno(stdout),O_BINARY) == -1)
|
|
|
|
perr_exit("setting binary stdout failed");
|
|
|
|
#endif
|
2003-06-03 19:10:57 +08:00
|
|
|
} else {
|
2005-10-27 06:54:57 +08:00
|
|
|
/* device_size_get() might need to read() */
|
2013-02-09 19:54:44 +08:00
|
|
|
int flags = O_RDWR | O_BINARY;
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
fd_out = 0;
|
2003-07-11 20:35:05 +08:00
|
|
|
if (!opt.blkdev_out) {
|
|
|
|
flags |= O_CREAT | O_TRUNC;
|
|
|
|
if (!opt.overwrite)
|
|
|
|
flags |= O_EXCL;
|
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.save_image || opt.metadata_image) {
|
2013-02-09 19:54:44 +08:00
|
|
|
stream_out = fopen(opt.output,BINWMODE);
|
2011-03-23 16:35:24 +08:00
|
|
|
if (!stream_out)
|
|
|
|
perr_exit("Opening file '%s' failed",
|
|
|
|
opt.output);
|
|
|
|
fd_out = fileno(stream_out);
|
2013-02-09 19:54:44 +08:00
|
|
|
} else {
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
if (!opt.no_action) {
|
|
|
|
dev_out = ntfs_device_alloc(opt.output, 0,
|
|
|
|
&ntfs_device_default_io_ops, NULL);
|
|
|
|
if (!dev_out
|
|
|
|
|| (dev_out->d_ops->open)(dev_out, flags))
|
|
|
|
perr_exit("Opening volume '%s' failed",
|
|
|
|
opt.output);
|
|
|
|
}
|
|
|
|
#else
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action
|
|
|
|
&& ((fd_out = open(opt.output, flags,
|
|
|
|
S_IRUSR | S_IWUSR)) == -1))
|
2011-03-23 16:35:24 +08:00
|
|
|
perr_exit("Opening file '%s' failed",
|
|
|
|
opt.output);
|
2013-02-09 19:54:44 +08:00
|
|
|
#endif
|
|
|
|
}
|
2004-07-11 04:01:17 +08:00
|
|
|
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.save_image && !opt.metadata_image && !opt.no_action)
|
2004-09-04 04:00:47 +08:00
|
|
|
check_output_device(ntfs_size);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (opt.restore_image) {
|
|
|
|
print_image_info();
|
|
|
|
restore_image();
|
2013-02-09 18:29:21 +08:00
|
|
|
if (!opt.no_action)
|
|
|
|
fsync_clone(fd_out);
|
2004-07-11 04:01:17 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
setup_lcn_bitmap();
|
|
|
|
memset(&image, 0, sizeof(image));
|
2005-06-20 05:09:40 +08:00
|
|
|
backup_clusters.image = ℑ
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
walk_clusters(vol, &backup_clusters);
|
2013-02-09 18:41:39 +08:00
|
|
|
image.more_use = compare_bitmaps(&lcn_bitmap,
|
|
|
|
opt.metadata && !opt.metadata_image);
|
2006-08-23 19:22:07 +08:00
|
|
|
print_disk_usage("", vol->cluster_size, vol->nr_clusters, image.inuse);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2005-11-08 04:49:18 +08:00
|
|
|
check_dest_free_space(vol->cluster_size * image.inuse);
|
|
|
|
|
2004-10-01 02:16:48 +08:00
|
|
|
ignore_bad_clusters(&image);
|
|
|
|
|
2004-07-11 04:01:17 +08:00
|
|
|
if (opt.save_image)
|
|
|
|
initialise_image_hdr(device_size, image.inuse);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if ((opt.std_out && !opt.metadata_image) || !opt.metadata) {
|
2004-10-01 02:16:48 +08:00
|
|
|
s64 nr_clusters_to_save = image.inuse;
|
|
|
|
if (opt.std_out && !opt.save_image)
|
|
|
|
nr_clusters_to_save = vol->nr_clusters;
|
2011-02-16 19:18:56 +08:00
|
|
|
nr_clusters_to_save++; /* account for the backup boot sector */
|
2004-10-01 02:16:48 +08:00
|
|
|
|
2013-02-09 18:41:39 +08:00
|
|
|
clone_ntfs(nr_clusters_to_save, image.more_use);
|
2004-01-16 01:07:57 +08:00
|
|
|
fsync_clone(fd_out);
|
2011-03-23 16:35:24 +08:00
|
|
|
if (opt.save_image)
|
|
|
|
fclose(stream_out);
|
2010-12-21 22:51:07 +08:00
|
|
|
ntfs_umount(vol,FALSE);
|
|
|
|
free(lcn_bitmap.bm);
|
2003-06-03 19:10:57 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2004-09-29 07:43:15 +08:00
|
|
|
|
2012-01-24 01:13:59 +08:00
|
|
|
wipe = 1;
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.metadata_image) {
|
|
|
|
initialise_image_hdr(device_size, image.inuse);
|
|
|
|
write_image_hdr();
|
|
|
|
} else {
|
2013-02-09 19:54:44 +08:00
|
|
|
if (dev_out) {
|
|
|
|
(dev_out->d_ops->close)(dev_out);
|
|
|
|
dev_out = NULL;
|
|
|
|
} else
|
|
|
|
fsync_clone(fd_out); /* sync copy before mounting */
|
2011-10-07 17:42:08 +08:00
|
|
|
opt.volume = opt.output;
|
2005-06-20 05:09:40 +08:00
|
|
|
/* 'force' again mount for dirty volumes (e.g. after resize).
|
2004-01-16 01:07:57 +08:00
|
|
|
FIXME: use mount flags to avoid potential side-effects in future */
|
2011-10-07 17:42:08 +08:00
|
|
|
opt.force++;
|
|
|
|
ntfs_umount(vol,FALSE);
|
2012-11-07 23:29:48 +08:00
|
|
|
mount_volume(0 /*NTFS_MNT_NOATIME*/);
|
2011-10-07 17:42:08 +08:00
|
|
|
}
|
2003-06-03 19:10:57 +08:00
|
|
|
|
2004-09-29 07:43:15 +08:00
|
|
|
free(lcn_bitmap.bm);
|
2003-06-03 19:10:57 +08:00
|
|
|
setup_lcn_bitmap();
|
|
|
|
memset(&image, 0, sizeof(image));
|
2005-06-20 05:09:40 +08:00
|
|
|
backup_clusters.image = ℑ
|
2003-06-03 19:10:57 +08:00
|
|
|
|
|
|
|
walk_clusters(vol, &backup_clusters);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2005-06-12 22:35:57 +08:00
|
|
|
Printf("Num of MFT records = %10lld\n",
|
2004-09-13 18:28:54 +08:00
|
|
|
(long long)vol->mft_na->initialized_size >>
|
2005-06-20 05:09:40 +08:00
|
|
|
vol->mft_record_size_bits);
|
|
|
|
Printf("Num of used MFT records = %10u\n", nr_used_mft_records);
|
|
|
|
|
|
|
|
Printf("Wiped unused MFT data = %10u\n", wiped_unused_mft_data);
|
|
|
|
Printf("Wiped deleted MFT data = %10u\n", wiped_unused_mft);
|
2005-06-12 22:35:57 +08:00
|
|
|
Printf("Wiped resident user data = %10u\n", wiped_resident_data);
|
|
|
|
Printf("Wiped timestamp data = %10u\n", wiped_timestamp_data);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2003-06-03 19:10:57 +08:00
|
|
|
wiped_total += wiped_unused_mft_data;
|
|
|
|
wiped_total += wiped_unused_mft;
|
|
|
|
wiped_total += wiped_resident_data;
|
|
|
|
wiped_total += wiped_timestamp_data;
|
2005-06-12 22:35:57 +08:00
|
|
|
Printf("Wiped totally = %10u\n", wiped_total);
|
2005-06-20 05:09:40 +08:00
|
|
|
|
2011-10-07 17:42:08 +08:00
|
|
|
if (opt.metadata_image)
|
|
|
|
fclose(stream_out);
|
|
|
|
else
|
|
|
|
fsync_clone(fd_out);
|
2010-12-21 22:51:07 +08:00
|
|
|
ntfs_umount(vol,FALSE);
|
|
|
|
free(lcn_bitmap.bm);
|
2010-12-21 22:51:08 +08:00
|
|
|
return (0);
|
2003-06-03 19:10:57 +08:00
|
|
|
}
|