From 274321524c395f583633989d040dd98ed4aa11ed Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 31 May 2020 23:02:52 +0900 Subject: [PATCH] Relink cache stats file atomically via rename The new file may already exist so link may fail. Further link/unlink is not atomic. Addresses an error when renaming an open with with use_cache. References #1296. --- src/fdcache.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/fdcache.cpp b/src/fdcache.cpp index 7ee80ea..ac5656f 100644 --- a/src/fdcache.cpp +++ b/src/fdcache.cpp @@ -1525,13 +1525,9 @@ bool FdEntity::RenamePath(const string& newpath, string& fentmapkey) return false; } - // link and unlink cache file - if(-1 == link(cachepath.c_str(), newcachepath.c_str())){ - S3FS_PRN_ERR("failed to link old cache path(%s) to new cache path(%s) by errno(%d).", cachepath.c_str(), newcachepath.c_str(), errno); - return false; - } - if(-1 == unlink(cachepath.c_str())){ - S3FS_PRN_ERR("failed to unlink old cache path(%s) by errno(%d).", cachepath.c_str(), errno); + // rename cache file + if(-1 == rename(cachepath.c_str(), newcachepath.c_str())){ + S3FS_PRN_ERR("failed to rename old cache path(%s) to new cache path(%s) by errno(%d).", cachepath.c_str(), newcachepath.c_str(), errno); return false; }