2007-03-27 Bruno Haible <bruno@clisp.org>
[pspp] / lib / stat-time.h
1 /* stat-related time functions.
2
3    Copyright (C) 2005, 2007 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #ifndef STAT_TIME_H
22 #define STAT_TIME_H 1
23
24 #include <sys/stat.h>
25 #include <time.h>
26
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.
32
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)
37 # else
38 #  define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
39 # endif
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)
46 #endif
47
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
50 #else
51 # undef USE_BIRTHTIME
52 #endif
53
54
55 /* Return the nanosecond component of *ST's access time.  */
56 static inline long int
57 get_stat_atime_ns (struct stat const *st)
58 {
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;
65 # else
66   return 0;
67 # endif
68 }
69
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)
73 {
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;
80 # else
81   return 0;
82 # endif
83 }
84
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)
88 {
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;
95 # else
96   return 0;
97 # endif
98 }
99
100 /* Return the nanosecond component of *ST's birth time.  */
101 static inline long int
102 get_stat_birthtime_ns (struct stat const *st)
103 {
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;
112 #  else
113   /* Birthtime is available, but not at nanosecond resolution.  */
114   return 0;
115 #  endif
116 # else
117   /* Birthtime is not available, so indicate this in the returned value.  */
118   return 0;
119 # endif 
120 }
121
122 /* Return *ST's access time.  */
123 static inline struct timespec
124 get_stat_atime (struct stat const *st)
125 {
126 #ifdef STAT_TIMESPEC
127   return STAT_TIMESPEC (st, st_atim);
128 #else
129   struct timespec t;
130   t.tv_sec = st->st_atime;
131   t.tv_nsec = get_stat_atime_ns (st);
132   return t;
133 #endif
134 }
135
136 /* Return *ST's status change time.  */
137 static inline struct timespec
138 get_stat_ctime (struct stat const *st)
139 {
140 #ifdef STAT_TIMESPEC
141   return STAT_TIMESPEC (st, st_ctim);
142 #else
143   struct timespec t;
144   t.tv_sec = st->st_ctime;
145   t.tv_nsec = get_stat_ctime_ns (st);
146   return t;
147 #endif
148 }
149
150 /* Return *ST's data modification time.  */
151 static inline struct timespec
152 get_stat_mtime (struct stat const *st)
153 {
154 #ifdef STAT_TIMESPEC
155   return STAT_TIMESPEC (st, st_mtim);
156 #else
157   struct timespec t;
158   t.tv_sec = st->st_mtime;
159   t.tv_nsec = get_stat_mtime_ns (st);
160   return t;
161 #endif
162 }
163
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.
167  *
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.
171  */
172 static inline int
173 get_stat_birthtime (struct stat const *st,
174                     struct timespec *pts)
175 {
176 #if defined USE_BIRTHTIME
177 # ifdef STAT_TIMESPEC
178   *pts = STAT_TIMESPEC (st, st_birthtim);
179 # else
180   struct timespec t;
181   pts->tv_sec = st->st_birthtime;
182   pts->tv_nsec = get_stat_birthtime_ns (st);
183 # endif
184
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.  
190    *
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.
199    */
200   if (pts->tv_sec == (time_t)0)
201     {
202       return 0;                 /* result probably invalid, see above. */
203     }
204   else
205     {
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. 
209        */
210       return (pts->tv_nsec >= 0) && (pts->tv_nsec <= 1000000000);
211     }
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
215    * article
216    * <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>
217    */
218   pts->tv_sec = st->st_ctime;
219   pts->tv_nsec = 0;
220   return 1;                     /* result is valid */
221 #else
222   /* Birth time not supported.  */
223   pts->tv_sec = 0;
224   pts->tv_nsec = 0;
225   return 0;                     /* result is not valid */
226 #endif
227 }
228
229 #endif