misc_file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * misc.c -- handles:
  3. * copyfile() movefile()
  4. *
  5. */
  6. #include "common.h"
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include "stat.h"
  12. #include "misc_file.h"
  13. #include "main.h"
  14. #include "shell.h"
  15. /* Copy a file from one place to another (possibly erasing old copy).
  16. *
  17. * returns: 0 if OK
  18. * 1 if can't open original file
  19. * 2 if can't open new file
  20. * 3 if original file isn't normal
  21. * 4 if ran out of disk space
  22. */
  23. int copyfile(const char *oldpath, const char *newpath)
  24. {
  25. int fi;
  26. #ifndef CYGWIN_HACKS
  27. fi = open(oldpath, O_RDONLY, 0);
  28. #else
  29. fi = open(oldpath, O_RDONLY | O_BINARY, 0);
  30. #endif
  31. if (fi < 0)
  32. return 1;
  33. struct stat st;
  34. fstat(fi, &st);
  35. if (!(st.st_mode & S_IFREG))
  36. return 3;
  37. int fo;
  38. fo = creat(newpath, (int) (st.st_mode & 0600));
  39. if (fo < 0) {
  40. close(fi);
  41. return 2;
  42. }
  43. char buf[512] = "";
  44. for (int x = 1; x > 0;) {
  45. x = read(fi, buf, 512);
  46. if (x > 0) {
  47. if (write(fo, buf, x) < x) { /* Couldn't write */
  48. close(fo);
  49. close(fi);
  50. unlink(newpath);
  51. return 4;
  52. }
  53. }
  54. }
  55. #ifdef HAVE_FSYNC
  56. fsync(fo);
  57. #endif /* HAVE_FSYNC */
  58. close(fo);
  59. close(fi);
  60. return 0;
  61. }
  62. int movefile(const char *oldpath, const char *newpath)
  63. {
  64. #ifdef HAVE_RENAME
  65. /* Try to use rename first */
  66. if (!rename(oldpath, newpath))
  67. return 0;
  68. #endif /* HAVE_RENAME */
  69. /* If that fails, fall back to just copying and then
  70. * deleting the file.
  71. */
  72. int ret = copyfile(oldpath, newpath);
  73. if (!ret)
  74. unlink(oldpath);
  75. return ret;
  76. }
  77. int is_file(const char *s)
  78. {
  79. struct stat ss;
  80. int i = stat(s, &ss);
  81. if (i < 0)
  82. return 0;
  83. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  84. return 1;
  85. return 0;
  86. }
  87. int can_stat(const char *s)
  88. {
  89. struct stat ss;
  90. int i = stat(s, &ss);
  91. if (i < 0)
  92. return 0;
  93. return 1;
  94. }
  95. int can_lstat(const char *s)
  96. {
  97. struct stat ss;
  98. int i = lstat(s, &ss);
  99. if (i < 0)
  100. return 0;
  101. return 1;
  102. }
  103. int is_symlink(const char *s)
  104. {
  105. struct stat ss;
  106. int i = lstat(s, &ss);
  107. if (i < 0)
  108. return 0;
  109. if (ss.st_mode & S_IFLNK)
  110. return 1;
  111. return 0;
  112. }
  113. int is_dir(const char *s)
  114. {
  115. struct stat ss;
  116. int i = stat(s, &ss);
  117. if (i < 0)
  118. return 0;
  119. if (ss.st_mode & S_IFDIR)
  120. return 1;
  121. return 0;
  122. }
  123. int fixmod(const char *s)
  124. {
  125. #ifndef CYGWIN_HACKS
  126. if (!can_stat(s))
  127. return 1;
  128. return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
  129. #else
  130. return 0;
  131. #endif /* !CYGWIN_HACKS */
  132. }
  133. Tempfile::Tempfile()
  134. {
  135. len = strlen(tempdir) + 1 + 6 + 1;
  136. file = new char[len];
  137. simple_sprintf(file, "%s.XXXXXX", tempdir);
  138. MakeTemp();
  139. }
  140. Tempfile::Tempfile(const char *prefix)
  141. {
  142. len = strlen(tempdir) + 1 + strlen(prefix) + 1 + 6 + 1;
  143. file = new char[len];
  144. simple_sprintf(file, "%s.%s-XXXXXX", tempdir, prefix);
  145. MakeTemp();
  146. }
  147. void Tempfile::MakeTemp()
  148. {
  149. if ((fd = mkstemp(file)) < 0) {
  150. f = NULL;
  151. goto error;
  152. }
  153. if ((f = fdopen(fd, "w+b")) == NULL)
  154. goto error;
  155. fchmod(fd, S_IRUSR | S_IWUSR);
  156. error = 0;
  157. return;
  158. error:
  159. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  160. error = 1;
  161. // fatal("Cannot create temporary file!", 0);
  162. }
  163. void Tempfile::my_close()
  164. {
  165. if (f) {
  166. fclose(f);
  167. f = NULL;
  168. } else if (fd >= 0) {
  169. close(fd);
  170. fd = -1;
  171. }
  172. }
  173. Tempfile::~Tempfile()
  174. {
  175. unlink(file);
  176. my_close();
  177. delete[] file;
  178. }
  179. static bool check_tempdir(bool do_mod)
  180. {
  181. if (!can_stat(tempdir)) {
  182. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  183. unlink(tempdir);
  184. if (!can_stat(tempdir))
  185. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  186. return 0;
  187. }
  188. }
  189. if (do_mod && fixmod(tempdir))
  190. return 0;
  191. /* test tempdir: it's vital */
  192. Tempfile *testdir = new Tempfile("test");
  193. int result;
  194. fprintf(testdir->f, "\n");
  195. result = fflush(testdir->f);
  196. delete testdir;
  197. if (result) {
  198. sdprintf("%s: %s", tempdir, strerror(errno));
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. void Tempfile::FindDir()
  204. {
  205. /* this is temporary until we make tmpdir customizable */
  206. if (conf.bots && conf.bots->nick && conf.bots->hub)
  207. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  208. else
  209. simple_snprintf(tempdir, DIRMAX, "%s/.ssh/.../", conf.homedir);
  210. #ifdef CYGWIN_HACKS
  211. simple_snprintf(tempdir, DIRMAX, "tmp/");
  212. #endif /* CYGWIN_HACKS */
  213. if (!check_tempdir(0)) {
  214. clear_tmpdir = 0;
  215. simple_snprintf(tempdir, DIRMAX, "/tmp/");
  216. }
  217. if (!check_tempdir(0)) {
  218. simple_snprintf(tempdir, DIRMAX, "/usr/tmp/");
  219. }
  220. if (!check_tempdir(0)) {
  221. simple_snprintf(tempdir, DIRMAX, "/var/tmp/");
  222. }
  223. if (!check_tempdir(0)) {
  224. simple_snprintf(tempdir, DIRMAX, "./");
  225. }
  226. if (!check_tempdir(0)) {
  227. check_tempdir(0);
  228. werr(ERR_TMPSTAT);
  229. }
  230. }