From b3f1f71b4278f9bb96d20e3e333680ffb3d27902 Mon Sep 17 00:00:00 2001
From: Peter Hjalmarsson <kanelxake@gmail.com>
Date: Mon, 21 Sep 2015 17:27:20 +0200
Subject: [PATCH] Uglybtrfsclone
---
lib/vfs/vfs.c | 23 +++++++++++++++++++++++
lib/vfs/vfs.h | 2 ++
src/filemanager/file.c | 9 +++++++++
3 files changed, 34 insertions(+)
diff --git a/lib/vfs/vfs.c b/lib/vfs/vfs.c
index 01278e9..2df6d4a 100644
a
|
b
|
|
45 | 45 | #include <errno.h> |
46 | 46 | #include <stdlib.h> |
47 | 47 | |
| 48 | #ifdef HAVE_SYS_IOCTL_H |
| 49 | #include <sys/ioctl.h> |
| 50 | #endif |
| 51 | |
48 | 52 | #include "lib/global.h" |
49 | 53 | #include "lib/strutil.h" |
50 | 54 | #include "lib/util.h" |
… |
… |
vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize) |
644 | 648 | #endif /* HAVE_POSIX_FALLOCATE */ |
645 | 649 | } |
646 | 650 | |
| 651 | int |
| 652 | vfs_clone_file (int dest_vfs_fd, int src_vfs_fd) |
| 653 | { |
| 654 | #ifdef __linux__ |
| 655 | # undef BTRFS_IOCTL_MAGIC |
| 656 | # define BTRFS_IOCTL_MAGIC 0x94 |
| 657 | # undef BTRFS_IOC_CLONE |
| 658 | # define BTRFS_IOC_CLONE _IOW (BTRFS_IOCTL_MAGIC, 9, int) |
| 659 | int *dest_fd = (int *) vfs_class_data_find_by_handle (dest_vfs_fd); |
| 660 | int *src_fd = (int *) vfs_class_data_find_by_handle (src_vfs_fd); |
| 661 | return ioctl (*dest_fd, BTRFS_IOC_CLONE, *src_fd); |
| 662 | #else |
| 663 | (void) dest_vfs_fd; |
| 664 | (void) src_vfs_fd; |
| 665 | errno = ENOTSUP; |
| 666 | return -1; |
| 667 | #endif |
| 668 | } |
| 669 | |
647 | 670 | /* --------------------------------------------------------------------------------------------- */ |
diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h
index 037f889..a1b3335 100644
a
|
b
|
char *_vfs_get_cwd (void); |
273 | 273 | |
274 | 274 | int vfs_preallocate (int dest_desc, off_t src_fsize, off_t dest_fsize); |
275 | 275 | |
| 276 | int vfs_clone_file (int dest_vfs_fd, int src_vfs_fd); |
| 277 | |
276 | 278 | /** |
277 | 279 | * Interface functions described in interface.c |
278 | 280 | */ |
diff --git a/src/filemanager/file.c b/src/filemanager/file.c
index 0bacc54..e6aebd2 100644
a
|
b
|
copy_file_file (file_op_total_context_t * tctx, file_op_context_t * ctx, |
1737 | 1737 | goto ret; |
1738 | 1738 | } |
1739 | 1739 | |
| 1740 | /*here*/ |
| 1741 | if (vfs_clone_file(dest_desc,src_desc) == 0) |
| 1742 | { |
| 1743 | dst_status = DEST_FULL; |
| 1744 | goto ret; |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | |
1740 | 1749 | /* try preallocate space; if fail, try copy anyway */ |
1741 | 1750 | while (vfs_preallocate (dest_desc, file_size, ctx->do_append != 0 ? sb.st_size : 0) != 0) |
1742 | 1751 | { |