qfile-agent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. void do_notify_progress(long long total, int flag)
  20. {
  21. const char *du_size_env = getenv("FILECOPY_TOTAL_SIZE");
  22. const char *progress_type_env = getenv("PROGRESS_TYPE");
  23. const char *saved_stdout_env = getenv("SAVED_FD_1");
  24. int ignore;
  25. if (!progress_type_env)
  26. return;
  27. if (!strcmp(progress_type_env, "console") && du_size_env) {
  28. char msg[256];
  29. snprintf(msg, sizeof(msg), "sent %lld/%lld KB\r",
  30. total / 1024, strtoull(du_size_env, NULL, 0));
  31. ignore = write(2, msg, strlen(msg));
  32. if (flag == PROGRESS_FLAG_DONE)
  33. ignore = write(2, "\n", 1);
  34. }
  35. if (!strcmp(progress_type_env, "gui") && saved_stdout_env) {
  36. char msg[256];
  37. snprintf(msg, sizeof(msg), "%lld\n", total);
  38. ignore = write(strtoul(saved_stdout_env, NULL, 0), msg,
  39. strlen(msg));
  40. }
  41. if (ignore < 0) {
  42. /* silence gcc warning */
  43. }
  44. }
  45. void notify_progress(int size, int flag)
  46. {
  47. static long long total = 0;
  48. static long long prev_total = 0;
  49. total += size;
  50. if (total > prev_total + PROGRESS_NOTIFY_DELTA
  51. || (flag != PROGRESS_FLAG_NORMAL)) {
  52. // check for possible error from qfile-unpacker; if error occured,
  53. // exit() will be called, so don't bother with current state
  54. // (notify_progress can be called as callback from copy_file())
  55. if (flag == PROGRESS_FLAG_NORMAL)
  56. wait_for_result();
  57. do_notify_progress(total, flag);
  58. prev_total = total;
  59. }
  60. }
  61. char *get_abs_path(const char *cwd, const char *pathname)
  62. {
  63. char *ret;
  64. if (pathname[0] == '/')
  65. return strdup(pathname);
  66. if (asprintf(&ret, "%s/%s", cwd, pathname) < 0)
  67. return NULL;
  68. else
  69. return ret;
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. int i;
  74. char *entry;
  75. char *cwd;
  76. char *sep;
  77. int ignore_symlinks = 0;
  78. qfile_pack_init();
  79. register_error_handler(qfile_gui_fatal);
  80. register_notify_progress(&notify_progress);
  81. notify_progress(0, PROGRESS_FLAG_INIT);
  82. cwd = getcwd(NULL, 0);
  83. for (i = 1; i < argc; i++) {
  84. if (strcmp(argv[i], "--ignore-symlinks")==0) {
  85. ignore_symlinks = 1;
  86. continue;
  87. }
  88. entry = get_abs_path(cwd, argv[i]);
  89. do {
  90. sep = rindex(entry, '/');
  91. if (!sep)
  92. gui_fatal
  93. ("Internal error: nonabsolute filenames not allowed");
  94. *sep = 0;
  95. } while (sep[1] == 0);
  96. if (entry[0] == 0) {
  97. if (chdir("/") < 0) {
  98. gui_fatal("Internal error: chdir(\"/\") failed?!");
  99. }
  100. } else if (chdir(entry))
  101. gui_fatal("chdir to %s", entry);
  102. do_fs_walk(sep + 1, ignore_symlinks);
  103. free(entry);
  104. }
  105. notify_end_and_wait_for_result();
  106. notify_progress(0, PROGRESS_FLAG_DONE);
  107. return 0;
  108. }