summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorTheodore Ts'o <tytso@mit.edu>2005-01-21 01:20:44 -0500
committerTheodore Ts'o <tytso@mit.edu>2005-01-21 01:20:44 -0500
commit0d2993dbab9f8365c85d5f476f28ffd44ca99896 (patch)
treeb70d9c0e66eb7466c080ca55f8c5b819f12eb276 /util
parent89c801561d5f0497f515ff2f28f3c0cd608388de (diff)
downloade2fsprogs-0d2993dbab9f8365c85d5f476f28ffd44ca99896.tar.gz
Add new utility program, copy_sparse.c, which is very useful
for dealing with large sparse files (such as e2image files).
Diffstat (limited to 'util')
-rw-r--r--util/ChangeLog5
-rw-r--r--util/Makefile.in7
-rw-r--r--util/copy_sparse.c228
3 files changed, 239 insertions, 1 deletions
diff --git a/util/ChangeLog b/util/ChangeLog
index 6199f1d7..1ede73b2 100644
--- a/util/ChangeLog
+++ b/util/ChangeLog
@@ -1,3 +1,8 @@
+2005-01-21 <tytso@snap.thunk.org>
+
+ * copy_sparse.c: Utility program which is very useful for dealing
+ with large sparse files (such as e2image files).
+
2005-01-07 Theodore Ts'o <tytso@mit.edu>
* gen-tarball.in: Remove the hyphen in 1.36-rc1, since Debian
diff --git a/util/Makefile.in b/util/Makefile.in
index 634cd753..8ec00c90 100644
--- a/util/Makefile.in
+++ b/util/Makefile.in
@@ -25,6 +25,10 @@ subst: subst.o
@echo " LD $@"
@$(BUILD_CC) $(ALL_LDFLAGS) -o subst subst.o
+copy_sparse: copy_sparse.o
+ @echo " LD $@"
+ @$(BUILD_CC) $(ALL_LDFLAGS) -o copy_sparse copy_sparse.o
+
gen-tarball: $(srcdir)/gen-tarball.in $(top_builddir)/config.status
@echo " CONFIG.STATUS $@"
@cd $(top_builddir); CONFIG_FILES=util/gen-tarball ./config.status
@@ -36,7 +40,8 @@ tarballs: gen-tarball
sh gen-tarball subset
clean:
- $(RM) -f $(PROGS) \#* *.s *.o *.a *~ core *.tar.gz gen-tarball
+ $(RM) -f $(PROGS) \#* *.s *.o *.a *~ core *.tar.gz gen-tarball \
+ copy-sparse
mostlyclean: clean
diff --git a/util/copy_sparse.c b/util/copy_sparse.c
new file mode 100644
index 00000000..aece6be2
--- /dev/null
+++ b/util/copy_sparse.c
@@ -0,0 +1,228 @@
+/*
+ * 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