gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

268 行
11KB

  1. /******************************************************************************
  2. * $Id: cpl_vsi_virtual.h 7b937306fdeb31f6adefa6675d83ccd60f99e619 2018-11-25 23:10:44 +0100 Even 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. #ifndef CPL_VSI_VIRTUAL_H_INCLUDED
  34. #define CPL_VSI_VIRTUAL_H_INCLUDED
  35. #include "cpl_vsi.h"
  36. #include "cpl_vsi_error.h"
  37. #include "cpl_string.h"
  38. #include "cpl_multiproc.h"
  39. #include <map>
  40. #include <vector>
  41. #include <string>
  42. // To avoid aliasing to GetDiskFreeSpace to GetDiskFreeSpaceA on Windows
  43. #ifdef GetDiskFreeSpace
  44. #undef GetDiskFreeSpace
  45. #endif
  46. /************************************************************************/
  47. /* VSIVirtualHandle */
  48. /************************************************************************/
  49. /** Virtual file handle */
  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 nCount ) = 0;
  55. virtual int ReadMultiRange( int nRanges, void ** ppData,
  56. const vsi_l_offset* panOffsets,
  57. const size_t* panSizes );
  58. virtual size_t Write( const void *pBuffer, size_t nSize,size_t nCount)=0;
  59. virtual int Eof() = 0;
  60. virtual int Flush() {return 0;}
  61. virtual int Close() = 0;
  62. // Base implementation that only supports file extension.
  63. virtual int Truncate( vsi_l_offset nNewSize );
  64. virtual void *GetNativeFileDescriptor() { return nullptr; }
  65. virtual VSIRangeStatus GetRangeStatus( CPL_UNUSED vsi_l_offset nOffset,
  66. CPL_UNUSED vsi_l_offset nLength )
  67. { return VSI_RANGE_STATUS_UNKNOWN; }
  68. virtual ~VSIVirtualHandle() { }
  69. };
  70. /************************************************************************/
  71. /* VSIFilesystemHandler */
  72. /************************************************************************/
  73. #ifndef DOXYGEN_SKIP
  74. class CPL_DLL VSIFilesystemHandler {
  75. public:
  76. virtual ~VSIFilesystemHandler() {}
  77. VSIVirtualHandle *Open( const char *pszFilename,
  78. const char *pszAccess );
  79. virtual VSIVirtualHandle *Open( const char *pszFilename,
  80. const char *pszAccess,
  81. bool bSetError ) = 0;
  82. virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags) = 0;
  83. virtual int Unlink( const char *pszFilename )
  84. { (void) pszFilename; errno=ENOENT; return -1; }
  85. virtual int Mkdir( const char *pszDirname, long nMode )
  86. {(void)pszDirname; (void)nMode; errno=ENOENT; return -1;}
  87. virtual int Rmdir( const char *pszDirname )
  88. { (void) pszDirname; errno=ENOENT; return -1; }
  89. virtual char **ReadDir( const char *pszDirname )
  90. { (void) pszDirname; return nullptr; }
  91. virtual char **ReadDirEx( const char *pszDirname, int /* nMaxFiles */ )
  92. { return ReadDir(pszDirname); }
  93. virtual int Rename( const char *oldpath, const char *newpath )
  94. { (void) oldpath; (void)newpath; errno=ENOENT; return -1; }
  95. virtual int IsCaseSensitive( const char* pszFilename )
  96. { (void) pszFilename; return TRUE; }
  97. virtual GIntBig GetDiskFreeSpace( const char* /* pszDirname */ ) { return -1; }
  98. virtual int SupportsSparseFiles( const char* /* pszPath */ ) { return FALSE; }
  99. virtual int HasOptimizedReadMultiRange(const char* /* pszPath */) { return FALSE; }
  100. virtual const char* GetActualURL(const char* /*pszFilename*/) { return nullptr; }
  101. virtual const char* GetOptions() { return nullptr; }
  102. virtual char* GetSignedURL(const char* /*pszFilename*/, CSLConstList /* papszOptions */) { return nullptr; }
  103. virtual bool Sync( const char* pszSource, const char* pszTarget,
  104. const char* const * papszOptions,
  105. GDALProgressFunc pProgressFunc,
  106. void *pProgressData,
  107. char*** ppapszOutputs );
  108. virtual VSIDIR* OpenDir( const char *pszPath, int nRecurseDepth,
  109. const char* const *papszOptions);
  110. };
  111. #endif /* #ifndef DOXYGEN_SKIP */
  112. /************************************************************************/
  113. /* VSIFileManager */
  114. /************************************************************************/
  115. #ifndef DOXYGEN_SKIP
  116. class CPL_DLL VSIFileManager
  117. {
  118. private:
  119. VSIFilesystemHandler *poDefaultHandler = nullptr;
  120. std::map<std::string, VSIFilesystemHandler *> oHandlers{};
  121. VSIFileManager();
  122. static VSIFileManager *Get();
  123. CPL_DISALLOW_COPY_ASSIGN(VSIFileManager)
  124. public:
  125. ~VSIFileManager();
  126. static VSIFilesystemHandler *GetHandler( const char * );
  127. static void InstallHandler( const std::string& osPrefix,
  128. VSIFilesystemHandler * );
  129. /* RemoveHandler is never defined. */
  130. /* static void RemoveHandler( const std::string& osPrefix ); */
  131. static char** GetPrefixes();
  132. };
  133. #endif /* #ifndef DOXYGEN_SKIP */
  134. /************************************************************************/
  135. /* ==================================================================== */
  136. /* VSIArchiveFilesystemHandler */
  137. /* ==================================================================== */
  138. /************************************************************************/
  139. #ifndef DOXYGEN_SKIP
  140. class VSIArchiveEntryFileOffset
  141. {
  142. public:
  143. virtual ~VSIArchiveEntryFileOffset();
  144. };
  145. typedef struct
  146. {
  147. char *fileName;
  148. vsi_l_offset uncompressed_size;
  149. VSIArchiveEntryFileOffset* file_pos;
  150. int bIsDir;
  151. GIntBig nModifiedTime;
  152. } VSIArchiveEntry;
  153. class VSIArchiveContent
  154. {
  155. public:
  156. time_t mTime = 0;
  157. vsi_l_offset nFileSize = 0;
  158. int nEntries = 0;
  159. VSIArchiveEntry* entries = nullptr;
  160. ~VSIArchiveContent();
  161. };
  162. class VSIArchiveReader
  163. {
  164. public:
  165. virtual ~VSIArchiveReader();
  166. virtual int GotoFirstFile() = 0;
  167. virtual int GotoNextFile() = 0;
  168. virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
  169. virtual GUIntBig GetFileSize() = 0;
  170. virtual CPLString GetFileName() = 0;
  171. virtual GIntBig GetModifiedTime() = 0;
  172. virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
  173. };
  174. class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
  175. {
  176. CPL_DISALLOW_COPY_ASSIGN(VSIArchiveFilesystemHandler)
  177. protected:
  178. CPLMutex* hMutex = nullptr;
  179. /* We use a cache that contains the list of files contained in a VSIArchive file as */
  180. /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
  181. /* containing ~1000 files like a CADRG product */
  182. std::map<CPLString,VSIArchiveContent*> oFileList{};
  183. virtual const char* GetPrefix() = 0;
  184. virtual std::vector<CPLString> GetExtensions() = 0;
  185. virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
  186. public:
  187. VSIArchiveFilesystemHandler();
  188. virtual ~VSIArchiveFilesystemHandler();
  189. int Stat( const char *pszFilename, VSIStatBufL *pStatBuf,
  190. int nFlags ) override;
  191. int Unlink( const char *pszFilename ) override;
  192. int Rename( const char *oldpath, const char *newpath ) override;
  193. int Mkdir( const char *pszDirname, long nMode ) override;
  194. int Rmdir( const char *pszDirname ) override;
  195. char **ReadDirEx( const char *pszDirname, int nMaxFiles ) override;
  196. virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = nullptr);
  197. virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive, int bCheckMainFileExists);
  198. virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
  199. virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
  200. };
  201. /************************************************************************/
  202. /* VSIDIR */
  203. /************************************************************************/
  204. struct CPL_DLL VSIDIR
  205. {
  206. VSIDIR() = default;
  207. virtual ~VSIDIR();
  208. virtual const VSIDIREntry* NextDirEntry() = 0;
  209. private:
  210. VSIDIR(const VSIDIR&) = delete;
  211. VSIDIR& operator=(const VSIDIR&) = delete;
  212. };
  213. #endif /* #ifndef DOXYGEN_SKIP */
  214. VSIVirtualHandle CPL_DLL *VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
  215. VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle,
  216. const GByte* pabyBeginningContent,
  217. vsi_l_offset nCheatFileSize);
  218. VSIVirtualHandle CPL_DLL *VSICreateCachedFile( VSIVirtualHandle* poBaseHandle, size_t nChunkSize = 32768, size_t nCacheSize = 0 );
  219. const int CPL_DEFLATE_TYPE_GZIP = 0;
  220. const int CPL_DEFLATE_TYPE_ZLIB = 1;
  221. const int CPL_DEFLATE_TYPE_RAW_DEFLATE = 2;
  222. VSIVirtualHandle CPL_DLL *VSICreateGZipWritable( VSIVirtualHandle* poBaseHandle, int nDeflateType, int bAutoCloseBaseHandle );
  223. #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */
上海开阖软件有限公司 沪ICP备12045867号-1