Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[kai/samba-autobuild/.git] / source3 / lib / system.c
index d9d3266fbc56dae494565e6beb85bcb18f5d0f8a..f9cd4a2355a114a082dbe898c2ab2f072b4e7168 100644 (file)
@@ -94,7 +94,7 @@ int sys_usleep(long usecs)
         * is not SPEC1170 complient... grumble... JRA.
         */
 
-       if(usecs < 0 || usecs > 1000000) {
+       if(usecs < 0 || usecs > 999999) {
                errno = EINVAL;
                return -1;
        }
@@ -290,67 +290,6 @@ int sys_fcntl_long(int fd, int cmd, long arg)
        return ret;
 }
 
-/****************************************************************************
- Return the best approximation to a 'create time' under UNIX from a stat
- structure.
-****************************************************************************/
-
-static time_t calc_create_time(const 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.
-****************************************************************************/
-
-static struct timespec get_create_timespec(const struct stat *pst)
-{
-       struct timespec ret;
-
-       if (S_ISDIR(pst->st_mode) && lp_fake_dir_create_times()) {
-               ret.tv_sec = 315493200L;          /* 1/1/1980 */
-               ret.tv_nsec = 0;
-               return ret;
-       }
-
-#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
-       ret = pst->st_birthtimespec;
-#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
-       ret.tv_sec = pst->st_birthtime;
-       ret.tv_nsec = pst->st_birthtimenspec;
-#elif defined(HAVE_STRUCT_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.
 ****************************************************************************/
@@ -460,6 +399,111 @@ static struct timespec get_ctimespec(const struct stat *pst)
 #endif
 }
 
+/****************************************************************************
+ Return the best approximation to a 'create time' under UNIX from a stat
+ structure.
+****************************************************************************/
+
+static struct timespec calc_create_time_stat(const struct stat *st)
+{
+       struct timespec ret, ret1;
+       struct timespec c_time = get_ctimespec(st);
+       struct timespec m_time = get_mtimespec(st);
+       struct timespec a_time = get_atimespec(st);
+
+       ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
+       ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
+
+       if(!null_timespec(ret1)) {
+               return ret1;
+       }
+
+       /*
+        * One of ctime, mtime or atime was zero (probably atime).
+        * Just return MIN(ctime, mtime).
+        */
+       return ret;
+}
+
+/****************************************************************************
+ Return the best approximation to a 'create time' under UNIX from a stat_ex
+ structure.
+****************************************************************************/
+
+static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
+{
+       struct timespec ret, ret1;
+       struct timespec c_time = st->st_ex_ctime;
+       struct timespec m_time = st->st_ex_mtime;
+       struct timespec a_time = st->st_ex_atime;
+
+       ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
+       ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
+
+       if(!null_timespec(ret1)) {
+               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.
+****************************************************************************/
+
+static void make_create_timespec(const struct stat *pst, struct stat_ex *dst)
+{
+       if (S_ISDIR(pst->st_mode) && lp_fake_dir_create_times()) {
+               dst->st_ex_btime.tv_sec = 315493200L;          /* 1/1/1980 */
+               dst->st_ex_btime.tv_nsec = 0;
+       }
+
+       dst->st_ex_calculated_birthtime = false;
+
+#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
+       dst->st_ex_btime = pst->st_birthtimespec;
+#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
+       dst->st_ex_btime.tv_sec = pst->st_birthtime;
+       dst->st_ex_btime.tv_nsec = pst->st_birthtimenspec;
+#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
+       dst->st_ex_btime.tv_sec = pst->st_birthtime;
+       dst->st_ex_btime.tv_nsec = 0;
+#else
+       dst->st_ex_btime = calc_create_time_stat(pst);
+       dst->st_ex_calculated_birthtime = true;
+#endif
+
+       /* Deal with systems that don't initialize birthtime correctly.
+        * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
+        */
+       if (null_timespec(dst->st_ex_btime)) {
+               dst->st_ex_btime = calc_create_time_stat(pst);
+               dst->st_ex_calculated_birthtime = true;
+       }
+}
+
+/****************************************************************************
+ If we update a timestamp in a stat_ex struct we may have to recalculate
+ the birthtime. For now only implement this for write time, but we may
+ also need to do it for atime and ctime. JRA.
+****************************************************************************/
+
+void update_stat_ex_mtime(struct stat_ex *dst,
+                               struct timespec write_ts)
+{
+       dst->st_ex_mtime = write_ts;
+
+       /* We may have to recalculate btime. */
+       if (dst->st_ex_calculated_birthtime) {
+               dst->st_ex_btime = calc_create_time_stat_ex(dst);
+       }
+}
+
 static void init_stat_ex_from_stat (struct stat_ex *dst,
                                    const struct stat *src)
 {
@@ -474,7 +518,7 @@ static void init_stat_ex_from_stat (struct stat_ex *dst,
        dst->st_ex_atime = get_atimespec(src);
        dst->st_ex_mtime = get_mtimespec(src);
        dst->st_ex_ctime = get_ctimespec(src);
-       dst->st_ex_btime = get_create_timespec(src);
+       make_create_timespec(src, dst);
        dst->st_ex_blksize = src->st_blksize;
        dst->st_ex_blocks = src->st_blocks;
 
@@ -658,7 +702,7 @@ FILE *sys_fopen(const char *path, const char *type)
  A flock() wrapper that will perform the kernel flock.
 ********************************************************************/
 
-void kernel_flock(int fd, uint32 share_mode)
+void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
 {
 #if HAVE_KERNEL_SHARE_MODES
        int kernel_mode = 0;