Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / filesystem / MultiPathFile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "MultiPathFile.h"
22 #include "MultiPathDirectory.h"
23 #include "utils/URIUtils.h"
24 #include "URL.h"
25
26 using namespace XFILE;
27 using namespace std;
28 using namespace XFILE;
29
30 CMultiPathFile::CMultiPathFile(void)
31 {
32 }
33
34 CMultiPathFile::~CMultiPathFile(void)
35 {
36   Close();
37 }
38
39 bool CMultiPathFile::Open(const CURL& url)
40 {
41   // grab the filename off the url
42   CStdString path, fileName;
43   URIUtils::Split(url.Get(), path, fileName);
44   vector<CStdString> vecPaths;
45   if (!CMultiPathDirectory::GetPaths(path, vecPaths))
46     return false;
47
48   for (unsigned int i = 0; i < vecPaths.size(); i++)
49   {
50     CStdString filePath = vecPaths[i];
51     filePath = URIUtils::AddFileToFolder(filePath, fileName);
52     if (m_file.Open(filePath))
53       return true;
54   }
55   return false;
56 }
57
58 bool CMultiPathFile::Exists(const CURL& url)
59 {
60   // grab the filename off the url
61   CStdString path, fileName;
62   URIUtils::Split(url.Get(), path, fileName);
63   vector<CStdString> vecPaths;
64   if (!CMultiPathDirectory::GetPaths(path, vecPaths))
65     return false;
66
67   for (unsigned int i = 0; i < vecPaths.size(); i++)
68   {
69     CStdString filePath = vecPaths[i];
70     filePath = URIUtils::AddFileToFolder(filePath, fileName);
71     if (CFile::Exists(filePath))
72       return true;
73   }
74   return false;
75 }
76
77 int CMultiPathFile::Stat(const CURL& url, struct __stat64* buffer)
78 {
79   // grab the filename off the url
80   CStdString path, fileName;
81   URIUtils::Split(url.Get(), path, fileName);
82   vector<CStdString> vecPaths;
83   if (!CMultiPathDirectory::GetPaths(path, vecPaths))
84     return false;
85
86   for (unsigned int i = 0; i < vecPaths.size(); i++)
87   {
88     CStdString filePath = vecPaths[i];
89     filePath = URIUtils::AddFileToFolder(filePath, fileName);
90     int ret = CFile::Stat(filePath, buffer);
91     if (ret == 0)
92       return ret;
93   }
94   return -1;
95 }
96
97 unsigned int CMultiPathFile::Read(void* lpBuf, int64_t uiBufSize)
98 {
99   return m_file.Read(lpBuf, uiBufSize);
100 }
101
102 int64_t CMultiPathFile::Seek(int64_t iFilePosition, int iWhence /*=SEEK_SET*/)
103 {
104   return m_file.Seek(iFilePosition, iWhence);
105 }
106
107 void CMultiPathFile::Close()
108 {
109   m_file.Close();
110 }
111
112 int64_t CMultiPathFile::GetPosition()
113 {
114   return m_file.GetPosition();
115 }
116
117 int64_t CMultiPathFile::GetLength()
118 {
119   return m_file.GetLength();
120 }