From 83d6e85edb7180dc8363cfcc3f87ef11cb177d6a Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Fri, 4 Aug 2017 03:23:15 +0100 Subject: [PATCH] refactor: move atime/mtime functions to lib/file --- lib/file.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/file.h | 6 +++++ muttlib.c | 57 ------------------------------------------ protos.h | 3 --- 4 files changed, 78 insertions(+), 60 deletions(-) diff --git a/lib/file.c b/lib/file.c index 3d6d2b504..95e944012 100644 --- a/lib/file.c +++ b/lib/file.c @@ -32,12 +32,15 @@ * | mutt_concat_path() | Join a directory name and a filename * | mutt_copy_bytes() | Copy some content from one file to another * | mutt_copy_stream() | Copy the contents of one file into another + * | mutt_decrease_mtime() | Decrease a file's modification time by 1 second * | mutt_mkdir() | Recursively create directories * | mutt_quote_filename() | Quote a filename to survive the shell's quoting rules * | mutt_read_line() | Read a line from a file * | mutt_rmtree() | Recursively remove a directory * | mutt_rx_sanitize_string() | Escape any regex-magic characters in a string * | mutt_sanitize_filename() | Replace unsafe characters in a filename + * | mutt_set_mtime() | Set the modification time of one file from another + * | mutt_touch_atime() | Set the access time to current time * | mutt_unlink() | Delete a file, carefully * | safe_fclose() | Close a FILE handle (and NULL the pointer) * | safe_fopen() | Call fopen() safely @@ -59,6 +62,7 @@ #include #include #include +#include #include "file.h" #include "debug.h" #include "memory.h" @@ -870,3 +874,71 @@ int mutt_mkdir(const char *path, mode_t mode) return 0; } + +/** + * mutt_decrease_mtime - Decrease a file's modification time by 1 second + * @param f Filename + * @param st struct stat for the file (optional) + * @retval num Updated Unix mtime + * @retval -1 Error, see errno + * + * If a file's mtime is NOW, then set it to 1 second in the past. + */ +time_t mutt_decrease_mtime(const char *f, struct stat *st) +{ + struct utimbuf utim; + struct stat _st; + time_t mtime; + + if (!st) + { + if (stat(f, &_st) == -1) + return -1; + st = &_st; + } + + mtime = st->st_mtime; + if (mtime == time(NULL)) + { + mtime -= 1; + utim.actime = mtime; + utim.modtime = mtime; + utime(f, &utim); + } + + return mtime; +} + +/** + * mutt_set_mtime - Set the modification time of one file from another + * @param from Filename whose mtime should be copied + * @param to Filename to update + */ +void mutt_set_mtime(const char *from, const char *to) +{ + struct utimbuf utim; + struct stat st; + + if (stat(from, &st) != -1) + { + utim.actime = st.st_mtime; + utim.modtime = st.st_mtime; + utime(to, &utim); + } +} + +/** + * mutt_touch_atime - Set the access time to current time + * @param f File descriptor of the file to alter + * + * This is just as read() would do on !noatime. + * Silently ignored if futimens() isn't supported. + */ +void mutt_touch_atime(int f) +{ +#ifdef HAVE_FUTIMENS + struct timespec times[2] = { { 0, UTIME_NOW }, { 0, UTIME_OMIT } }; + futimens(f, times); +#endif +} + diff --git a/lib/file.h b/lib/file.h index c0c136524..dcaa3365b 100644 --- a/lib/file.h +++ b/lib/file.h @@ -25,6 +25,9 @@ #include #include +#include + +struct stat; #undef MAX #undef MIN @@ -40,12 +43,15 @@ char * mutt_concatn_path(char *dst, size_t dstlen, const char *dir, size_t char * mutt_concat_path(char *d, const char *dir, const char *fname, size_t l); int mutt_copy_bytes(FILE *in, FILE *out, size_t size); int mutt_copy_stream(FILE *fin, FILE *fout); +time_t mutt_decrease_mtime(const char *f, struct stat *st); int mutt_mkdir(const char *path, mode_t mode); size_t mutt_quote_filename(char *d, size_t l, const char *f); char * mutt_read_line(char *s, size_t *size, FILE *fp, int *line, int flags); int mutt_rmtree(const char *path); int mutt_rx_sanitize_string(char *dest, size_t destlen, const char *src); void mutt_sanitize_filename(char *f, short slash); +void mutt_set_mtime(const char *from, const char *to); +void mutt_touch_atime(int f); void mutt_unlink(const char *s); int safe_fclose(FILE **f); FILE * safe_fopen(const char *path, const char *mode); diff --git a/muttlib.c b/muttlib.c index 3e822abbd..d51d2583b 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1912,63 +1912,6 @@ void mutt_sleep(short s) sleep(s); } -/** - * mutt_decrease_mtime - Decrease a file's modification time by 1 second - */ -time_t mutt_decrease_mtime(const char *f, struct stat *st) -{ - struct utimbuf utim; - struct stat _st; - time_t mtime; - - if (!st) - { - if (stat(f, &_st) == -1) - return -1; - st = &_st; - } - - if ((mtime = st->st_mtime) == time(NULL)) - { - mtime -= 1; - utim.actime = mtime; - utim.modtime = mtime; - utime(f, &utim); - } - - return mtime; -} - -/** - * mutt_set_mtime - sets mtime of 'to' to mtime of 'from' - */ -void mutt_set_mtime(const char *from, const char *to) -{ - struct utimbuf utim; - struct stat st; - - if (stat(from, &st) != -1) - { - utim.actime = st.st_mtime; - utim.modtime = st.st_mtime; - utime(to, &utim); - } -} - -/** - * mutt_touch_atime - set atime to current time - * - * This is just as read() would do on !noatime. - * Silently ignored if unsupported. - */ -void mutt_touch_atime(int f) -{ -#ifdef HAVE_FUTIMENS - struct timespec times[2] = { { 0, UTIME_NOW }, { 0, UTIME_OMIT } }; - futimens(f, times); -#endif -} - const char *mutt_make_version(void) { static char vstring[STRING]; diff --git a/protos.h b/protos.h index 27fb80c8b..88f40574a 100644 --- a/protos.h +++ b/protos.h @@ -122,11 +122,8 @@ struct Content *mutt_get_content_info(const char *fname, struct Body *b); char *mutt_read_rfc822_line(FILE *f, char *line, size_t *linelen); struct Envelope *mutt_read_rfc822_header(FILE *f, struct Header *hdr, short user_hdrs, short weed); -void mutt_set_mtime(const char *from, const char *to); -time_t mutt_decrease_mtime(const char *f, struct stat *st); time_t mutt_parse_date(const char *s, struct Header *h); int is_from(const char *s, char *path, size_t pathlen, time_t *tp); -void mutt_touch_atime(int f); const char *mutt_attach_fmt(char *dest, size_t destlen, size_t col, int cols, char op, const char *src, const char *prefix, -- 2.40.0