copy_mmap_whole.c 855 B

123456789101112131415161718192021
  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. // map whole input/output file
  5. void* in = mmap_fd(input_path, fi, PROT_READ, 0, size);
  6. printf("mmap_fd(%zu) input OK\n", size);
  7. void* out = mmap_fd(output_path, fo, PROT_WRITE, 0, size);
  8. printf("mmap_fd(%zu) output OK\n", size);
  9. // copy data
  10. if (ftruncate(fo, size) != 0)
  11. fatal_error("ftruncate(%s, %zu) failed: %s\n", output_path, size, strerror(errno));
  12. printf("ftruncate(%zu) output OK\n", size);
  13. memcpy(out, in, size);
  14. // unmap
  15. munmap_fd(input_path, in, size);
  16. printf("munmap_fd(%zu) input OK\n", size);
  17. munmap_fd(output_path, out, size);
  18. printf("munmap_fd(%zu) output OK\n", size);
  19. }
  20. }