stat-time.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* stat-related time functions.
  2. Copyright (C) 2005, 2007, 2009-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #ifndef STAT_TIME_H
  15. #define STAT_TIME_H 1
  16. #include <sys/stat.h>
  17. #include <time.h>
  18. #ifndef _GL_INLINE_HEADER_BEGIN
  19. #error "Please include config.h first."
  20. #endif
  21. _GL_INLINE_HEADER_BEGIN
  22. #ifndef _GL_STAT_TIME_INLINE
  23. # define _GL_STAT_TIME_INLINE _GL_INLINE
  24. #endif
  25. /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
  26. struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
  27. ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
  28. if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
  29. for access, status change, data modification, or birth (creation)
  30. time respectively.
  31. These macros are private to stat-time.h. */
  32. #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
  33. # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
  34. # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
  35. # else
  36. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
  37. # endif
  38. #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
  39. # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
  40. #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
  41. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
  42. #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
  43. # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
  44. #endif
  45. /* Return the nanosecond component of *ST's access time. */
  46. _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
  47. get_stat_atime_ns (struct stat const *st)
  48. {
  49. # if defined STAT_TIMESPEC
  50. return STAT_TIMESPEC (st, st_atim).tv_nsec;
  51. # elif defined STAT_TIMESPEC_NS
  52. return STAT_TIMESPEC_NS (st, st_atim);
  53. # else
  54. return 0;
  55. # endif
  56. }
  57. /* Return the nanosecond component of *ST's status change time. */
  58. _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
  59. get_stat_ctime_ns (struct stat const *st)
  60. {
  61. # if defined STAT_TIMESPEC
  62. return STAT_TIMESPEC (st, st_ctim).tv_nsec;
  63. # elif defined STAT_TIMESPEC_NS
  64. return STAT_TIMESPEC_NS (st, st_ctim);
  65. # else
  66. return 0;
  67. # endif
  68. }
  69. /* Return the nanosecond component of *ST's data modification time. */
  70. _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
  71. get_stat_mtime_ns (struct stat const *st)
  72. {
  73. # if defined STAT_TIMESPEC
  74. return STAT_TIMESPEC (st, st_mtim).tv_nsec;
  75. # elif defined STAT_TIMESPEC_NS
  76. return STAT_TIMESPEC_NS (st, st_mtim);
  77. # else
  78. return 0;
  79. # endif
  80. }
  81. /* Return the nanosecond component of *ST's birth time. */
  82. _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
  83. get_stat_birthtime_ns (struct stat const *st)
  84. {
  85. # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
  86. return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
  87. # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
  88. return STAT_TIMESPEC_NS (st, st_birthtim);
  89. # else
  90. /* Avoid a "parameter unused" warning. */
  91. (void) st;
  92. return 0;
  93. # endif
  94. }
  95. /* Return *ST's access time. */
  96. _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
  97. get_stat_atime (struct stat const *st)
  98. {
  99. #ifdef STAT_TIMESPEC
  100. return STAT_TIMESPEC (st, st_atim);
  101. #else
  102. struct timespec t;
  103. t.tv_sec = st->st_atime;
  104. t.tv_nsec = get_stat_atime_ns (st);
  105. return t;
  106. #endif
  107. }
  108. /* Return *ST's status change time. */
  109. _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
  110. get_stat_ctime (struct stat const *st)
  111. {
  112. #ifdef STAT_TIMESPEC
  113. return STAT_TIMESPEC (st, st_ctim);
  114. #else
  115. struct timespec t;
  116. t.tv_sec = st->st_ctime;
  117. t.tv_nsec = get_stat_ctime_ns (st);
  118. return t;
  119. #endif
  120. }
  121. /* Return *ST's data modification time. */
  122. _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
  123. get_stat_mtime (struct stat const *st)
  124. {
  125. #ifdef STAT_TIMESPEC
  126. return STAT_TIMESPEC (st, st_mtim);
  127. #else
  128. struct timespec t;
  129. t.tv_sec = st->st_mtime;
  130. t.tv_nsec = get_stat_mtime_ns (st);
  131. return t;
  132. #endif
  133. }
  134. /* Return *ST's birth time, if available; otherwise return a value
  135. with tv_sec and tv_nsec both equal to -1. */
  136. _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
  137. get_stat_birthtime (struct stat const *st)
  138. {
  139. struct timespec t;
  140. #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
  141. || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
  142. t = STAT_TIMESPEC (st, st_birthtim);
  143. #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
  144. t.tv_sec = st->st_birthtime;
  145. t.tv_nsec = st->st_birthtimensec;
  146. #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  147. /* Native Windows platforms (but not Cygwin) put the "file creation
  148. time" in st_ctime (!). See
  149. <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
  150. t.tv_sec = st->st_ctime;
  151. t.tv_nsec = 0;
  152. #else
  153. /* Birth time is not supported. */
  154. t.tv_sec = -1;
  155. t.tv_nsec = -1;
  156. /* Avoid a "parameter unused" warning. */
  157. (void) st;
  158. #endif
  159. #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
  160. || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
  161. || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
  162. /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
  163. using zero. Attempt to work around this problem. Alas, this can
  164. report failure even for valid timestamps. Also, NetBSD
  165. sometimes returns junk in the birth time fields; work around this
  166. bug if it is detected. */
  167. if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
  168. {
  169. t.tv_sec = -1;
  170. t.tv_nsec = -1;
  171. }
  172. #endif
  173. return t;
  174. }
  175. _GL_INLINE_HEADER_END
  176. #endif