@@ -0,0 +1,569 @@
1+From 55a5b9c8254126d0acef8702526c92a31200a07c Mon Sep 17 00:00:00 2001
2+From: Matthew DeVore <matvore@google.com>
3+Date: Tue, 4 Aug 2020 17:49:42 -0700
4+Subject: [PATCH] lib/util: Standardize use of st_[acm]time ns
5+
6+Commit 810397f89a10, and possibly others, broke the build for macOS and
7+other environments which don't have st_[acm]tim fields on 'struct stat'.
8+
9+Multiple places in the codebase used the config.h values to determine
10+how to access the nanosecond or microsecond values of the stat
11+timestamps, so rather than add more, centralize them all into
12+lib/util/time.c.
13+
14+Also allow pvfs_fileinfo.c to read nanosecond-granularity timestamps on
15+platforms where it didn't before, since its #if branches were not
16+complete.
17+
18+Signed-off-by: Matthew DeVore <matvore@google.com>
19+Reviewed-by: Jeremy Allison <jra@samba.org>
20+Reviewed-by: Volker Lendecke <vl@samba.org>
21+
22+Autobuild-User(master): Volker Lendecke <vl@samba.org>
23+Autobuild-Date(master): Sat Aug 15 08:51:09 UTC 2020 on sn-devel-184
24+---
25+ lib/replace/wscript | 2 -
26+ lib/util/time.c | 230 ++++++++++++++++++++
27+ lib/util/time.h | 18 ++
28+ source3/lib/system.c | 121 +---------
29+ source3/libsmb/libsmb_stat.c | 24 +-
30+ source4/ntvfs/posix/pvfs_fileinfo.c | 11 +-
31+ source4/torture/libsmbclient/libsmbclient.c | 7 +-
32+ 7 files changed, 277 insertions(+), 136 deletions(-)
33+
34+diff --git a/lib/replace/wscript b/lib/replace/wscript
35+index 64f305d6df0..85bc11d2f01 100644
36+--- a/lib/replace/wscript
37++++ b/lib/replace/wscript
38+@@ -746,8 +746,6 @@ def configure(conf):
39+
40+ conf.CHECK_CODE('mkdir("foo",0777)', define='HAVE_MKDIR_MODE', headers='sys/stat.h')
41+
42+- conf.CHECK_STRUCTURE_MEMBER('struct stat', 'st_mtim.tv_nsec', define='HAVE_STAT_TV_NSEC',
43+- headers='sys/stat.h')
44+ # we need the st_rdev test under two names
45+ conf.CHECK_STRUCTURE_MEMBER('struct stat', 'st_rdev',
46+ define='HAVE_STRUCT_STAT_ST_RDEV',
47+diff --git a/lib/util/time.c b/lib/util/time.c
48+index 0fac5e2e397..b5c1d700b23 100644
49+--- a/lib/util/time.c
50++++ b/lib/util/time.c
51+@@ -26,6 +26,10 @@
52+ #include "byteorder.h"
53+ #include "time_basic.h"
54+ #include "lib/util/time.h" /* Avoid /usr/include/time.h */
55++#include <sys/stat.h>
56++#ifndef NO_CONFIG_H
57++#include "config.h"
58++#endif
59+
60+ /**
61+ * @file
62+@@ -1232,3 +1236,229 @@ struct timespec time_t_to_full_timespec(time_t t)
63+ }
64+ return (struct timespec){.tv_sec = t};
65+ }
66++
67++#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
68++
69++/* Old system - no ns timestamp. */
70++time_t get_atimensec(const struct stat *st)
71++{
72++ return 0;
73++}
74++
75++time_t get_mtimensec(const struct stat *st)
76++{
77++ return 0;
78++}
79++
80++time_t get_ctimensec(const struct stat *st)
81++{
82++ return 0;
83++}
84++
85++/* Set does nothing with no ns timestamp. */
86++void set_atimensec(struct stat *st, time_t ns)
87++{
88++ return;
89++}
90++
91++void set_mtimensec(struct stat *st, time_t ns)
92++{
93++ return;
94++}
95++
96++void set_ctimensec(struct stat *st, time_t ns)
97++{
98++ return;
99++}
100++
101++#elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
102++
103++time_t get_atimensec(const struct stat *st)
104++{
105++ return st->st_atimespec.tv_nsec;
106++}
107++
108++time_t get_mtimensec(const struct stat *st)
109++{
110++ return st->st_mtimespec.tv_nsec;
111++}
112++
113++time_t get_ctimensec(const struct stat *st)
114++{
115++ return st->st_ctimespec.tv_nsec;
116++}
117++
118++void set_atimensec(struct stat *st, time_t ns)
119++{
120++ st->st_atimespec.tv_nsec = ns;
121++}
122++
123++void set_mtimensec(struct stat *st, time_t ns)
124++{
125++ st->st_mtimespec.tv_nsec = ns;
126++}
127++
128++void set_ctimensec(struct stat *st, time_t ns)
129++{
130++ st->st_ctimespec.tv_nsec = ns;
131++}
132++
133++#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
134++
135++time_t get_atimensec(const struct stat *st)
136++{
137++ return st->st_atim.tv_nsec;
138++}
139++
140++time_t get_mtimensec(const struct stat *st)
141++{
142++ return st->st_mtim.tv_nsec;
143++}
144++
145++time_t get_ctimensec(const struct stat *st)
146++{
147++ return st->st_ctim.tv_nsec;
148++}
149++
150++void set_atimensec(struct stat *st, time_t ns)
151++{
152++ st->st_atim.tv_nsec = ns;
153++}
154++
155++void set_mtimensec(struct stat *st, time_t ns)
156++{
157++ st->st_mtim.tv_nsec = ns;
158++}
159++void set_ctimensec(struct stat *st, time_t ns)
160++{
161++ st->st_ctim.tv_nsec = ns;
162++}
163++
164++#elif HAVE_STRUCT_STAT_ST_MTIMENSEC
165++
166++time_t get_atimensec(const struct stat *st)
167++{
168++ return st->st_atimensec;
169++}
170++
171++time_t get_mtimensec(const struct stat *st)
172++{
173++ return st->st_mtimensec;
174++}
175++
176++time_t get_ctimensec(const struct stat *st)
177++{
178++ return st->st_ctimensec;
179++}
180++
181++void set_atimensec(struct stat *st, time_t ns)
182++{
183++ st->st_atimensec = ns;
184++}
185++
186++void set_mtimensec(struct stat *st, time_t ns)
187++{
188++ st->st_mtimensec = ns;
189++}
190++
191++void set_ctimensec(struct stat *st, time_t ns)
192++{
193++ st->st_ctimensec = ns;
194++}
195++
196++#elif HAVE_STRUCT_STAT_ST_MTIME_N
197++
198++time_t get_atimensec(const struct stat *st)
199++{
200++ return st->st_atime_n;
201++}
202++
203++time_t get_mtimensec(const struct stat *st)
204++{
205++ return st->st_mtime_n;
206++}
207++
208++time_t get_ctimensec(const struct stat *st)
209++{
210++ return st->st_ctime_n;
211++}
212++
213++void set_atimensec(struct stat *st, time_t ns)
214++{
215++ st->st_atime_n = ns;
216++}
217++
218++void set_mtimensec(struct stat *st, time_t ns)
219++{
220++ st->st_mtime_n = ns;
221++}
222++
223++void set_ctimensec(struct stat *st, time_t ns)
224++{
225++ st->st_ctime_n = ns;
226++}
227++
228++#elif HAVE_STRUCT_STAT_ST_UMTIME
229++
230++/* Only usec timestamps available. Convert to/from nsec. */
231++
232++time_t get_atimensec(const struct stat *st)
233++{
234++ return st->st_uatime * 1000;
235++}
236++
237++time_t get_mtimensec(const struct stat *st)
238++{
239++ return st->st_umtime * 1000;
240++}
241++
242++time_t get_ctimensec(const struct stat *st)
243++{
244++ return st->st_uctime * 1000;
245++}
246++
247++void set_atimensec(struct stat *st, time_t ns)
248++{
249++ st->st_uatime = ns / 1000;
250++}
251++
252++void set_mtimensec(struct stat *st, time_t ns)
253++{
254++ st->st_umtime = ns / 1000;
255++}
256++
257++void set_ctimensec(struct stat *st, time_t ns)
258++{
259++ st->st_uctime = ns / 1000;
260++}
261++
262++#else
263++#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
264++#endif
265++
266++struct timespec get_atimespec(const struct stat *pst)
267++{
268++ struct timespec ret;
269++
270++ ret.tv_sec = pst->st_atime;
271++ ret.tv_nsec = get_atimensec(pst);
272++ return ret;
273++}
274++
275++struct timespec get_mtimespec(const struct stat *pst)
276++{
277++ struct timespec ret;
278++
279++ ret.tv_sec = pst->st_mtime;
280++ ret.tv_nsec = get_mtimensec(pst);
281++ return ret;
282++}
283++
284++struct timespec get_ctimespec(const struct stat *pst)
285++{
286++ struct timespec ret;
287++
288++ ret.tv_sec = pst->st_mtime;
289++ ret.tv_nsec = get_ctimensec(pst);
290++ return ret;
291++}
292+diff --git a/lib/util/time.h b/lib/util/time.h
293+index 4a90b40d5ce..04945b5f25f 100644
294+--- a/lib/util/time.h
295++++ b/lib/util/time.h
296+@@ -375,4 +375,22 @@ time_t full_timespec_to_time_t(const struct timespec *ts);
297+ time_t nt_time_to_full_time_t(NTTIME nt);
298+ struct timespec time_t_to_full_timespec(time_t t);
299+
300++/*
301++ * Functions to get and set the number of nanoseconds for times in a stat field.
302++ * If the stat has timestamp granularity less than nanosecond, then the set_*
303++ * operations will be lossy.
304++ */
305++struct stat;
306++time_t get_atimensec(const struct stat *);
307++time_t get_mtimensec(const struct stat *);
308++time_t get_ctimensec(const struct stat *);
309++void set_atimensec(struct stat *, time_t);
310++void set_mtimensec(struct stat *, time_t);
311++void set_ctimensec(struct stat *, time_t);
312++
313++/* These are convenience wrappers for the above getters. */
314++struct timespec get_atimespec(const struct stat *);
315++struct timespec get_mtimespec(const struct stat *);
316++struct timespec get_ctimespec(const struct stat *);
317++
318+ #endif /* _SAMBA_TIME_H_ */
319+diff --git a/source3/lib/system.c b/source3/lib/system.c
320+index f1265e0c43f..7c8cd19d11f 100644
321+--- a/source3/lib/system.c
322++++ b/source3/lib/system.c
323+@@ -25,7 +25,8 @@
324+ #include "system/capability.h"
325+ #include "system/passwd.h"
326+ #include "system/filesys.h"
327+-#include "../lib/util/setid.h"
328++#include "lib/util/setid.h"
329++#include "lib/util/time.h"
330+
331+ #ifdef HAVE_SYS_SYSCTL_H
332+ #include <sys/sysctl.h>
333+@@ -122,124 +123,6 @@ int sys_fcntl_int(int fd, int cmd, int arg)
334+ return ret;
335+ }
336+
337+-/****************************************************************************
338+- Get/Set all the possible time fields from a stat struct as a timespec.
339+-****************************************************************************/
340+-
341+-static struct timespec get_atimespec(const struct stat *pst)
342+-{
343+-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
344+- struct timespec ret;
345+-
346+- /* Old system - no ns timestamp. */
347+- ret.tv_sec = pst->st_atime;
348+- ret.tv_nsec = 0;
349+- return ret;
350+-#else
351+-#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
352+- struct timespec ret;
353+- ret.tv_sec = pst->st_atim.tv_sec;
354+- ret.tv_nsec = pst->st_atim.tv_nsec;
355+- return ret;
356+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
357+- struct timespec ret;
358+- ret.tv_sec = pst->st_atime;
359+- ret.tv_nsec = pst->st_atimensec;
360+- return ret;
361+-#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
362+- struct timespec ret;
363+- ret.tv_sec = pst->st_atime;
364+- ret.tv_nsec = pst->st_atime_n;
365+- return ret;
366+-#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
367+- struct timespec ret;
368+- ret.tv_sec = pst->st_atime;
369+- ret.tv_nsec = pst->st_uatime * 1000;
370+- return ret;
371+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
372+- return pst->st_atimespec;
373+-#else
374+-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
375+-#endif
376+-#endif
377+-}
378+-
379+-static struct timespec get_mtimespec(const struct stat *pst)
380+-{
381+-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
382+- struct timespec ret;
383+-
384+- /* Old system - no ns timestamp. */
385+- ret.tv_sec = pst->st_mtime;
386+- ret.tv_nsec = 0;
387+- return ret;
388+-#else
389+-#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
390+- struct timespec ret;
391+- ret.tv_sec = pst->st_mtim.tv_sec;
392+- ret.tv_nsec = pst->st_mtim.tv_nsec;
393+- return ret;
394+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
395+- struct timespec ret;
396+- ret.tv_sec = pst->st_mtime;
397+- ret.tv_nsec = pst->st_mtimensec;
398+- return ret;
399+-#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
400+- struct timespec ret;
401+- ret.tv_sec = pst->st_mtime;
402+- ret.tv_nsec = pst->st_mtime_n;
403+- return ret;
404+-#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
405+- struct timespec ret;
406+- ret.tv_sec = pst->st_mtime;
407+- ret.tv_nsec = pst->st_umtime * 1000;
408+- return ret;
409+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
410+- return pst->st_mtimespec;
411+-#else
412+-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
413+-#endif
414+-#endif
415+-}
416+-
417+-static struct timespec get_ctimespec(const struct stat *pst)
418+-{
419+-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
420+- struct timespec ret;
421+-
422+- /* Old system - no ns timestamp. */
423+- ret.tv_sec = pst->st_ctime;
424+- ret.tv_nsec = 0;
425+- return ret;
426+-#else
427+-#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
428+- struct timespec ret;
429+- ret.tv_sec = pst->st_ctim.tv_sec;
430+- ret.tv_nsec = pst->st_ctim.tv_nsec;
431+- return ret;
432+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
433+- struct timespec ret;
434+- ret.tv_sec = pst->st_ctime;
435+- ret.tv_nsec = pst->st_ctimensec;
436+- return ret;
437+-#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
438+- struct timespec ret;
439+- ret.tv_sec = pst->st_ctime;
440+- ret.tv_nsec = pst->st_ctime_n;
441+- return ret;
442+-#elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
443+- struct timespec ret;
444+- ret.tv_sec = pst->st_ctime;
445+- ret.tv_nsec = pst->st_uctime * 1000;
446+- return ret;
447+-#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
448+- return pst->st_ctimespec;
449+-#else
450+-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
451+-#endif
452+-#endif
453+-}
454+-
455+ /****************************************************************************
456+ Return the best approximation to a 'create time' under UNIX from a stat
457+ structure.
458+diff --git a/source3/libsmb/libsmb_stat.c b/source3/libsmb/libsmb_stat.c
459+index 790934bd565..b01aeb51ac1 100644
460+--- a/source3/libsmb/libsmb_stat.c
461++++ b/source3/libsmb/libsmb_stat.c
462+@@ -27,6 +27,7 @@
463+ #include "libsmbclient.h"
464+ #include "libsmb_internal.h"
465+ #include "../libcli/smb/smbXcli_base.h"
466++#include "lib/util/time.h"
467+
468+ /*
469+ * Generate an inode number from file name for those things that need it
470+@@ -102,18 +103,29 @@ void setup_stat(struct stat *st,
471+ }
472+
473+ st->st_dev = dev;
474+- st->st_atim = access_time_ts;
475+- st->st_ctim = change_time_ts;
476+- st->st_mtim = write_time_ts;
477++
478++ st->st_atime = access_time_ts.tv_sec;
479++ set_atimensec(st, access_time_ts.tv_nsec);
480++
481++ st->st_ctime = change_time_ts.tv_sec;
482++ set_ctimensec(st, change_time_ts.tv_nsec);
483++
484++ st->st_mtime = write_time_ts.tv_sec;
485++ set_mtimensec(st, write_time_ts.tv_nsec);
486+ }
487+
488+ void setup_stat_from_stat_ex(const struct stat_ex *stex,
489+ const char *fname,
490+ struct stat *st)
491+ {
492+- st->st_atim = stex->st_ex_atime;
493+- st->st_ctim = stex->st_ex_ctime;
494+- st->st_mtim = stex->st_ex_mtime;
495++ st->st_atime = stex->st_ex_atime.tv_sec;
496++ set_atimensec(st, stex->st_ex_atime.tv_nsec);
497++
498++ st->st_ctime = stex->st_ex_ctime.tv_sec;
499++ set_ctimensec(st, stex->st_ex_ctime.tv_nsec);
500++
501++ st->st_mtime = stex->st_ex_mtime.tv_sec;
502++ set_mtimensec(st, stex->st_ex_mtime.tv_nsec);
503+
504+ st->st_mode = stex->st_ex_mode;
505+ st->st_size = stex->st_ex_size;
506+diff --git a/source4/ntvfs/posix/pvfs_fileinfo.c b/source4/ntvfs/posix/pvfs_fileinfo.c
507+index d2e2aeea265..977ea4fa3d5 100644
508+--- a/source4/ntvfs/posix/pvfs_fileinfo.c
509++++ b/source4/ntvfs/posix/pvfs_fileinfo.c
510+@@ -21,6 +21,7 @@
511+
512+ #include "includes.h"
513+ #include "vfs_posix.h"
514++#include "lib/util/time.h"
515+
516+ /****************************************************************************
517+ Change a unix mode to a dos mode.
518+@@ -72,12 +73,10 @@ NTSTATUS pvfs_fill_dos_info(struct pvfs_state *pvfs, struct pvfs_filename *name,
519+ unix_to_nt_time(&name->dos.access_time, name->st.st_atime);
520+ unix_to_nt_time(&name->dos.write_time, name->st.st_mtime);
521+ unix_to_nt_time(&name->dos.change_time, name->st.st_ctime);
522+-#ifdef HAVE_STAT_TV_NSEC
523+- name->dos.create_time += name->st.st_ctim.tv_nsec / 100;
524+- name->dos.access_time += name->st.st_atim.tv_nsec / 100;
525+- name->dos.write_time += name->st.st_mtim.tv_nsec / 100;
526+- name->dos.change_time += name->st.st_ctim.tv_nsec / 100;
527+-#endif
528++ name->dos.create_time += get_ctimensec(&name->st) / 100;
529++ name->dos.access_time += get_atimensec(&name->st) / 100;
530++ name->dos.write_time += get_mtimensec(&name->st) / 100;
531++ name->dos.change_time += get_ctimensec(&name->st) / 100;
532+ name->dos.attrib = dos_mode_from_stat(pvfs, &name->st);
533+ name->dos.alloc_size = pvfs_round_alloc_size(pvfs, name->st.st_size);
534+ name->dos.nlink = name->st.st_nlink;
535+diff --git a/source4/torture/libsmbclient/libsmbclient.c b/source4/torture/libsmbclient/libsmbclient.c
536+index 3f3992593f9..4fbd759487b 100644
537+--- a/source4/torture/libsmbclient/libsmbclient.c
538++++ b/source4/torture/libsmbclient/libsmbclient.c
539+@@ -27,6 +27,7 @@
540+ #include "lib/param/loadparm.h"
541+ #include "lib/param/param_global.h"
542+ #include "dynconfig.h"
543++#include "lib/util/time.h"
544+
545+ /* test string to compare with when debug_callback is called */
546+ #define TEST_STRING "smbc_setLogCallback test"
547+@@ -1231,8 +1232,8 @@ static bool torture_libsmbclient_utimes(struct torture_context *tctx)
548+ ret = smbc_fstat(fhandle, &st);
549+ torture_assert_int_not_equal(tctx, ret, -1, "smbc_fstat failed");
550+
551+- tbuf[0] = convert_timespec_to_timeval(st.st_atim);
552+- tbuf[1] = convert_timespec_to_timeval(st.st_mtim);
553++ tbuf[0] = convert_timespec_to_timeval(get_atimespec(&st));
554++ tbuf[1] = convert_timespec_to_timeval(get_mtimespec(&st));
555+
556+ tbuf[1] = timeval_add(&tbuf[1], 0, 100000); /* 100 msec */
557+
558+@@ -1244,7 +1245,7 @@ static bool torture_libsmbclient_utimes(struct torture_context *tctx)
559+
560+ torture_assert_int_equal(
561+ tctx,
562+- st.st_mtim.tv_nsec / 1000,
563++ get_mtimensec(&st) / 1000,
564+ tbuf[1].tv_usec,
565+ "smbc_utimes did not update msec");
566+
567+--
568+2.29.2
569+