rename.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* rename.c -- rename a file, preserving symlinks.
  2. Copyright (C) 1999-2022 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. #if defined HAVE_UTIMES
  20. #include <sys/time.h>
  21. #elif defined HAVE_GOOD_UTIME_H
  22. #include <utime.h>
  23. #endif
  24. /* The number of bytes to copy at once. */
  25. #define COPY_BUF 8192
  26. /* Copy file FROMFD to file TO, performing no translations.
  27. Return 0 if ok, -1 if error. */
  28. static int
  29. simple_copy (int fromfd, const char *to,
  30. struct stat *target_stat ATTRIBUTE_UNUSED)
  31. {
  32. int tofd, nread;
  33. int saved;
  34. char buf[COPY_BUF];
  35. if (fromfd < 0
  36. || lseek (fromfd, 0, SEEK_SET) != 0)
  37. return -1;
  38. tofd = open (to, O_WRONLY | O_TRUNC | O_BINARY);
  39. if (tofd < 0)
  40. {
  41. saved = errno;
  42. close (fromfd);
  43. errno = saved;
  44. return -1;
  45. }
  46. while ((nread = read (fromfd, buf, sizeof buf)) > 0)
  47. {
  48. if (write (tofd, buf, nread) != nread)
  49. {
  50. saved = errno;
  51. close (fromfd);
  52. close (tofd);
  53. errno = saved;
  54. return -1;
  55. }
  56. }
  57. saved = errno;
  58. #if !defined (_WIN32) || defined (__CYGWIN32__)
  59. /* Writing to a setuid/setgid file may clear S_ISUID and S_ISGID.
  60. Try to restore them, ignoring failure. */
  61. if (target_stat != NULL)
  62. fchmod (tofd, target_stat->st_mode);
  63. #endif
  64. close (fromfd);
  65. close (tofd);
  66. if (nread < 0)
  67. {
  68. errno = saved;
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. /* The following defines and inline functions are copied from gnulib.
  74. FIXME: Use a gnulib import and stat-time.h instead. */
  75. #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
  76. # if defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
  77. # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
  78. # else
  79. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
  80. # endif
  81. #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
  82. # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
  83. #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
  84. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
  85. #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
  86. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
  87. #endif
  88. static inline long int get_stat_atime_ns (struct stat const *) ATTRIBUTE_UNUSED;
  89. static inline long int get_stat_mtime_ns (struct stat const *) ATTRIBUTE_UNUSED;
  90. /* Return the nanosecond component of *ST's access time. */
  91. static inline long int
  92. get_stat_atime_ns (struct stat const *st ATTRIBUTE_UNUSED)
  93. {
  94. # if defined STAT_TIMESPEC
  95. return STAT_TIMESPEC (st, st_atim).tv_nsec;
  96. # elif defined STAT_TIMESPEC_NS
  97. return STAT_TIMESPEC_NS (st, st_atim);
  98. # else
  99. return 0;
  100. # endif
  101. }
  102. /* Return the nanosecond component of *ST's data modification time. */
  103. static inline long int
  104. get_stat_mtime_ns (struct stat const *st ATTRIBUTE_UNUSED)
  105. {
  106. # if defined STAT_TIMESPEC
  107. return STAT_TIMESPEC (st, st_mtim).tv_nsec;
  108. # elif defined STAT_TIMESPEC_NS
  109. return STAT_TIMESPEC_NS (st, st_mtim);
  110. # else
  111. return 0;
  112. # endif
  113. }
  114. #if defined HAVE_UTIMENSAT
  115. /* Return *ST's access time. */
  116. static inline struct timespec
  117. get_stat_atime (struct stat const *st)
  118. {
  119. #ifdef STAT_TIMESPEC
  120. return STAT_TIMESPEC (st, st_atim);
  121. #else
  122. struct timespec t;
  123. t.tv_sec = st->st_atime;
  124. t.tv_nsec = get_stat_atime_ns (st);
  125. return t;
  126. #endif
  127. }
  128. /* Return *ST's data modification time. */
  129. static inline struct timespec
  130. get_stat_mtime (struct stat const *st)
  131. {
  132. #ifdef STAT_TIMESPEC
  133. return STAT_TIMESPEC (st, st_mtim);
  134. #else
  135. struct timespec t;
  136. t.tv_sec = st->st_mtime;
  137. t.tv_nsec = get_stat_mtime_ns (st);
  138. return t;
  139. #endif
  140. }
  141. #endif
  142. /* End FIXME. */
  143. /* Set the times of the file DESTINATION to be the same as those in
  144. STATBUF. */
  145. void
  146. set_times (const char *destination, const struct stat *statbuf)
  147. {
  148. int result;
  149. #if defined HAVE_UTIMENSAT
  150. struct timespec times[2];
  151. times[0] = get_stat_atime (statbuf);
  152. times[1] = get_stat_mtime (statbuf);
  153. result = utimensat (AT_FDCWD, destination, times, 0);
  154. #elif defined HAVE_UTIMES
  155. struct timeval tv[2];
  156. tv[0].tv_sec = statbuf->st_atime;
  157. tv[0].tv_usec = get_stat_atime_ns (statbuf) / 1000;
  158. tv[1].tv_sec = statbuf->st_mtime;
  159. tv[1].tv_usec = get_stat_mtime_ns (statbuf) / 1000;
  160. result = utimes (destination, tv);
  161. #elif defined HAVE_GOOD_UTIME_H
  162. struct utimbuf tb;
  163. tb.actime = statbuf->st_atime;
  164. tb.modtime = statbuf->st_mtime;
  165. result = utime (destination, &tb);
  166. #else
  167. long tb[2];
  168. tb[0] = statbuf->st_atime;
  169. tb[1] = statbuf->st_mtime;
  170. result = utime (destination, tb);
  171. #endif
  172. if (result != 0)
  173. non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
  174. }
  175. /* Copy FROM to TO. TARGET_STAT has the file status that, if non-NULL,
  176. is used to fix up timestamps. Return 0 if ok, -1 if error.
  177. At one time this function renamed files, but file permissions are
  178. tricky to update given the number of different schemes used by
  179. various systems. So now we just copy. */
  180. int
  181. smart_rename (const char *from, const char *to, int fromfd,
  182. struct stat *target_stat, bool preserve_dates)
  183. {
  184. int ret = 0;
  185. if (to != from)
  186. {
  187. ret = simple_copy (fromfd, to, target_stat);
  188. if (ret != 0)
  189. non_fatal (_("unable to copy file '%s'; reason: %s"),
  190. to, strerror (errno));
  191. unlink (from);
  192. }
  193. if (preserve_dates)
  194. set_times (to, target_stat);
  195. return ret;
  196. }