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