misc_file.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * misc.c -- handles:
  22. * copyfile() movefile()
  23. *
  24. */
  25. #include "common.h"
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <limits.h>
  31. #include "stat.h"
  32. #include "misc_file.h"
  33. #include "main.h"
  34. #include "shell.h"
  35. #include "binary.h"
  36. static bool looking = 0;
  37. /* Copy a file from one place to another (possibly erasing old copy).
  38. *
  39. * returns: 0 if OK
  40. * 1 if can't open original file
  41. * 2 if can't open new file
  42. * 3 if original file isn't normal
  43. * 4 if ran out of disk space
  44. */
  45. int copyfile(const char *oldpath, const char *newpath)
  46. {
  47. int fi;
  48. fi = open(oldpath, O_RDONLY, 0);
  49. if (fi < 0)
  50. return 1;
  51. struct stat st;
  52. fstat(fi, &st);
  53. if (!(st.st_mode & S_IFREG))
  54. return 3;
  55. int fo;
  56. fo = creat(newpath, (int) (st.st_mode & 0600));
  57. if (fo < 0) {
  58. close(fi);
  59. return 2;
  60. }
  61. char buf[512] = "";
  62. for (int x = 1; x > 0;) {
  63. x = read(fi, buf, 512);
  64. if (x > 0) {
  65. if (write(fo, buf, x) < x) { /* Couldn't write */
  66. close(fo);
  67. close(fi);
  68. unlink(newpath);
  69. return 4;
  70. }
  71. }
  72. }
  73. #ifdef HAVE_FSYNC
  74. fsync(fo);
  75. #endif /* HAVE_FSYNC */
  76. close(fo);
  77. close(fi);
  78. return 0;
  79. }
  80. int movefile(const char *oldpath, const char *newpath)
  81. {
  82. #ifdef HAVE_RENAME
  83. /* Try to use rename first */
  84. if (!rename(oldpath, newpath))
  85. return 0;
  86. #endif /* HAVE_RENAME */
  87. /* If that fails, fall back to just copying and then
  88. * deleting the file.
  89. */
  90. int ret = copyfile(oldpath, newpath);
  91. if (!ret)
  92. unlink(oldpath);
  93. return ret;
  94. }
  95. int is_file(const char *s)
  96. {
  97. struct stat ss;
  98. int i = stat(s, &ss);
  99. if (i < 0)
  100. return 0;
  101. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  102. return 1;
  103. return 0;
  104. }
  105. int can_stat(const char *s)
  106. {
  107. struct stat ss;
  108. int i = stat(s, &ss);
  109. if (i < 0)
  110. return 0;
  111. return 1;
  112. }
  113. int can_lstat(const char *s)
  114. {
  115. struct stat ss;
  116. int i = lstat(s, &ss);
  117. if (i < 0)
  118. return 0;
  119. return 1;
  120. }
  121. int is_symlink(const char *s)
  122. {
  123. struct stat ss;
  124. int i = lstat(s, &ss);
  125. if (i < 0)
  126. return 0;
  127. if (ss.st_mode & S_IFLNK)
  128. return 1;
  129. return 0;
  130. }
  131. int is_dir(const char *s)
  132. {
  133. struct stat ss;
  134. int i = stat(s, &ss);
  135. if (i < 0)
  136. return 0;
  137. if (ss.st_mode & S_IFDIR)
  138. return 1;
  139. return 0;
  140. }
  141. int fixmod(const char *s)
  142. {
  143. if (!can_stat(s))
  144. return 1;
  145. return chmod(s, BINMOD);
  146. }
  147. Tempfile::Tempfile(const char *_prefix, bool _useFopen)
  148. {
  149. this->useFopen = _useFopen;
  150. this->f = NULL;
  151. this->fd = -1;
  152. if (_prefix) {
  153. plen = strlen(_prefix) + 1;
  154. this->prefix = new char[plen];
  155. strlcpy(this->prefix, _prefix, plen);
  156. } else {
  157. this->prefix = NULL;
  158. plen = -1; /* to swallow the '-' */
  159. }
  160. AllocTempfile();
  161. }
  162. void Tempfile::AllocTempfile()
  163. {
  164. len = strlen(tempdir) + 1 + plen + 1 + 6 + 1;
  165. file = new char[len];
  166. if (prefix)
  167. simple_snprintf(file, len, "%s.%s-XXXXXX", tempdir, prefix);
  168. else
  169. simple_snprintf(file, len, "%s.XXXXXX", tempdir);
  170. MakeTemp();
  171. }
  172. void Tempfile::MakeTemp()
  173. {
  174. if ((fd = mkstemp(file)) < 0) {
  175. f = NULL;
  176. goto error;
  177. }
  178. if (this->useFopen) {
  179. if ((f = fdopen(fd, "w+b")) == NULL)
  180. goto error;
  181. }
  182. fchmod(fd, S_IRUSR | S_IWUSR);
  183. error = 0;
  184. return;
  185. error:
  186. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  187. error = 1;
  188. /* Since we failed to create a file in the given tempdir, let's try finding a new one */
  189. if (!looking) {
  190. /* ... Not finding a new tempdir is fatal. */
  191. if (FindDir() == ERROR) {
  192. delete[] file;
  193. file = NULL;
  194. return;
  195. }
  196. /* ... If we found one, let's try all over! */
  197. else {
  198. error = 0;
  199. delete[] file;
  200. file = NULL;
  201. AllocTempfile();
  202. }
  203. }
  204. }
  205. void Tempfile::my_close()
  206. {
  207. if (f) {
  208. fclose(f);
  209. f = NULL;
  210. } else if (fd >= 0) {
  211. close(fd);
  212. fd = -1;
  213. }
  214. }
  215. Tempfile::~Tempfile()
  216. {
  217. if (this->prefix)
  218. delete[] this->prefix;
  219. unlink(file);
  220. my_close();
  221. delete[] file;
  222. }
  223. static bool check_tempdir(bool do_mod)
  224. {
  225. mkdir_p(tempdir);
  226. if (!can_stat(tempdir))
  227. return 0;
  228. if (do_mod && fixmod(tempdir))
  229. return 0;
  230. /* test tempdir: it's vital */
  231. Tempfile *testdir = new Tempfile("test");
  232. /* There was an error creating a file in this directory, return to move on in list of dirs */
  233. if (!testdir || testdir->error) {
  234. if (testdir)
  235. delete testdir;
  236. return 0;
  237. }
  238. fprintf(testdir->f, "\n");
  239. int result = fflush(testdir->f);
  240. delete testdir;
  241. if (result) {
  242. sdprintf("%s: %s", tempdir, strerror(errno));
  243. return 0;
  244. }
  245. return 1;
  246. }
  247. bool Tempfile::FindDir()
  248. {
  249. /* this is temporary until we make tmpdir customizable */
  250. looking = 1;
  251. /* If this is a hub, use, "./tmp/" */
  252. if (conf.bots && conf.bots->nick && conf.bots->hub) {
  253. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", dirname(binname));
  254. if (check_tempdir(0)) {
  255. looking = 0;
  256. return OK;
  257. }
  258. }
  259. /* The dirs we WANT to use aren't accessible, try a random one instead to get the job done. */
  260. clear_tmpdir = 0;
  261. const char *dirs[] = {
  262. "/tmp/",
  263. "/usr/tmp/",
  264. "/var/tmp/",
  265. "./",
  266. NULL
  267. };
  268. for (int i = 0; dirs[i]; i++) {
  269. if (!realpath(dirs[i], tempdir)) {
  270. ;
  271. }
  272. tempdir[(DIRMAX < PATH_MAX ? DIRMAX : PATH_MAX) - 1] = 0;
  273. size_t len = strlen(tempdir);
  274. tempdir[len] = '/';
  275. tempdir[len + 1] = '\0';
  276. if (check_tempdir(0)) {
  277. looking = 0;
  278. return OK;
  279. }
  280. }
  281. return ERROR;
  282. }
  283. /* vim: set sts=2 sw=2 ts=8 et: */