copy_mmap_rev.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "common.h"
  2. void copy_data(int fi, int fo, const char* input_path, const char* output_path, size_t size) {
  3. if (size > 0) {
  4. size_t max_step = 16;
  5. if (size > 65536)
  6. max_step = 256;
  7. // map whole input/output file
  8. void* in = mmap_fd(input_path, fi, PROT_READ, 0, size);
  9. printf("mmap_fd(%zu) input OK\n", size);
  10. void* out = mmap_fd(output_path, fo, PROT_WRITE, 0, size);
  11. printf("mmap_fd(%zu) output OK\n", size);
  12. // copy data
  13. if (ftruncate(fo, size) != 0)
  14. fatal_error("ftruncate(%s, %zu) failed: %s\n", output_path, size, strerror(errno));
  15. printf("ftruncate(%zu) output OK\n", size);
  16. ssize_t offset = size;
  17. ssize_t step;
  18. while (offset > 0) {
  19. if (offset > max_step)
  20. step = rand() % max_step + 1;
  21. else
  22. step = offset;
  23. offset -= step;
  24. memcpy(out + offset, in + offset, step);
  25. }
  26. // unmap
  27. munmap_fd(input_path, in, size);
  28. printf("munmap_fd(%zu) input OK\n", size);
  29. munmap_fd(output_path, out, size);
  30. printf("munmap_fd(%zu) output OK\n", size);
  31. }
  32. }