qfile-agent.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "qfile-utils.h"
  2. char *get_abs_path(const char *cwd, const char *pathname)
  3. {
  4. char *ret;
  5. if (pathname[0] == '/')
  6. return strdup(pathname);
  7. asprintf(&ret, "%s/%s", cwd, pathname);
  8. return ret;
  9. }
  10. int do_fs_walk(const char *file)
  11. {
  12. char *newfile;
  13. struct stat st;
  14. struct dirent *ent;
  15. DIR *dir;
  16. if (lstat(file, &st))
  17. gui_fatal("stat %s", file);
  18. single_file_processor(file, &st);
  19. if (!S_ISDIR(st.st_mode))
  20. return 0;
  21. dir = opendir(file);
  22. if (!dir)
  23. gui_fatal("opendir %s", file);
  24. while ((ent = readdir(dir))) {
  25. char *fname = ent->d_name;
  26. if (!strcmp(fname, ".") || !strcmp(fname, ".."))
  27. continue;
  28. asprintf(&newfile, "%s/%s", file, fname);
  29. do_fs_walk(newfile);
  30. free(newfile);
  31. }
  32. closedir(dir);
  33. // directory metadata is resent; this makes the code simple,
  34. // and the atime/mtime is set correctly at the second time
  35. single_file_processor(file, &st);
  36. return 0;
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. int i;
  41. char *entry;
  42. char *cwd;
  43. char *sep;
  44. signal(SIGPIPE, SIG_IGN);
  45. // this will allow checking for possible feedback packet in the middle of transfer
  46. set_nonblock(0);
  47. notify_progress(0, PROGRESS_FLAG_INIT);
  48. crc32_sum = 0;
  49. cwd = getcwd(NULL, 0);
  50. for (i = 1; i < argc; i++) {
  51. if (strcmp(argv[i], "--ignore-symlinks")==0) {
  52. ignore_symlinks = 1;
  53. continue;
  54. }
  55. entry = get_abs_path(cwd, argv[i]);
  56. do {
  57. sep = rindex(entry, '/');
  58. if (!sep)
  59. gui_fatal
  60. ("Internal error: nonabsolute filenames not allowed");
  61. *sep = 0;
  62. } while (sep[1] == 0);
  63. if (entry[0] == 0)
  64. chdir("/");
  65. else if (chdir(entry))
  66. gui_fatal("chdir to %s", entry);
  67. do_fs_walk(sep + 1);
  68. free(entry);
  69. }
  70. notify_end_and_wait_for_result();
  71. notify_progress(0, PROGRESS_FLAG_DONE);
  72. return 0;
  73. }