1 /* stat-related time functions.
3 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
27 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
28 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
29 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
30 if available. ST_XTIM can be st_atim, st_ctim, or st_mtim for
31 access, status change, or data modification time, respectively.
33 These macros are private to stat-time.h. */
34 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
35 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
36 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
38 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
40 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
41 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
42 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
43 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
44 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
45 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
48 #if defined HAVE_STRUCT_STAT_ST_BIRTHTIME || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC || defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC || defined HAVE_STRUCT_STAT_ST_SPARE4
49 # define USE_BIRTHTIME 1
55 /* Return the nanosecond component of *ST's access time. */
56 static inline long int
57 get_stat_atime_ns (struct stat const *st)
59 # if defined STAT_TIMESPEC
60 return STAT_TIMESPEC (st, st_atim).tv_nsec;
61 # elif defined STAT_TIMESPEC_NS
62 return STAT_TIMESPEC_NS (st, st_atim);
63 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
64 return st->st_spare1 * 1000;
70 /* Return the nanosecond component of *ST's status change time. */
71 static inline long int
72 get_stat_ctime_ns (struct stat const *st)
74 # if defined STAT_TIMESPEC
75 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
76 # elif defined STAT_TIMESPEC_NS
77 return STAT_TIMESPEC_NS (st, st_ctim);
78 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
79 return st->st_spare3 * 1000;
85 /* Return the nanosecond component of *ST's data modification time. */
86 static inline long int
87 get_stat_mtime_ns (struct stat const *st)
89 # if defined STAT_TIMESPEC
90 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
91 # elif defined STAT_TIMESPEC_NS
92 return STAT_TIMESPEC_NS (st, st_mtim);
93 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
94 return st->st_spare2 * 1000;
100 /* Return the nanosecond component of *ST's birth time. */
101 static inline long int
102 get_stat_birthtime_ns (struct stat const *st)
104 # if defined USE_BIRTHTIME
105 # if defined STAT_TIMESPEC && defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
106 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
107 # elif defined STAT_TIMESPEC_NS && defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_SEC
108 return STAT_TIMESPEC_NS (st, st_birthtim);
109 # elif defined HAVE_STRUCT_STAT_ST_SPARE4
110 /* Cygwin, without __CYGWIN_USE_BIG_TYPES__ */
111 return st->st_spare4[1] * 1000L;
113 /* Birthtime is available, but not at nanosecond resolution. */
117 /* Birthtime is not available, so indicate this in the returned value. */
122 /* Return *ST's access time. */
123 static inline struct timespec
124 get_stat_atime (struct stat const *st)
127 return STAT_TIMESPEC (st, st_atim);
130 t.tv_sec = st->st_atime;
131 t.tv_nsec = get_stat_atime_ns (st);
136 /* Return *ST's status change time. */
137 static inline struct timespec
138 get_stat_ctime (struct stat const *st)
141 return STAT_TIMESPEC (st, st_ctim);
144 t.tv_sec = st->st_ctime;
145 t.tv_nsec = get_stat_ctime_ns (st);
150 /* Return *ST's data modification time. */
151 static inline struct timespec
152 get_stat_mtime (struct stat const *st)
155 return STAT_TIMESPEC (st, st_mtim);
158 t.tv_sec = st->st_mtime;
159 t.tv_nsec = get_stat_mtime_ns (st);
164 /* Return *ST's birth time, if available, in *PTS. A nonzero value is
165 * returned if the stat structure appears to indicate that the
166 * timestamp is available.
168 * The return value of this function does not reliably indicate that the
169 * returned data is valid; see the comments within the body of the
170 * function for an explanation.
173 get_stat_birthtime (struct stat const *st,
174 struct timespec *pts)
176 #if defined USE_BIRTHTIME
177 # ifdef STAT_TIMESPEC
178 *pts = STAT_TIMESPEC (st, st_birthtim);
181 pts->tv_sec = st->st_birthtime;
182 pts->tv_nsec = get_stat_birthtime_ns (st);
185 /* NetBSD sometimes signals the absence of knowledge of the file's
186 * birth time by using zero. We indicate we don't know, by
187 * returning 0 from this function when that happens. This is
188 * slightly problematic since (time_t)0 is otherwise a valid, albeit
189 * unlikely, timestamp.
191 * NetBSD sometimes returns 0 for unknown values (for example on
192 * ffs) and sometimes begative values for tv_nsec (for example on
193 * NFS). For some filesystems (e.g. msdos) NetBSD also appears to
194 * fail to update the st_birthtime member at all, and just leaves in
195 * there whatever junk existed int he uninitialised stat structure
196 * the caller provided. Therefore, callers are advised to initialise
197 * the tv_nsec number to a negative value before they call stat in
198 * order to detect this problem.
200 if (pts->tv_sec == (time_t)0)
202 return 0; /* result probably invalid, see above. */
206 /* Sometimes NetBSD returns junk in the birth time fields, so
207 * do a simple range check on the data, and return 0 to indicate
208 * that the data is invalid if it just looks wrong.
210 return (pts->tv_nsec >= 0) && (pts->tv_nsec <= 1000000000);
212 #elif (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
213 /* Woe32 native platforms (mingw, msvc, but not Cygwin) put the
214 * "file creation time" in st_ctime (!). See for example the
216 * <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>
218 pts->tv_sec = st->st_ctime;
220 return 1; /* result is valid */
222 /* Birth time not supported. */
225 return 0; /* result is not valid */