qfile-agent.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #define _GNU_SOURCE
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <signal.h>
  7. #include <fcntl.h>
  8. #include <malloc.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <gui-fatal.h>
  13. #include <libqubes-rpc-filecopy.h>
  14. enum {
  15. PROGRESS_FLAG_NORMAL,
  16. PROGRESS_FLAG_INIT,
  17. PROGRESS_FLAG_DONE
  18. };
  19. unsigned long crc32_sum;
  20. int write_all_with_crc(int fd, void *buf, int size)
  21. {
  22. crc32_sum = Crc32_ComputeBuf(crc32_sum, buf, size);
  23. return write_all(fd, buf, size);
  24. }
  25. void do_notify_progress(long long total, int flag)
  26. {
  27. char *du_size_env = getenv("FILECOPY_TOTAL_SIZE");
  28. char *progress_type_env = getenv("PROGRESS_TYPE");
  29. char *saved_stdout_env = getenv("SAVED_FD_1");
  30. if (!progress_type_env)
  31. return;
  32. if (!strcmp(progress_type_env, "console") && du_size_env) {
  33. char msg[256];
  34. snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r",
  35. total / 1024, strtoull(du_size_env, NULL, 0));
  36. write(2, msg, strlen(msg));
  37. if (flag == PROGRESS_FLAG_DONE)
  38. write(2, "\n", 1);
  39. }
  40. if (!strcmp(progress_type_env, "gui") && saved_stdout_env) {
  41. char msg[256];
  42. snprintf(msg, sizeof(msg), "%lld\n", total);
  43. write(strtoul(saved_stdout_env, NULL, 0), msg,
  44. strlen(msg));
  45. }
  46. }
  47. void wait_for_result()
  48. {
  49. struct result_header hdr;
  50. if (!read_all(0, &hdr, sizeof(hdr))) {
  51. if (errno == EAGAIN) {
  52. // no result sent and stdin still open
  53. return;
  54. } else {
  55. // other read error or EOF
  56. exit(1); // hopefully remote has produced error message
  57. }
  58. }
  59. if (hdr.error_code != 0) {
  60. switch (hdr.error_code) {
  61. case EEXIST:
  62. gui_fatal("File copy: not overwriting existing file. Clean QubesIncoming dir, and retry copy");
  63. break;
  64. case EINVAL:
  65. gui_fatal("File copy: Corrupted data from packer");
  66. break;
  67. default:
  68. gui_fatal("File copy: %s",
  69. strerror(hdr.error_code));
  70. }
  71. }
  72. if (hdr.crc32 != crc32_sum) {
  73. gui_fatal("File transfer failed: checksum mismatch");
  74. }
  75. }
  76. void notify_progress(int size, int flag)
  77. {
  78. static long long total = 0;
  79. static long long prev_total = 0;
  80. total += size;
  81. if (total > prev_total + PROGRESS_NOTIFY_DELTA
  82. || (flag != PROGRESS_FLAG_NORMAL)) {
  83. // check for possible error from qfile-unpacker; if error occured,
  84. // exit() will be called, so don't bother with current state
  85. // (notify_progress can be called as callback from copy_file())
  86. if (flag == PROGRESS_FLAG_NORMAL)
  87. wait_for_result();
  88. do_notify_progress(total, flag);
  89. prev_total = total;
  90. }
  91. }
  92. void write_headers(struct file_header *hdr, char *filename)
  93. {
  94. if (!write_all_with_crc(1, hdr, sizeof(*hdr))
  95. || !write_all_with_crc(1, filename, hdr->namelen)) {
  96. set_block(0);
  97. wait_for_result();
  98. exit(1);
  99. }
  100. }
  101. int single_file_processor(char *filename, struct stat *st)
  102. {
  103. struct file_header hdr;
  104. int fd;
  105. mode_t mode = st->st_mode;
  106. hdr.namelen = strlen(filename) + 1;
  107. hdr.mode = mode;
  108. hdr.atime = st->st_atim.tv_sec;
  109. hdr.atime_nsec = st->st_atim.tv_nsec;
  110. hdr.mtime = st->st_mtim.tv_sec;
  111. hdr.mtime_nsec = st->st_mtim.tv_nsec;
  112. if (S_ISREG(mode)) {
  113. int ret;
  114. fd = open(filename, O_RDONLY);
  115. if (fd < 0)
  116. gui_fatal("open %s", filename);
  117. hdr.filelen = st->st_size;
  118. write_headers(&hdr, filename);
  119. ret = copy_file(1, fd, hdr.filelen, &crc32_sum);
  120. if (ret != COPY_FILE_OK) {
  121. if (ret != COPY_FILE_WRITE_ERROR)
  122. gui_fatal("Copying file %s: %s", filename,
  123. copy_file_status_to_str(ret));
  124. else {
  125. set_block(0);
  126. wait_for_result();
  127. exit(1);
  128. }
  129. }
  130. close(fd);
  131. }
  132. if (S_ISDIR(mode)) {
  133. hdr.filelen = 0;
  134. write_headers(&hdr, filename);
  135. }
  136. if (S_ISLNK(mode)) {
  137. char name[st->st_size + 1];
  138. if (readlink(filename, name, sizeof(name)) != st->st_size)
  139. gui_fatal("readlink %s", filename);
  140. hdr.filelen = st->st_size + 1;
  141. write_headers(&hdr, filename);
  142. if (!write_all_with_crc(1, name, st->st_size + 1)) {
  143. set_block(0);
  144. wait_for_result();
  145. exit(1);
  146. }
  147. }
  148. // check for possible error from qfile-unpacker
  149. wait_for_result();
  150. return 0;
  151. }
  152. int do_fs_walk(char *file)
  153. {
  154. char *newfile;
  155. struct stat st;
  156. struct dirent *ent;
  157. DIR *dir;
  158. if (lstat(file, &st))
  159. gui_fatal("stat %s", file);
  160. single_file_processor(file, &st);
  161. if (!S_ISDIR(st.st_mode))
  162. return 0;
  163. dir = opendir(file);
  164. if (!dir)
  165. gui_fatal("opendir %s", file);
  166. while ((ent = readdir(dir))) {
  167. char *fname = ent->d_name;
  168. if (!strcmp(fname, ".") || !strcmp(fname, ".."))
  169. continue;
  170. asprintf(&newfile, "%s/%s", file, fname);
  171. do_fs_walk(newfile);
  172. free(newfile);
  173. }
  174. closedir(dir);
  175. // directory metadata is resent; this makes the code simple,
  176. // and the atime/mtime is set correctly at the second time
  177. single_file_processor(file, &st);
  178. return 0;
  179. }
  180. void notify_end_and_wait_for_result()
  181. {
  182. struct file_header end_hdr;
  183. /* nofity end of transfer */
  184. memset(&end_hdr, 0, sizeof(end_hdr));
  185. end_hdr.namelen = 0;
  186. end_hdr.filelen = 0;
  187. write_all_with_crc(1, &end_hdr, sizeof(end_hdr));
  188. set_block(0);
  189. wait_for_result();
  190. }
  191. char *get_abs_path(char *cwd, char *pathname)
  192. {
  193. char *ret;
  194. if (pathname[0] == '/')
  195. return strdup(pathname);
  196. asprintf(&ret, "%s/%s", cwd, pathname);
  197. return ret;
  198. }
  199. int main(int argc, char **argv)
  200. {
  201. int i;
  202. char *entry;
  203. char *cwd;
  204. char *sep;
  205. signal(SIGPIPE, SIG_IGN);
  206. // this will allow checking for possible feedback packet in the middle of transfer
  207. set_nonblock(0);
  208. notify_progress(0, PROGRESS_FLAG_INIT);
  209. crc32_sum = 0;
  210. cwd = getcwd(NULL, 0);
  211. for (i = 1; i < argc; i++) {
  212. entry = get_abs_path(cwd, argv[i]);
  213. do {
  214. sep = rindex(entry, '/');
  215. if (!sep)
  216. gui_fatal
  217. ("Internal error: nonabsolute filenames not allowed");
  218. *sep = 0;
  219. } while (sep[1] == 0);
  220. if (entry[0] == 0)
  221. chdir("/");
  222. else if (chdir(entry))
  223. gui_fatal("chdir to %s", entry);
  224. do_fs_walk(sep + 1);
  225. free(entry);
  226. }
  227. notify_end_and_wait_for_result();
  228. notify_progress(0, PROGRESS_FLAG_DONE);
  229. return 0;
  230. }