rename.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* rename.c -- rename a file, preserving symlinks.
  2. Copyright (C) 1999-2017 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bucomm.h"
  19. #ifdef HAVE_GOOD_UTIME_H
  20. #include <utime.h>
  21. #else /* ! HAVE_GOOD_UTIME_H */
  22. #ifdef HAVE_UTIMES
  23. #include <sys/time.h>
  24. #endif /* HAVE_UTIMES */
  25. #endif /* ! HAVE_GOOD_UTIME_H */
  26. #if ! defined (_WIN32) || defined (__CYGWIN32__)
  27. static int simple_copy (const char *, const char *);
  28. /* The number of bytes to copy at once. */
  29. #define COPY_BUF 8192
  30. /* Copy file FROM to file TO, performing no translations.
  31. Return 0 if ok, -1 if error. */
  32. static int
  33. simple_copy (const char *from, const char *to)
  34. {
  35. int fromfd, tofd, nread;
  36. int saved;
  37. char buf[COPY_BUF];
  38. fromfd = open (from, O_RDONLY | O_BINARY);
  39. if (fromfd < 0)
  40. return -1;
  41. #ifdef O_CREAT
  42. tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
  43. #else
  44. tofd = creat (to, 0777);
  45. #endif
  46. if (tofd < 0)
  47. {
  48. saved = errno;
  49. close (fromfd);
  50. errno = saved;
  51. return -1;
  52. }
  53. while ((nread = read (fromfd, buf, sizeof buf)) > 0)
  54. {
  55. if (write (tofd, buf, nread) != nread)
  56. {
  57. saved = errno;
  58. close (fromfd);
  59. close (tofd);
  60. errno = saved;
  61. return -1;
  62. }
  63. }
  64. saved = errno;
  65. close (fromfd);
  66. close (tofd);
  67. if (nread < 0)
  68. {
  69. errno = saved;
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. #endif /* __CYGWIN32__ or not _WIN32 */
  75. /* Set the times of the file DESTINATION to be the same as those in
  76. STATBUF. */
  77. void
  78. set_times (const char *destination, const struct stat *statbuf)
  79. {
  80. int result;
  81. {
  82. #ifdef HAVE_GOOD_UTIME_H
  83. struct utimbuf tb;
  84. tb.actime = statbuf->st_atime;
  85. tb.modtime = statbuf->st_mtime;
  86. result = utime (destination, &tb);
  87. #else /* ! HAVE_GOOD_UTIME_H */
  88. #ifndef HAVE_UTIMES
  89. long tb[2];
  90. tb[0] = statbuf->st_atime;
  91. tb[1] = statbuf->st_mtime;
  92. result = utime (destination, tb);
  93. #else /* HAVE_UTIMES */
  94. struct timeval tv[2];
  95. tv[0].tv_sec = statbuf->st_atime;
  96. tv[0].tv_usec = 0;
  97. tv[1].tv_sec = statbuf->st_mtime;
  98. tv[1].tv_usec = 0;
  99. result = utimes (destination, tv);
  100. #endif /* HAVE_UTIMES */
  101. #endif /* ! HAVE_GOOD_UTIME_H */
  102. }
  103. if (result != 0)
  104. non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
  105. }
  106. #ifndef S_ISLNK
  107. #ifdef S_IFLNK
  108. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  109. #else
  110. #define S_ISLNK(m) 0
  111. #define lstat stat
  112. #endif
  113. #endif
  114. /* Rename FROM to TO, copying if TO is a link.
  115. Return 0 if ok, -1 if error. */
  116. int
  117. smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
  118. {
  119. bfd_boolean exists;
  120. struct stat s;
  121. int ret = 0;
  122. exists = lstat (to, &s) == 0;
  123. #if defined (_WIN32) && !defined (__CYGWIN32__)
  124. /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
  125. fail instead. Also, chown is not present. */
  126. if (exists)
  127. remove (to);
  128. ret = rename (from, to);
  129. if (ret != 0)
  130. {
  131. /* We have to clean up here. */
  132. non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
  133. unlink (from);
  134. }
  135. #else
  136. /* Use rename only if TO is not a symbolic link and has
  137. only one hard link, and we have permission to write to it. */
  138. if (! exists
  139. || (!S_ISLNK (s.st_mode)
  140. && S_ISREG (s.st_mode)
  141. && (s.st_mode & S_IWUSR)
  142. && s.st_nlink == 1)
  143. )
  144. {
  145. ret = rename (from, to);
  146. if (ret == 0)
  147. {
  148. if (exists)
  149. {
  150. /* Try to preserve the permission bits and ownership of
  151. TO. First get the mode right except for the setuid
  152. bit. Then change the ownership. Then fix the setuid
  153. bit. We do the chmod before the chown because if the
  154. chown succeeds, and we are a normal user, we won't be
  155. able to do the chmod afterward. We don't bother to
  156. fix the setuid bit first because that might introduce
  157. a fleeting security problem, and because the chown
  158. will clear the setuid bit anyhow. We only fix the
  159. setuid bit if the chown succeeds, because we don't
  160. want to introduce an unexpected setuid file owned by
  161. the user running objcopy. */
  162. chmod (to, s.st_mode & 0777);
  163. if (chown (to, s.st_uid, s.st_gid) >= 0)
  164. chmod (to, s.st_mode & 07777);
  165. }
  166. }
  167. else
  168. {
  169. /* We have to clean up here. */
  170. non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
  171. unlink (from);
  172. }
  173. }
  174. else
  175. {
  176. ret = simple_copy (from, to);
  177. if (ret != 0)
  178. non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
  179. if (preserve_dates)
  180. set_times (to, &s);
  181. unlink (from);
  182. }
  183. #endif /* _WIN32 && !__CYGWIN32__ */
  184. return ret;
  185. }