[videodb] Remove nested transaction when saving state after stopping PVR playback
[xbmc.git] / xbmc / filesystem / XbtManager.cpp
blob0346add1cec1ae72ac5a5d93faf32a85a5b3de3e
1 /*
2 * Copyright (C) 2015-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "XbtManager.h"
11 #include "URL.h"
12 #include "guilib/XBTF.h"
13 #include "guilib/XBTFReader.h"
15 #include <utility>
17 namespace XFILE
20 CXbtManager::CXbtManager() = default;
22 CXbtManager::~CXbtManager() = default;
24 CXbtManager& CXbtManager::GetInstance()
26 static CXbtManager xbtManager;
27 return xbtManager;
30 bool CXbtManager::HasFiles(const CURL& path) const
32 std::lock_guard l(m_lock);
33 return ProcessFile(path) != m_readers.end();
36 bool CXbtManager::GetFiles(const CURL& path, std::vector<CXBTFFile>& files) const
38 std::lock_guard l(m_lock);
39 const auto& reader = ProcessFile(path);
40 if (reader == m_readers.end())
41 return false;
43 files = reader->second.reader->GetFiles();
44 return true;
47 bool CXbtManager::GetReader(const CURL& path, CXBTFReaderPtr& reader) const
49 std::lock_guard l(m_lock);
50 const auto& it = ProcessFile(path);
51 if (it == m_readers.end())
52 return false;
54 reader = it->second.reader;
55 return true;
58 void CXbtManager::Release(const CURL& path)
60 std::lock_guard l(m_lock);
61 const auto& it = GetReader(path);
62 if (it == m_readers.end())
63 return;
65 RemoveReader(it);
68 CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const CURL& path) const
70 return GetReader(NormalizePath(path));
73 CXbtManager::XBTFReaders::iterator CXbtManager::GetReader(const std::string& path) const
75 if (path.empty())
76 return m_readers.end();
78 return m_readers.find(path);
81 void CXbtManager::RemoveReader(XBTFReaders::iterator readerIterator) const
83 if (readerIterator == m_readers.end())
84 return;
86 // close the reader
87 readerIterator->second.reader->Close();
89 // and remove it from the map
90 m_readers.erase(readerIterator);
93 CXbtManager::XBTFReaders::const_iterator CXbtManager::ProcessFile(const CURL& path) const
95 std::string filePath = NormalizePath(path);
97 // check if the file is known
98 auto it = GetReader(filePath);
99 if (it != m_readers.end())
101 // check if the XBT file has been modified
102 if (it->second.reader->GetLastModificationTimestamp() <= it->second.lastModification)
103 return it;
105 // the XBT file has been modified so close and remove it from the cache
106 // it will be re-opened by the following logic
107 RemoveReader(it);
110 // try to read the file
111 CXBTFReaderPtr reader(new CXBTFReader());
112 if (!reader->Open(filePath))
113 return m_readers.end();
115 XBTFReader xbtfReader = {
116 reader,
117 reader->GetLastModificationTimestamp()
119 std::pair<XBTFReaders::iterator, bool> result = m_readers.insert(std::make_pair(filePath, xbtfReader));
120 return result.first;
123 std::string CXbtManager::NormalizePath(const CURL& path)
125 if (path.IsProtocol("xbt"))
126 return path.GetHostName();
128 return path.Get();