blob: e04cc619ee2333fc873ee79fae573cda86575a85 [file] [log] [blame]
Chet Rameyac50fba2014-02-26 09:36:43 -05001/* stat-related time functions.
2
3 Copyright (C) 2005, 2007, 2009-2012 Free Software Foundation, Inc.
4
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 3 of the License, or
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18/* Written by Paul Eggert. */
19
20#ifndef STAT_TIME_H
21#define STAT_TIME_H 1
22
23#include <sys/stat.h>
24
25#if defined (TIME_H_DEFINES_STRUCT_TIMESPEC)
26# include <time.h>
27#elif defined (SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)
28# include <sys/time.h>
29#elif defined (PTHREAD_H_DEFINES_STRUCT_TIMESPEC)
30# include <pthread.h>
31#endif
32
33#ifndef HAVE_STRUCT_TIMESPEC
34struct timespec
35{
36 time_t tv_sec;
37 long int tv_nsec;
38};
39#endif
40
41/* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
42 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
43 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
44 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
45 for access, status change, data modification, or birth (creation)
46 time respectively.
47
48 These macros are private to stat-time.h. */
49#if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
50# ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
51# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
52# else
53# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
54# endif
55#elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
56# define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
57#elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
58# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
59#elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
60# define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
61#endif
62
63/* Return the nanosecond component of *ST's access time. */
64static inline long int
65get_stat_atime_ns (struct stat const *st)
66{
67# if defined STAT_TIMESPEC
68 return STAT_TIMESPEC (st, st_atim).tv_nsec;
69# elif defined STAT_TIMESPEC_NS
70 return STAT_TIMESPEC_NS (st, st_atim);
71# else
72 return 0;
73# endif
74}
75
76/* Return the nanosecond component of *ST's status change time. */
77static inline long int
78get_stat_ctime_ns (struct stat const *st)
79{
80# if defined STAT_TIMESPEC
81 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
82# elif defined STAT_TIMESPEC_NS
83 return STAT_TIMESPEC_NS (st, st_ctim);
84# else
85 return 0;
86# endif
87}
88
89/* Return the nanosecond component of *ST's data modification time. */
90static inline long int
91get_stat_mtime_ns (struct stat const *st)
92{
93# if defined STAT_TIMESPEC
94 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
95# elif defined STAT_TIMESPEC_NS
96 return STAT_TIMESPEC_NS (st, st_mtim);
97# else
98 return 0;
99# endif
100}
101
102/* Return the nanosecond component of *ST's birth time. */
103static inline long int
104get_stat_birthtime_ns (struct stat const *st)
105{
106# if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
107 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
108# elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
109 return STAT_TIMESPEC_NS (st, st_birthtim);
110# else
111 /* Avoid a "parameter unused" warning. */
112 (void) st;
113 return 0;
114# endif
115}
116
117/* Return *ST's access time. */
118static inline struct timespec
119get_stat_atime (struct stat const *st)
120{
121#ifdef STAT_TIMESPEC
122 return STAT_TIMESPEC (st, st_atim);
123#else
124 struct timespec t;
125 t.tv_sec = st->st_atime;
126 t.tv_nsec = get_stat_atime_ns (st);
127 return t;
128#endif
129}
130
131/* Return *ST's status change time. */
132static inline struct timespec
133get_stat_ctime (struct stat const *st)
134{
135#ifdef STAT_TIMESPEC
136 return STAT_TIMESPEC (st, st_ctim);
137#else
138 struct timespec t;
139 t.tv_sec = st->st_ctime;
140 t.tv_nsec = get_stat_ctime_ns (st);
141 return t;
142#endif
143}
144
145/* Return *ST's data modification time. */
146static inline struct timespec
147get_stat_mtime (struct stat const *st)
148{
149#ifdef STAT_TIMESPEC
150 return STAT_TIMESPEC (st, st_mtim);
151#else
152 struct timespec t;
153 t.tv_sec = st->st_mtime;
154 t.tv_nsec = get_stat_mtime_ns (st);
155 return t;
156#endif
157}
158
159static inline int
160timespec_cmp (struct timespec a, struct timespec b)
161{
162 return (a.tv_sec < b.tv_sec
163 ? -1
164 : (a.tv_sec > b.tv_sec
165 ? 1
166 : (int) (a.tv_nsec - b.tv_nsec)));
167}
168
169/* Return *ST's birth time, if available; otherwise return a value
170 with tv_sec and tv_nsec both equal to -1. */
171static inline struct timespec
172get_stat_birthtime (struct stat const *st)
173{
174 struct timespec t;
175
176#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
177 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
178 t = STAT_TIMESPEC (st, st_birthtim);
179#elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
180 t.tv_sec = st->st_birthtime;
181 t.tv_nsec = st->st_birthtimensec;
182#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
183 /* Native Windows platforms (but not Cygwin) put the "file creation
184 time" in st_ctime (!). See
185 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
186 t.tv_sec = st->st_ctime;
187 t.tv_nsec = 0;
188#else
189 /* Birth time is not supported. */
190 t.tv_sec = -1;
191 t.tv_nsec = -1;
192 /* Avoid a "parameter unused" warning. */
193 (void) st;
194#endif
195
196#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
197 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
198 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
199 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
200 using zero. Attempt to work around this problem. Alas, this can
201 report failure even for valid time stamps. Also, NetBSD
202 sometimes returns junk in the birth time fields; work around this
203 bug if it is detected. */
204 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
205 {
206 t.tv_sec = -1;
207 t.tv_nsec = -1;
208 }
209#endif
210
211 return t;
212}
213
214#endif