cpl_vsi_virtual.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /******************************************************************************
  2. * $Id: cpl_vsi_virtual.h 29388 2015-06-17 18:28:29Z 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 implmenting
  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_string.h"
  37. #include "cpl_multiproc.h"
  38. #if defined(WIN32CE)
  39. # include "cpl_wince.h"
  40. # include <wce_errno.h>
  41. # pragma warning(disable:4786) /* Remove annoying warnings in eVC++ and VC++ 6.0 */
  42. #endif
  43. #include <map>
  44. #include <vector>
  45. #include <string>
  46. /************************************************************************/
  47. /* VSIVirtualHandle */
  48. /************************************************************************/
  49. class CPL_DLL VSIVirtualHandle {
  50. public:
  51. virtual int Seek( vsi_l_offset nOffset, int nWhence ) = 0;
  52. virtual vsi_l_offset Tell() = 0;
  53. virtual size_t Read( void *pBuffer, size_t nSize, size_t nMemb ) = 0;
  54. virtual int ReadMultiRange( int nRanges, void ** ppData, const vsi_l_offset* panOffsets, const size_t* panSizes );
  55. virtual size_t Write( const void *pBuffer, size_t nSize,size_t nMemb)=0;
  56. virtual int Eof() = 0;
  57. virtual int Flush() {return 0;}
  58. virtual int Close() = 0;
  59. virtual int Truncate( CPL_UNUSED vsi_l_offset nNewSize ) { return -1; }
  60. virtual void *GetNativeFileDescriptor() { return NULL; }
  61. virtual ~VSIVirtualHandle() { }
  62. };
  63. /************************************************************************/
  64. /* VSIFilesystemHandler */
  65. /************************************************************************/
  66. class CPL_DLL VSIFilesystemHandler {
  67. public:
  68. virtual ~VSIFilesystemHandler() {}
  69. virtual VSIVirtualHandle *Open( const char *pszFilename,
  70. const char *pszAccess) = 0;
  71. virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags) = 0;
  72. virtual int Unlink( const char *pszFilename )
  73. { (void) pszFilename; errno=ENOENT; return -1; }
  74. virtual int Mkdir( const char *pszDirname, long nMode )
  75. {(void)pszDirname; (void)nMode; errno=ENOENT; return -1;}
  76. virtual int Rmdir( const char *pszDirname )
  77. { (void) pszDirname; errno=ENOENT; return -1; }
  78. virtual char **ReadDir( const char *pszDirname )
  79. { (void) pszDirname; return NULL; }
  80. virtual int Rename( const char *oldpath, const char *newpath )
  81. { (void) oldpath; (void)newpath; errno=ENOENT; return -1; }
  82. virtual int IsCaseSensitive( const char* pszFilename )
  83. { (void) pszFilename; return TRUE; }
  84. };
  85. /************************************************************************/
  86. /* VSIFileManager */
  87. /************************************************************************/
  88. class CPL_DLL VSIFileManager
  89. {
  90. private:
  91. VSIFilesystemHandler *poDefaultHandler;
  92. std::map<std::string, VSIFilesystemHandler *> oHandlers;
  93. VSIFileManager();
  94. static VSIFileManager *Get();
  95. public:
  96. ~VSIFileManager();
  97. static VSIFilesystemHandler *GetHandler( const char * );
  98. static void InstallHandler( const std::string& osPrefix,
  99. VSIFilesystemHandler * );
  100. /* RemoveHandler is never defined. */
  101. /* static void RemoveHandler( const std::string& osPrefix ); */
  102. };
  103. /************************************************************************/
  104. /* ==================================================================== */
  105. /* VSIArchiveFilesystemHandler */
  106. /* ==================================================================== */
  107. /************************************************************************/
  108. class VSIArchiveEntryFileOffset
  109. {
  110. public:
  111. virtual ~VSIArchiveEntryFileOffset();
  112. };
  113. typedef struct
  114. {
  115. char *fileName;
  116. vsi_l_offset uncompressed_size;
  117. VSIArchiveEntryFileOffset* file_pos;
  118. int bIsDir;
  119. GIntBig nModifiedTime;
  120. } VSIArchiveEntry;
  121. typedef struct
  122. {
  123. time_t mTime;
  124. vsi_l_offset nFileSize;
  125. int nEntries;
  126. VSIArchiveEntry* entries;
  127. } VSIArchiveContent;
  128. class VSIArchiveReader
  129. {
  130. public:
  131. virtual ~VSIArchiveReader();
  132. virtual int GotoFirstFile() = 0;
  133. virtual int GotoNextFile() = 0;
  134. virtual VSIArchiveEntryFileOffset* GetFileOffset() = 0;
  135. virtual GUIntBig GetFileSize() = 0;
  136. virtual CPLString GetFileName() = 0;
  137. virtual GIntBig GetModifiedTime() = 0;
  138. virtual int GotoFileOffset(VSIArchiveEntryFileOffset* pOffset) = 0;
  139. };
  140. class VSIArchiveFilesystemHandler : public VSIFilesystemHandler
  141. {
  142. protected:
  143. CPLMutex* hMutex;
  144. /* We use a cache that contains the list of files containes in a VSIArchive file as */
  145. /* unarchive.c is quite inefficient in listing them. This speeds up access to VSIArchive files */
  146. /* containing ~1000 files like a CADRG product */
  147. std::map<CPLString,VSIArchiveContent*> oFileList;
  148. virtual const char* GetPrefix() = 0;
  149. virtual std::vector<CPLString> GetExtensions() = 0;
  150. virtual VSIArchiveReader* CreateReader(const char* pszArchiveFileName) = 0;
  151. public:
  152. VSIArchiveFilesystemHandler();
  153. virtual ~VSIArchiveFilesystemHandler();
  154. virtual int Stat( const char *pszFilename, VSIStatBufL *pStatBuf, int nFlags );
  155. virtual int Unlink( const char *pszFilename );
  156. virtual int Rename( const char *oldpath, const char *newpath );
  157. virtual int Mkdir( const char *pszDirname, long nMode );
  158. virtual int Rmdir( const char *pszDirname );
  159. virtual char **ReadDir( const char *pszDirname );
  160. virtual const VSIArchiveContent* GetContentOfArchive(const char* archiveFilename, VSIArchiveReader* poReader = NULL);
  161. virtual char* SplitFilename(const char *pszFilename, CPLString &osFileInArchive, int bCheckMainFileExists);
  162. virtual VSIArchiveReader* OpenArchiveFile(const char* archiveFilename, const char* fileInArchiveName);
  163. virtual int FindFileInArchive(const char* archiveFilename, const char* fileInArchiveName, const VSIArchiveEntry** archiveEntry);
  164. };
  165. VSIVirtualHandle CPL_DLL *VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle);
  166. VSIVirtualHandle* VSICreateBufferedReaderHandle(VSIVirtualHandle* poBaseHandle,
  167. const GByte* pabyBeginningContent,
  168. vsi_l_offset nSheatFileSize);
  169. VSIVirtualHandle* VSICreateCachedFile( VSIVirtualHandle* poBaseHandle, size_t nChunkSize = 32768, size_t nCacheSize = 0 );
  170. VSIVirtualHandle CPL_DLL *VSICreateGZipWritable( VSIVirtualHandle* poBaseHandle, int bRegularZLibIn, int bAutoCloseBaseHandle );
  171. #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */