From: Timo Sirainen Date: Thu, 26 Apr 2018 16:38:55 +0000 (+0300) Subject: lib-fs: fs-posix - Add accurate-mtime parameter X-Git-Tag: 2.3.9~1908 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d0d7b0a1de722f181605a98b003005f91077f5a5;p=thirdparty%2Fdovecot%2Fcore.git lib-fs: fs-posix - Add accurate-mtime parameter This is mainly useful for testing to find out whether one file was created after another. --- diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 5c7952c60f..b999f7d8f7 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -37,6 +37,7 @@ struct posix_fs { bool mode_auto; bool have_dirs; bool disable_fsync; + bool accurate_mtime; }; struct posix_fs_file { @@ -106,6 +107,8 @@ fs_posix_init(struct fs *_fs, const char *args, const struct fs_settings *set) fs->have_dirs = TRUE; } else if (strcmp(arg, "no-fsync") == 0) { fs->disable_fsync = TRUE; + } else if (strcmp(arg, "accurate-mtime") == 0) { + fs->accurate_mtime = TRUE; } else if (str_begins(arg, "mode=")) { unsigned int mode; if (str_to_uint_oct(arg+5, &mode) < 0) { @@ -475,6 +478,21 @@ static int fs_posix_write_finish(struct posix_fs_file *file) return -1; } } + if (fs->accurate_mtime) { + /* Linux updates the mtime timestamp only on timer interrupts. + This isn't anywhere close to being microsecond precision. + If requested, use utimes() to explicitly set a more accurate + mtime. */ + struct timeval tv[2]; + if (gettimeofday(&tv[0], NULL) < 0) + i_fatal("gettimeofday() failed: %m"); + tv[1] = tv[0]; + if ((utimes(file->temp_path, tv)) < 0) { + fs_set_error(file->file.fs, "utimes(%s) failed: %m", + file->temp_path); + return -1; + } + } fs_posix_write_rename_if_needed(file); switch (file->open_mode) {