qfile-agent.c 6.7 KB

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