2 * Copyright (C) 2013 Arne Morten Kvarving
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSES/README.md for more information.
11 #include "FileItemList.h"
12 #include "addons/binary-addons/AddonDll.h"
13 #include "addons/binary-addons/AddonInstanceHandler.h"
14 #include "addons/kodi-dev-kit/include/kodi/addon-instance/VFS.h"
15 #include "filesystem/IDirectory.h"
16 #include "filesystem/IFile.h"
17 #include "filesystem/IFileDirectory.h"
26 typedef std::shared_ptr
<CVFSEntry
> VFSEntryPtr
;
28 class CVFSAddonCache
: public CAddonDllInformer
31 virtual ~CVFSAddonCache();
34 const std::vector
<VFSEntryPtr
> GetAddonInstances();
35 VFSEntryPtr
GetAddonInstance(const std::string
& strId
);
38 void Update(const std::string
& id
);
39 void OnEvent(const AddonEvent
& event
);
40 bool IsInUse(const std::string
& id
) override
;
42 CCriticalSection m_critSection
;
43 std::vector
<VFSEntryPtr
> m_addonsInstances
;
46 //! \brief A virtual filesystem entry add-on.
47 class CVFSEntry
: public IAddonInstanceHandler
50 //! \brief A structure encapsulating properties of supplied protocol.
53 bool supportPath
; //!< Protocol has path in addition to server name
54 bool supportUsername
; //!< Protocol uses logins
55 bool supportPassword
; //!< Protocol supports passwords
56 bool supportPort
; //!< Protocol supports port customization
57 bool supportBrowsing
; //!< Protocol supports server browsing
58 bool supportWrite
; //!< Protocol supports write operations
59 int defaultPort
; //!< Default port to use for protocol
60 std::string type
; //!< URL type for protocol
61 int label
; //!< String ID to use as label in dialog
63 //! \brief The constructor reads the info from an add-on info structure.
64 ProtocolInfo(const AddonInfoPtr
& addonInfo
);
67 //! \brief Construct from add-on properties.
68 //! \param addonInfo General addon properties
69 explicit CVFSEntry(const AddonInfoPtr
& addonInfo
);
70 ~CVFSEntry() override
;
72 // Things that MUST be supplied by the child classes
73 void* Open(const CURL
& url
);
74 void* OpenForWrite(const CURL
& url
, bool bOverWrite
);
75 bool Exists(const CURL
& url
);
76 int Stat(const CURL
& url
, struct __stat64
* buffer
);
77 ssize_t
Read(void* ctx
, void* lpBuf
, size_t uiBufSize
);
78 ssize_t
Write(void* ctx
, const void* lpBuf
, size_t uiBufSize
);
79 int64_t Seek(void* ctx
, int64_t iFilePosition
, int iWhence
= SEEK_SET
);
80 int Truncate(void* ctx
, int64_t size
);
81 void Close(void* ctx
);
82 int64_t GetPosition(void* ctx
);
83 int64_t GetLength(void* ctx
);
84 int GetChunkSize(void* ctx
);
85 int IoControl(void* ctx
, XFILE::EIoControl request
, void* param
);
86 bool Delete(const CURL
& url
);
87 bool Rename(const CURL
& url
, const CURL
& url2
);
89 bool GetDirectory(const CURL
& url
, CFileItemList
& items
, void* ctx
);
90 bool DirectoryExists(const CURL
& url
);
91 bool RemoveDirectory(const CURL
& url
);
92 bool CreateDirectory(const CURL
& url
);
96 bool ContainsFiles(const CURL
& url
, CFileItemList
& items
);
98 const std::string
& GetProtocols() const { return m_protocols
; }
99 const std::string
& GetExtensions() const { return m_extensions
; }
100 bool HasFiles() const { return m_files
; }
101 bool HasDirectories() const { return m_directories
; }
102 bool HasFileDirectories() const { return m_filedirectories
; }
103 const std::string
& GetZeroconfType() const { return m_zeroconf
; }
104 const ProtocolInfo
& GetProtocolInfo() const { return m_protocolInfo
; }
106 std::string m_protocols
; //!< Protocols for VFS entry.
107 std::string m_extensions
; //!< Extensions for VFS entry.
108 std::string m_zeroconf
; //!< Zero conf announce string for VFS protocol.
109 bool m_files
; //!< Vfs entry can read files.
110 bool m_directories
; //!< VFS entry can list directories.
111 bool m_filedirectories
; //!< VFS entry contains file directories.
112 ProtocolInfo m_protocolInfo
; //!< Info about protocol for network dialog.
115 //! \brief Wrapper equipping a CVFSEntry with an IFile interface.
116 //! \details Needed as CVFSEntry implements several VFS interfaces
117 //! with overlapping methods.
118 class CVFSEntryIFileWrapper
: public XFILE::IFile
121 //! \brief The constructor initializes the reference to the wrapped CVFSEntry.
122 //! \param ptr The CVFSEntry to wrap.
123 explicit CVFSEntryIFileWrapper(VFSEntryPtr ptr
);
125 //! \brief Empty destructor.
126 ~CVFSEntryIFileWrapper() override
;
128 //! \brief Open a file.
129 //! \param[in] url URL to open.
130 //! \returns True if file was opened, false otherwise.
131 bool Open(const CURL
& url
) override
;
133 //! \brief Open a file for writing.
134 //! \param[in] url URL to open.
135 //! \param[in] bOverWrite If true, overwrite an existing file.
136 //! \returns True if file was opened, false otherwise.
137 bool OpenForWrite(const CURL
& url
, bool bOverWrite
) override
;
139 //! \brief Check for file existence.
140 //! \param[in] url URL of file.
141 bool Exists(const CURL
& url
) override
;
143 //! \brief Stat a file.
144 //! \param[in] url URL of file.
145 //! \param[out] buffer The stat info.
146 //! \details Returns 0 on success, non-zero otherwise (see fstat() return values).
147 int Stat(const CURL
& url
, struct __stat64
* buffer
) override
;
149 //! \brief Read data from file:
150 //! \param lpBuf Buffer to read data into.
151 //! \param[in] uiBufSize Number of bytes to read.
152 //! \returns Number of bytes read.
153 ssize_t
Read(void* lpBuf
, size_t uiBufSize
) override
;
155 //! \brief Write data to file.
156 //! \param[in] lpBuf Data to write.
157 //! \param[in] uiBufSize Number of bytes to write.
158 //! \returns Number of bytes written.
159 ssize_t
Write(const void* lpBuf
, size_t uiBufSize
) override
;
161 //! \brief Seek in file.
162 //! \param[in] iFilePosition Position to seek to.
163 //! \param[in] whence Origin for position.
164 //! \returns New file position.
165 int64_t Seek(int64_t iFilePosition
, int whence
= SEEK_SET
) override
;
167 //! \brief Truncate a file.
168 //! \param[in] size Size of new file.
169 int Truncate(int64_t size
) override
;
171 //! \brief Close file.
172 void Close() override
;
174 //! \brief Obtain current file position.
175 int64_t GetPosition() override
;
177 //! \brief Obtain file size.
178 int64_t GetLength() override
;
180 //! \brief Obtain chunksize of file.
181 int GetChunkSize() override
;
183 //! \brief Perform I/O controls for file.
184 int IoControl(XFILE::EIoControl request
, void* param
) override
;
186 //! \brief Delete a file.
187 //! \param[in] url URL of file to delete.
188 bool Delete(const CURL
& url
) override
;
190 //! \brief Rename a file.
191 //! \param[in] url URL of file to rename.
192 //! \param[in] url2 New URL of file.
193 bool Rename(const CURL
& url
, const CURL
& url2
) override
;
195 void* m_context
= nullptr; //!< Opaque add-on specific context for opened file.
196 VFSEntryPtr m_addon
; //!< Pointer to wrapped CVFSEntry.
199 //! \brief Wrapper equpping a CVFSEntry with an IDirectory interface.
200 //! \details Needed as CVFSEntry implements several VFS interfaces
201 //! with overlapping methods.
202 class CVFSEntryIDirectoryWrapper
: public XFILE::IDirectory
205 //! \brief The constructor initializes the reference to the wrapped CVFSEntry.
206 //! \param ptr The CVFSEntry to wrap.
207 explicit CVFSEntryIDirectoryWrapper(VFSEntryPtr ptr
);
209 //! \brief Empty destructor.
210 ~CVFSEntryIDirectoryWrapper() override
= default;
212 //! \brief Return directory listing.
213 //! \param[in] url URL to file to list.
214 //! \param items List of items in file.
215 //! \return True if listing succeeded, false otherwise.
216 bool GetDirectory(const CURL
& url
, CFileItemList
& items
) override
;
218 //! \brief Check if directory exists.
219 //! \param[in] url URL to check.
220 bool Exists(const CURL
& url
) override
;
222 //! \brief Delete directory.
223 //! \param[in] url URL to delete.
224 bool Remove(const CURL
& url
) override
;
226 //! \brief Create directory.
227 //! \param[in] url URL to delete.
228 bool Create(const CURL
& url
) override
;
230 //! \brief Static helper for doing a keyboard callback.
231 static bool DoGetKeyboardInput(void* context
, const char* heading
,
232 char** input
, bool hidden_input
);
234 //! \brief Get keyboard input.
235 bool GetKeyboardInput2(const char* heading
, char** input
, bool hidden_input
);
237 //! \brief Static helper for displaying an error dialog.
238 static void DoSetErrorDialog(void* ctx
, const char* heading
,
239 const char* line1
, const char* line2
,
242 //! \brief Show an error dialog.
243 void SetErrorDialog2(const char* heading
, const char* line1
,
244 const char* line2
, const char* line3
);
246 //! \brief Static helper for requiring authentication.
247 static void DoRequireAuthentication(void* ctx
, const char* url
);
249 //! \brief Require authentication.
250 void RequireAuthentication2(const CURL
& url
);
252 VFSEntryPtr m_addon
; //!< Pointer to wrapper CVFSEntry.
255 //! \brief Wrapper equpping a CVFSEntry with an IFileDirectory interface.
256 //! \details Needed as CVFSEntry implements several VFS interfaces
257 //! with overlapping methods.
258 class CVFSEntryIFileDirectoryWrapper
: public XFILE::IFileDirectory
,
259 public CVFSEntryIDirectoryWrapper
262 //! \brief The constructor initializes the reference to the wrapped CVFSEntry.
263 //! \param ptr The CVFSEntry to wrap.
264 explicit CVFSEntryIFileDirectoryWrapper(VFSEntryPtr ptr
)
265 : CVFSEntryIDirectoryWrapper(std::move(ptr
))
269 //! \brief Empty destructor.
270 ~CVFSEntryIFileDirectoryWrapper() override
= default;
272 //! \brief Check if the given file should be treated as a directory.
273 //! \param[in] url URL for file to probe.
274 bool ContainsFiles(const CURL
& url
) override
276 return m_addon
->ContainsFiles(url
, m_items
);
279 //! \brief Return directory listing.
280 //! \param[in] url URL to file to list.
281 //! \param items List of items in file.
282 //! \return True if listing succeeded, false otherwise.
283 bool GetDirectory(const CURL
& url
, CFileItemList
& items
) override
285 return CVFSEntryIDirectoryWrapper::GetDirectory(url
, items
);
288 //! \brief Check if directory exists.
289 //! \param[in] url URL to check.
290 bool Exists(const CURL
& url
) override
292 return CVFSEntryIDirectoryWrapper::Exists(url
);
295 //! \brief Delete directory.
296 //! \param[in] url URL to delete.
297 bool Remove(const CURL
& url
) override
299 return CVFSEntryIDirectoryWrapper::Remove(url
);
302 //! \brief Create directory.
303 //! \param[in] url URL to delete.
304 bool Create(const CURL
& url
) override
306 return CVFSEntryIDirectoryWrapper::Create(url
);
309 CFileItemList m_items
; //!< Internal list of items, used for cache purposes.
312 } /*namespace ADDON*/