qfile-utils.c 5.0 KB

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