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