copy_mmap_seq.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. ssize_t offset = 0;
  14. ssize_t step;
  15. while (offset < size) {
  16. if (offset + max_step <= size)
  17. step = rand() % max_step + 1;
  18. else
  19. step = size - offset;
  20. if (ftruncate(fo, offset + step) != 0)
  21. fatal_error("ftruncate(%s, %zu) failed: %s\n", output_path, offset + step, strerror(errno));
  22. memcpy(out + offset, in + offset, step);
  23. offset += step;
  24. }
  25. // unmap
  26. munmap_fd(input_path, in, size);
  27. printf("munmap_fd(%zu) input OK\n", size);
  28. munmap_fd(output_path, out, size);
  29. printf("munmap_fd(%zu) output OK\n", size);
  30. }
  31. }