qfile-agent.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <gui-fatal.h>
  13. #include "filecopy.h"
  14. #include "crc32.h"
  15. enum {
  16. PROGRESS_FLAG_NORMAL,
  17. PROGRESS_FLAG_INIT,
  18. PROGRESS_FLAG_DONE
  19. };
  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 notify_progress(int size, int flag)
  49. {
  50. static long long total = 0;
  51. static long long prev_total = 0;
  52. total += size;
  53. if (total > prev_total + PROGRESS_NOTIFY_DELTA
  54. || (flag != PROGRESS_FLAG_NORMAL)) {
  55. do_notify_progress(total, flag);
  56. prev_total = total;
  57. }
  58. }
  59. void write_headers(struct file_header *hdr, char *filename)
  60. {
  61. if (!write_all_with_crc(1, hdr, sizeof(*hdr))
  62. || !write_all_with_crc(1, filename, hdr->namelen))
  63. exit(1);
  64. }
  65. int single_file_processor(char *filename, struct stat *st)
  66. {
  67. struct file_header hdr;
  68. int fd;
  69. mode_t mode = st->st_mode;
  70. hdr.namelen = strlen(filename) + 1;
  71. hdr.mode = mode;
  72. hdr.atime = st->st_atim.tv_sec;
  73. hdr.atime_nsec = st->st_atim.tv_nsec;
  74. hdr.mtime = st->st_mtim.tv_sec;
  75. hdr.mtime_nsec = st->st_mtim.tv_nsec;
  76. if (S_ISREG(mode)) {
  77. int ret;
  78. fd = open(filename, O_RDONLY);
  79. if (fd < 0)
  80. gui_fatal("open %s", filename);
  81. hdr.filelen = st->st_size;
  82. write_headers(&hdr, filename);
  83. ret = copy_file(1, fd, hdr.filelen, &crc32_sum);
  84. // if COPY_FILE_WRITE_ERROR, hopefully remote will produce a message
  85. if (ret != COPY_FILE_OK) {
  86. if (ret != COPY_FILE_WRITE_ERROR)
  87. gui_fatal("Copying file %s: %s", filename,
  88. copy_file_status_to_str(ret));
  89. else
  90. exit(1);
  91. }
  92. close(fd);
  93. }
  94. if (S_ISDIR(mode)) {
  95. hdr.filelen = 0;
  96. write_headers(&hdr, filename);
  97. }
  98. if (S_ISLNK(mode)) {
  99. char name[st->st_size + 1];
  100. if (readlink(filename, name, sizeof(name)) != st->st_size)
  101. gui_fatal("readlink %s", filename);
  102. hdr.filelen = st->st_size + 1;
  103. write_headers(&hdr, filename);
  104. if (!write_all_with_crc(1, name, st->st_size + 1))
  105. exit(1);
  106. }
  107. return 0;
  108. }
  109. int do_fs_walk(char *file)
  110. {
  111. char *newfile;
  112. struct stat st;
  113. struct dirent *ent;
  114. DIR *dir;
  115. if (lstat(file, &st))
  116. gui_fatal("stat %s", file);
  117. single_file_processor(file, &st);
  118. if (!S_ISDIR(st.st_mode))
  119. return 0;
  120. dir = opendir(file);
  121. if (!dir)
  122. gui_fatal("opendir %s", file);
  123. while ((ent = readdir(dir))) {
  124. char *fname = ent->d_name;
  125. if (!strcmp(fname, ".") || !strcmp(fname, ".."))
  126. continue;
  127. asprintf(&newfile, "%s/%s", file, fname);
  128. do_fs_walk(newfile);
  129. free(newfile);
  130. }
  131. closedir(dir);
  132. // directory metadata is resent; this makes the code simple,
  133. // and the atime/mtime is set correctly at the second time
  134. single_file_processor(file, &st);
  135. return 0;
  136. }
  137. void notify_end_and_wait_for_result()
  138. {
  139. struct result_header hdr;
  140. struct file_header end_hdr;
  141. /* nofity end of transfer */
  142. memset(&end_hdr, 0, sizeof(end_hdr));
  143. end_hdr.namelen = 0;
  144. end_hdr.filelen = 0;
  145. write_all_with_crc(1, &end_hdr, sizeof(end_hdr));
  146. /* wait for result */
  147. if (!read_all(0, &hdr, sizeof(hdr))) {
  148. exit(1); // hopefully remote has produced error message
  149. }
  150. if (hdr.error_code != 0) {
  151. gui_fatal("Error writing files: %s",
  152. strerror(hdr.error_code));
  153. }
  154. if (hdr.crc32 != crc32_sum) {
  155. gui_fatal("File transfer failed: checksum mismatch");
  156. }
  157. }
  158. char *get_abs_path(char *cwd, char *pathname)
  159. {
  160. char *ret;
  161. if (pathname[0] == '/')
  162. return strdup(pathname);
  163. asprintf(&ret, "%s/%s", cwd, pathname);
  164. return ret;
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. int i;
  169. char *entry;
  170. char *cwd;
  171. char *sep;
  172. signal(SIGPIPE, SIG_IGN);
  173. notify_progress(0, PROGRESS_FLAG_INIT);
  174. crc32_sum = 0;
  175. cwd = getcwd(NULL, 0);
  176. for (i = 1; i < argc; i++) {
  177. entry = get_abs_path(cwd, argv[i]);
  178. do {
  179. sep = rindex(entry, '/');
  180. if (!sep)
  181. gui_fatal
  182. ("Internal error: nonabsolute filenames not allowed");
  183. *sep = 0;
  184. } while (sep[1] == 0);
  185. if (entry[0] == 0)
  186. chdir("/");
  187. else if (chdir(entry))
  188. gui_fatal("chdir to %s", entry);
  189. do_fs_walk(sep + 1);
  190. free(entry);
  191. }
  192. notify_end_and_wait_for_result();
  193. notify_progress(0, PROGRESS_FLAG_DONE);
  194. return 0;
  195. }