GDAL
cpl_vsi_virtual.h
1/******************************************************************************
2 * $Id$
3 *
4 * Project: VSI Virtual File System
5 * Purpose: Declarations for classes related to the virtual filesystem.
6 * These would only be normally required by applications implementing
7 * their own virtual file system classes which should be rare.
8 * The class interface may be fragile through versions.
9 * Author: Frank Warmerdam, warmerdam@pobox.com
10 *
11 ******************************************************************************
12 * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
13 * Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys.com>
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included
23 * in all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
32 ****************************************************************************/
33
34#ifndef CPL_VSI_VIRTUAL_H_INCLUDED
35#define CPL_VSI_VIRTUAL_H_INCLUDED
36
37#include "cpl_vsi.h"
38#include "cpl_vsi_error.h"
39#include "cpl_string.h"
40#include "cpl_multiproc.h"
41
42#include <map>
43#include <vector>
44#include <string>
45
46// To avoid aliasing to GetDiskFreeSpace to GetDiskFreeSpaceA on Windows
47#ifdef GetDiskFreeSpace
48#undef GetDiskFreeSpace
49#endif
50
51/************************************************************************/
52/* VSIVirtualHandle */
53/************************************************************************/
54
56class CPL_DLL VSIVirtualHandle
57{
58 public:
59 virtual int Seek(vsi_l_offset nOffset, int nWhence) = 0;
60 virtual vsi_l_offset Tell() = 0;
61 virtual size_t Read(void *pBuffer, size_t nSize, size_t nCount) = 0;
62 virtual int ReadMultiRange(int nRanges, void **ppData,
63 const vsi_l_offset *panOffsets,
64 const size_t *panSizes);
65 virtual size_t Write(const void *pBuffer, size_t nSize, size_t nCount) = 0;
66 virtual int Eof() = 0;
67 virtual int Flush()
68 {
69 return 0;
70 }
71 virtual int Close() = 0;
72 // Base implementation that only supports file extension.
73 virtual int Truncate(vsi_l_offset nNewSize);
75 {
76 return nullptr;
77 }
83 virtual bool HasPRead() const;
84 virtual size_t PRead(void *pBuffer, size_t nSize,
85 vsi_l_offset nOffset) const;
86
87 // NOTE: when adding new methods, besides the "actual" implementations,
88 // also consider the VSICachedFile one.
89
90 virtual ~VSIVirtualHandle()
91 {
92 }
93};
94
95/************************************************************************/
96/* VSIFilesystemHandler */
97/************************************************************************/
98
99#ifndef DOXYGEN_SKIP
100class CPL_DLL VSIFilesystemHandler
101{
102
103 public:
104 virtual ~VSIFilesystemHandler()
105 {
106 }
107
108 VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess);
109
110 virtual VSIVirtualHandle *Open(const char *pszFilename,
111 const char *pszAccess, bool bSetError,
112 CSLConstList papszOptions) = 0;
113 virtual int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
114 int nFlags) = 0;
115 virtual int Unlink(const char *pszFilename)
116 {
117 (void)pszFilename;
118 errno = ENOENT;
119 return -1;
120 }
121 virtual int *UnlinkBatch(CSLConstList papszFiles);
122 virtual int Mkdir(const char *pszDirname, long nMode)
123 {
124 (void)pszDirname;
125 (void)nMode;
126 errno = ENOENT;
127 return -1;
128 }
129 virtual int Rmdir(const char *pszDirname)
130 {
131 (void)pszDirname;
132 errno = ENOENT;
133 return -1;
134 }
135 virtual int RmdirRecursive(const char *pszDirname);
136 virtual char **ReadDir(const char *pszDirname)
137 {
138 (void)pszDirname;
139 return nullptr;
140 }
141 virtual char **ReadDirEx(const char *pszDirname, int /* nMaxFiles */)
142 {
143 return ReadDir(pszDirname);
144 }
145 virtual char **SiblingFiles(const char * /*pszFilename*/)
146 {
147 return nullptr;
148 }
149 virtual int Rename(const char *oldpath, const char *newpath)
150 {
151 (void)oldpath;
152 (void)newpath;
153 errno = ENOENT;
154 return -1;
155 }
156 virtual int IsCaseSensitive(const char *pszFilename)
157 {
158 (void)pszFilename;
159 return TRUE;
160 }
161 virtual GIntBig GetDiskFreeSpace(const char * /* pszDirname */)
162 {
163 return -1;
164 }
165 virtual int SupportsSparseFiles(const char * /* pszPath */)
166 {
167 return FALSE;
168 }
169 virtual int HasOptimizedReadMultiRange(const char * /* pszPath */)
170 {
171 return FALSE;
172 }
173 virtual const char *GetActualURL(const char * /*pszFilename*/)
174 {
175 return nullptr;
176 }
177 virtual const char *GetOptions()
178 {
179 return nullptr;
180 }
181 virtual char *GetSignedURL(const char * /*pszFilename*/,
182 CSLConstList /* papszOptions */)
183 {
184 return nullptr;
185 }
186 virtual bool Sync(const char *pszSource, const char *pszTarget,
187 const char *const *papszOptions,
188 GDALProgressFunc pProgressFunc, void *pProgressData,
189 char ***ppapszOutputs);
190
191 virtual VSIDIR *OpenDir(const char *pszPath, int nRecurseDepth,
192 const char *const *papszOptions);
193
194 virtual char **GetFileMetadata(const char *pszFilename,
195 const char *pszDomain,
196 CSLConstList papszOptions);
197
198 virtual bool SetFileMetadata(const char *pszFilename,
199 CSLConstList papszMetadata,
200 const char *pszDomain,
201 CSLConstList papszOptions);
202
203 virtual bool AbortPendingUploads(const char * /*pszFilename*/)
204 {
205 return true;
206 }
207
208 virtual std::string
209 GetStreamingFilename(const std::string &osFilename) const
210 {
211 return osFilename;
212 }
213
214 virtual bool IsLocal(const char * /* pszPath */)
215 {
216 return true;
217 }
218 virtual bool SupportsSequentialWrite(const char * /* pszPath */,
219 bool /* bAllowLocalTempFile */)
220 {
221 return true;
222 }
223 virtual bool SupportsRandomWrite(const char * /* pszPath */,
224 bool /* bAllowLocalTempFile */)
225 {
226 return true;
227 }
228 virtual bool SupportsRead(const char * /* pszPath */)
229 {
230 return true;
231 }
232};
233#endif /* #ifndef DOXYGEN_SKIP */
234
235/************************************************************************/
236/* VSIFileManager */
237/************************************************************************/
238
239#ifndef DOXYGEN_SKIP
240class CPL_DLL VSIFileManager
241{
242 private:
243 VSIFilesystemHandler *poDefaultHandler = nullptr;
244 std::map<std::string, VSIFilesystemHandler *> oHandlers{};
245
246 VSIFileManager();
247
248 static VSIFileManager *Get();
249
250 CPL_DISALLOW_COPY_ASSIGN(VSIFileManager)
251
252 public:
253 ~VSIFileManager();
254
255 static VSIFilesystemHandler *GetHandler(const char *);
256 static void InstallHandler(const std::string &osPrefix,
257 VSIFilesystemHandler *);
258 /* RemoveHandler is never defined. */
259 /* static void RemoveHandler( const std::string& osPrefix ); */
260
261 static char **GetPrefixes();
262};
263#endif /* #ifndef DOXYGEN_SKIP */
264
265/************************************************************************/
266/* ==================================================================== */
267/* VSIArchiveFilesystemHandler */
268/* ==================================================================== */
269/************************************************************************/
270
271#ifndef DOXYGEN_SKIP
272
273class VSIArchiveEntryFileOffset
274{
275 public:
276 virtual ~VSIArchiveEntryFileOffset();
277};
278
279typedef struct
280{
281 char *fileName;
282 vsi_l_offset uncompressed_size;
283 VSIArchiveEntryFileOffset *file_pos;
284 int bIsDir;
285 GIntBig nModifiedTime;
286} VSIArchiveEntry;
287
288class VSIArchiveContent
289{
290 public:
291 time_t mTime = 0;
292 vsi_l_offset nFileSize = 0;
293 int nEntries = 0;
294 VSIArchiveEntry *entries = nullptr;
295
296 ~VSIArchiveContent();
297};
298
299class VSIArchiveReader
300{
301 public:
302 virtual ~VSIArchiveReader();
303
304 virtual int GotoFirstFile() = 0;
305 virtual int GotoNextFile() = 0;
306 virtual VSIArchiveEntryFileOffset *GetFileOffset() = 0;
307 virtual GUIntBig GetFileSize() = 0;
308 virtual CPLString GetFileName() = 0;
309 virtual GIntBig GetModifiedTime() = 0;
310 virtual int GotoFileOffset(VSIArchiveEntryFileOffset *pOffset) = 0;
311};
312
313class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
314{
315 CPL_DISALLOW_COPY_ASSIGN(VSIArchiveFilesystemHandler)
316
317 protected:
318 CPLMutex *hMutex = nullptr;
319 /* We use a cache that contains the list of files contained in a VSIArchive
320 * file as */
321 /* unarchive.c is quite inefficient in listing them. This speeds up access
322 * to VSIArchive files */
323 /* containing ~1000 files like a CADRG product */
324 std::map<CPLString, VSIArchiveContent *> oFileList{};
325
326 virtual const char *GetPrefix() = 0;
327 virtual std::vector<CPLString> GetExtensions() = 0;
328 virtual VSIArchiveReader *CreateReader(const char *pszArchiveFileName) = 0;
329
330 public:
331 VSIArchiveFilesystemHandler();
332 virtual ~VSIArchiveFilesystemHandler();
333
334 int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
335 int nFlags) override;
336 int Unlink(const char *pszFilename) override;
337 int Rename(const char *oldpath, const char *newpath) override;
338 int Mkdir(const char *pszDirname, long nMode) override;
339 int Rmdir(const char *pszDirname) override;
340 char **ReadDirEx(const char *pszDirname, int nMaxFiles) override;
341
342 virtual const VSIArchiveContent *
343 GetContentOfArchive(const char *archiveFilename,
344 VSIArchiveReader *poReader = nullptr);
345 virtual char *SplitFilename(const char *pszFilename,
346 CPLString &osFileInArchive,
347 int bCheckMainFileExists);
348 virtual VSIArchiveReader *OpenArchiveFile(const char *archiveFilename,
349 const char *fileInArchiveName);
350 virtual int FindFileInArchive(const char *archiveFilename,
351 const char *fileInArchiveName,
352 const VSIArchiveEntry **archiveEntry);
353
354 virtual bool IsLocal(const char *pszPath) override;
355 virtual bool
356 SupportsSequentialWrite(const char * /* pszPath */,
357 bool /* bAllowLocalTempFile */) override
358 {
359 return false;
360 }
361 virtual bool SupportsRandomWrite(const char * /* pszPath */,
362 bool /* bAllowLocalTempFile */) override
363 {
364 return false;
365 }
366};
367
368/************************************************************************/
369/* VSIDIR */
370/************************************************************************/
371
372struct CPL_DLL VSIDIR
373{
374 VSIDIR() = default;
375 virtual ~VSIDIR();
376
377 virtual const VSIDIREntry *NextDirEntry() = 0;
378
379 private:
380 VSIDIR(const VSIDIR &) = delete;
381 VSIDIR &operator=(const VSIDIR &) = delete;
382};
383
384#endif /* #ifndef DOXYGEN_SKIP */
385
386VSIVirtualHandle CPL_DLL *
387VSICreateBufferedReaderHandle(VSIVirtualHandle *poBaseHandle);
389VSICreateBufferedReaderHandle(VSIVirtualHandle *poBaseHandle,
390 const GByte *pabyBeginningContent,
391 vsi_l_offset nCheatFileSize);
392VSIVirtualHandle CPL_DLL *VSICreateCachedFile(VSIVirtualHandle *poBaseHandle,
393 size_t nChunkSize = 32768,
394 size_t nCacheSize = 0);
395
396const int CPL_DEFLATE_TYPE_GZIP = 0;
397const int CPL_DEFLATE_TYPE_ZLIB = 1;
398const int CPL_DEFLATE_TYPE_RAW_DEFLATE = 2;
399VSIVirtualHandle CPL_DLL *VSICreateGZipWritable(VSIVirtualHandle *poBaseHandle,
400 int nDeflateType,
401 int bAutoCloseBaseHandle);
402
403VSIVirtualHandle *VSICreateUploadOnCloseFile(VSIVirtualHandle *poBaseHandle);
404
405#endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */
Convenient string class based on std::string.
Definition cpl_string.h:312
Virtual file handle.
Definition cpl_vsi_virtual.h:57
virtual int Flush()
Flush pending writes to disk.
Definition cpl_vsi_virtual.h:67
virtual int Close()=0
Close file.
virtual VSIRangeStatus GetRangeStatus(vsi_l_offset nOffset, vsi_l_offset nLength)
Return if a given file range contains data or holes filled with zeroes.
Definition cpl_vsi_virtual.h:78
virtual vsi_l_offset Tell()=0
Tell current file offset.
virtual int Seek(vsi_l_offset nOffset, int nWhence)=0
Seek to requested offset.
virtual size_t Read(void *pBuffer, size_t nSize, size_t nCount)=0
Read bytes from file.
virtual void * GetNativeFileDescriptor()
Returns the "native" file descriptor for the virtual handle.
Definition cpl_vsi_virtual.h:74
virtual int ReadMultiRange(int nRanges, void **ppData, const vsi_l_offset *panOffsets, const size_t *panSizes)
Read several ranges of bytes from file.
virtual size_t Write(const void *pBuffer, size_t nSize, size_t nCount)=0
Write bytes to file.
virtual int Eof()=0
Test for end of file.
virtual int Truncate(vsi_l_offset nNewSize)
Truncate/expand the file to the specified size.
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition cpl_port.h:236
#define CPL_UNUSED
Qualifier for an argument that is unused.
Definition cpl_port.h:992
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:1049
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition cpl_port.h:1190
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:205
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition cpl_port.h:233
Various convenience functions for working with strings and string lists.
Standard C Covers.
#define VSIStatBufL
Type for VSIStatL()
Definition cpl_vsi.h:215
VSIRangeStatus
Range status.
Definition cpl_vsi.h:194
@ VSI_RANGE_STATUS_UNKNOWN
Unknown.
Definition cpl_vsi.h:195
struct VSIDIR VSIDIR
Opaque type for a directory iterator.
Definition cpl_vsi.h:394
GUIntBig vsi_l_offset
Type for a file offset.
Definition cpl_vsi.h:146
Directory entry.
Definition cpl_vsi.h:405