misc_file.c 6.4 KB

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