/**
* ntfscp - Part of the Linux-NTFS project.
*
* Copyright (c) 2004-2007 Yura Pakhuchiy
* Copyright (c) 2005 Anton Altaparmakov
* Copyright (c) 2006 Hil Liao
*
* This utility will copy file to an NTFS volume.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (in the main directory of the Linux-NTFS
* distribution in the file COPYING); if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <signal.h>
#ifdef HAVE_SYS_STAT_H
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_LIBGEN_H
#include <libgen.h>
#endif
#include "compat.h"
#include "types.h"
#include "attrib.h"
#include "utils.h"
#include "volume.h"
#include "dir.h"
#include "debug.h"
#include "version.h"
#include "logging.h"
struct options {
};
/**
* version - Print version information about the program
*
* Print a copyright statement and a brief description of the program.
*
* Return: none
*/
static void version(void)
{
ntfs_log_info("\n%s v%s (libntfs %s) - Copy file to an NTFS "
ntfs_log_info("Copyright (c) 2004-2007 Yura Pakhuchiy\n");
ntfs_log_info("Copyright (c) 2005 Anton Altaparmakov\n");
ntfs_log_info("Copyright (c) 2006 Hil Liao\n");
}
/**
* usage - Print a list of the parameters to the program
*
* Print a list of the parameters and options for the program.
*
* Return: none
*/
static void usage(void)
{
ntfs_log_info("\nUsage: %s [options] device src_file dest_file\n\n"
" -a, --attribute NUM Write to this attribute\n"
" -i, --inode Treat dest_file as inode number\n"
" -f, --force Use less caution\n"
" -h, --help Print this help\n"
" -N, --attr-name NAME Write to attribute with this name\n"
" -n, --no-action Do not write to disk\n"
" -q, --quiet Less output\n"
" -V, --version Version information\n"
" -v, --verbose More output\n\n",
}
/**
* parse_options - Read and validate the programs command line
*
* Read the command line, verify the syntax and parse the options.
* This function is very long, but quite simple.
*
* Return: 1 Success
* 0 Error, one or more problems
*/
{
};
char *s;
int c = -1;
int err = 0;
int ver = 0;
int help = 0;
int levels = 0;
opterr = 0; /* We'll handle the errors, thank you. */
switch (c) {
case 1: /* A non-option argument */
} else {
ntfs_log_error("You must specify exactly two "
"files.\n");
err++;
}
break;
case 'a':
ntfs_log_error("You can specify only one "
"attribute.\n");
err++;
break;
}
if (*s) {
ntfs_log_error("Couldn't parse attribute.\n");
err++;
} else
break;
case 'i':
break;
case 'f':
break;
case 'h':
case '?':
err++;
break;
}
help++;
break;
case 'N':
ntfs_log_error("You can specify only one "
"attribute name.\n");
err++;
} else
break;
case 'n':
break;
case 'q':
break;
case 'V':
ver++;
break;
case 'v':
break;
default:
ntfs_log_error("Unknown option '%s'.\n",
err++;
break;
}
}
/* Make sure we're in sync with the log levels */
if (levels & NTFS_LOG_LEVEL_VERBOSE)
if (!(levels & NTFS_LOG_LEVEL_QUIET))
} else {
ntfs_log_error("You must specify a device.\n");
err++;
ntfs_log_error("You must specify a source file.\n");
err++;
ntfs_log_error("You must specify a destination "
"file.\n");
err++;
}
ntfs_log_error("You may not use --quiet and --verbose "
"at the same time.\n");
err++;
}
}
if (ver)
version();
usage();
}
/**
* signal_handler - Handle SIGINT and SIGTERM: abort write, sync and exit.
*/
{
}
/**
* Create a regular file under the given directory inode
*
* It is a wrapper function to ntfs_create(...)
*
* Return: the created file inode
*/
const char *filename)
{
/* inode to the file that is being created */
int ufilename_len;
/* ntfs_mbstoucs(...) will allocate memory for ufilename if it's NULL */
if (ufilename_len == -1) {
ntfs_log_perror("ERROR: Failed to convert '%s' to unicode",
filename);
return NULL;
}
return ni;
}
/**
* main - Begin here
*
* Start from here.
*
* Return: 0 Success, the program worked
* 1 Error, something went wrong
*/
{
int flags = 0;
char *buf;
int attr_name_len = 0;
return 1;
/* Set SIGINT handler. */
ntfs_log_perror("Failed to set SIGINT handler");
return 1;
}
/* Set SIGTERM handler. */
ntfs_log_perror("Failed to set SIGTERM handler");
return 1;
}
flags |= NTFS_MNT_FORCE;
if (!vol) {
ntfs_log_perror("ERROR: couldn't mount volume");
return 1;
}
goto umount;
{
ntfs_log_perror("ERROR: Couldn't stat source file");
goto umount;
}
}
if (!in) {
ntfs_log_perror("ERROR: Couldn't open source file");
goto umount;
}
char *s;
if (*s) {
ntfs_log_error("ERROR: Couldn't parse inode number.\n");
goto close_src;
}
} else
if (!out) {
/* Copy the file if the dest_file's parent dir can be opened. */
char *parent_dirname;
char *filename;
int dest_path_len;
char *dirname_last_whack;
if (!parent_dirname) {
ntfs_log_perror("strdup() failed");
goto close_src;
}
if (dirname_last_whack) {
dirname_last_whack[1] = 0;
} else {
ntfs_log_verbose("Target path does not contain '/'. "
"Using root directory as parent.\n");
}
if (dir_ni) {
/* Remove the last '/' for estetic reasons. */
dirname_last_whack[0] = 0;
ntfs_log_error("The file '%s' already exists "
"and is not a directory. "
"Aborting.\n", parent_dirname);
goto close_src;
}
ntfs_log_verbose("Creating a new file '%s' under '%s'"
if (!ni) {
ntfs_log_perror("Failed to create '%s' under "
"'%s'", filename,
goto close_src;
}
} else {
ntfs_log_perror("ERROR: Couldn't open '%s'",
goto close_src;
}
}
/* The destination is a directory. */
char *filename;
char *overwrite_filename;
int filename_len;
int dest_dirname_len;
if (!overwrite_filename) {
ntfs_log_perror("ERROR: Failed to allocate %i bytes "
"memory for the overwrite filename",
goto close_src;
}
}
/* Does a file with the same name exist in the dest dir? */
if (ni) {
ntfs_log_verbose("Destination path has a file with "
"the same name\nOverwriting the file "
"'%s'\n", overwrite_filename);
} else {
ntfs_log_verbose("Creating a new file '%s' under "
if (!ni) {
ntfs_log_perror("ERROR: Failed to create the "
"destination file under '%s'",
goto close_src;
}
}
}
if (!attr_name) {
ntfs_log_perror("ERROR: Failed to parse attribute name '%s'",
goto close_dst;
}
if (!na) {
ntfs_log_perror("ERROR: Couldn't open attribute");
goto close_dst;
}
/* Requested attribute isn't present, add it. */
attr_name_len, NULL, 0)) {
ntfs_log_perror("ERROR: Couldn't add attribute");
goto close_dst;
}
if (!na) {
ntfs_log_perror("ERROR: Couldn't open just added "
"attribute");
goto close_dst;
}
}
ntfs_log_perror("ERROR: Couldn't resize attribute");
goto close_attr;
}
}
if (!buf) {
ntfs_log_perror("ERROR: malloc failed");
goto close_attr;
}
ntfs_log_verbose("Starting write.\n");
offset = 0;
if (caught_terminate) {
ntfs_log_error("SIGTERM or SIGINT received. "
"Aborting write.\n");
break;
}
if (!br) {
break;
}
ntfs_log_perror("ERROR: ntfs_attr_pwrite failed");
break;
}
}
ntfs_log_verbose("Syncing.\n");
result = 0;
while (ntfs_inode_close(out)) {
ntfs_log_error("Sync failed. Run chkdsk.\n");
break;
}
ntfs_log_error("Device busy. Will retry sync in 3 seconds.\n");
sleep(3);
}
ntfs_log_verbose("Done.\n");
return result;
}