qfile-utils.c 4.7 KB

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