misc_file.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. fsync(fo);
  74. close(fo);
  75. close(fi);
  76. return 0;
  77. }
  78. int movefile(const char *oldpath, const char *newpath)
  79. {
  80. /* Try to use rename first */
  81. if (!rename(oldpath, newpath))
  82. return 0;
  83. /* If that fails, fall back to just copying and then
  84. * deleting the file.
  85. */
  86. int ret = copyfile(oldpath, newpath);
  87. if (!ret)
  88. unlink(oldpath);
  89. return ret;
  90. }
  91. int is_file(const char *s)
  92. {
  93. struct stat ss;
  94. int i = stat(s, &ss);
  95. if (i < 0)
  96. return 0;
  97. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  98. return 1;
  99. return 0;
  100. }
  101. int can_stat(const char *s)
  102. {
  103. struct stat ss;
  104. int i = stat(s, &ss);
  105. if (i < 0)
  106. return 0;
  107. return 1;
  108. }
  109. int can_lstat(const char *s)
  110. {
  111. struct stat ss;
  112. int i = lstat(s, &ss);
  113. if (i < 0)
  114. return 0;
  115. return 1;
  116. }
  117. int is_symlink(const char *s)
  118. {
  119. struct stat ss;
  120. int i = lstat(s, &ss);
  121. if (i < 0)
  122. return 0;
  123. if (ss.st_mode & S_IFLNK)
  124. return 1;
  125. return 0;
  126. }
  127. int is_dir(const char *s)
  128. {
  129. struct stat ss;
  130. int i = stat(s, &ss);
  131. if (i < 0)
  132. return 0;
  133. if (ss.st_mode & S_IFDIR)
  134. return 1;
  135. return 0;
  136. }
  137. int fixmod(const char *s)
  138. {
  139. if (!can_stat(s))
  140. return 1;
  141. return chmod(s, BINMOD);
  142. }
  143. Tempfile::Tempfile(const char *_prefix, bool _useFopen)
  144. {
  145. this->useFopen = _useFopen;
  146. this->f = NULL;
  147. this->fd = -1;
  148. if (_prefix) {
  149. plen = strlen(_prefix) + 1;
  150. this->prefix = new char[plen];
  151. strlcpy(this->prefix, _prefix, plen);
  152. } else {
  153. this->prefix = NULL;
  154. plen = -1; /* to swallow the '-' */
  155. }
  156. AllocTempfile();
  157. }
  158. void Tempfile::AllocTempfile()
  159. {
  160. len = strlen(tempdir) + 1 + plen + 1 + 6 + 1;
  161. file = new char[len];
  162. if (prefix)
  163. simple_snprintf(file, len, "%s.%s-XXXXXX", tempdir, prefix);
  164. else
  165. simple_snprintf(file, len, "%s.XXXXXX", tempdir);
  166. MakeTemp();
  167. }
  168. void Tempfile::MakeTemp()
  169. {
  170. if ((fd = mkstemp(file)) < 0) {
  171. f = NULL;
  172. goto error;
  173. }
  174. if (this->useFopen) {
  175. if ((f = fdopen(fd, "w+b")) == NULL)
  176. goto error;
  177. }
  178. fchmod(fd, S_IRUSR | S_IWUSR);
  179. error = 0;
  180. return;
  181. error:
  182. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  183. error = 1;
  184. /* Since we failed to create a file in the given tempdir, let's try finding a new one */
  185. if (!looking) {
  186. /* ... Not finding a new tempdir is fatal. */
  187. if (FindDir() == ERROR) {
  188. delete[] file;
  189. file = NULL;
  190. return;
  191. }
  192. /* ... If we found one, let's try all over! */
  193. else {
  194. error = 0;
  195. delete[] file;
  196. file = NULL;
  197. AllocTempfile();
  198. }
  199. }
  200. }
  201. void Tempfile::my_close()
  202. {
  203. if (f) {
  204. fclose(f);
  205. f = NULL;
  206. } else if (fd >= 0) {
  207. close(fd);
  208. fd = -1;
  209. }
  210. }
  211. Tempfile::~Tempfile()
  212. {
  213. if (this->prefix)
  214. delete[] this->prefix;
  215. unlink(file);
  216. my_close();
  217. delete[] file;
  218. }
  219. static bool check_tempdir(bool do_mod)
  220. {
  221. mkdir_p(tempdir);
  222. if (!can_stat(tempdir))
  223. return 0;
  224. if (do_mod && fixmod(tempdir))
  225. return 0;
  226. /* test tempdir: it's vital */
  227. Tempfile *testdir = new Tempfile("test");
  228. /* There was an error creating a file in this directory, return to move on in list of dirs */
  229. if (!testdir || testdir->error) {
  230. delete testdir;
  231. return 0;
  232. }
  233. fprintf(testdir->f, "\n");
  234. int result = fflush(testdir->f);
  235. delete testdir;
  236. if (result) {
  237. sdprintf("%s: %s", tempdir, strerror(errno));
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. bool Tempfile::FindDir()
  243. {
  244. /* this is temporary until we make tmpdir customizable */
  245. looking = 1;
  246. /* If this is a hub, use, "./tmp/" */
  247. if (conf.bots && conf.bots->nick && conf.bots->hub) {
  248. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", dirname(binname));
  249. if (check_tempdir(0)) {
  250. looking = 0;
  251. return OK;
  252. }
  253. }
  254. /* The dirs we WANT to use aren't accessible, try a random one instead to get the job done. */
  255. clear_tmpdir = 0;
  256. const char *dirs[] = {
  257. "/tmp/",
  258. "/usr/tmp/",
  259. "/var/tmp/",
  260. "./",
  261. NULL
  262. };
  263. for (int i = 0; dirs[i]; i++) {
  264. if (!realpath(dirs[i], tempdir)) {
  265. ;
  266. }
  267. tempdir[(DIRMAX < PATH_MAX ? DIRMAX : PATH_MAX) - 1] = 0;
  268. size_t len = strlen(tempdir);
  269. tempdir[len] = '/';
  270. tempdir[len + 1] = '\0';
  271. if (check_tempdir(0)) {
  272. looking = 0;
  273. return OK;
  274. }
  275. }
  276. return ERROR;
  277. }
  278. /* vim: set sts=2 sw=2 ts=8 et: */