misc_file.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * misc.c -- handles:
  3. * copyfile() movefile()
  4. *
  5. */
  6. #include "common.h"
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include "stat.h"
  12. #include "misc_file.h"
  13. #include "main.h"
  14. #include "shell.h"
  15. /* Copy a file from one place to another (possibly erasing old copy).
  16. *
  17. * returns: 0 if OK
  18. * 1 if can't open original file
  19. * 2 if can't open new file
  20. * 3 if original file isn't normal
  21. * 4 if ran out of disk space
  22. */
  23. int copyfile(const char *oldpath, const char *newpath)
  24. {
  25. int fi;
  26. #ifndef CYGWIN_HACKS
  27. fi = open(oldpath, O_RDONLY, 0);
  28. #else
  29. fi = open(oldpath, O_RDONLY | O_BINARY, 0);
  30. #endif
  31. if (fi < 0)
  32. return 1;
  33. struct stat st;
  34. fstat(fi, &st);
  35. if (!(st.st_mode & S_IFREG))
  36. return 3;
  37. int fo;
  38. fo = creat(newpath, (int) (st.st_mode & 0600));
  39. if (fo < 0) {
  40. close(fi);
  41. return 2;
  42. }
  43. char buf[512] = "";
  44. for (int x = 1; x > 0;) {
  45. x = read(fi, buf, 512);
  46. if (x > 0) {
  47. if (write(fo, buf, x) < x) { /* Couldn't write */
  48. close(fo);
  49. close(fi);
  50. unlink(newpath);
  51. return 4;
  52. }
  53. }
  54. }
  55. #ifdef HAVE_FSYNC
  56. fsync(fo);
  57. #endif /* HAVE_FSYNC */
  58. close(fo);
  59. close(fi);
  60. return 0;
  61. }
  62. int movefile(const char *oldpath, const char *newpath)
  63. {
  64. #ifdef HAVE_RENAME
  65. /* Try to use rename first */
  66. if (!rename(oldpath, newpath))
  67. return 0;
  68. #endif /* HAVE_RENAME */
  69. /* If that fails, fall back to just copying and then
  70. * deleting the file.
  71. */
  72. int ret = copyfile(oldpath, newpath);
  73. if (!ret)
  74. unlink(oldpath);
  75. return ret;
  76. }
  77. int is_file(const char *s)
  78. {
  79. struct stat ss;
  80. int i = stat(s, &ss);
  81. if (i < 0)
  82. return 0;
  83. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  84. return 1;
  85. return 0;
  86. }
  87. int can_stat(const char *s)
  88. {
  89. struct stat ss;
  90. int i = stat(s, &ss);
  91. if (i < 0)
  92. return 0;
  93. return 1;
  94. }
  95. int can_lstat(const char *s)
  96. {
  97. struct stat ss;
  98. int i = lstat(s, &ss);
  99. if (i < 0)
  100. return 0;
  101. return 1;
  102. }
  103. int is_symlink(const char *s)
  104. {
  105. struct stat ss;
  106. int i = lstat(s, &ss);
  107. if (i < 0)
  108. return 0;
  109. if (ss.st_mode & S_IFLNK)
  110. return 1;
  111. return 0;
  112. }
  113. int is_dir(const char *s)
  114. {
  115. struct stat ss;
  116. int i = stat(s, &ss);
  117. if (i < 0)
  118. return 0;
  119. if (ss.st_mode & S_IFDIR)
  120. return 1;
  121. return 0;
  122. }
  123. int fixmod(const char *s)
  124. {
  125. #ifndef CYGWIN_HACKS
  126. if (!can_stat(s))
  127. return 1;
  128. return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
  129. #else
  130. return 0;
  131. #endif /* !CYGWIN_HACKS */
  132. }
  133. Tempfile::Tempfile()
  134. {
  135. len = strlen(tempdir) + 1 + 6 + 1;
  136. file = new char[len];
  137. sprintf(file, "%s.XXXXXX", tempdir);
  138. MakeTemp();
  139. }
  140. Tempfile::Tempfile(const char *prefix)
  141. {
  142. len = strlen(tempdir) + 1 + strlen(prefix) + 1 + 6 + 1;
  143. file = new char[len];
  144. sprintf(file, "%s.%s-XXXXXX", tempdir, prefix);
  145. MakeTemp();
  146. }
  147. void Tempfile::MakeTemp()
  148. {
  149. if ((fd = mkstemp(file)) < 0) {
  150. f = NULL;
  151. goto error;
  152. }
  153. if ((f = fdopen(fd, "w+b")) == NULL)
  154. goto error;
  155. fchmod(fd, S_IRUSR | S_IWUSR);
  156. return;
  157. error:
  158. putlog(LOG_ERRORS, "*", "Couldn't create temporary file '%s': %s", file, strerror(errno));
  159. delete this;
  160. fatal("Cannot create tempory file!", 0);
  161. }
  162. Tempfile::~Tempfile()
  163. {
  164. unlink(file);
  165. if (f)
  166. fclose(f);
  167. else
  168. close(fd);
  169. delete[] file;
  170. }
  171. void check_tempdir()
  172. {
  173. if (!can_stat(tempdir)) {
  174. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR)) {
  175. unlink(tempdir);
  176. if (!can_stat(tempdir))
  177. if (mkdir(tempdir, S_IRUSR | S_IWUSR | S_IXUSR))
  178. werr(ERR_TMPSTAT);
  179. }
  180. }
  181. if (fixmod(tempdir))
  182. werr(ERR_TMPMOD);
  183. /* test tempdir: it's vital */
  184. Tempfile *testdir = new Tempfile("test");
  185. int result;
  186. fprintf(testdir->f, "\n");
  187. result = fflush(testdir->f);
  188. delete testdir;
  189. if (result)
  190. fatal(strerror(errno), 0);
  191. }