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 
56 class CPL_DLL VSIVirtualHandle {
57  public:
58  virtual int Seek( vsi_l_offset nOffset, int nWhence ) = 0;
59  virtual vsi_l_offset Tell() = 0;
60  virtual size_t Read( void *pBuffer, size_t nSize, size_t nCount ) = 0;
61  virtual int ReadMultiRange( int nRanges, void ** ppData,
62  const vsi_l_offset* panOffsets,
63  const size_t* panSizes );
64  virtual size_t Write( const void *pBuffer, size_t nSize,size_t nCount)=0;
65  virtual int Eof() = 0;
66  virtual int Flush() {return 0;}
67  virtual int Close() = 0;
68  // Base implementation that only supports file extension.
69  virtual int Truncate( vsi_l_offset nNewSize );
70  virtual void *GetNativeFileDescriptor() { return nullptr; }
71  virtual VSIRangeStatus GetRangeStatus( CPL_UNUSED vsi_l_offset nOffset,
72  CPL_UNUSED vsi_l_offset nLength )
73  { return VSI_RANGE_STATUS_UNKNOWN; }
74  virtual bool HasPRead() const;
75  virtual size_t PRead( void* pBuffer, size_t nSize, vsi_l_offset nOffset ) const;
76 
77  // NOTE: when adding new methods, besides the "actual" implementations,
78  // also consider the VSICachedFile one.
79 
80  virtual ~VSIVirtualHandle() { }
81 };
82 
83 /************************************************************************/
84 /* VSIFilesystemHandler */
85 /************************************************************************/
86 
87 #ifndef DOXYGEN_SKIP
88 class CPL_DLL VSIFilesystemHandler {
89 
90 public:
91 
92  virtual ~VSIFilesystemHandler() {}
93 
94  VSIVirtualHandle *Open( const char *pszFilename,
95  const char *pszAccess );
96 
97  virtual VSIVirtualHandle *Open( const char *pszFilename,
98  const char *pszAccess,
99  bool bSetError,
100  CSLConstList papszOptions ) = 0;
101  virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags) = 0;
102  virtual int Unlink( const char *pszFilename )
103  { (void) pszFilename; errno=ENOENT; return -1; }
104  virtual int* UnlinkBatch( CSLConstList papszFiles );
105  virtual int Mkdir( const char *pszDirname, long nMode )
106  {(void)pszDirname; (void)nMode; errno=ENOENT; return -1;}
107  virtual int Rmdir( const char *pszDirname )
108  { (void) pszDirname; errno=ENOENT; return -1; }
109  virtual int RmdirRecursive( const char *pszDirname );
110  virtual char **ReadDir( const char *pszDirname )
111  { (void) pszDirname; return nullptr; }
112  virtual char **ReadDirEx( const char *pszDirname, int /* nMaxFiles */ )
113  { return ReadDir(pszDirname); }
114  virtual char **SiblingFiles( const char * /*pszFilename*/ )
115  { return nullptr; }
116  virtual int Rename( const char *oldpath, const char *newpath )
117  { (void) oldpath; (void)newpath; errno=ENOENT; return -1; }
118  virtual int IsCaseSensitive( const char* pszFilename )
119  { (void) pszFilename; return TRUE; }
120  virtual GIntBig GetDiskFreeSpace( const char* /* pszDirname */ ) { return -1; }
121  virtual int SupportsSparseFiles( const char* /* pszPath */ ) { return FALSE; }
122  virtual int HasOptimizedReadMultiRange(const char* /* pszPath */) { return FALSE; }
123  virtual const char* GetActualURL(const char* /*pszFilename*/) { return nullptr; }
124  virtual const char* GetOptions() { return nullptr; }
125  virtual char* GetSignedURL(const char* /*pszFilename*/, CSLConstList /* papszOptions */) { return nullptr; }
126  virtual bool Sync( const char* pszSource, const char* pszTarget,
127  const char* const * papszOptions,
128  GDALProgressFunc pProgressFunc,
129  void *pProgressData,
130  char*** ppapszOutputs );
131 
132  virtual VSIDIR* OpenDir( const char *pszPath, int nRecurseDepth,
133  const char* const *papszOptions);
134 
135  virtual char** GetFileMetadata( const char * pszFilename, const char* pszDomain,
136  CSLConstList papszOptions );
137 
138  virtual bool SetFileMetadata( const char * pszFilename,
139  CSLConstList papszMetadata,
140  const char* pszDomain,
141  CSLConstList papszOptions );
142 
143  virtual bool AbortPendingUploads(const char* /*pszFilename*/) { return true;}
144 
145  virtual std::string GetStreamingFilename(const std::string& osFilename) const { return osFilename; }
146 
147  virtual bool IsLocal( const char* /* pszPath */ ) { return true; }
148  virtual bool SupportsSequentialWrite( const char* /* pszPath */, bool /* bAllowLocalTempFile */ ) { return true; }
149  virtual bool SupportsRandomWrite( const char* /* pszPath */, bool /* bAllowLocalTempFile */ ) { return true; }
150  virtual bool SupportsRead( const char* /* pszPath */ ) { return true; }
151 };
152 #endif /* #ifndef DOXYGEN_SKIP */
153 
154 /************************************************************************/
155 /* VSIFileManager */
156 /************************************************************************/
157 
158 #ifndef DOXYGEN_SKIP
159 class CPL_DLL VSIFileManager
160 {
161 private:
162  VSIFilesystemHandler *poDefaultHandler = nullptr;
163  std::map<std::string, VSIFilesystemHandler *> oHandlers{};
164 
165  VSIFileManager();
166 
167  static VSIFileManager *Get();
168 
169  CPL_DISALLOW_COPY_ASSIGN(VSIFileManager)
170 
171 public:
172  ~VSIFileManager();
173 
174  static VSIFilesystemHandler *GetHandler( const char * );
175  static void InstallHandler( const std::string& osPrefix,
176  VSIFilesystemHandler * );
177  /* RemoveHandler is never defined. */
178  /* static void RemoveHandler( const std::string& osPrefix ); */
179 
180  static char** GetPrefixes();
181 };
182 #endif /* #ifndef DOXYGEN_SKIP */
183 
184 /************************************************************************/
185 /* ==================================================================== */
186 /* VSIArchiveFilesystemHandler */
187 /* ==================================================================== */
188 /************************************************************************/
189 
190 #ifndef DOXYGEN_SKIP
191 
192 class VSIArchiveEntryFileOffset
193 {
194  public:
195  virtual ~VSIArchiveEntryFileOffset();
196 };
197 
198 typedef struct
199 {
200  char *fileName;
201  vsi_l_offset uncompressed_size;
202  VSIArchiveEntryFileOffset* file_pos;
203  int bIsDir;
204  GIntBig nModifiedTime;
205 } VSIArchiveEntry;
206 
207 class VSIArchiveContent
208 {
209 public:
210  time_t mTime = 0;
211  vsi_l_offset nFileSize = 0;
212  int nEntries = 0;
213  VSIArchiveEntry* entries = nullptr;
214 
215  ~VSIArchiveContent();
216 };
217 
218 class VSIArchiveReader
219 {
220  public:
221  virtual ~VSIArchiveReader();
222 
223  virtual int GotoFirstFile() = 0;
224  virtual int GotoNextFile() = 0;
225  virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
226  virtual GUIntBig GetFileSize() = 0;
227  virtual CPLString GetFileName() = 0;
228  virtual GIntBig GetModifiedTime() = 0;
229  virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
230 };
231 
232 class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
233 {
234  CPL_DISALLOW_COPY_ASSIGN(VSIArchiveFilesystemHandler)
235 
236 protected:
237  CPLMutex* hMutex = nullptr;
238  /* We use a cache that contains the list of files contained in a VSIArchive file as */
239  /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
240  /* containing ~1000 files like a CADRG product */
241  std::map<CPLString,VSIArchiveContent*> oFileList{};
242 
243  virtual const char* GetPrefix() = 0;
244  virtual std::vector<CPLString> GetExtensions() = 0;
245  virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
246 
247 public:
248  VSIArchiveFilesystemHandler();
249  virtual ~VSIArchiveFilesystemHandler();
250 
251  int Stat( const char *pszFilename, VSIStatBufL *pStatBuf,
252  int nFlags ) override;
253  int Unlink( const char *pszFilename ) override;
254  int Rename( const char *oldpath, const char *newpath ) override;
255  int Mkdir( const char *pszDirname, long nMode ) override;
256  int Rmdir( const char *pszDirname ) override;
257  char **ReadDirEx( const char *pszDirname, int nMaxFiles ) override;
258 
259  virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = nullptr);
260  virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive, int bCheckMainFileExists);
261  virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
262  virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
263 
264  virtual bool IsLocal( const char* pszPath ) override;
265  virtual bool SupportsSequentialWrite( const char* /* pszPath */, bool /* bAllowLocalTempFile */ ) override { return false; }
266  virtual bool SupportsRandomWrite( const char* /* pszPath */, bool /* bAllowLocalTempFile */ ) override { return false; }
267 };
268 
269 /************************************************************************/
270 /* VSIDIR */
271 /************************************************************************/
272 
273 struct CPL_DLL VSIDIR
274 {
275  VSIDIR() = default;
276  virtual ~VSIDIR();
277 
278  virtual const VSIDIREntry* NextDirEntry() = 0;
279 
280  private:
281  VSIDIR(const VSIDIR&) = delete;
282  VSIDIR& operator=(const VSIDIR&) = delete;
283 };
284 
285 #endif /* #ifndef DOXYGEN_SKIP */
286 
287 VSIVirtualHandle CPL_DLL *VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
288 VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle,
289  const GByte* pabyBeginningContent,
290  vsi_l_offset nCheatFileSize);
291 VSIVirtualHandle CPL_DLL *VSICreateCachedFile( VSIVirtualHandle* poBaseHandle, size_t nChunkSize = 32768, size_t nCacheSize = 0 );
292 
293 const int CPL_DEFLATE_TYPE_GZIP = 0;
294 const int CPL_DEFLATE_TYPE_ZLIB = 1;
295 const int CPL_DEFLATE_TYPE_RAW_DEFLATE = 2;
296 VSIVirtualHandle CPL_DLL *VSICreateGZipWritable( VSIVirtualHandle* poBaseHandle, int nDeflateType, int bAutoCloseBaseHandle );
297 
298 VSIVirtualHandle *VSICreateUploadOnCloseFile( VSIVirtualHandle* poBaseHandle );
299 
300 #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:203
cpl_vsi.h
VSIRangeStatus
VSIRangeStatus
Range status.
Definition: cpl_vsi.h:178
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:320
VSI_RANGE_STATUS_UNKNOWN
@ VSI_RANGE_STATUS_UNKNOWN
Unknown.
Definition: cpl_vsi.h:180
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1056
GUIntBig
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:233
VSIDIREntry
Directory entry.
Definition: cpl_vsi.h:366
cpl_string.h
vsi_l_offset
GUIntBig vsi_l_offset
Type for a file offset.
Definition: cpl_vsi.h:142
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:230
CPL_UNUSED
#define CPL_UNUSED
Qualifier for an argument that is unused.
Definition: cpl_port.h:878
VSIDIR
struct VSIDIR VSIDIR
Opaque type for a directory iterator.
Definition: cpl_vsi.h:355
VSIStatBufL
#define VSIStatBufL
Type for VSIStatL()
Definition: cpl_vsi.h:200
VSIVirtualHandle
Virtual file handle.
Definition: cpl_vsi_virtual.h:56
CPL_DISALLOW_COPY_ASSIGN
#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:930