From: Richard Russon Date: Sun, 25 Nov 2018 15:32:48 +0000 (+0000) Subject: move timespec functions to library X-Git-Tag: 2019-10-25~493 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=116b3a5554270b207d1f15e1bbb89ea899899079;p=neomutt move timespec functions to library --- diff --git a/maildir/mh.c b/maildir/mh.c index 80ba7269e..083f38fd0 100644 --- a/maildir/mh.c +++ b/maildir/mh.c @@ -299,7 +299,7 @@ static int mh_sequences_changed(struct Mailbox *m) if ((snprintf(path, sizeof(path), "%s/.mh_sequences", m->path) < sizeof(path)) && (stat(path, &sb) == 0)) { - return (mutt_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) > 0); + return (mutt_file_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) > 0); } return -1; } @@ -320,7 +320,7 @@ static int mh_already_notified(struct Mailbox *m, int msgno) if ((snprintf(path, sizeof(path), "%s/%d", m->path, msgno) < sizeof(path)) && (stat(path, &sb) == 0)) { - return (mutt_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) <= 0); + return (mutt_file_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) <= 0); } return -1; } @@ -704,20 +704,20 @@ static void maildir_update_mtime(struct Mailbox *m) { snprintf(buf, sizeof(buf), "%s/%s", m->path, "cur"); if (stat(buf, &st) == 0) - mutt_get_stat_timespec(&mdata->mtime_cur, &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&mdata->mtime_cur, &st, MUTT_STAT_MTIME); snprintf(buf, sizeof(buf), "%s/%s", m->path, "new"); } else { snprintf(buf, sizeof(buf), "%s/.mh_sequences", m->path); if (stat(buf, &st) == 0) - mutt_get_stat_timespec(&mdata->mtime_cur, &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&mdata->mtime_cur, &st, MUTT_STAT_MTIME); mutt_str_strfcpy(buf, m->path, sizeof(buf)); } if (stat(buf, &st) == 0) - mutt_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); } /** @@ -2479,9 +2479,9 @@ static int maildir_mbox_check(struct Context *ctx, int *index_hint) } /* determine which subdirectories need to be scanned */ - if (mutt_stat_timespec_compare(&st_new, MUTT_STAT_MTIME, &m->mtime) > 0) + if (mutt_file_stat_timespec_compare(&st_new, MUTT_STAT_MTIME, &m->mtime) > 0) changed = 1; - if (mutt_stat_timespec_compare(&st_cur, MUTT_STAT_MTIME, &mdata->mtime_cur) > 0) + if (mutt_file_stat_timespec_compare(&st_cur, MUTT_STAT_MTIME, &mdata->mtime_cur) > 0) changed |= 2; if (!changed) @@ -2502,8 +2502,8 @@ static int maildir_mbox_check(struct Context *ctx, int *index_hint) else #endif { - mutt_get_stat_timespec(&mdata->mtime_cur, &st_cur, MUTT_STAT_MTIME); - mutt_get_stat_timespec(&m->mtime, &st_new, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&mdata->mtime_cur, &st_cur, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&m->mtime, &st_new, MUTT_STAT_MTIME); } /* do a fast scan of just the filenames in @@ -2873,8 +2873,8 @@ static int mh_mbox_check(struct Context *ctx, int *index_hint) if (i == -1 && stat(buf, &st_cur) == -1) modified = true; - if ((mutt_stat_timespec_compare(&st, MUTT_STAT_MTIME, &m->mtime) > 0) || - (mutt_stat_timespec_compare(&st_cur, MUTT_STAT_MTIME, &mdata->mtime_cur) > 0)) + if ((mutt_file_stat_timespec_compare(&st, MUTT_STAT_MTIME, &m->mtime) > 0) || + (mutt_file_stat_timespec_compare(&st_cur, MUTT_STAT_MTIME, &mdata->mtime_cur) > 0)) { modified = true; } @@ -2894,8 +2894,8 @@ static int mh_mbox_check(struct Context *ctx, int *index_hint) else #endif { - mutt_get_stat_timespec(&mdata->mtime_cur, &st_cur, MUTT_STAT_MTIME); - mutt_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&mdata->mtime_cur, &st_cur, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); } md = NULL; @@ -3164,7 +3164,7 @@ int maildir_check_dir(struct Mailbox *m, const char *dir_name, bool check_new, b if (check_new && MailCheckRecent) { if (stat(mutt_b2s(path), &sb) == 0 && - mutt_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) < 0) + mutt_file_stat_timespec_compare(&sb, MUTT_STAT_MTIME, &m->last_visited) < 0) { rc = 0; check_new = false; @@ -3208,7 +3208,7 @@ int maildir_check_dir(struct Mailbox *m, const char *dir_name, bool check_new, b mutt_buffer_printf(msgpath, "%s/%s", mutt_b2s(path), de->d_name); /* ensure this message was received since leaving this m */ if (stat(mutt_b2s(msgpath), &sb) == 0 && - (mutt_stat_timespec_compare(&sb, MUTT_STAT_CTIME, &m->last_visited) <= 0)) + (mutt_file_stat_timespec_compare(&sb, MUTT_STAT_CTIME, &m->last_visited) <= 0)) { continue; } diff --git a/mbox/mbox.c b/mbox/mbox.c index e9d528e93..c4a2efd24 100644 --- a/mbox/mbox.c +++ b/mbox/mbox.c @@ -205,8 +205,8 @@ static int mmdf_parse_mailbox(struct Context *ctx) mutt_perror(m->path); return -1; } - mutt_get_stat_timespec(&adata->atime, &sb, MUTT_STAT_ATIME); - mutt_get_stat_timespec(&m->mtime, &sb, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&adata->atime, &sb, MUTT_STAT_ATIME); + mutt_file_get_stat_timespec(&m->mtime, &sb, MUTT_STAT_MTIME); m->size = sb.st_size; buf[sizeof(buf) - 1] = '\0'; @@ -376,8 +376,8 @@ static int mbox_parse_mailbox(struct Context *ctx) } m->size = sb.st_size; - mutt_get_stat_timespec(&m->mtime, &sb, MUTT_STAT_MTIME); - mutt_get_stat_timespec(&adata->atime, &sb, MUTT_STAT_ATIME); + mutt_file_get_stat_timespec(&m->mtime, &sb, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&adata->atime, &sb, MUTT_STAT_ATIME); if (!m->readonly) m->readonly = access(m->path, W_OK) ? true : false; @@ -1046,7 +1046,7 @@ static int mbox_mbox_check(struct Context *ctx, int *index_hint) if (stat(m->path, &st) == 0) { - if ((mutt_stat_timespec_compare(&st, MUTT_STAT_MTIME, &m->mtime) == 0) && + if ((mutt_file_stat_timespec_compare(&st, MUTT_STAT_MTIME, &m->mtime) == 0) && st.st_size == m->size) { return 0; @@ -1055,7 +1055,7 @@ static int mbox_mbox_check(struct Context *ctx, int *index_hint) if (st.st_size == m->size) { /* the file was touched, but it is still the same length, so just exit */ - mutt_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&m->mtime, &st, MUTT_STAT_MTIME); return 0; } @@ -1550,7 +1550,7 @@ static int mbox_mbox_close(struct Context *ctx) /* fix up the times so mailbox won't get confused */ if (m->peekonly && (m->path[0] != '\0') && - (mutt_timespec_compare(&m->mtime, &adata->atime) > 0)) + (mutt_file_timespec_compare(&m->mtime, &adata->atime) > 0)) { #ifdef HAVE_UTIMENSAT struct timespec ts[2]; @@ -1694,8 +1694,8 @@ enum MailboxType mbox_path_probe(const char *path, const struct stat *st) */ #ifdef HAVE_UTIMENSAT struct timespec ts[2]; - mutt_get_stat_timespec(&ts[0], &st, MUTT_STAT_ATIME); - mutt_get_stat_timespec(&ts[1], &st, MUTT_STAT_MTIME); + mutt_file_get_stat_timespec(&ts[0], &st, MUTT_STAT_ATIME); + mutt_file_get_stat_timespec(&ts[1], &st, MUTT_STAT_MTIME); utimensat(0, path, ts, 0); #else struct utimbuf times; @@ -1801,16 +1801,16 @@ int mbox_check(struct Mailbox *m, struct stat *sb, bool check_stats) else { new_or_changed = - (mutt_stat_compare(sb, MUTT_STAT_MTIME, sb, MUTT_STAT_ATIME) > 0) || + (mutt_file_stat_compare(sb, MUTT_STAT_MTIME, sb, MUTT_STAT_ATIME) > 0) || (m->newly_created && - (mutt_stat_compare(sb, MUTT_STAT_CTIME, sb, MUTT_STAT_MTIME) == 0) && - (mutt_stat_compare(sb, MUTT_STAT_CTIME, sb, MUTT_STAT_ATIME) == 0)); + (mutt_file_stat_compare(sb, MUTT_STAT_CTIME, sb, MUTT_STAT_MTIME) == 0) && + (mutt_file_stat_compare(sb, MUTT_STAT_CTIME, sb, MUTT_STAT_ATIME) == 0)); } if (new_or_changed) { if (!MailCheckRecent || - (mutt_stat_timespec_compare(sb, MUTT_STAT_MTIME, &m->last_visited) > 0)) + (mutt_file_stat_timespec_compare(sb, MUTT_STAT_MTIME, &m->last_visited) > 0)) { rc = 1; m->has_new = true; @@ -1826,7 +1826,7 @@ int mbox_check(struct Mailbox *m, struct stat *sb, bool check_stats) m->newly_created = false; if (check_stats && - (mutt_stat_timespec_compare(sb, MUTT_STAT_MTIME, &m->stats_last_checked) > 0)) + (mutt_file_stat_timespec_compare(sb, MUTT_STAT_MTIME, &m->stats_last_checked) > 0)) { struct Context *ctx = mx_mbox_open(m, NULL, MUTT_READONLY | MUTT_QUIET | MUTT_NOSORT | MUTT_PEEK); diff --git a/mutt.h b/mutt.h index 45c2216ff..46009188a 100644 --- a/mutt.h +++ b/mutt.h @@ -54,29 +54,6 @@ struct Mapping; #define fgetc fgetc_unlocked #endif -#ifndef HAVE_STRUCT_TIMESPEC -/** - * struct timespec - Time value with nanosecond precision - */ -struct timespec -{ - time_t tv_sec; - long tv_nsec; -}; -#endif - -/** - * enum MuttStatType - Flags for mutt_get_stat_timespec - * - * These represent filesystem timestamps returned by stat() - */ -enum MuttStatType -{ - MUTT_STAT_ATIME, - MUTT_STAT_MTIME, - MUTT_STAT_CTIME -}; - /* flags for mutt_enter_string_full() */ #define MUTT_ALIAS (1 << 0) /**< do alias "completion" by calling up the alias-menu */ #define MUTT_FILE (1 << 1) /**< do file completion */ diff --git a/mutt/file.c b/mutt/file.c index 4d3febae3..aa999fbfb 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -1352,3 +1352,98 @@ long mutt_file_get_size(const char *path) return sb.st_size; } + +/** + * mutt_file_timespec_compare - Compare to time values + * @param a First time value + * @param b Second time value + * @retval -1 a precedes b + * @retval 0 a and b are identical + * @retval 1 b precedes a + */ +int mutt_file_timespec_compare(struct timespec *a, struct timespec *b) +{ + if (a->tv_sec < b->tv_sec) + return -1; + if (a->tv_sec > b->tv_sec) + return 1; + + if (a->tv_nsec < b->tv_nsec) + return -1; + if (a->tv_nsec > b->tv_nsec) + return 1; + return 0; +} + +/** + * mutt_file_get_stat_timespec - Read the stat() time into a time value + * @param dest Time value to populate + * @param sb stat info + * @param type Type of stat info to read, e.g. #MUTT_STAT_ATIME + */ +void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *sb, enum MuttStatType type) +{ + dest->tv_sec = 0; + dest->tv_nsec = 0; + + switch (type) + { + case MUTT_STAT_ATIME: + dest->tv_sec = sb->st_atime; +#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC + dest->tv_nsec = sb->st_atim.tv_nsec; +#endif + break; + case MUTT_STAT_MTIME: + dest->tv_sec = sb->st_mtime; +#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC + dest->tv_nsec = sb->st_mtim.tv_nsec; +#endif + break; + case MUTT_STAT_CTIME: + dest->tv_sec = sb->st_ctime; +#ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC + dest->tv_nsec = sb->st_ctim.tv_nsec; +#endif + break; + } +} + +/** + * mutt_file_stat_timespec_compare - Compare stat info with a time value + * @param sba stat info + * @param type Type of stat info, e.g. #MUTT_STAT_ATIME + * @param b Time value + * @retval -1 a precedes b + * @retval 0 a and b are identical + * @retval 1 b precedes a + */ +int mutt_file_stat_timespec_compare(struct stat *sba, enum MuttStatType type, struct timespec *b) +{ + struct timespec a = { 0 }; + + mutt_file_get_stat_timespec(&a, sba, type); + return mutt_file_timespec_compare(&a, b); +} + +/** + * mutt_file_stat_compare - Compare two stat infos + * @param sba First stat info + * @param sba_type Type of first stat info, e.g. #MUTT_STAT_ATIME + * @param sbb Second stat info + * @param sbb_type Type of second stat info, e.g. #MUTT_STAT_ATIME + * @retval -1 a precedes b + * @retval 0 a and b are identical + * @retval 1 b precedes a + */ +int mutt_file_stat_compare(struct stat *sba, enum MuttStatType sba_type, + struct stat *sbb, enum MuttStatType sbb_type) +{ + struct timespec a = { 0 }; + struct timespec b = { 0 }; + + mutt_file_get_stat_timespec(&a, sba, sba_type); + mutt_file_get_stat_timespec(&b, sbb, sbb_type); + return mutt_file_timespec_compare(&a, &b); +} + diff --git a/mutt/file.h b/mutt/file.h index b857c1d3b..92ac96b28 100644 --- a/mutt/file.h +++ b/mutt/file.h @@ -23,6 +23,7 @@ #ifndef MUTT_LIB_FILE_H #define MUTT_LIB_FILE_H +#include "config.h" #include #include #include @@ -35,6 +36,29 @@ extern char *Tmpdir; #define MUTT_CONT (1 << 0) /**< \-continuation */ #define MUTT_EOL (1 << 1) /**< don't strip `\n` / `\r\n` */ +#ifndef HAVE_STRUCT_TIMESPEC +/** + * struct timespec - Time value with nanosecond precision + */ +struct timespec +{ + time_t tv_sec; + long tv_nsec; +}; +#endif + +/** + * enum MuttStatType - Flags for mutt_file_get_stat_timespec + * + * These represent filesystem timestamps returned by stat() + */ +enum MuttStatType +{ + MUTT_STAT_ATIME, + MUTT_STAT_MTIME, + MUTT_STAT_CTIME +}; + int mutt_file_check_empty(const char *path); int mutt_file_chmod(const char *path, mode_t mode); int mutt_file_chmod_add(const char *path, mode_t mode); @@ -50,6 +74,7 @@ int mutt_file_fclose(FILE **f); FILE * mutt_file_fopen(const char *path, const char *mode); int mutt_file_fsync_close(FILE **f); long mutt_file_get_size(const char *path); +void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *sb, enum MuttStatType type); int mutt_file_lock(int fd, bool excl, bool timeout); int mutt_file_mkdir(const char *path, mode_t mode); FILE * mutt_file_mkstemp_full(const char *file, int line, const char *func); @@ -64,7 +89,10 @@ int mutt_file_safe_rename(const char *src, const char *target); void mutt_file_sanitize_filename(char *f, bool slash); int mutt_file_sanitize_regex(char *dest, size_t destlen, const char *src); void mutt_file_set_mtime(const char *from, const char *to); +int mutt_file_stat_compare(struct stat *sba, enum MuttStatType sba_type, struct stat *sbb, enum MuttStatType sbb_type); +int mutt_file_stat_timespec_compare(struct stat *sba, enum MuttStatType type, struct timespec *b); int mutt_file_symlink(const char *oldpath, const char *newpath); +int mutt_file_timespec_compare(struct timespec *a, struct timespec *b); void mutt_file_touch_atime(int fd); void mutt_file_unlink(const char *s); void mutt_file_unlink_empty(const char *path); diff --git a/muttlib.c b/muttlib.c index aa06d981f..718765aa4 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1421,100 +1421,6 @@ void mutt_sleep(short s) sleep(s); } -/** - * mutt_timespec_compare - Compare to time values - * @param a First time value - * @param b Second time value - * @retval -1 a precedes b - * @retval 0 a and b are identical - * @retval 1 b precedes a - */ -int mutt_timespec_compare(struct timespec *a, struct timespec *b) -{ - if (a->tv_sec < b->tv_sec) - return -1; - if (a->tv_sec > b->tv_sec) - return 1; - - if (a->tv_nsec < b->tv_nsec) - return -1; - if (a->tv_nsec > b->tv_nsec) - return 1; - return 0; -} - -/** - * mutt_get_stat_timespec - Read the stat() time into a time value - * @param dest Time value to populate - * @param sb stat info - * @param type Type of stat info to read, e.g. #MUTT_STAT_ATIME - */ -void mutt_get_stat_timespec(struct timespec *dest, struct stat *sb, enum MuttStatType type) -{ - dest->tv_sec = 0; - dest->tv_nsec = 0; - - switch (type) - { - case MUTT_STAT_ATIME: - dest->tv_sec = sb->st_atime; -#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC - dest->tv_nsec = sb->st_atim.tv_nsec; -#endif - break; - case MUTT_STAT_MTIME: - dest->tv_sec = sb->st_mtime; -#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC - dest->tv_nsec = sb->st_mtim.tv_nsec; -#endif - break; - case MUTT_STAT_CTIME: - dest->tv_sec = sb->st_ctime; -#ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC - dest->tv_nsec = sb->st_ctim.tv_nsec; -#endif - break; - } -} - -/** - * mutt_stat_timespec_compare - Compare stat info with a time value - * @param sba stat info - * @param type Type of stat info, e.g. #MUTT_STAT_ATIME - * @param b Time value - * @retval -1 a precedes b - * @retval 0 a and b are identical - * @retval 1 b precedes a - */ -int mutt_stat_timespec_compare(struct stat *sba, enum MuttStatType type, struct timespec *b) -{ - struct timespec a = { 0 }; - - mutt_get_stat_timespec(&a, sba, type); - return mutt_timespec_compare(&a, b); -} - -/** - * mutt_stat_compare - Compare two stat infos - * @param sba First stat info - * @param sba_type Type of first stat info, e.g. #MUTT_STAT_ATIME - * @param sbb Second stat info - * @param sbb_type Type of second stat info, e.g. #MUTT_STAT_ATIME - * @retval -1 a precedes b - * @retval 0 a and b are identical - * @retval 1 b precedes a - */ -int mutt_stat_compare(struct stat *sba, enum MuttStatType sba_type, - struct stat *sbb, enum MuttStatType sbb_type) -{ - struct timespec a = { 0 }; - struct timespec b = { 0 }; - - mutt_get_stat_timespec(&a, sba, sba_type); - mutt_get_stat_timespec(&b, sbb, sbb_type); - return mutt_timespec_compare(&a, &b); -} - /** * mutt_make_version - Generate the NeoMutt version string * @retval ptr Version string diff --git a/muttlib.h b/muttlib.h index 4fed3c50f..df2b1a393 100644 --- a/muttlib.h +++ b/muttlib.h @@ -66,11 +66,6 @@ int mutt_save_confirm(const char *s, struct stat *st); void mutt_save_path(char *d, size_t dsize, struct Address *a); void mutt_sleep(short s); -int mutt_timespec_compare(struct timespec *a, struct timespec *b); -void mutt_get_stat_timespec(struct timespec *dest, struct stat *sb, enum MuttStatType type); -int mutt_stat_timespec_compare(struct stat *sba, enum MuttStatType type, struct timespec *b); -int mutt_stat_compare(struct stat *sba, enum MuttStatType sba_type, struct stat *sbb, enum MuttStatType sbb_type); - #define mutt_mktemp(a, b) mutt_mktemp_pfx_sfx(a, b, "neomutt", NULL) #define mutt_mktemp_pfx_sfx(a, b, c, d) mutt_mktemp_full(a, b, c, d, __FILE__, __LINE__)