s3: check if glibc has broken posix_fallocate
[amitay/samba.git] / source3 / lib / system.c
index 47bb5259ccb973eb2e3cc956d45f8f05cac6bc7e..905beed9ff1686261be0330fb715c93a264862e2 100644 (file)
@@ -404,7 +404,7 @@ static struct timespec get_ctimespec(const struct stat *pst)
  structure.
 ****************************************************************************/
 
-static struct timespec calc_create_time(const struct stat *st)
+static struct timespec calc_create_time_stat(const struct stat *st)
 {
        struct timespec ret, ret1;
        struct timespec c_time = get_ctimespec(st);
@@ -425,45 +425,96 @@ static struct timespec calc_create_time(const struct stat *st)
        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 struct timespec get_create_timespec(const struct stat *pst)
+static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
+                                bool fake_dir_create_times)
 {
-       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 (S_ISDIR(pst->st_mode) && 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)
-       ret = pst->st_birthtimespec;
+       dst->st_ex_btime = pst->st_birthtimespec;
 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
-       ret.tv_sec = pst->st_birthtime;
-       ret.tv_nsec = pst->st_birthtimenspec;
+       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)
-       ret.tv_sec = pst->st_birthtime;
-       ret.tv_nsec = 0;
+       dst->st_ex_btime.tv_sec = pst->st_birthtime;
+       dst->st_ex_btime.tv_nsec = 0;
 #else
-       ret = calc_create_time(pst);
+       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(ret)) {
-               ret = calc_create_time(pst);
+       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);
        }
-       return ret;
 }
 
+void update_stat_ex_create_time(struct stat_ex *dst,
+                                struct timespec create_time)
+{
+       dst->st_ex_btime = create_time;
+       dst->st_ex_calculated_birthtime = false;
+}
 
 static void init_stat_ex_from_stat (struct stat_ex *dst,
-                                   const struct stat *src)
+                                   const struct stat *src,
+                                   bool fake_dir_create_times)
 {
        dst->st_ex_dev = src->st_dev;
        dst->st_ex_ino = src->st_ino;
@@ -476,7 +527,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, fake_dir_create_times);
        dst->st_ex_blksize = src->st_blksize;
        dst->st_ex_blocks = src->st_blocks;
 
@@ -491,7 +542,8 @@ static void init_stat_ex_from_stat (struct stat_ex *dst,
 A stat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
+int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
+            bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
@@ -505,7 +557,7 @@ int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
                if (S_ISDIR(statbuf.st_mode)) {
                        statbuf.st_size = 0;
                }
-               init_stat_ex_from_stat(sbuf, &statbuf);
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
        }
        return ret;
 }
@@ -514,7 +566,7 @@ int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
  An fstat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
+int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
@@ -528,7 +580,7 @@ int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
                if (S_ISDIR(statbuf.st_mode)) {
                        statbuf.st_size = 0;
                }
-               init_stat_ex_from_stat(sbuf, &statbuf);
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
        }
        return ret;
 }
@@ -537,7 +589,8 @@ int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
  An lstat() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
 
-int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
+int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
+             bool fake_dir_create_times)
 {
        int ret;
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
@@ -551,11 +604,25 @@ int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
                if (S_ISDIR(statbuf.st_mode)) {
                        statbuf.st_size = 0;
                }
-               init_stat_ex_from_stat(sbuf, &statbuf);
+               init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
        }
        return ret;
 }
 
+/*******************************************************************
+ An posix_fallocate() wrapper that will deal with 64 bit filesizes.
+********************************************************************/
+#if (defined(HAVE_POSIX_FALLOCATE64) || defined(HAVE_POSIX_FALLOCATE)) && !defined(HAVE_BROKEN_POSIX_FALLOCATE)
+int sys_posix_fallocate(int fd, SMB_OFF_T offset, SMB_OFF_T len)
+{
+#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_POSIX_FALLOCATE64)
+       return posix_fallocate64(fd, offset, len);
+#else
+       return posix_fallocate(fd, offset, len);
+#endif
+}
+#endif
+
 /*******************************************************************
  An ftruncate() wrapper that will deal with 64 bit filesizes.
 ********************************************************************/
@@ -660,7 +727,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;
@@ -1247,6 +1314,7 @@ static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
        }
 
        argl[i++] = NULL;
+       TALLOC_FREE(trunc_cmd);
        return argl;
 
  nomem: