xbmc
NFSFile.h
1 /*
2  * Copyright (C) 2011-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 // FileNFS.h: interface for the CNFSFile class.
12 
13 #include "IFile.h"
14 #include "URL.h"
15 #include "threads/CriticalSection.h"
16 
17 #include <chrono>
18 #include <list>
19 #include <map>
20 
21 struct nfs_stat_64;
22 
23 class CNfsConnection : public CCriticalSection
24 {
25 public:
27  {
28  std::string exportPath;
29  std::chrono::time_point<std::chrono::steady_clock> refreshTime;
30  };
31  typedef std::map<struct nfsfh *, struct keepAliveStruct> tFileKeepAliveMap;
32 
34  {
35  struct nfs_context *pContext;
36  std::chrono::time_point<std::chrono::steady_clock> lastAccessedTime;
37  };
38 
39  typedef std::map<std::string, struct contextTimeout> tOpenContextMap;
40 
42  ~CNfsConnection();
43  bool Connect(const CURL &url, std::string &relativePath);
44  struct nfs_context *GetNfsContext() {return m_pNfsContext;}
45  uint64_t GetMaxReadChunkSize() {return m_readChunkSize;}
46  uint64_t GetMaxWriteChunkSize() {return m_writeChunkSize;}
47  std::list<std::string> GetExportList(const CURL &url);
48  //this functions splits the url into the exportpath (feed to mount) and the rest of the path
49  //relative to the mounted export
50  bool splitUrlIntoExportAndPath(const CURL& url, std::string &exportPath, std::string &relativePath, std::list<std::string> &exportList);
51  bool splitUrlIntoExportAndPath(const CURL& url, std::string &exportPath, std::string &relativePath);
52 
53  //special stat which uses its own context
54  //needed for getting intervolume symlinks to work
55  int stat(const CURL& url, nfs_stat_64* statbuff);
56 
57  void AddActiveConnection();
58  void AddIdleConnection();
59  void CheckIfIdle();
60  void Deinit();
61  //adds the filehandle to the keep alive list or resets
62  //the timeout for this filehandle if already in list
63  void resetKeepAlive(const std::string& _exportPath, struct nfsfh* _pFileHandle);
64  //removes file handle from keep alive list
65  void removeFromKeepAliveList(struct nfsfh *_pFileHandle);
66 
67  const std::string& GetConnectedIp() const {return m_resolvedHostName;}
68  const std::string& GetConnectedExport() const {return m_exportPath;}
69  const std::string GetContextMapId() const {return m_hostName + m_exportPath;}
70 
71 private:
72  enum class ContextStatus
73  {
74  INVALID,
75  NEW,
76  CACHED
77  };
78 
79  struct nfs_context *m_pNfsContext;//current nfs context
80  std::string m_exportPath;//current connected export path
81  std::string m_hostName;//current connected host
82  std::string m_resolvedHostName;//current connected host - as ip
83  uint64_t m_readChunkSize = 0;//current read chunksize of connected server
84  uint64_t m_writeChunkSize = 0;//current write chunksize of connected server
85  int m_OpenConnections = 0; //number of open connections
86  std::chrono::time_point<std::chrono::steady_clock> m_IdleTimeout;
87  tFileKeepAliveMap m_KeepAliveTimeouts;//mapping filehandles to its idle timeout
88  tOpenContextMap m_openContextMap;//unique map for tracking all open contexts
89  std::chrono::time_point<std::chrono::steady_clock>
90  m_lastAccessedTime; //last access time for m_pNfsContext
91  std::list<std::string> m_exportList;//list of exported paths of current connected servers
92  CCriticalSection keepAliveLock;
93  CCriticalSection openContextLock;
94 
95  void clearMembers();
96  struct nfs_context *getContextFromMap(const std::string &exportname, bool forceCacheHit = false);
97 
98  // get context for given export and add to open contexts map - sets m_pNfsContext (may return an already mounted cached context)
99  ContextStatus getContextForExport(const std::string& exportname);
100  void destroyOpenContexts();
101  void destroyContext(const std::string &exportName);
102  void resolveHost(const CURL &url);//resolve hostname by dnslookup
103  void keepAlive(const std::string& _exportPath, struct nfsfh* _pFileHandle);
104  static void setOptions(struct nfs_context* context);
105 };
106 
107 extern CNfsConnection gNfsConnection;
108 
109 namespace XFILE
110 {
111  class CNFSFile : public IFile
112  {
113  public:
114  CNFSFile();
115  ~CNFSFile() override;
116  void Close() override;
117  int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET) override;
118  ssize_t Read(void* lpBuf, size_t uiBufSize) override;
119  bool Open(const CURL& url) override;
120  bool Exists(const CURL& url) override;
121  int Stat(const CURL& url, struct __stat64* buffer) override;
122  int Stat(struct __stat64* buffer) override;
123  int64_t GetLength() override;
124  int64_t GetPosition() override;
125  ssize_t Write(const void* lpBuf, size_t uiBufSize) override;
126  int Truncate(int64_t iSize) override;
127 
128  //implement iocontrol for seek_possible for preventing the stat in File class for
129  //getting this info ...
130  int IoControl(EIoControl request, void* param) override
131  {
132  return request == IOCTRL_SEEK_POSSIBLE ? 1 : -1;
133  }
134  int GetChunkSize() override {return static_cast<int>(gNfsConnection.GetMaxReadChunkSize());}
135 
136  bool OpenForWrite(const CURL& url, bool bOverWrite = false) override;
137  bool Delete(const CURL& url) override;
138  bool Rename(const CURL& url, const CURL& urlnew) override;
139  protected:
140  CURL m_url;
141  bool IsValidFile(const std::string& strFileName);
142  int64_t m_fileSize = 0;
143  struct nfsfh *m_pFileHandle;
144  struct nfs_context *m_pNfsContext;//current nfs context
145  std::string m_exportPath;
146  };
147 }
148 
Definition: NFSFile.h:33
Definition: Scraper.h:41
Definition: URL.h:21
Definition: NFSFile.h:23
Definition: NFSFile.h:111
Definition: IFile.h:42
Definition: NFSFile.h:26