2 * Copyright (C) 2011-2013 Team XBMC
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)
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.
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/>.
21 // FileNFS.h: interface for the CNFSFile class.
27 #include "threads/CriticalSection.h"
29 #include "SectionLoader.h"
35 #define S_IWUSR _S_IWRITE
36 #define S_IRUSR _S_IREAD
37 #define S_IFLNK 0120000
39 #define S_ISBLK(m) (0)
40 #define S_ISSOCK(m) (0)
41 #define S_ISLNK(m) ((m & S_IFLNK) != 0)
42 #define S_ISCHR(m) ((m & _S_IFCHR) != 0)
43 #define S_ISDIR(m) ((m & _S_IFDIR) != 0)
44 #define S_ISFIFO(m) ((m & _S_IFIFO) != 0)
45 #define S_ISREG(m) ((m & _S_IFREG) != 0)
50 class CNfsConnection : public CCriticalSection
53 struct keepAliveStruct
55 std::string exportPath;
56 uint64_t refreshCounter;
58 typedef std::map<struct nfsfh *, struct keepAliveStruct> tFileKeepAliveMap;
62 struct nfs_context *pContext;
63 uint64_t lastAccessedTime;
66 typedef std::map<std::string, struct contextTimeout> tOpenContextMap;
70 bool Connect(const CURL &url, CStdString &relativePath);
71 struct nfs_context *GetNfsContext(){return m_pNfsContext;}
72 uint64_t GetMaxReadChunkSize(){return m_readChunkSize;}
73 uint64_t GetMaxWriteChunkSize(){return m_writeChunkSize;}
74 DllLibNfs *GetImpl(){return m_pLibNfs;}
75 std::list<std::string> GetExportList(const CURL &url);
76 //this functions splits the url into the exportpath (feed to mount) and the rest of the path
77 //relative to the mounted export
78 bool splitUrlIntoExportAndPath(const CURL& url, CStdString &exportPath, CStdString &relativePath, std::list<std::string> &exportList);
79 bool splitUrlIntoExportAndPath(const CURL& url, CStdString &exportPath, CStdString &relativePath);
81 //special stat which uses its own context
82 //needed for getting intervolume symlinks to work
83 int stat(const CURL &url, struct stat *statbuff);
85 void AddActiveConnection();
86 void AddIdleConnection();
89 bool HandleDyLoad();//loads the lib if needed
90 //adds the filehandle to the keep alive list or resets
91 //the timeout for this filehandle if already in list
92 void resetKeepAlive(std::string _exportPath, struct nfsfh *_pFileHandle);
93 //removes file handle from keep alive list
94 void removeFromKeepAliveList(struct nfsfh *_pFileHandle);
96 const CStdString& GetConnectedIp() const {return m_resolvedHostName;}
97 const CStdString& GetConnectedExport() const {return m_exportPath;}
98 const CStdString GetContextMapId() const {return m_hostName + m_exportPath;}
101 struct nfs_context *m_pNfsContext;//current nfs context
102 CStdString m_exportPath;//current connected export path
103 CStdString m_hostName;//current connected host
104 CStdString m_resolvedHostName;//current connected host - as ip
105 uint64_t m_readChunkSize;//current read chunksize of connected server
106 uint64_t m_writeChunkSize;//current write chunksize of connected server
107 int m_OpenConnections;//number of open connections
108 unsigned int m_IdleTimeout;//timeout for idle connection close and dyunload
109 tFileKeepAliveMap m_KeepAliveTimeouts;//mapping filehandles to its idle timeout
110 tOpenContextMap m_openContextMap;//unique map for tracking all open contexts
111 uint64_t m_lastAccessedTime;//last access time for m_pNfsContext
112 DllLibNfs *m_pLibNfs;//the lib
113 std::list<std::string> m_exportList;//list of exported pathes of current connected servers
114 CCriticalSection keepAliveLock;
115 CCriticalSection openContextLock;
118 struct nfs_context *getContextFromMap(const CStdString &exportname, bool forceCacheHit = false);
119 int getContextForExport(const CStdString &exportname);//get context for given export and add to open contexts map - sets m_pNfsContext (my return a already mounted cached context)
120 void destroyOpenContexts();
121 void destroyContext(const CStdString &exportName);
122 void resolveHost(const CURL &url);//resolve hostname by dnslookup
123 void keepAlive(std::string _exportPath, struct nfsfh *_pFileHandle);
126 extern CNfsConnection gNfsConnection;
130 class CNFSFile : public IFile
135 virtual void Close();
136 virtual int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
137 virtual unsigned int Read(void* lpBuf, int64_t uiBufSize);
138 virtual bool Open(const CURL& url);
139 virtual bool Exists(const CURL& url);
140 virtual int Stat(const CURL& url, struct __stat64* buffer);
141 virtual int Stat(struct __stat64* buffer);
142 virtual int64_t GetLength();
143 virtual int64_t GetPosition();
144 virtual int Write(const void* lpBuf, int64_t uiBufSize);
145 virtual int Truncate(int64_t iSize);
147 //implement iocontrol for seek_possible for preventing the stat in File class for
148 //getting this info ...
149 virtual int IoControl(EIoControl request, void* param){ if(request == IOCTRL_SEEK_POSSIBLE) return 1;return -1;};
150 virtual int GetChunkSize() {return 1;}
152 virtual bool OpenForWrite(const CURL& url, bool bOverWrite = false);
153 virtual bool Delete(const CURL& url);
154 virtual bool Rename(const CURL& url, const CURL& urlnew);
157 bool IsValidFile(const CStdString& strFileName);
159 struct nfsfh *m_pFileHandle;
160 struct nfs_context *m_pNfsContext;//current nfs context
161 std::string m_exportPath;