misc_file.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 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 "stat.h"
  31. #include "misc_file.h"
  32. #include "main.h"
  33. #include "shell.h"
  34. #include "binary.h"
  35. static bool looking = 0;
  36. /* Copy a file from one place to another (possibly erasing old copy).
  37. *
  38. * returns: 0 if OK
  39. * 1 if can't open original file
  40. * 2 if can't open new file
  41. * 3 if original file isn't normal
  42. * 4 if ran out of disk space
  43. */
  44. int copyfile(const char *oldpath, const char *newpath)
  45. {
  46. int fi;
  47. #ifndef CYGWIN_HACKS
  48. fi = open(oldpath, O_RDONLY, 0);
  49. #else
  50. fi = open(oldpath, O_RDONLY | O_BINARY, 0);
  51. #endif
  52. if (fi < 0)
  53. return 1;
  54. struct stat st;
  55. fstat(fi, &st);
  56. if (!(st.st_mode & S_IFREG))
  57. return 3;
  58. int fo;
  59. fo = creat(newpath, (int) (st.st_mode & 0600));
  60. if (fo < 0) {
  61. close(fi);
  62. return 2;
  63. }
  64. char buf[512] = "";
  65. for (int x = 1; x > 0;) {
  66. x = read(fi, buf, 512);
  67. if (x > 0) {
  68. if (write(fo, buf, x) < x) { /* Couldn't write */
  69. close(fo);
  70. close(fi);
  71. unlink(newpath);
  72. return 4;
  73. }
  74. }
  75. }
  76. #ifdef HAVE_FSYNC
  77. fsync(fo);
  78. #endif /* HAVE_FSYNC */
  79. close(fo);
  80. close(fi);
  81. return 0;
  82. }
  83. int movefile(const char *oldpath, const char *newpath)
  84. {
  85. #ifdef HAVE_RENAME
  86. /* Try to use rename first */
  87. if (!rename(oldpath, newpath))
  88. return 0;
  89. #endif /* HAVE_RENAME */
  90. /* If that fails, fall back to just copying and then
  91. * deleting the file.
  92. */
  93. int ret = copyfile(oldpath, newpath);
  94. if (!ret)
  95. unlink(oldpath);
  96. return ret;
  97. }
  98. int is_file(const char *s)
  99. {
  100. struct stat ss;
  101. int i = stat(s, &ss);
  102. if (i < 0)
  103. return 0;
  104. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  105. return 1;
  106. return 0;
  107. }
  108. int can_stat(const char *s)
  109. {
  110. struct stat ss;
  111. int i = stat(s, &ss);
  112. if (i < 0)
  113. return 0;
  114. return 1;
  115. }
  116. int can_lstat(const char *s)
  117. {
  118. struct stat ss;
  119. int i = lstat(s, &ss);
  120. if (i < 0)
  121. return 0;
  122. return 1;
  123. }
  124. int is_symlink(const char *s)
  125. {
  126. struct stat ss;
  127. int i = lstat(s, &ss);
  128. if (i < 0)
  129. return 0;
  130. if (ss.st_mode & S_IFLNK)
  131. return 1;
  132. return 0;
  133. }
  134. int is_dir(const char *s)
  135. {
  136. struct stat ss;
  137. int i = stat(s, &ss);
  138. if (i < 0)
  139. return 0;
  140. if (ss.st_mode & S_IFDIR)
  141. return 1;
  142. return 0;
  143. }
  144. int fixmod(const char *s)
  145. {
  146. if (!can_stat(s))
  147. return 1;
  148. return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
  149. }
  150. Tempfile::Tempfile(const char *prefix, bool useFopen)
  151. {
  152. this->useFopen = useFopen;
  153. this->f = NULL;
  154. this->fd = -1;
  155. if (prefix) {
  156. plen = strlen(prefix) + 1;
  157. this->prefix = new char[plen];
  158. strlcpy(this->prefix, prefix, plen);
  159. } else {
  160. this->prefix = NULL;
  161. plen = -1; /* to swallow the '-' */
  162. }
  163. AllocTempfile();
  164. }
  165. void Tempfile::AllocTempfile()
  166. {
  167. len = strlen(tempdir) + 1 + plen + 1 + 6 + 1;
  168. file = new char[len];
  169. if (prefix)
  170. simple_snprintf(file, len, "%s.%s-XXXXXX", tempdir, prefix);
  171. else
  172. simple_snprintf(file, len, "%s.XXXXXX", tempdir);
  173. MakeTemp();
  174. }
  175. void Tempfile::MakeTemp()
  176. {
  177. if ((fd = mkstemp(file)) < 0) {
  178. f = NULL;
  179. goto error;
  180. }
  181. if (this->useFopen) {
  182. if ((f = fdopen(fd, "w+b")) == NULL)
  183. goto error;
  184. }
  185. fchmod(fd, S_IRUSR | S_IWUSR);
  186. error = 0;
  187. return;
  188. error:
  189. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  190. error = 1;
  191. /* Since we failed to create a file in the given tempdir, let's try finding a new one */
  192. if (!looking) {
  193. /* ... Not finding a new tempdir is fatal. */
  194. if (FindDir() == ERROR)
  195. werr(ERR_TMPSTAT); /* FIXME: Perhaps this should be an exception? */
  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. unlink(file);
  218. my_close();
  219. delete[] file;
  220. }
  221. static bool check_tempdir(bool do_mod)
  222. {
  223. mkdir_p(tempdir);
  224. if (!can_stat(tempdir))
  225. return 0;
  226. if (do_mod && fixmod(tempdir))
  227. return 0;
  228. /* test tempdir: it's vital */
  229. Tempfile *testdir = new Tempfile("test");
  230. /* There was an error creating a file in this directory, return to move on in list of dirs */
  231. if (!testdir || testdir->error)
  232. return 0;
  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. #ifdef CYGWIN_HACKS
  246. simple_snprintf(tempdir, DIRMAX, "./tmp/");
  247. if (!check_tempdir(0)) {
  248. clear_tmpdir = 0;
  249. simple_snprintf(tempdir, DIRMAX, "./");
  250. }
  251. return OK;
  252. #else
  253. looking = 1;
  254. /* If this is a hub, use, "./tmp/" */
  255. if (conf.bots && conf.bots->nick && conf.bots->hub) {
  256. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  257. if (check_tempdir(0)) {
  258. looking = 0;
  259. return OK;
  260. }
  261. }
  262. /* The dirs we WANT to use aren't accessible, try a random one instead to get the job done. */
  263. clear_tmpdir = 0;
  264. char *dirs[] = {
  265. "/tmp/",
  266. "/usr/tmp/",
  267. "/var/tmp/",
  268. "./",
  269. NULL
  270. };
  271. for (int i = 0; dirs[i]; i++) {
  272. realpath(dirs[i], tempdir);
  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. werr(ERR_TMPSTAT);
  282. #endif /* CYGWIN_HACKS */
  283. }