/*
 * copy_sparse.c -- copy a very large sparse files efficiently
 * 	(requires root privileges)
 *
 * Copyright 2003, 2004 by Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#ifndef __linux__
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    fputs("This program is only supported on Linux!\n", stderr);
    exit(EXIT_FAILURE);
}
#else
#define _LARGEFILE64_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern char *optarg;
extern int optind;
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/ioctl.h>
#include <linux/fd.h>

int verbose = 0;

#define FIBMAP	   _IO(0x00,1)	/* bmap access */
#define FIGETBSZ   _IO(0x00,2)	/* get the block size used for bmap */

static unsigned long get_bmap(int fd, unsigned long block)
{
	int	ret;
	unsigned long b;

	b = block;
	ret = ioctl(fd, FIBMAP, &b);
	if (ret < 0) {
		if (errno == EPERM) {
			fprintf(stderr, "No permission to use FIBMAP ioctl; must have root privileges\n");
			exit(1);
		}
		perror("FIBMAP");
	}
	return b;
}

static int full_read(int fd, char *buf, size_t count)
{
	int got, total = 0;
	int pass = 0;

	while (count > 0) {
		got = read(fd, buf, count);
		if (got == -1) {
			if ((errno == EINTR) || (errno == EAGAIN))
				continue;
			return total ? total : -1;
		}
		if (got == 0) {
			if (pass++ >= 3)
				return total;
			continue;
		}
		pass = 0;
		buf += got;
		total += got;
		count -= got;
	}
	return total;
}

static void copy_sparse_file(const char *src, const char *dest)
{
	struct stat64	fileinfo;
	long		lb, i, fd, ofd, bs, block, numblocks;
	ssize_t		got, got2;
	off64_t		offset = 0, should_be;
	char		*buf;

	if (verbose)
		printf("Copying sparse file from %s to %s\n", src, dest);

	if (strcmp(src, "-")) {
		if (stat64(src, &fileinfo) < 0) {
			perror("stat");
			exit(1);
		}
		if (!S_ISREG(fileinfo.st_mode)) {
			printf("%s: Not a regular file\n", src);
			exit(1);
		}
		fd = open(src, O_RDONLY | O_LARGEFILE);
		if (fd < 0) {
			perror("open");
			exit(1);
		}
		if (ioctl(fd, FIGETBSZ, &bs) < 0) {
			perror("FIGETBSZ");
			close(fd);
			exit(1);
		}
		if (bs < 0) {
			printf("%s: Invalid block size: %ld\n", src, bs);
			exit(1);
		}
		if (verbose)
			printf("Blocksize of file %s is %ld\n", src, bs);
		numblocks = (fileinfo.st_size + (bs-1)) / bs;
		if (verbose)
			printf("File size of %s is %lld (%ld blocks)\n", src,
			       (long long) fileinfo.st_size, numblocks);
	} else {
		fd = 0;
		bs = 1024;
	}

	ofd = open(dest, O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0777);
	if (ofd < 0) {
		perror(dest);
		exit(1);
	}

	buf = malloc(bs);
	if (!buf) {
		fprintf(stderr, "Couldn't allocate buffer");
		exit(1);
	}

	for (lb = 0; !fd || lb < numblocks; lb++) {
		if (fd) {
			block = get_bmap(fd, lb);
			if (!block)
				continue;
			should_be = ((off64_t) lb) * bs;
			if (offset != should_be) {
				if (verbose)
					printf("Seeking to %lld\n", should_be);
				if (lseek64(fd, should_be, SEEK_SET) == (off_t) -1) {
					perror("lseek src");
					exit(1);
				}
				if (lseek64(ofd, should_be, SEEK_SET) == (off_t) -1) {
					perror("lseek dest");
					exit(1);
				}
				offset = should_be;
			}
		}
		got = full_read(fd, buf, bs);

		if (fd == 0 && got == 0)
			break;

		if (got == bs) {
			for (i=0; i < bs; i++)
				if (buf[i])
					break;
			if (i == bs) {
				lseek(ofd, bs, SEEK_CUR);
				offset += bs;
				continue;
			}
		}
		got2 = write(ofd, buf, got);
		if (got != got2) {
			printf("short write\n");
			exit(1);
		}
		offset += got;
	}
	offset = fileinfo.st_size;
	if (fstat64(ofd, &fileinfo) < 0) {
		perror("fstat");
		exit(1);
	}
	if (fileinfo.st_size != offset) {
		lseek64(ofd, offset-1, SEEK_CUR);
		buf[0] = 0;
		write(ofd, buf, 1);
	}
	close(fd);
	close(ofd);
}

static void usage(const char *progname)
{
	fprintf(stderr, "Usage: %s [-v] source_file destination_file\n", progname);
	exit(1);
}

int main(int argc, char**argv)
{
	int c;

	while ((c = getopt(argc, argv, "v")) != EOF)
		switch (c) {
		case 'v':
			verbose++;
			break;
		default:
			usage(argv[0]);
			break;
		}
	if (optind+2 != argc)
		usage(argv[0]);
	copy_sparse_file(argv[optind], argv[optind+1]);

	return 0;
}
#endif