[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / filesystem / NFSFile.h
1 /*
2  *      Copyright (C) 2011-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 // FileNFS.h: interface for the CNFSFile class.
22 #ifndef FILENFS_H_
23 #define FILENFS_H_
24
25 #include "IFile.h"
26 #include "URL.h"
27 #include "threads/CriticalSection.h"
28 #include <list>
29 #include "SectionLoader.h"
30 #include <map>
31
32 #ifdef TARGET_WINDOWS
33 #define S_IRGRP 0
34 #define S_IROTH 0
35 #define S_IWUSR _S_IWRITE
36 #define S_IRUSR _S_IREAD
37 #define S_IFLNK 0120000
38
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)
46 #endif
47
48 class DllLibNfs;
49
50 class CNfsConnection : public CCriticalSection
51 {     
52 public:
53   struct keepAliveStruct
54   {
55     std::string exportPath;
56     uint64_t refreshCounter;
57   };
58   typedef std::map<struct nfsfh  *, struct keepAliveStruct> tFileKeepAliveMap;  
59
60   struct contextTimeout
61   {
62     struct nfs_context *pContext;
63     uint64_t lastAccessedTime;
64   };
65
66   typedef std::map<std::string, struct contextTimeout> tOpenContextMap;    
67   
68   CNfsConnection();
69   ~CNfsConnection();
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);
80   
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);
84
85   void AddActiveConnection();
86   void AddIdleConnection();
87   void CheckIfIdle();
88   void Deinit();
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);  
95   
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;}
99
100 private:
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;
116  
117   void clearMembers();
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);
124 };
125
126 extern CNfsConnection gNfsConnection;
127
128 namespace XFILE
129 {
130   class CNFSFile : public IFile
131   {
132   public:
133     CNFSFile();
134     virtual ~CNFSFile();
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);
146
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;}
151     
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);    
155   protected:
156     CURL m_url;
157     bool IsValidFile(const CStdString& strFileName);
158     int64_t m_fileSize;
159     struct nfsfh  *m_pFileHandle;
160     struct nfs_context *m_pNfsContext;//current nfs context
161     std::string m_exportPath;
162   };
163 }
164 #endif // FILENFS_H_
165
166