nx_storage_sdk  1.0
Storage SDK
ftp_library.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2 
3 #pragma once
4 
41 #include <memory>
42 #include <mutex>
43 #include <stdexcept>
44 #include <string>
45 #include <vector>
46 
47 #include <stdint.h>
48 
49 #include "ftplibpp/ftplib.h"
50 #include "storage/third_party_storage.h"
51 
52 namespace nx_spl
53 {
54  namespace aux
55  { // Generic reference counter Mix-In. Private inherit it.
56  template <typename P>
58  {
59  public:
61  : m_count(1)
62  {}
63 
64  int p_addRef() const { return ++m_count; }
65 
66  int p_releaseRef() const
67  {
68  int new_count = --m_count;
69  if (new_count <= 0) {
70  delete static_cast<const P*>(this);
71  }
72  return new_count;
73  }
74  private:
75  mutable std::atomic<int> m_count;
76  }; // class PluginRefCounter
77 
79  {
80  public:
81  NonCopyable() {}
82  NonCopyable(const NonCopyable&);
83  NonCopyable& operator =(const NonCopyable&);
84 
86  NonCopyable& operator =(NonCopyable&&);
87  }; // class NonCopyable
88 
95  {
96  std::string name;
97  std::string fullPath;
98  };
99 
100  } //namespace aux
101 
102  typedef std::shared_ptr<ftplib> implPtrType;
103  class FtpStorage;
104  // At construction phase we synchronise remote file with local one.
105  // During destruction synchronisation attempt is repeated.
106  // All intermediate actions (read/write/seek) are made with the local copy.
108  : public IODevice,
109  private aux::NonCopyable,
110  private aux::PluginRefCounter<FtpIODevice>
111  {
112  friend class aux::PluginRefCounter<FtpIODevice>;
113  public:
114  FtpIODevice(
115  const char *uri,
116  int mode,
117  const std::string &storageUrl,
118  const std::string &uname,
119  const std::string &upasswd
120  );
121 
122  virtual uint32_t STORAGE_METHOD_CALL write(
123  const void* src,
124  const uint32_t size,
125  int* ecode
126  ) override;
127 
128  virtual uint32_t STORAGE_METHOD_CALL read(
129  void* dst,
130  const uint32_t size,
131  int* ecode
132  ) const override;
133 
134  virtual int STORAGE_METHOD_CALL seek(
135  uint64_t pos,
136  int* ecode
137  ) override;
138 
139  virtual int STORAGE_METHOD_CALL getMode() const override;
140  virtual uint32_t STORAGE_METHOD_CALL size(int* ecode) const override;
141 
142  public: // plugin interface implementation
143  virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
144 
145  virtual int addRef() const override;
146  virtual int releaseRef() const override;
147 
148  private:
149  // synchronize localfile with remote one
150  void flush();
151  // delete only via releaseRef()
152  ~FtpIODevice();
153 
154  private:
155  int m_mode;
156  mutable int64_t m_pos;
157  std::string m_uri; //file URI
158  implPtrType m_impl;
159  aux::FileNameAndPath m_localfile;
160  bool m_altered;
161  long long m_localsize;
162  mutable
163  std::mutex m_mutex;
164  std::string m_implurl;
165  std::string m_user;
166  std::string m_passwd;
167  }; // class FtpIODevice
168 
169  // Fileinfo list is obtained from the server at construction phase.
170  // After this phase there are no real interactions with FTP server.
172  : public FileInfoIterator,
173  private aux::NonCopyable,
174  private aux::PluginRefCounter<FtpFileInfoIterator>
175  {
177 
178  typedef std::vector<std::string> FileListType;
179  typedef FileListType::const_iterator FileListIteratorType;
180  public:
182  ftplib &impl,
183  FileListType &&fileList, // caller doesn't really need this list after Iterator is constructed
184  const std::string &baseDir
185  );
186 
187  virtual FileInfo* STORAGE_METHOD_CALL next(int* ecode) const override;
188 
189  public: // plugin interface implementation
190  virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
191 
192  virtual int addRef() const override;
193  virtual int releaseRef() const override;
194 
195  private:
196  // In response to MLSD request list of structured file dsecription lines is returned.
197  // In this function we try to parse this line and get information we need.
198  int fileInfoFromMLSDString(
199  const char *mlsd, // In. Null-terminated.
200  FileInfo *fi, // Out
201  char *urlBuf // Out. Should be preallocated.
202  ) const;
203  // delete only with releaseRef()
205 
206  private:
207  mutable std::vector<char> m_urlData;
208  mutable FileInfo m_fileInfo;
209  ftplib& m_impl;
210  FileListType m_fileList;
211  mutable
212  FileListIteratorType m_curFile;
213  int m_basedirsize;
214  }; // class FtpFileListIterator
215 
217  : public Storage,
218  private aux::NonCopyable,
219  private aux::PluginRefCounter<FtpStorage>
220  {
221  friend class aux::PluginRefCounter<FtpStorage>;
222  // we need pointer because 'ftplib' default constructor can throw
223  // and we want to handle it explicitly.
224  public: // ctors, helper functions
225  FtpStorage(const std::string& url);
226  int getAvail() const {return m_available;}
227 
228  public: // Storage interface implementation
229  virtual int STORAGE_METHOD_CALL isAvailable() const override;
230 
231  virtual IODevice* STORAGE_METHOD_CALL open(
232  const char* uri,
233  int flags,
234  int* ecode
235  ) const override;
236 
237  virtual uint64_t STORAGE_METHOD_CALL getFreeSpace(int* ecode) const override;
238  virtual uint64_t STORAGE_METHOD_CALL getTotalSpace(int* ecode) const override;
239  virtual int STORAGE_METHOD_CALL getCapabilities() const override;
240 
241  virtual void STORAGE_METHOD_CALL removeFile(
242  const char* url,
243  int* ecode
244  ) override;
245 
246  virtual void STORAGE_METHOD_CALL removeDir(
247  const char* url,
248  int* ecode
249  ) override;
250 
251  virtual void STORAGE_METHOD_CALL renameFile(
252  const char* oldUrl,
253  const char* newUrl,
254  int* ecode
255  ) override;
256 
257  virtual FileInfoIterator* STORAGE_METHOD_CALL getFileIterator(
258  const char* dirUrl,
259  int* ecode
260  ) const override;
261 
262  virtual int STORAGE_METHOD_CALL fileExists(
263  const char* url,
264  int* ecode
265  ) const override;
266 
267  virtual int STORAGE_METHOD_CALL dirExists(
268  const char* url,
269  int* ecode
270  ) const override;
271 
272  virtual uint64_t STORAGE_METHOD_CALL fileSize(
273  const char* url,
274  int* ecode
275  ) const override;
276 
277  public: // plugin interface implementation
278  virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
279 
280  virtual int addRef() const override;
281  virtual int releaseRef() const override;
282 
283  private:
284  // destroy only via releaseRef()
285  ~FtpStorage();
286 
287  private:
288  implPtrType m_impl;
289  std::string m_implurl;
290  std::string m_user;
291  std::string m_passwd;
292  mutable std::mutex m_mutex;
293  mutable int m_available;
294  }; // class Ftpstorage
295 
297  : public StorageFactory,
298  private aux::NonCopyable,
299  private aux::PluginRefCounter<FtpStorageFactory>
300  {
302  public:
304  // currently unimplemented
305  virtual const char** STORAGE_METHOD_CALL findAvailable() const override;
306 
307  virtual Storage* STORAGE_METHOD_CALL createStorage(
308  const char* url,
309  int* ecode
310  ) override;
311 
312  virtual const char* STORAGE_METHOD_CALL storageType() const override;
313  virtual const char* lastErrorMessage(int ecode) const override;
314 
315  public: // plugin interface implementation
316  virtual void* queryInterface(const nxpl::NX_GUID& interfaceID) override;
317 
318  virtual int addRef() const override;
319  virtual int releaseRef() const override;
320  }; // class FtpStorageFactory
321 } // namespace Qn
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:1364
Definition: ftp_library.h:57
Definition: ftp_library.h:171
virtual int STORAGE_METHOD_CALL getCapabilities() const override
Definition: ftp_library.cpp:753
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:998
virtual void STORAGE_METHOD_CALL renameFile(const char *oldUrl, const char *newUrl, int *ecode) override
Definition: ftp_library.cpp:820
File information iterator abstraction.
Definition: third_party_storage.h:138
virtual void STORAGE_METHOD_CALL removeDir(const char *url, int *ecode) override
Definition: ftp_library.cpp:807
Definition: ftp_library.h:296
Definition: ftp_library.h:107
Storage factory abstraction.
Definition: third_party_storage.h:273
virtual int STORAGE_METHOD_CALL isAvailable() const override
Definition: ftp_library.cpp:712
virtual IODevice *STORAGE_METHOD_CALL open(const char *uri, int flags, int *ecode) const override
Definition: ftp_library.cpp:937
virtual const char **STORAGE_METHOD_CALL findAvailable() const override
Definition: ftp_library.cpp:1044
virtual const char * lastErrorMessage(int ecode) const override
Definition: ftp_library.cpp:1100
virtual uint64_t STORAGE_METHOD_CALL getTotalSpace(int *ecode) const override
Definition: ftp_library.cpp:742
virtual int STORAGE_METHOD_CALL seek(uint64_t pos, int *ecode) override
Definition: ftp_library.cpp:1300
Common file information.
Definition: third_party_storage.h:126
virtual Storage *STORAGE_METHOD_CALL createStorage(const char *url, int *ecode) override
Definition: ftp_library.cpp:1050
Storage plugin namespace.
Definition: ftp_library.cpp:28
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:668
Definition: ftplib.h:123
virtual uint64_t STORAGE_METHOD_CALL getFreeSpace(int *ecode) const override
Definition: ftp_library.cpp:731
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:1039
virtual int STORAGE_METHOD_CALL dirExists(const char *url, int *ecode) const override
Definition: ftp_library.cpp:899
Definition: ftp_library.h:216
GUID of plugin interface.
Definition: plugin_api.h:26
Definition: ftp_library.h:94
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:1015
Storage abstraction.
Definition: third_party_storage.h:155
virtual int STORAGE_METHOD_CALL fileExists(const char *url, int *ecode) const override
Definition: ftp_library.cpp:876
virtual FileInfo *STORAGE_METHOD_CALL next(int *ecode) const override
Definition: ftp_library.cpp:629
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:1340
virtual FileInfoIterator *STORAGE_METHOD_CALL getFileIterator(const char *dirUrl, int *ecode) const override
Definition: ftp_library.cpp:834
virtual const char *STORAGE_METHOD_CALL storageType() const override
Definition: ftp_library.cpp:1082
virtual uint32_t STORAGE_METHOD_CALL size(int *ecode) const override
Definition: ftp_library.cpp:1324
Definition: ftp_library.h:78
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:1034
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:979
virtual uint64_t STORAGE_METHOD_CALL fileSize(const char *url, int *ecode) const override
Definition: ftp_library.cpp:917
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:1003
virtual void STORAGE_METHOD_CALL removeFile(const char *url, int *ecode) override
Definition: ftp_library.cpp:795
virtual int STORAGE_METHOD_CALL getMode() const override
Definition: ftp_library.cpp:1319
virtual uint32_t STORAGE_METHOD_CALL read(void *dst, const uint32_t size, int *ecode) const override
Definition: ftp_library.cpp:1262
virtual uint32_t STORAGE_METHOD_CALL write(const void *src, const uint32_t size, int *ecode) override
Definition: ftp_library.cpp:1225
virtual int releaseRef() const override
Decrement reference counter.
Definition: ftp_library.cpp:673
virtual void * queryInterface(const nxpl::NX_GUID &interfaceID) override
Cast to type, specified by interfaceID.
Definition: ftp_library.cpp:649
virtual int addRef() const override
Increment reference counter.
Definition: ftp_library.cpp:1359
IO device abstraction.
Definition: third_party_storage.h:73