qfile-utils.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <qfile-utils.h>
  2. unsigned long crc32_sum;
  3. int ignore_symlinks = 0;
  4. int ignore_quota_error = 0;
  5. void notify_progress(int size, int flag)
  6. {
  7. static long long total = 0;
  8. static long long prev_total = 0;
  9. total += size;
  10. if (total > prev_total + PROGRESS_NOTIFY_DELTA
  11. || (flag != PROGRESS_FLAG_NORMAL)) {
  12. // check for possible error from qfile-unpacker; if error occured,
  13. // exit() will be called, so don't bother with current state
  14. // (notify_progress can be called as callback from copy_file())
  15. if (flag == PROGRESS_FLAG_NORMAL)
  16. wait_for_result();
  17. do_notify_progress(total, flag);
  18. prev_total = total;
  19. }
  20. }
  21. void do_notify_progress(long long total, int flag)
  22. {
  23. const char *du_size_env = getenv("FILECOPY_TOTAL_SIZE");
  24. const char *progress_type_env = getenv("PROGRESS_TYPE");
  25. const char *saved_stdout_env = getenv("SAVED_FD_1");
  26. if (!progress_type_env)
  27. return;
  28. if (!strcmp(progress_type_env, "console") && du_size_env) {
  29. char msg[256];
  30. snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r",
  31. total / 1024, strtoull(du_size_env, NULL, 0));
  32. write(2, msg, strlen(msg));
  33. if (flag == PROGRESS_FLAG_DONE)
  34. write(2, "\n", 1);
  35. }
  36. if (!strcmp(progress_type_env, "gui") && saved_stdout_env) {
  37. char msg[256];
  38. snprintf(msg, sizeof(msg), "%lld\n", total);
  39. write(strtoul(saved_stdout_env, NULL, 0), msg,
  40. strlen(msg));
  41. }
  42. }
  43. void notify_end_and_wait_for_result(void)
  44. {
  45. struct file_header end_hdr;
  46. /* nofity end of transfer */
  47. memset(&end_hdr, 0, sizeof(end_hdr));
  48. end_hdr.namelen = 0;
  49. end_hdr.filelen = 0;
  50. write_all_with_crc(1, &end_hdr, sizeof(end_hdr));
  51. set_block(0);
  52. wait_for_result();
  53. }
  54. int write_all_with_crc(int fd, const void *buf, int size)
  55. {
  56. crc32_sum = Crc32_ComputeBuf(crc32_sum, buf, size);
  57. return write_all(fd, buf, size);
  58. }
  59. void wait_for_result(void)
  60. {
  61. struct result_header hdr;
  62. struct result_header_ext hdr_ext;
  63. char last_filename[MAX_PATH_LENGTH + 1];
  64. char last_filename_prefix[] = "; Last file: ";
  65. if (!read_all(0, &hdr, sizeof(hdr))) {
  66. if (errno == EAGAIN) {
  67. // no result sent and stdin still open
  68. return;
  69. } else {
  70. // other read error or EOF
  71. exit(1); // hopefully remote has produced error message
  72. }
  73. }
  74. if (!read_all(0, &hdr_ext, sizeof(hdr_ext))) {
  75. // remote used old result_header struct
  76. hdr_ext.last_namelen = 0;
  77. }
  78. if (hdr_ext.last_namelen > MAX_PATH_LENGTH) {
  79. // read only at most MAX_PATH_LENGTH chars
  80. hdr_ext.last_namelen = MAX_PATH_LENGTH;
  81. }
  82. if (!read_all(0, last_filename, hdr_ext.last_namelen)) {
  83. fprintf(stderr, "Failed to get last filename\n");
  84. hdr_ext.last_namelen = 0;
  85. }
  86. last_filename[hdr_ext.last_namelen] = '\0';
  87. if (!hdr_ext.last_namelen)
  88. /* set prefix to empty string */
  89. last_filename_prefix[0] = '\0';
  90. errno = hdr.error_code;
  91. if (hdr.error_code != 0) {
  92. switch (hdr.error_code) {
  93. case EEXIST:
  94. gui_fatal("File copy: not overwriting existing file. Clean QubesIncoming dir, and retry copy%s%s", last_filename_prefix, last_filename);
  95. break;
  96. case EINVAL:
  97. gui_fatal("File copy: Corrupted data from packer%s%s", last_filename_prefix, last_filename);
  98. break;
  99. case EDQUOT:
  100. if (ignore_quota_error) {
  101. /* skip also CRC check as sender and receiver might be
  102. * desynchronized in this case */
  103. return;
  104. }
  105. /* fall though */
  106. default:
  107. gui_fatal("File copy: %s%s%s",
  108. strerror(hdr.error_code), last_filename_prefix, last_filename);
  109. }
  110. }
  111. if (hdr.crc32 != crc32_sum) {
  112. gui_fatal("File transfer failed: checksum mismatch");
  113. }
  114. }
  115. void write_headers(const struct file_header *hdr, const char *filename)
  116. {
  117. if (!write_all_with_crc(1, hdr, sizeof(*hdr))
  118. || !write_all_with_crc(1, filename, hdr->namelen)) {
  119. set_block(0);
  120. wait_for_result();
  121. exit(1);
  122. }
  123. }
  124. int single_file_processor(const char *filename, const struct stat *st)
  125. {
  126. struct file_header hdr;
  127. int fd;
  128. mode_t mode = st->st_mode;
  129. hdr.namelen = strlen(filename) + 1;
  130. hdr.mode = mode;
  131. hdr.atime = st->st_atim.tv_sec;
  132. hdr.atime_nsec = st->st_atim.tv_nsec;
  133. hdr.mtime = st->st_mtim.tv_sec;
  134. hdr.mtime_nsec = st->st_mtim.tv_nsec;
  135. if (S_ISREG(mode)) {
  136. int ret;
  137. fd = open(filename, O_RDONLY);
  138. if (fd < 0)
  139. gui_fatal("open %s", filename);
  140. hdr.filelen = st->st_size;
  141. write_headers(&hdr, filename);
  142. ret = copy_file(1, fd, hdr.filelen, &crc32_sum);
  143. if (ret != COPY_FILE_OK) {
  144. if (ret != COPY_FILE_WRITE_ERROR)
  145. gui_fatal("Copying file %s: %s", filename,
  146. copy_file_status_to_str(ret));
  147. else {
  148. set_block(0);
  149. wait_for_result();
  150. exit(1);
  151. }
  152. }
  153. close(fd);
  154. }
  155. if (S_ISDIR(mode)) {
  156. hdr.filelen = 0;
  157. write_headers(&hdr, filename);
  158. }
  159. if (S_ISLNK(mode) && !ignore_symlinks) {
  160. char name[st->st_size + 1];
  161. if (readlink(filename, name, sizeof(name)) != st->st_size)
  162. gui_fatal("readlink %s", filename);
  163. hdr.filelen = st->st_size;
  164. write_headers(&hdr, filename);
  165. if (!write_all_with_crc(1, name, st->st_size)) {
  166. set_block(0);
  167. wait_for_result();
  168. exit(1);
  169. }
  170. }
  171. // check for possible error from qfile-unpacker
  172. wait_for_result();
  173. return 0;
  174. }