SDTS_AL
cpl_vsi_virtual.h
1 /******************************************************************************
2  * $Id: cpl_vsi_virtual.h 34165 2016-05-03 13:14:27Z rouault $
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 mines-paris dot org>
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 /************************************************************************/
47 /* VSIVirtualHandle */
48 /************************************************************************/
49 
50 class CPL_DLL VSIVirtualHandle {
51  public:
52  virtual int Seek( vsi_l_offset nOffset, int nWhence ) = 0;
53  virtual vsi_l_offset Tell() = 0;
54  virtual size_t Read( void *pBuffer, size_t nSize, size_t nMemb ) = 0;
55  virtual int ReadMultiRange( int nRanges, void ** ppData, const vsi_l_offset* panOffsets, const size_t* panSizes );
56  virtual size_t Write( const void *pBuffer, size_t nSize,size_t nMemb)=0;
57  virtual int Eof() = 0;
58  virtual int Flush() {return 0;}
59  virtual int Close() = 0;
60  virtual int Truncate( CPL_UNUSED vsi_l_offset nNewSize ) { return -1; }
61  virtual void *GetNativeFileDescriptor() { return NULL; }
62  virtual ~VSIVirtualHandle() { }
63 };
64 
65 /************************************************************************/
66 /* VSIFilesystemHandler */
67 /************************************************************************/
68 
69 class CPL_DLL VSIFilesystemHandler {
70 
71 public:
72 
73  virtual ~VSIFilesystemHandler() {}
74 
75  VSIVirtualHandle *Open( const char *pszFilename,
76  const char *pszAccess );
77 
78  virtual VSIVirtualHandle *Open( const char *pszFilename,
79  const char *pszAccess,
80  bool bSetError ) = 0;
81  virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags) = 0;
82  virtual int Unlink( const char *pszFilename )
83  { (void) pszFilename; errno=ENOENT; return -1; }
84  virtual int Mkdir( const char *pszDirname, long nMode )
85  {(void)pszDirname; (void)nMode; errno=ENOENT; return -1;}
86  virtual int Rmdir( const char *pszDirname )
87  { (void) pszDirname; errno=ENOENT; return -1; }
88  virtual char **ReadDir( const char *pszDirname )
89  { (void) pszDirname; return NULL; }
90  virtual char **ReadDirEx( const char *pszDirname, int /* nMaxFiles */ )
91  { return ReadDir(pszDirname); }
92  virtual int Rename( const char *oldpath, const char *newpath )
93  { (void) oldpath; (void)newpath; errno=ENOENT; return -1; }
94  virtual int IsCaseSensitive( const char* pszFilename )
95  { (void) pszFilename; return TRUE; }
96  virtual GIntBig GetDiskFreeSpace( const char* /* pszDirname */ ) { return -1; }
97 };
98 
99 /************************************************************************/
100 /* VSIFileManager */
101 /************************************************************************/
102 
103 class CPL_DLL VSIFileManager
104 {
105 private:
106  VSIFilesystemHandler *poDefaultHandler;
107  std::map<std::string, VSIFilesystemHandler *> oHandlers;
108 
109  VSIFileManager();
110 
111  static VSIFileManager *Get();
112 
113 public:
114  ~VSIFileManager();
115 
116  static VSIFilesystemHandler *GetHandler( const char * );
117  static void InstallHandler( const std::string& osPrefix,
119  /* RemoveHandler is never defined. */
120  /* static void RemoveHandler( const std::string& osPrefix ); */
121 };
122 
123 
124 /************************************************************************/
125 /* ==================================================================== */
126 /* VSIArchiveFilesystemHandler */
127 /* ==================================================================== */
128 /************************************************************************/
129 
131 {
132  public:
133  virtual ~VSIArchiveEntryFileOffset();
134 };
135 
136 typedef struct
137 {
138  char *fileName;
139  vsi_l_offset uncompressed_size;
140  VSIArchiveEntryFileOffset* file_pos;
141  int bIsDir;
142  GIntBig nModifiedTime;
144 
146 {
147 public:
148  time_t mTime;
149  vsi_l_offset nFileSize;
150  int nEntries;
151  VSIArchiveEntry* entries;
152 
153  VSIArchiveContent() : mTime(0), nFileSize(0), nEntries(0), entries(NULL) {}
155 };
156 
158 {
159  public:
160  virtual ~VSIArchiveReader();
161 
162  virtual int GotoFirstFile() = 0;
163  virtual int GotoNextFile() = 0;
164  virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
165  virtual GUIntBig GetFileSize() = 0;
166  virtual CPLString GetFileName() = 0;
167  virtual GIntBig GetModifiedTime() = 0;
168  virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
169 };
170 
172 {
173 protected:
174  CPLMutex* hMutex;
175  /* We use a cache that contains the list of files contained in a VSIArchive file as */
176  /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
177  /* containing ~1000 files like a CADRG product */
178  std::map<CPLString,VSIArchiveContent*> oFileList;
179 
180  virtual const char* GetPrefix() = 0;
181  virtual std::vector<CPLString> GetExtensions() = 0;
182  virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
183 
184 public:
186  virtual ~VSIArchiveFilesystemHandler();
187 
188  virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags );
189  virtual int Unlink( const char *pszFilename );
190  virtual int Rename( const char *oldpath, const char *newpath );
191  virtual int Mkdir( const char *pszDirname, long nMode );
192  virtual int Rmdir( const char *pszDirname );
193  virtual char **ReadDirEx( const char *pszDirname, int nMaxFiles );
194 
195  virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = NULL);
196  virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive, int bCheckMainFileExists);
197  virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
198  virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
199 };
200 
201 VSIVirtualHandle CPL_DLL *VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
202 VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle,
203  const GByte* pabyBeginningContent,
204  vsi_l_offset nCheatFileSize);
205 VSIVirtualHandle CPL_DLL *VSICreateCachedFile( VSIVirtualHandle* poBaseHandle, size_t nChunkSize = 32768, size_t nCacheSize = 0 );
206 VSIVirtualHandle CPL_DLL *VSICreateGZipWritable( VSIVirtualHandle* poBaseHandle, int bRegularZLibIn, int bAutoCloseBaseHandle );
207 
208 #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */
Definition: cpl_vsi_virtual.h:69
Definition: cpl_vsi_virtual.h:171
Definition: cpl_vsi_virtual.h:50
Definition: cpl_vsi_virtual.h:145
Definition: cpl_vsi_virtual.h:136
Definition: cpl_vsi_virtual.h:157
Definition: cpl_vsi_virtual.h:103
Definition: cpl_vsi_virtual.h:130