copy_file.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <unistd.h>
  2. #include <ioall.h>
  3. #include "filecopy.h"
  4. #include "crc32.h"
  5. extern void notify_progress(int, int);
  6. int copy_file(int outfd, int infd, long long size, unsigned long *crc32)
  7. {
  8. char buf[4096];
  9. long long written = 0;
  10. int ret;
  11. int count;
  12. while (written < size) {
  13. if (size - written > sizeof(buf))
  14. count = sizeof buf;
  15. else
  16. count = size - written;
  17. ret = read(infd, buf, count);
  18. if (!ret)
  19. return COPY_FILE_READ_EOF;
  20. if (ret < 0)
  21. return COPY_FILE_READ_ERROR;
  22. /* acumulate crc32 if requested */
  23. if (crc32)
  24. *crc32 = Crc32_ComputeBuf(*crc32, buf, ret);
  25. if (!write_all(outfd, buf, ret))
  26. return COPY_FILE_WRITE_ERROR;
  27. notify_progress(ret, 0);
  28. written += ret;
  29. }
  30. return COPY_FILE_OK;
  31. }
  32. char * copy_file_status_to_str(int status)
  33. {
  34. switch (status) {
  35. case COPY_FILE_OK: return "OK";
  36. case COPY_FILE_READ_EOF: return "Unexpected end of data while reading";
  37. case COPY_FILE_READ_ERROR: return "Error reading";
  38. case COPY_FILE_WRITE_ERROR: return "Error writing";
  39. default: return "????????";
  40. }
  41. }