misc_file.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /* Copy a file from one place to another (possibly erasing old copy).
  13. *
  14. * returns: 0 if OK
  15. * 1 if can't open original file
  16. * 2 if can't open new file
  17. * 3 if original file isn't normal
  18. * 4 if ran out of disk space
  19. */
  20. int copyfile(const char *oldpath, const char *newpath)
  21. {
  22. int fi, fo, x;
  23. char buf[512] = "";
  24. struct stat st;
  25. #ifndef CYGWIN_HACKS
  26. fi = open(oldpath, O_RDONLY, 0);
  27. #else
  28. fi = open(oldpath, O_RDONLY | O_BINARY, 0);
  29. #endif
  30. if (fi < 0)
  31. return 1;
  32. fstat(fi, &st);
  33. if (!(st.st_mode & S_IFREG))
  34. return 3;
  35. fo = creat(newpath, (int) (st.st_mode & 0600));
  36. if (fo < 0) {
  37. close(fi);
  38. return 2;
  39. }
  40. for (x = 1; x > 0;) {
  41. x = read(fi, buf, 512);
  42. if (x > 0) {
  43. if (write(fo, buf, x) < x) { /* Couldn't write */
  44. close(fo);
  45. close(fi);
  46. unlink(newpath);
  47. return 4;
  48. }
  49. }
  50. }
  51. #ifdef HAVE_FSYNC
  52. fsync(fo);
  53. #endif /* HAVE_FSYNC */
  54. close(fo);
  55. close(fi);
  56. return 0;
  57. }
  58. int movefile(const char *oldpath, const char *newpath)
  59. {
  60. int ret;
  61. #ifdef HAVE_RENAME
  62. /* Try to use rename first */
  63. if (!rename(oldpath, newpath))
  64. return 0;
  65. #endif /* HAVE_RENAME */
  66. /* If that fails, fall back to just copying and then
  67. * deleting the file.
  68. */
  69. ret = copyfile(oldpath, newpath);
  70. if (!ret)
  71. unlink(oldpath);
  72. return ret;
  73. }
  74. int is_file(const char *s)
  75. {
  76. struct stat ss;
  77. int i = stat(s, &ss);
  78. if (i < 0)
  79. return 0;
  80. if ((ss.st_mode & S_IFREG) || (ss.st_mode & S_IFLNK))
  81. return 1;
  82. return 0;
  83. }
  84. int can_stat(const char *s)
  85. {
  86. struct stat ss;
  87. int i = stat(s, &ss);
  88. if (i < 0)
  89. return 0;
  90. return 1;
  91. }
  92. int can_lstat(const char *s)
  93. {
  94. struct stat ss;
  95. int i = lstat(s, &ss);
  96. if (i < 0)
  97. return 0;
  98. return 1;
  99. }
  100. int is_symlink(const char *s)
  101. {
  102. struct stat ss;
  103. int i = lstat(s, &ss);
  104. if (i < 0)
  105. return 0;
  106. if (ss.st_mode & S_IFLNK)
  107. return 1;
  108. return 0;
  109. }
  110. int is_dir(const char *s)
  111. {
  112. struct stat ss;
  113. int i = stat(s, &ss);
  114. if (i < 0)
  115. return 0;
  116. if (ss.st_mode & S_IFDIR)
  117. return 1;
  118. return 0;
  119. }
  120. int fixmod(const char *s)
  121. {
  122. #ifndef CYGWIN_HACKS
  123. if (!can_stat(s))
  124. return 1;
  125. return chmod(s, S_IRUSR | S_IWUSR | S_IXUSR);
  126. #else
  127. return 0;
  128. #endif /* !CYGWIN_HACKS */
  129. }