summaryrefslogtreecommitdiff
path: root/src/samizdat-ficlonerange.c
blob: a620cbfe27b170f81bed8ec154f86a705aec0e50 (plain)
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
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
  if (argc != 6)
    {
      fputs("Error: usage: wrong number of arguments\n", stderr);
      return -1;
    }
  char *src_name = argv[1];
  char *dest_name = argv[2];
  u_int64_t src_offset = atoll(argv[3]);
  u_int64_t src_length = atoll(argv[4]);
  u_int64_t dest_offset = atoll(argv[5]);

  int src, dest;
  if ((src = open(src_name, O_RDONLY)) < 0)
    {
      perror("Error opening input file");
      return 1;
    }
  if ((dest = open(dest_name, O_RDWR | O_CREAT)) < 0)
    {
      perror("Error opening output file");
      return 2;
    }
  if (dest_offset == 0)
    {
      struct stat buf;
      stat(dest_name, &buf);
      dest_offset = buf.st_size;
    }

  struct file_clone_range s = {src, src_offset, src_length, dest_offset};
  if (ioctl(dest, FICLONERANGE, &s))
    {
      perror("ioctl FICLONERANGE failed");
      return 3;
    }
  else
    {
      return 0;
    }
}