From ccbe5aa376c5d69811b167e78cbb6e389cbdc9a2 Mon Sep 17 00:00:00 2001 From: peak3d Date: Sat, 8 Oct 2016 21:00:45 +0200 Subject: [PATCH] support reusing of CFile::m_pFile --- xbmc/filesystem/CurlFile.cpp | 6 ++++++ xbmc/filesystem/CurlFile.h | 1 + xbmc/filesystem/File.cpp | 17 +++++++++++++++++ xbmc/filesystem/File.h | 17 +++++++++++++++-- xbmc/filesystem/IFile.h | 1 + xbmc/filesystem/IFileTypes.h | 3 +++ 6 files changed, 43 insertions(+), 2 deletions(-) diff --git a/xbmc/filesystem/CurlFile.cpp b/xbmc/filesystem/CurlFile.cpp index a6d7fe0952b90..2da3f160c683e 100644 --- a/xbmc/filesystem/CurlFile.cpp +++ b/xbmc/filesystem/CurlFile.cpp @@ -1189,6 +1189,12 @@ bool CCurlFile::CReadState::ReadString(char *szLine, int iLineLength) return (bool)((pLine - szLine) > 0); } +bool CCurlFile::ReOpen(const CURL& url) +{ + Close(); + return Open(url); +} + bool CCurlFile::Exists(const CURL& url) { // if file is already running, get info from it diff --git a/xbmc/filesystem/CurlFile.h b/xbmc/filesystem/CurlFile.h index 9fedd554f894a..d65b843105a0d 100644 --- a/xbmc/filesystem/CurlFile.h +++ b/xbmc/filesystem/CurlFile.h @@ -51,6 +51,7 @@ namespace XFILE virtual ~CCurlFile(); virtual bool Open(const CURL& url); virtual bool OpenForWrite(const CURL& url, bool bOverWrite = false); + virtual bool ReOpen(const CURL& url); virtual bool Exists(const CURL& url); virtual int64_t Seek(int64_t iFilePosition, int iWhence=SEEK_SET); virtual int64_t GetPosition(); diff --git a/xbmc/filesystem/File.cpp b/xbmc/filesystem/File.cpp index 27fcd554c32e2..c41db03b65c77 100644 --- a/xbmc/filesystem/File.cpp +++ b/xbmc/filesystem/File.cpp @@ -254,6 +254,19 @@ bool CFile::Open(const std::string& strFileName, const unsigned int flags) bool CFile::Open(const CURL& file, const unsigned int flags) { + if (m_pFile) + { + if ((flags & READ_REOPEN) == 0) + { + CLog::Log(LOGERROR, "File::Open - already open: %s", file.GetRedacted().c_str()); + return false; + } + else + { + return m_pFile->ReOpen(URIUtils::SubstitutePath(file)); + } + } + m_flags = flags; try { @@ -280,6 +293,10 @@ bool CFile::Open(const CURL& file, const unsigned int flags) { // for internet stream, if it contains multiple stream, file cache need handle it specially. m_pFile = new CFileCache(m_flags); + + if (!m_pFile) + return false; + return m_pFile->Open(url); } } diff --git a/xbmc/filesystem/File.h b/xbmc/filesystem/File.h index 272ec256d0bd6..30f38965dcd18 100644 --- a/xbmc/filesystem/File.h +++ b/xbmc/filesystem/File.h @@ -64,12 +64,25 @@ class CFile bool CURLAddOption(XFILE::CURLOPTIONTYPE type, const char* name, const char * value); bool CURLOpen(unsigned int flags); + /** + * Attempt to open an IFile instance. + * @param file reference to CCurl file description + * @param flags see IFileTypes.h + * @return true on success, false otherwise + * + * Remarks: Open can only be called once. Calling + * Open() on an already opened file will fail + * exept flag READ_REOPEN is set and the underlying + * file has an implementation of ReOpen(). + */ bool Open(const CURL& file, const unsigned int flags = 0); + bool Open(const std::string& strFileName, const unsigned int flags = 0); + bool OpenForWrite(const CURL& file, bool bOverWrite = false); + bool OpenForWrite(const std::string& strFileName, bool bOverWrite = false); + ssize_t LoadFile(const CURL &file, auto_buffer& outputBuffer); - bool Open(const std::string& strFileName, const unsigned int flags = 0); - bool OpenForWrite(const std::string& strFileName, bool bOverWrite = false); /** * Attempt to read bufSize bytes from currently opened file into buffer bufPtr. * @param bufPtr pointer to buffer diff --git a/xbmc/filesystem/IFile.h b/xbmc/filesystem/IFile.h index 36bb37c4cc40c..49307d12c0dc6 100644 --- a/xbmc/filesystem/IFile.h +++ b/xbmc/filesystem/IFile.h @@ -61,6 +61,7 @@ class IFile virtual bool Open(const CURL& url) = 0; virtual bool OpenForWrite(const CURL& url, bool bOverWrite = false) { return false; }; + virtual bool ReOpen(const CURL& url) { return false; }; virtual bool Exists(const CURL& url) = 0; /** * Fills struct __stat64 with information about file specified by url. diff --git a/xbmc/filesystem/IFileTypes.h b/xbmc/filesystem/IFileTypes.h index 4c6dd98703528..7cafa2c69ff0e 100644 --- a/xbmc/filesystem/IFileTypes.h +++ b/xbmc/filesystem/IFileTypes.h @@ -49,6 +49,9 @@ namespace XFILE /* indicate that caller will do write operations before reading */ static const unsigned int READ_AFTER_WRITE = 0x80; +/* indicate that caller want to reopen a file if its already open */ + static const unsigned int READ_REOPEN = 0x100; + struct SNativeIoControl { unsigned long int request;