circache: handle the case where the cache dir exists but not the file

This commit is contained in:
Jean-Francois Dockes 2012-09-14 08:24:40 +02:00
parent 713a98de30
commit ff1bf58ae0

View File

@ -621,21 +621,33 @@ bool CirCache::create(off_t m_maxsize, int flags)
LOGERR(("CirCache::create: null data\n")); LOGERR(("CirCache::create: null data\n"));
return false; return false;
} }
{
struct stat st; struct stat st;
if (stat(m_dir.c_str(), &st) < 0) { if (stat(m_dir.c_str(), &st) < 0) {
// Directory does not exist, create it
if (mkdir(m_dir.c_str(), 0777) < 0) { if (mkdir(m_dir.c_str(), 0777) < 0) {
m_d->m_reason << "CirCache::create: mkdir(" << m_dir << m_d->m_reason << "CirCache::create: mkdir(" << m_dir <<
") failed" << " errno " << errno; ") failed" << " errno " << errno;
return false; return false;
} }
} else { } else {
if (!(flags & CC_CRTRUNCATE)) // Directory exists but file might still not exist:
// e.g. the user is using a non default directory and
// created it for us.
if (access(m_d->datafn(m_dir).c_str(), 0) >= 0) {
// File exists, switch to "open" mode, except if we're told to
// truncate.
if (!(flags & CC_CRTRUNCATE)) {
return open(CC_OPWRITE); return open(CC_OPWRITE);
} }
}
// Else fall through to create file
}
}
if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(),
O_CREAT | O_RDWR | O_TRUNC, O_CREAT | O_RDWR | O_TRUNC, 0666)) < 0) {
0666)) < 0) {
m_d->m_reason << "CirCache::create: open/creat(" << m_d->m_reason << "CirCache::create: open/creat(" <<
m_d->datafn(m_dir) << ") failed " << "errno " << errno; m_d->datafn(m_dir) << ") failed " << "errno " << errno;
return false; return false;