qfile-agent.c 1.7 KB

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