qfile-agent.c 5.9 KB

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