misc_file.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. delete[] file;
  196. file = NULL;
  197. return;
  198. }
  199. /* ... If we found one, let's try all over! */
  200. else {
  201. error = 0;
  202. delete[] file;
  203. file = NULL;
  204. AllocTempfile();
  205. }
  206. }
  207. }
  208. void Tempfile::my_close()
  209. {
  210. if (f) {
  211. fclose(f);
  212. f = NULL;
  213. } else if (fd >= 0) {
  214. close(fd);
  215. fd = -1;
  216. }
  217. }
  218. Tempfile::~Tempfile()
  219. {
  220. unlink(file);
  221. my_close();
  222. delete[] file;
  223. }
  224. static bool check_tempdir(bool do_mod)
  225. {
  226. mkdir_p(tempdir);
  227. if (!can_stat(tempdir))
  228. return 0;
  229. if (do_mod && fixmod(tempdir))
  230. return 0;
  231. /* test tempdir: it's vital */
  232. Tempfile *testdir = new Tempfile("test");
  233. /* There was an error creating a file in this directory, return to move on in list of dirs */
  234. if (!testdir || testdir->error)
  235. return 0;
  236. fprintf(testdir->f, "\n");
  237. int result = fflush(testdir->f);
  238. delete testdir;
  239. if (result) {
  240. sdprintf("%s: %s", tempdir, strerror(errno));
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. bool Tempfile::FindDir()
  246. {
  247. /* this is temporary until we make tmpdir customizable */
  248. #ifdef CYGWIN_HACKS
  249. simple_snprintf(tempdir, DIRMAX, "./tmp/");
  250. if (!check_tempdir(0)) {
  251. clear_tmpdir = 0;
  252. simple_snprintf(tempdir, DIRMAX, "./");
  253. }
  254. return OK;
  255. #else
  256. looking = 1;
  257. /* If this is a hub, use, "./tmp/" */
  258. if (conf.bots && conf.bots->nick && conf.bots->hub) {
  259. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  260. if (check_tempdir(0)) {
  261. looking = 0;
  262. return OK;
  263. }
  264. }
  265. /* The dirs we WANT to use aren't accessible, try a random one instead to get the job done. */
  266. clear_tmpdir = 0;
  267. char *dirs[] = {
  268. "/tmp/",
  269. "/usr/tmp/",
  270. "/var/tmp/",
  271. "./",
  272. NULL
  273. };
  274. for (int i = 0; dirs[i]; i++) {
  275. if (!realpath(dirs[i], tempdir))
  276. continue;
  277. tempdir[(DIRMAX < PATH_MAX ? DIRMAX : PATH_MAX) - 1] = 0;
  278. size_t len = strlen(tempdir);
  279. tempdir[len] = '/';
  280. tempdir[len + 1] = '\0';
  281. if (check_tempdir(0)) {
  282. looking = 0;
  283. return OK;
  284. }
  285. }
  286. return ERROR;
  287. #endif /* CYGWIN_HACKS */
  288. }