From d23e918d954e985b13295ba00353e4f47759e59e Mon Sep 17 00:00:00 2001 From: Xavier Hernandez Date: Fri, 19 Jan 2018 12:18:13 +0100 Subject: storage/posix: fix fresh file detection delay Current implementation made it possible to consider that a file was not fresh even if it was created less than a second ago. This patch fixes the way in which the delay is computed to ensure that at least one second has elapsed. Change-Id: I05f7b99e7e8dd97e31f7ebaaec6c39eecf98b00f Updates: bz#1193929 Signed-off-by: Xavier Hernandez --- xlators/storage/posix/src/posix-helpers.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'xlators/storage/posix/src') diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c index d0fd45a805a..401f8ca8578 100644 --- a/xlators/storage/posix/src/posix-helpers.c +++ b/xlators/storage/posix/src/posix-helpers.c @@ -1641,14 +1641,26 @@ unlock: } static int -is_fresh_file(int64_t ctime_sec) +is_fresh_file(int64_t sec, int64_t ns) { struct timeval tv; + int64_t elapsed; gettimeofday(&tv, NULL); - if ((ctime_sec >= (tv.tv_sec - 1)) && (ctime_sec <= tv.tv_sec)) + elapsed = (tv.tv_sec - sec) * 1000000L; + elapsed += tv.tv_usec - (ns / 1000L); + if (elapsed < 0) { + /* The file has been modified in the future !!! + * Is it fresh ? previous implementation considered this as a + * non-fresh file, so maintaining the same behavior. */ + return 0; + } + + /* If the file is newer than a second, we consider it fresh. */ + if (elapsed < 1000000) { return 1; + } return 0; } @@ -1711,7 +1723,9 @@ posix_gfid_heal(xlator_t *this, const char *path, loc_t *loc, dict_t *xattr_req) } ret = sys_lgetxattr(path, GFID_XATTR_KEY, uuid_curr, 16); if (ret != 16) { - if (is_fresh_file(stbuf.ia_ctime)) { + /* TODO: This is a very hacky way of doing this, and very prone to + * errors and unexpected behavior. This should be changed. */ + if (is_fresh_file(stbuf.ia_ctime, stbuf.ia_ctime_nsec)) { gf_msg(this->name, GF_LOG_ERROR, ENOENT, P_MSG_FRESHFILE, "Fresh file: %s", path); return -ENOENT; @@ -1723,7 +1737,9 @@ posix_gfid_heal(xlator_t *this, const char *path, loc_t *loc, dict_t *xattr_req) } ret = sys_lgetxattr(path, GFID_XATTR_KEY, uuid_curr, 16); if (ret != 16) { - if (is_fresh_file(stat.st_ctime)) { + /* TODO: This is a very hacky way of doing this, and very prone to + * errors and unexpected behavior. This should be changed. */ + if (is_fresh_file(stat.st_ctim.tv_sec, stat.st_ctim.tv_nsec)) { gf_msg(this->name, GF_LOG_ERROR, ENOENT, P_MSG_FRESHFILE, "Fresh file: %s", path); return -ENOENT; -- cgit