-2

I'm using brtfs, and I'd like my Java program to copy a set of large files copy-on-write.

In other words, what's the equivalent of cp --reflink=auto in some library that hopefully exists and somebody has heard of, so they can tell me? :-)

Johannes Ernst
  • 3,072
  • 3
  • 42
  • 56

1 Answers1

0

There isn't a Java-specific API to my knowledge, because this is a rather specific, OS-dependent, and filesystem-dependent feature. However, with the help of a library that can issue ioctls (e.g. this one which I have no affiliation with and found by googling), you can issue the ficlonerange ioctl.

To invoke it you'll need to put together a struct:

           struct file_clone_range {
               __s64 src_fd;
               __u64 src_offset;
               __u64 src_length;
               __u64 dest_offset;
           };

It's a bit roundabout in Java, but as an example, you should be able to do this as follows using the linked library:

  1. allocate a direct buffer,
  2. populate its parameters (being careful to properly deal with machine endianness); you'll need to open FDs as well.
  3. get a pointer with Native.getDirectBufferPointer
  4. Invoke the ioctl with this.
  5. error-check the result and take appropriate action if the ioctl fails or is not supported.

If that seems too brittle, consider writing a C or C++ library that calls the ioctl and has a more convenient API, and then call into it via JNI.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Right. I had been hoping this code exists somewhere and I don't have to write it. – Johannes Ernst Dec 07 '22 at 04:23
  • @JohannesErnst Somehow missed this comment at first, so my apologies for the delay in responding - I don't have any knowledge of a prewritten library for specifically this. Either you're going to have to write it, or you'd have to find a library (and library recommendations are off-topic for Stack Overflow). Short of me creating, publishing, and maintaining a library for an ioctl that doesn't even exist on my non-BTRFS machine, the best I can do is to tell you the steps to take, which I've done to the best of my ability. I'm not sure how else I can help here as far as addressing that comment. – nanofarad Dec 15 '22 at 23:03