]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Add timespec_equal()
authorVolker Lendecke <vl@samba.org>
Thu, 6 Nov 2025 14:15:51 +0000 (15:15 +0100)
committerVolker Lendecke <vl@samba.org>
Wed, 7 Jan 2026 09:57:40 +0000 (09:57 +0000)
I've just gotten the !=0 vs ==0 wrong. Make comparing timespecs for
equality more intuitive.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
lib/util/time.c
lib/util/time.h
source3/modules/vfs_default.c
source3/modules/vfs_dirsort.c
source3/modules/vfs_glusterfs.c
source3/param/loadparm.c
source3/smbd/durable.c
source3/torture/test_smb1_dfs.c
source3/torture/torture.c

index 7ed8fb00447c8252f252f98cfcf46030be34d123..220cdca3d9bc4134106c6c1c75619ca570212bb5 100644 (file)
@@ -958,6 +958,12 @@ _PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec
        return 0;
 }
 
+_PUBLIC_ bool timespec_equal(const struct timespec *ts1, const struct timespec *ts2)
+{
+       return ((ts1->tv_sec == ts2->tv_sec) &&
+               (ts1->tv_nsec == ts2->tv_nsec));
+}
+
 /****************************************************************************
  Round up a timespec if nsec > 500000000, round down if lower,
  then zero nsec.
index 1180dd0cfc4bf376b69cab301e44b971ae614dd5..5fc70bf2a450d794f3ed300fe2e0f2163cf76dd3 100644 (file)
@@ -350,6 +350,7 @@ struct timespec timespec_current(void);
 struct timespec timespec_min(const struct timespec *ts1,
                             const struct timespec *ts2);
 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2);
+bool timespec_equal(const struct timespec *ts1, const struct timespec *ts2);
 void round_timespec_to_sec(struct timespec *ts);
 void round_timespec_to_usec(struct timespec *ts);
 void round_timespec_to_nttime(struct timespec *ts);
index 266fa87b3eddb1d513b8f596a3fbf65e85fde4e8..480886d7d3856b97f2d2d81d2523381d0540adca 100644 (file)
@@ -2908,10 +2908,11 @@ static int vfswrap_fntimes(vfs_handle_struct *handle,
                                               ft->create_time);
                }
 
-               if ((timespec_compare(&ft->atime,
-                                     &fsp->fsp_name->st.st_ex_atime) == 0) &&
-                   (timespec_compare(&ft->mtime,
-                                     &fsp->fsp_name->st.st_ex_mtime) == 0)) {
+               if (timespec_equal(&ft->atime,
+                                  &fsp->fsp_name->st.st_ex_atime) &&
+                   timespec_equal(&ft->mtime,
+                                  &fsp->fsp_name->st.st_ex_mtime))
+               {
                        result = 0;
                        goto out;
                }
index c4baf819b0384cee94d5099dad7e730a1a002bf1..84888fbcbf0573313197d7b6756443d3695ee352 100644 (file)
@@ -201,7 +201,7 @@ static struct dirent *dirsort_readdir(vfs_handle_struct *handle,
        }
 
        /* throw away cache and re-read the directory if we've changed */
-       if (timespec_compare(&current_mtime, &data->mtime)) {
+       if (!timespec_equal(&current_mtime, &data->mtime)) {
                SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory);
                open_and_sort_dir(handle, data);
        }
index 9293f7ee996f8babef919bde97bbcc82b7095981..247564f91f8b31664f9f93fb482073751faa03fb 100644 (file)
@@ -1732,10 +1732,9 @@ static int vfs_gluster_fntimes(struct vfs_handle_struct *handle,
                times[1].tv_nsec = ft->mtime.tv_nsec;
        }
 
-       if ((timespec_compare(&times[0],
-                             &fsp->fsp_name->st.st_ex_atime) == 0) &&
-           (timespec_compare(&times[1],
-                             &fsp->fsp_name->st.st_ex_mtime) == 0)) {
+       if (timespec_equal(&times[0], &fsp->fsp_name->st.st_ex_atime) &&
+           timespec_equal(&times[1], &fsp->fsp_name->st.st_ex_mtime))
+       {
                END_PROFILE(syscall_fntimes);
                return 0;
        }
index 42f126acc2107e2e356c1b9e1f1358cd83e5b711..2c3a5239447a8c6b72696973e99a5c339f012f66 100644 (file)
@@ -2501,7 +2501,7 @@ bool lp_file_list_changed(void)
                        }
 
                        if (mod_time.tv_sec > 0 &&
-                           ((timespec_compare(&mod_time, &f->modtime) != 0) ||
+                           (!timespec_equal(&mod_time, &f->modtime) ||
                             (f->subfname == NULL) ||
                             (strcmp(n2, f->subfname) != 0)))
                        {
@@ -3547,8 +3547,9 @@ static int process_usershare_file(const char *dir_name, const char *file_name, i
        }
 
        if (iService != -1 &&
-           timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
-                            &lsbuf.st_ex_mtime) == 0) {
+           timespec_equal(&ServicePtrs[iService]->usershare_last_mod,
+                          &lsbuf.st_ex_mtime))
+       {
                /* Nothing changed - Mark valid and return. */
                DEBUG(10,("process_usershare_file: service %s not changed.\n",
                        canon_name ));
index 108e964228721cd52c673e21f6f4d8e0493db627..5feb68b37aae914a202874980a1758428968b814 100644 (file)
@@ -313,7 +313,7 @@ static bool vfs_default_durable_reconnect_check_stat(
                                struct files_struct *fsp)
 {
        SMB_STRUCT_STAT *fsp_st = &fsp->fsp_name->st;
-       int ret;
+       bool equal;
 
        if (cookie_st->st_ex_mode != fsp_st->st_ex_mode) {
                DEBUG(1, ("vfs_default_durable_reconnect (%s): "
@@ -387,9 +387,8 @@ static bool vfs_default_durable_reconnect_check_stat(
                return false;
        }
 
-       ret = timespec_compare(&cookie_st->st_ex_atime,
-                              &fsp_st->st_ex_atime);
-       if (ret != 0) {
+       equal = timespec_equal(&cookie_st->st_ex_atime, &fsp_st->st_ex_atime);
+       if (!equal) {
                struct timeval tc, ts;
                tc = convert_timespec_to_timeval(cookie_st->st_ex_atime);
                ts = convert_timespec_to_timeval(fsp_st->st_ex_atime);
@@ -405,9 +404,8 @@ static bool vfs_default_durable_reconnect_check_stat(
                return false;
        }
 
-       ret = timespec_compare(&cookie_st->st_ex_mtime,
-                              &fsp_st->st_ex_mtime);
-       if (ret != 0) {
+       equal = timespec_equal(&cookie_st->st_ex_mtime, &fsp_st->st_ex_mtime);
+       if (!equal) {
                struct timeval tc, ts;
                tc = convert_timespec_to_timeval(cookie_st->st_ex_mtime);
                ts = convert_timespec_to_timeval(fsp_st->st_ex_mtime);
@@ -423,9 +421,8 @@ static bool vfs_default_durable_reconnect_check_stat(
                return false;
        }
 
-       ret = timespec_compare(&cookie_st->st_ex_ctime,
-                              &fsp_st->st_ex_ctime);
-       if (ret != 0) {
+       equal = timespec_equal(&cookie_st->st_ex_ctime, &fsp_st->st_ex_ctime);
+       if (!equal) {
                struct timeval tc, ts;
                tc = convert_timespec_to_timeval(cookie_st->st_ex_ctime);
                ts = convert_timespec_to_timeval(fsp_st->st_ex_ctime);
@@ -441,9 +438,8 @@ static bool vfs_default_durable_reconnect_check_stat(
                return false;
        }
 
-       ret = timespec_compare(&cookie_st->st_ex_btime,
-                              &fsp_st->st_ex_btime);
-       if (ret != 0) {
+       equal = timespec_equal(&cookie_st->st_ex_btime, &fsp_st->st_ex_btime);
+       if (!equal) {
                struct timeval tc, ts;
                tc = convert_timespec_to_timeval(cookie_st->st_ex_btime);
                ts = convert_timespec_to_timeval(fsp_st->st_ex_btime);
index 4cd75c9e056ea863a04d01d1cb725c8305c50c1e..42ef340dc1c6c06efc05db8f6a4dc9dcb7f6feda 100644 (file)
@@ -129,7 +129,7 @@ static bool smb1_crtime_matches(struct cli_state *cli,
                        nt_errstr(status));
                return false;
        }
-       equal = (timespec_compare(&test_crtime, &crtime_tomatch) == 0);
+       equal = timespec_equal(&test_crtime, &crtime_tomatch);
        if (!equal) {
                struct timeval_buf test_buf;
                struct timeval_buf tomatch_buf;
@@ -1852,7 +1852,7 @@ bool run_smb1_dfs_paths(int dummy)
         * This checks we're actually correctly reading crtimes
         * from the filesystem.
         */
-       equal = (timespec_compare(&test_crtime, &root_crtime) == 0);
+       equal = timespec_equal(&test_crtime, &root_crtime);
        if (equal) {
                printf("%s:%d Error. crtime of %s must differ from "
                        "root_crtime\n",
@@ -3227,7 +3227,7 @@ static bool test_smb1_open(struct cli_state *cli)
                        nt_errstr(status));
                goto err;
        }
-       equal = (timespec_compare(&testfile_crtime, &open_crtime) == 0);
+       equal = timespec_equal(&testfile_crtime, &open_crtime);
        if (!equal) {
                printf("%s:%d crtime mismatch of test file %s\n",
                        __FILE__,
index afb0ba52ebcf3c64f67f63a1667a23ffc901c1a9..6d2a719144f4eafcd230a385c65287a36d4a9e49 100644 (file)
@@ -12770,7 +12770,7 @@ static bool run_dir_createtime(int dummy)
                goto out;
        }
 
-       if (timespec_compare(&create_time1, &create_time)) {
+       if (!timespec_equal(&create_time1, &create_time)) {
                printf("run_dir_createtime: create time was updated (error)\n");
        } else {
                printf("run_dir_createtime: create time was not updated (correct)\n");