File: btrfs-extent-same.c

package info (click to toggle)
duperemove 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 700 kB
  • sloc: ansic: 7,719; makefile: 92; sh: 5
file content (119 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * btrfs-extent-same.c
 *
 * Copyright (C) 2013 SUSE.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License version 2 as published by the Free Software Foundation.
 *
 * 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.
 *
 * Authors: Mark Fasheh <mfasheh@suse.de>
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "ioctl.h"

unsigned int blocksize;

static void usage(const char *prog)
{
	printf("Usage: %s len file1 loff1 file2 loff2\n", prog);
}

int main(int argc, char **argv)
{
	int ret, src_fd, i, numfiles;
	char *srcf, *destf;
	struct file_dedupe_range *same;
	struct file_dedupe_range_info *info;
	unsigned long long bytes = 0ULL;

	if (argc < 6 || (argc % 2)) {
		usage(argv[0]);
		return 1;
	}

	numfiles = (argc / 2) - 2;

	printf("Deduping %d total files\n", numfiles + 1);

	same = calloc(1,
		      sizeof(struct file_dedupe_range) +
		      sizeof(struct file_dedupe_range_info) * numfiles);
	if (!same)
		return -ENOMEM;

	srcf = argv[2];
	same->src_length = atoll(argv[1]);
	same->src_offset = atoll(argv[3]);
	same->dest_count = numfiles;

	ret = open(srcf, O_RDONLY);
	if (ret < 0) {
		ret = errno;
		fprintf(stderr, "Could not open file %s: (%d) %s\n", srcf, ret,
			strerror(ret));
		return -ret;
	}
	src_fd = ret;

	printf("(%llu, %llu): %s\n", (unsigned long long)same->src_offset,
	       (unsigned long long)same->src_length, srcf);

	for (i = 0; i < same->dest_count; i++) {
		destf = argv[4 + (i * 2)];

		ret = open(destf, O_WRONLY);
		if (ret < 0) {
			ret = errno;
			fprintf(stderr, "Could not open file %s: (%d) %s\n",
				destf, ret, strerror(ret));
			return -ret;
		}

		same->info[i].dest_fd = ret;
		same->info[i].dest_offset = atoll(argv[5 + (i * 2)]);
		printf("(%llu, %llu): %s\n",
		       (unsigned long long)same->info[i].dest_offset,
		       (unsigned long long)same->src_length, destf);

	}

	ret = ioctl(src_fd, FIDEDUPERANGE, same);
	if (ret < 0) {
		ret = errno;
		fprintf(stderr, "fideduprange returned error: (%d) %s\n", ret,
			strerror(ret));
		return -ret;
	}

	printf("%u files asked to be deduped\n", same->dest_count);

	for (i = 0; i < same->dest_count; i++) {
		info = &same->info[i];

		printf("i: %d, status: %d, bytes_deduped: %llu\n", i,
		       info->status, (unsigned long long)info->bytes_deduped);

		bytes += info->bytes_deduped;
	}

	printf("%llu total bytes deduped in this operation\n", bytes);

	return ret;
}