0

I'm trying to copy a sparse file in C++. Unfortunately, std::experimental::copy_file ignores the sparsity (at least in GCC 7.5.0) and turns my 16M file into a 52G copy.

What's the easiest way to copy a sparse file while preserving the sparsity?

Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98
  • Sparsity can be reached only by using seek call when you read zero. Look at the discussion [Copying Sparse Files](https://cboard.cprogramming.com/c-programming/164333-copying-sparse-files.html) – 273K May 03 '20 at 20:45
  • Also useful reading [std::filesystem::resize_file](https://en.cppreference.com/w/cpp/filesystem/resize_file) – 273K May 03 '20 at 20:47
  • 1
    Yeah I know how to read and write sparse files, I was just hoping there was some easy way to do it without doing all the seek_data/seek_hole/copy stuff myself. For now I've chosen the super-hacky way of calling `/bin/cp --sparse=always` with `system()` – Thomas Johnson May 03 '20 at 20:50
  • I don't think there's anything in the standard libraries. Would you consider a third-party library? – Nate Eldredge May 03 '20 at 22:23
  • Does this answer your question? [Copy sparse files](https://stackoverflow.com/questions/12299010/copy-sparse-files) – Robert Andrzejuk May 03 '20 at 23:20
  • @NateEldredge Thid-party seems fine. Given that the standard refers to sparse files in std::filesystem::resize_file, one would think that it would make some mention of it in std::filesystem::copy_file but apparently not – Thomas Johnson May 04 '20 at 15:46

0 Answers0