Second attempt at fix for bug 6529 - Offline files conflict with Vista and Office...
[ira/wip.git] / source3 / lib / time.c
index e2cfe687b2f07c58077274e087b6352be947fda4..f6ff6d3407480020389dd1911c5e7157e51e63ec 100644 (file)
@@ -301,14 +301,30 @@ void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
        put_dos_date3(buf, offset, unixdate, server_zone_offset);
 }
 
+void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
+{
+       switch (res) {
+               case TIMESTAMP_SET_SECONDS:
+                       round_timespec_to_sec(ts);
+                       break;
+               case TIMESTAMP_SET_MSEC:
+                       round_timespec_to_usec(ts);
+                       break;
+               case TIMESTAMP_SET_NT_OR_BETTER:
+                       /* No rounding needed. */
+                       break;
+        }
+}
+
 /****************************************************************************
  Take a Unix time and convert to an NTTIME structure and place in buffer 
- pointed to by p.
+ pointed to by p, rounded to the correct resolution.
 ****************************************************************************/
 
-void put_long_date_timespec(char *p, struct timespec ts)
+void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
 {
        NTTIME nt;
+       round_timespec(res, &ts);
        unix_timespec_to_nt_time(&nt, ts);
        SIVAL(p, 0, nt & 0xFFFFFFFF);
        SIVAL(p, 4, nt >> 32);
@@ -319,252 +335,7 @@ void put_long_date(char *p, time_t t)
        struct timespec ts;
        ts.tv_sec = t;
        ts.tv_nsec = 0;
-       put_long_date_timespec(p, ts);
-}
-
-/****************************************************************************
- Return the best approximation to a 'create time' under UNIX from a stat
- structure.
-****************************************************************************/
-
-static time_t calc_create_time(const SMB_STRUCT_STAT *st)
-{
-       time_t ret, ret1;
-
-       ret = MIN(st->st_ctime, st->st_mtime);
-       ret1 = MIN(ret, st->st_atime);
-
-       if(ret1 != (time_t)0) {
-               return ret1;
-       }
-
-       /*
-        * One of ctime, mtime or atime was zero (probably atime).
-        * Just return MIN(ctime, mtime).
-        */
-       return ret;
-}
-
-/****************************************************************************
- Return the 'create time' from a stat struct if it exists (birthtime) or else
- use the best approximation.
-****************************************************************************/
-
-struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs)
-{
-       struct timespec ret;
-
-       if(S_ISDIR(pst->st_mode) && fake_dirs) {
-               ret.tv_sec = 315493200L;          /* 1/1/1980 */
-               ret.tv_nsec = 0;
-               return ret;
-       }
-
-#if defined(HAVE_STAT_ST_BIRTHTIMESPEC)
-       ret = pst->st_birthtimespec;
-#elif defined(HAVE_STAT_ST_BIRTHTIMENSEC)
-       ret.tv_sec = pst->st_birthtime;
-       ret.tv_nsec = pst->st_birthtimenspec;
-#elif defined(HAVE_STAT_ST_BIRTHTIME)
-       ret.tv_sec = pst->st_birthtime;
-       ret.tv_nsec = 0;
-#else
-       ret.tv_sec = calc_create_time(pst);
-       ret.tv_nsec = 0;
-#endif
-
-       /* Deal with systems that don't initialize birthtime correctly.
-        * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
-        */
-       if (null_timespec(ret)) {
-               ret.tv_sec = calc_create_time(pst);
-               ret.tv_nsec = 0;
-       }
-       return ret;
-}
-
-/****************************************************************************
- Get/Set all the possible time fields from a stat struct as a timespec.
-****************************************************************************/
-
-struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       struct timespec ret;
-
-       /* Old system - no ns timestamp. */
-       ret.tv_sec = pst->st_atime;
-       ret.tv_nsec = 0;
-       return ret;
-#else
-#if defined(HAVE_STAT_ST_ATIM)
-       return pst->st_atim;
-#elif defined(HAVE_STAT_ST_ATIMENSEC)
-       struct timespec ret;
-       ret.tv_sec = pst->st_atime;
-       ret.tv_nsec = pst->st_atimensec;
-       return ret;
-#elif defined(HAVE_STAT_ST_ATIME_N)
-       struct timespec ret;
-       ret.tv_sec = pst->st_atime;
-       ret.tv_nsec = pst->st_atime_n;
-       return ret;
-#elif defined(HAVE_STAT_ST_UATIME)
-       struct timespec ret;
-       ret.tv_sec = pst->st_atime;
-       ret.tv_nsec = pst->st_uatime * 1000;
-       return ret;
-#elif defined(HAVE_STAT_ST_ATIMESPEC)
-       return pst->st_atimespec;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
-}
-
-void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       /* Old system - no ns timestamp. */
-       pst->st_atime = ts.tv_sec;
-#else
-#if defined(HAVE_STAT_ST_ATIM)
-       pst->st_atim = ts;
-#elif defined(HAVE_STAT_ST_ATIMENSEC)
-       pst->st_atime = ts.tv_sec;
-       pst->st_atimensec = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_ATIME_N)
-       pst->st_atime = ts.tv_sec;
-       pst->st_atime_n = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_UATIME)
-       pst->st_atime = ts.tv_sec;
-       pst->st_uatime = ts.tv_nsec / 1000;
-#elif defined(HAVE_STAT_ST_ATIMESPEC)
-       pst->st_atimespec = ts;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
-}
-
-struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       struct timespec ret;
-
-       /* Old system - no ns timestamp. */
-       ret.tv_sec = pst->st_mtime;
-       ret.tv_nsec = 0;
-       return ret;
-#else
-#if defined(HAVE_STAT_ST_MTIM)
-       return pst->st_mtim;
-#elif defined(HAVE_STAT_ST_MTIMENSEC)
-       struct timespec ret;
-       ret.tv_sec = pst->st_mtime;
-       ret.tv_nsec = pst->st_mtimensec;
-       return ret;
-#elif defined(HAVE_STAT_ST_MTIME_N)
-       struct timespec ret;
-       ret.tv_sec = pst->st_mtime;
-       ret.tv_nsec = pst->st_mtime_n;
-       return ret;
-#elif defined(HAVE_STAT_ST_UMTIME)
-       struct timespec ret;
-       ret.tv_sec = pst->st_mtime;
-       ret.tv_nsec = pst->st_umtime * 1000;
-       return ret;
-#elif defined(HAVE_STAT_ST_MTIMESPEC)
-       return pst->st_mtimespec;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
-}
-
-void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       /* Old system - no ns timestamp. */
-       pst->st_mtime = ts.tv_sec;
-#else
-#if defined(HAVE_STAT_ST_MTIM)
-       pst->st_mtim = ts;
-#elif defined(HAVE_STAT_ST_MTIMENSEC)
-       pst->st_mtime = ts.tv_sec;
-       pst->st_mtimensec = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_MTIME_N)
-       pst->st_mtime = ts.tv_sec;
-       pst->st_mtime_n = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_UMTIME)
-       pst->st_mtime = ts.tv_sec;
-       pst->st_umtime = ts.tv_nsec / 1000;
-#elif defined(HAVE_STAT_ST_MTIMESPEC)
-       pst->st_mtimespec = ts;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
-}
-
-struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       struct timespec ret;
-
-       /* Old system - no ns timestamp. */
-       ret.tv_sec = pst->st_ctime;
-       ret.tv_nsec = 0;
-       return ret;
-#else
-#if defined(HAVE_STAT_ST_CTIM)
-       return pst->st_ctim;
-#elif defined(HAVE_STAT_ST_CTIMENSEC)
-       struct timespec ret;
-       ret.tv_sec = pst->st_ctime;
-       ret.tv_nsec = pst->st_ctimensec;
-       return ret;
-#elif defined(HAVE_STAT_ST_CTIME_N)
-       struct timespec ret;
-       ret.tv_sec = pst->st_ctime;
-       ret.tv_nsec = pst->st_ctime_n;
-       return ret;
-#elif defined(HAVE_STAT_ST_UCTIME)
-       struct timespec ret;
-       ret.tv_sec = pst->st_ctime;
-       ret.tv_nsec = pst->st_uctime * 1000;
-       return ret;
-#elif defined(HAVE_STAT_ST_CTIMESPEC)
-       return pst->st_ctimespec;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
-}
-
-void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
-{
-#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
-       /* Old system - no ns timestamp. */
-       pst->st_ctime = ts.tv_sec;
-#else
-#if defined(HAVE_STAT_ST_CTIM)
-       pst->st_ctim = ts;
-#elif defined(HAVE_STAT_ST_CTIMENSEC)
-       pst->st_ctime = ts.tv_sec;
-       pst->st_ctimensec = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_CTIME_N)
-       pst->st_ctime = ts.tv_sec;
-       pst->st_ctime_n = ts.tv_nsec;
-#elif defined(HAVE_STAT_ST_UCTIME)
-       pst->st_ctime = ts.tv_sec;
-       pst->st_uctime = ts.tv_nsec / 1000;
-#elif defined(HAVE_STAT_ST_CTIMESPEC)
-       pst->st_ctimespec = ts;
-#else
-#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
-#endif
-#endif
+       put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
 }
 
 void dos_filetime_timespec(struct timespec *tsp)
@@ -605,7 +376,7 @@ static time_t make_unix_date(const void *date_ptr, int zone_offset)
  Like make_unix_date() but the words are reversed.
 ********************************************************************/
 
-static time_t make_unix_date2(const void *date_ptr, int zone_offset)
+time_t make_unix_date2(const void *date_ptr, int zone_offset)
 {
        uint32_t x,x2;
 
@@ -621,7 +392,7 @@ static time_t make_unix_date2(const void *date_ptr, int zone_offset)
  these generally arrive as localtimes, with corresponding DST.
 ******************************************************************/
 
-static time_t make_unix_date3(const void *date_ptr, int zone_offset)
+time_t make_unix_date3(const void *date_ptr, int zone_offset)
 {
        time_t t = (time_t)IVAL(date_ptr,0);
        if (!null_mtime(t)) {
@@ -712,6 +483,27 @@ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
        return 0;
 }
 
+/****************************************************************************
+ Round up a timespec if nsec > 500000000, round down if lower,
+ then zero nsec.
+****************************************************************************/
+
+void round_timespec_to_sec(struct timespec *ts)
+{
+       ts->tv_sec = convert_timespec_to_time_t(*ts);
+       ts->tv_nsec = 0;
+}
+
+/****************************************************************************
+ Round a timespec to usec value.
+****************************************************************************/
+
+void round_timespec_to_usec(struct timespec *ts)
+{
+       struct timeval tv = convert_timespec_to_timeval(*ts);
+       *ts = convert_timeval_to_timespec(tv);
+}
+
 /****************************************************************************
  Interprets an nt time into a unix struct timespec.
  Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff