misc_file.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. if (!can_stat(s))
  126. return 1;
  127. return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
  128. }
  129. Tempfile::Tempfile()
  130. {
  131. len = strlen(tempdir) + 1 + 6 + 1;
  132. file = new char[len];
  133. simple_sprintf(file, "%s.XXXXXX", tempdir);
  134. MakeTemp();
  135. }
  136. Tempfile::Tempfile(const char *prefix)
  137. {
  138. len = strlen(tempdir) + 1 + strlen(prefix) + 1 + 6 + 1;
  139. file = new char[len];
  140. simple_sprintf(file, "%s.%s-XXXXXX", tempdir, prefix);
  141. MakeTemp();
  142. }
  143. void Tempfile::MakeTemp()
  144. {
  145. if ((fd = mkstemp(file)) < 0) {
  146. f = NULL;
  147. goto error;
  148. }
  149. if ((f = fdopen(fd, "w+b")) == NULL)
  150. goto error;
  151. fchmod(fd, S_IRUSR | S_IWUSR);
  152. error = 0;
  153. return;
  154. error:
  155. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  156. error = 1;
  157. // fatal("Cannot create temporary file!", 0);
  158. }
  159. void Tempfile::my_close()
  160. {
  161. if (f) {
  162. fclose(f);
  163. f = NULL;
  164. } else if (fd >= 0) {
  165. close(fd);
  166. fd = -1;
  167. }
  168. }
  169. Tempfile::~Tempfile()
  170. {
  171. unlink(file);
  172. my_close();
  173. delete[] file;
  174. }
  175. static bool check_tempdir(bool do_mod)
  176. {
  177. if (!can_stat(tempdir)) {
  178. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  179. unlink(tempdir);
  180. if (!can_stat(tempdir))
  181. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  182. return 0;
  183. }
  184. }
  185. if (do_mod && fixmod(tempdir))
  186. return 0;
  187. /* test tempdir: it's vital */
  188. Tempfile *testdir = new Tempfile("test");
  189. int result;
  190. fprintf(testdir->f, "\n");
  191. result = fflush(testdir->f);
  192. delete testdir;
  193. if (result) {
  194. sdprintf("%s: %s", tempdir, strerror(errno));
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. void Tempfile::FindDir()
  200. {
  201. /* this is temporary until we make tmpdir customizable */
  202. #ifdef CYGWIN_HACKS
  203. simple_snprintf(tempdir, DIRMAX, "tmp/");
  204. if (!check_tempdir(0)) {
  205. clear_tmpdir = 0;
  206. simple_snprintf(tempdir, DIRMAX, "./");
  207. }
  208. #else
  209. if (conf.bots && conf.bots->nick && conf.bots->hub)
  210. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  211. else {
  212. //need to create ~/.ssh/
  213. simple_snprintf(tempdir, DIRMAX, "%s/.ssh/", conf.homedir);
  214. clear_tmpdir = 0;
  215. check_tempdir(0);
  216. clear_tmpdir = 1;
  217. simple_snprintf(tempdir, DIRMAX, "%s/.ssh/.../", conf.homedir);
  218. }
  219. if (!check_tempdir(0)) {
  220. clear_tmpdir = 0;
  221. simple_snprintf(tempdir, DIRMAX, "/tmp/");
  222. }
  223. if (!check_tempdir(0)) {
  224. simple_snprintf(tempdir, DIRMAX, "/usr/tmp/");
  225. }
  226. if (!check_tempdir(0)) {
  227. simple_snprintf(tempdir, DIRMAX, "/var/tmp/");
  228. }
  229. if (!check_tempdir(0)) {
  230. simple_snprintf(tempdir, DIRMAX, "./");
  231. }
  232. if (!check_tempdir(0)) {
  233. check_tempdir(0);
  234. werr(ERR_TMPSTAT);
  235. }
  236. #endif /* CYGWIN_HACKS */
  237. }