qfile-agent.c 5.8 KB

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