From: Volker Lendecke Date: Thu, 6 Nov 2025 14:15:51 +0000 (+0100) Subject: lib: Add timespec_equal() X-Git-Tag: tdb-1.4.15~164 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=729d030c98d28e08ad365bb7b991583a7a956c80;p=thirdparty%2Fsamba.git lib: Add timespec_equal() I've just gotten the !=0 vs ==0 wrong. Make comparing timespecs for equality more intuitive. Signed-off-by: Volker Lendecke Reviewed-by: Anoop C S --- diff --git a/lib/util/time.c b/lib/util/time.c index 7ed8fb00447..220cdca3d9b 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -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. diff --git a/lib/util/time.h b/lib/util/time.h index 1180dd0cfc4..5fc70bf2a45 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -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); diff --git a/source3/modules/vfs_default.c b/source3/modules/vfs_default.c index 266fa87b3ed..480886d7d38 100644 --- a/source3/modules/vfs_default.c +++ b/source3/modules/vfs_default.c @@ -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; } diff --git a/source3/modules/vfs_dirsort.c b/source3/modules/vfs_dirsort.c index c4baf819b03..84888fbcbf0 100644 --- a/source3/modules/vfs_dirsort.c +++ b/source3/modules/vfs_dirsort.c @@ -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(¤t_mtime, &data->mtime)) { + if (!timespec_equal(¤t_mtime, &data->mtime)) { SMB_VFS_NEXT_REWINDDIR(handle, data->source_directory); open_and_sort_dir(handle, data); } diff --git a/source3/modules/vfs_glusterfs.c b/source3/modules/vfs_glusterfs.c index 9293f7ee996..247564f91f8 100644 --- a/source3/modules/vfs_glusterfs.c +++ b/source3/modules/vfs_glusterfs.c @@ -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(×[0], - &fsp->fsp_name->st.st_ex_atime) == 0) && - (timespec_compare(×[1], - &fsp->fsp_name->st.st_ex_mtime) == 0)) { + if (timespec_equal(×[0], &fsp->fsp_name->st.st_ex_atime) && + timespec_equal(×[1], &fsp->fsp_name->st.st_ex_mtime)) + { END_PROFILE(syscall_fntimes); return 0; } diff --git a/source3/param/loadparm.c b/source3/param/loadparm.c index 42f126acc21..2c3a5239447 100644 --- a/source3/param/loadparm.c +++ b/source3/param/loadparm.c @@ -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 )); diff --git a/source3/smbd/durable.c b/source3/smbd/durable.c index 108e9642287..5feb68b37aa 100644 --- a/source3/smbd/durable.c +++ b/source3/smbd/durable.c @@ -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); diff --git a/source3/torture/test_smb1_dfs.c b/source3/torture/test_smb1_dfs.c index 4cd75c9e056..42ef340dc1c 100644 --- a/source3/torture/test_smb1_dfs.c +++ b/source3/torture/test_smb1_dfs.c @@ -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__, diff --git a/source3/torture/torture.c b/source3/torture/torture.c index afb0ba52ebc..6d2a719144f 100644 --- a/source3/torture/torture.c +++ b/source3/torture/torture.c @@ -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");