diff --git a/src/XrdApps.cmake b/src/XrdApps.cmake index d990636d079..e5c208ae768 100644 --- a/src/XrdApps.cmake +++ b/src/XrdApps.cmake @@ -229,6 +229,8 @@ add_library( XrdApps/XrdClJCachePlugin/cache/Journal.hh XrdApps/XrdClJCachePlugin/cache/IntervalTree.hh XrdApps/XrdClJCachePlugin/cache/RbTree.hh + XrdApps/XrdClJCachePlugin/cleaner/Cleaner.cc + XrdApps/XrdClJCachePlugin/cleaner/Cleaner.hh ) target_include_directories( ${LIB_XRDCL_JCACHE_PLUGIN} PRIVATE "${CMAKE_SOURCE_DIR}/src/XrdApps/XrdClJCachePlugin/" ) diff --git a/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc b/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc index c164236e9b5..5bcec2ed07c 100644 --- a/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc +++ b/src/XrdApps/XrdClJCachePlugin/cache/Journal.cc @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include /*----------------------------------------------------------------------------*/ //------------------------------------------------------------------------------ @@ -53,6 +56,14 @@ Journal::Journal() : cachesize(0), max_offset(0), fd(-1) { Journal::~Journal() { std::lock_guard guard(mtx); if (fd > 0) { + struct flock lock; + std::memset(&lock, 0, sizeof(lock)); + + lock.l_type = F_UNLCK; // Unlock the file + if (fcntl(fd, F_SETLK, &lock) == -1) { + std::cerr << "error: failed to unlock journal: " << std::strerror(errno) << std::endl; + } + int rc = close(fd); if (rc) { @@ -171,6 +182,30 @@ int Journal::attach(const std::string &lpath, uint64_t mtime, return -errno; } + // get a POSIX lock on the file + struct flock lock; + std::memset(&lock, 0, sizeof(lock)); + lock.l_type = F_WRLCK; // Request a write lock + lock.l_whence = SEEK_SET; // Lock from the beginning of the file + lock.l_start = 0; // Starting offset for lock + lock.l_len = 0; // 0 means to lock the entire file + + if (fcntl(fd, F_SETLK, &lock) == -1) { + if (errno == EACCES || errno == EAGAIN) { + std::cerr << "error: journal file is already locked by another process." + << std::endl; + close(fd); + fd = -1; + return -errno; + } else { + std::cerr << "error: failed to lock journal file: " << std::strerror(errno) + << std::endl; + close(fd); + fd = -1; + return -errno; + } + } + break; } while (1); diff --git a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc index d11f5725187..8f3af81f254 100644 --- a/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc +++ b/src/XrdApps/XrdClJCachePlugin/file/XrdClJCacheFile.cc @@ -463,12 +463,12 @@ bool JCacheFile::AttachForRead() { } if (pJournal->attach(pJournalPath, sinfo->GetModTime(), 0, sinfo->GetSize())) { - mLog->Error(1, "JCache : failed to attach to cache directory: %s", + mLog->Error(1, "JCache : failed to attach to cache file: %s", pJournalPath.c_str()); mAttachedForRead = true; return false; } else { - mLog->Info(1, "JCache : attached to cache directory: %s", + mLog->Info(1, "JCache : attached to cache file: %s", pJournalPath.c_str()); } }