cpl_vsi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /******************************************************************************
  2. * $Id: cpl_vsi.h 28476 2015-02-13 14:40:11Z rouault $
  3. *
  4. * Project: CPL - Common Portability Library
  5. * Author: Frank Warmerdam, warmerdam@pobox.com
  6. * Purpose: Include file defining Virtual File System (VSI) functions, a
  7. * layer over POSIX file and other system services.
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 1998, Frank Warmerdam
  11. * Copyright (c) 2008-2014, Even Rouault <even dot rouault at mines-paris dot org>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included
  21. * in all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. ****************************************************************************/
  31. #ifndef CPL_VSI_H_INCLUDED
  32. #define CPL_VSI_H_INCLUDED
  33. #include "cpl_port.h"
  34. /**
  35. * \file cpl_vsi.h
  36. *
  37. * Standard C Covers
  38. *
  39. * The VSI functions are intended to be hookable aliases for Standard C
  40. * I/O, memory allocation and other system functions. They are intended
  41. * to allow virtualization of disk I/O so that non file data sources
  42. * can be made to appear as files, and so that additional error trapping
  43. * and reporting can be interested. The memory access API is aliased
  44. * so that special application memory management services can be used.
  45. *
  46. * Is is intended that each of these functions retains exactly the same
  47. * calling pattern as the original Standard C functions they relate to.
  48. * This means we don't have to provide custom documentation, and also means
  49. * that the default implementation is very simple.
  50. */
  51. /* -------------------------------------------------------------------- */
  52. /* We need access to ``struct stat''. */
  53. /* -------------------------------------------------------------------- */
  54. /* Unix */
  55. #if !defined(_WIN32) && !defined(_WIN32_WCE)
  56. # include <unistd.h>
  57. #endif
  58. /* Windows */
  59. #if !defined(macos_pre10) && !defined(_WIN32_WCE)
  60. # include <sys/stat.h>
  61. #endif
  62. /* Windows CE */
  63. #if defined(_WIN32_WCE)
  64. # include <wce_stat.h>
  65. #endif
  66. CPL_C_START
  67. /* ==================================================================== */
  68. /* stdio file access functions. These may not support large */
  69. /* files, and don't necessarily go through the virtualization */
  70. /* API. */
  71. /* ==================================================================== */
  72. FILE CPL_DLL * VSIFOpen( const char *, const char * ) CPL_WARN_UNUSED_RESULT;
  73. int CPL_DLL VSIFClose( FILE * );
  74. int CPL_DLL VSIFSeek( FILE *, long, int );
  75. long CPL_DLL VSIFTell( FILE * );
  76. void CPL_DLL VSIRewind( FILE * );
  77. void CPL_DLL VSIFFlush( FILE * );
  78. size_t CPL_DLL VSIFRead( void *, size_t, size_t, FILE * );
  79. size_t CPL_DLL VSIFWrite( const void *, size_t, size_t, FILE * );
  80. char CPL_DLL *VSIFGets( char *, int, FILE * );
  81. int CPL_DLL VSIFPuts( const char *, FILE * );
  82. int CPL_DLL VSIFPrintf( FILE *, const char *, ... ) CPL_PRINT_FUNC_FORMAT(2, 3);
  83. int CPL_DLL VSIFGetc( FILE * );
  84. int CPL_DLL VSIFPutc( int, FILE * );
  85. int CPL_DLL VSIUngetc( int, FILE * );
  86. int CPL_DLL VSIFEof( FILE * );
  87. /* ==================================================================== */
  88. /* VSIStat() related. */
  89. /* ==================================================================== */
  90. typedef struct stat VSIStatBuf;
  91. int CPL_DLL VSIStat( const char *, VSIStatBuf * );
  92. #ifdef _WIN32
  93. # define VSI_ISLNK(x) ( 0 ) /* N/A on Windows */
  94. # define VSI_ISREG(x) ((x) & S_IFREG)
  95. # define VSI_ISDIR(x) ((x) & S_IFDIR)
  96. # define VSI_ISCHR(x) ((x) & S_IFCHR)
  97. # define VSI_ISBLK(x) ( 0 ) /* N/A on Windows */
  98. #else
  99. # define VSI_ISLNK(x) S_ISLNK(x)
  100. # define VSI_ISREG(x) S_ISREG(x)
  101. # define VSI_ISDIR(x) S_ISDIR(x)
  102. # define VSI_ISCHR(x) S_ISCHR(x)
  103. # define VSI_ISBLK(x) S_ISBLK(x)
  104. #endif
  105. /* ==================================================================== */
  106. /* 64bit stdio file access functions. If we have a big size */
  107. /* defined, then provide protypes for the large file API, */
  108. /* otherwise redefine to use the regular api. */
  109. /* ==================================================================== */
  110. typedef GUIntBig vsi_l_offset;
  111. /* Make VSIL_STRICT_ENFORCE active in DEBUG builds */
  112. #ifdef DEBUG
  113. #define VSIL_STRICT_ENFORCE
  114. #endif
  115. #ifdef VSIL_STRICT_ENFORCE
  116. typedef struct _VSILFILE VSILFILE;
  117. #else
  118. typedef FILE VSILFILE;
  119. #endif
  120. VSILFILE CPL_DLL * VSIFOpenL( const char *, const char * ) CPL_WARN_UNUSED_RESULT;
  121. int CPL_DLL VSIFCloseL( VSILFILE * );
  122. int CPL_DLL VSIFSeekL( VSILFILE *, vsi_l_offset, int );
  123. vsi_l_offset CPL_DLL VSIFTellL( VSILFILE * );
  124. void CPL_DLL VSIRewindL( VSILFILE * );
  125. size_t CPL_DLL VSIFReadL( void *, size_t, size_t, VSILFILE * );
  126. int CPL_DLL VSIFReadMultiRangeL( int nRanges, void ** ppData, const vsi_l_offset* panOffsets, const size_t* panSizes, VSILFILE * );
  127. size_t CPL_DLL VSIFWriteL( const void *, size_t, size_t, VSILFILE * );
  128. int CPL_DLL VSIFEofL( VSILFILE * );
  129. int CPL_DLL VSIFTruncateL( VSILFILE *, vsi_l_offset );
  130. int CPL_DLL VSIFFlushL( VSILFILE * );
  131. int CPL_DLL VSIFPrintfL( VSILFILE *, const char *, ... ) CPL_PRINT_FUNC_FORMAT(2, 3);
  132. int CPL_DLL VSIFPutcL( int, VSILFILE * );
  133. int CPL_DLL VSIIngestFile( VSILFILE* fp,
  134. const char* pszFilename,
  135. GByte** ppabyRet,
  136. vsi_l_offset* pnSize,
  137. GIntBig nMaxSize );
  138. #if defined(VSI_STAT64_T)
  139. typedef struct VSI_STAT64_T VSIStatBufL;
  140. #else
  141. #define VSIStatBufL VSIStatBuf
  142. #endif
  143. int CPL_DLL VSIStatL( const char *, VSIStatBufL * );
  144. #define VSI_STAT_EXISTS_FLAG 0x1
  145. #define VSI_STAT_NATURE_FLAG 0x2
  146. #define VSI_STAT_SIZE_FLAG 0x4
  147. int CPL_DLL VSIStatExL( const char * pszFilename, VSIStatBufL * psStatBuf, int nFlags );
  148. int CPL_DLL VSIIsCaseSensitiveFS( const char * pszFilename );
  149. void CPL_DLL *VSIFGetNativeFileDescriptorL( VSILFILE* );
  150. /* ==================================================================== */
  151. /* Memory allocation */
  152. /* ==================================================================== */
  153. void CPL_DLL *VSICalloc( size_t, size_t ) CPL_WARN_UNUSED_RESULT;
  154. void CPL_DLL *VSIMalloc( size_t ) CPL_WARN_UNUSED_RESULT;
  155. void CPL_DLL VSIFree( void * );
  156. void CPL_DLL *VSIRealloc( void *, size_t ) CPL_WARN_UNUSED_RESULT;
  157. char CPL_DLL *VSIStrdup( const char * ) CPL_WARN_UNUSED_RESULT;
  158. /**
  159. VSIMalloc2 allocates (nSize1 * nSize2) bytes.
  160. In case of overflow of the multiplication, or if memory allocation fails, a
  161. NULL pointer is returned and a CE_Failure error is raised with CPLError().
  162. If nSize1 == 0 || nSize2 == 0, a NULL pointer will also be returned.
  163. CPLFree() or VSIFree() can be used to free memory allocated by this function.
  164. */
  165. void CPL_DLL *VSIMalloc2( size_t nSize1, size_t nSize2 ) CPL_WARN_UNUSED_RESULT;
  166. /**
  167. VSIMalloc3 allocates (nSize1 * nSize2 * nSize3) bytes.
  168. In case of overflow of the multiplication, or if memory allocation fails, a
  169. NULL pointer is returned and a CE_Failure error is raised with CPLError().
  170. If nSize1 == 0 || nSize2 == 0 || nSize3 == 0, a NULL pointer will also be returned.
  171. CPLFree() or VSIFree() can be used to free memory allocated by this function.
  172. */
  173. void CPL_DLL *VSIMalloc3( size_t nSize1, size_t nSize2, size_t nSize3 ) CPL_WARN_UNUSED_RESULT;
  174. GIntBig CPL_DLL CPLGetPhysicalRAM(void);
  175. GIntBig CPL_DLL CPLGetUsablePhysicalRAM(void);
  176. /* ==================================================================== */
  177. /* Other... */
  178. /* ==================================================================== */
  179. #define CPLReadDir VSIReadDir
  180. char CPL_DLL **VSIReadDir( const char * );
  181. char CPL_DLL **VSIReadDirRecursive( const char *pszPath );
  182. int CPL_DLL VSIMkdir( const char * pathname, long mode );
  183. int CPL_DLL VSIRmdir( const char * pathname );
  184. int CPL_DLL VSIUnlink( const char * pathname );
  185. int CPL_DLL VSIRename( const char * oldpath, const char * newpath );
  186. char CPL_DLL *VSIStrerror( int );
  187. /* ==================================================================== */
  188. /* Install special file access handlers. */
  189. /* ==================================================================== */
  190. void CPL_DLL VSIInstallMemFileHandler(void);
  191. void CPL_DLL VSIInstallLargeFileHandler(void);
  192. void CPL_DLL VSIInstallSubFileHandler(void);
  193. void VSIInstallCurlFileHandler(void);
  194. void VSIInstallCurlStreamingFileHandler(void);
  195. void VSIInstallGZipFileHandler(void); /* No reason to export that */
  196. void VSIInstallZipFileHandler(void); /* No reason to export that */
  197. void VSIInstallStdinHandler(void); /* No reason to export that */
  198. void VSIInstallStdoutHandler(void); /* No reason to export that */
  199. void CPL_DLL VSIInstallSparseFileHandler(void);
  200. void VSIInstallTarFileHandler(void); /* No reason to export that */
  201. void CPL_DLL VSICleanupFileManager(void);
  202. VSILFILE CPL_DLL *VSIFileFromMemBuffer( const char *pszFilename,
  203. GByte *pabyData,
  204. vsi_l_offset nDataLength,
  205. int bTakeOwnership );
  206. GByte CPL_DLL *VSIGetMemFileBuffer( const char *pszFilename,
  207. vsi_l_offset *pnDataLength,
  208. int bUnlinkAndSeize );
  209. typedef size_t (*VSIWriteFunction)(const void* ptr, size_t size, size_t nmemb, FILE* stream);
  210. void CPL_DLL VSIStdoutSetRedirection( VSIWriteFunction pFct, FILE* stream );
  211. /* ==================================================================== */
  212. /* Time quering. */
  213. /* ==================================================================== */
  214. unsigned long CPL_DLL VSITime( unsigned long * );
  215. const char CPL_DLL *VSICTime( unsigned long );
  216. struct tm CPL_DLL *VSIGMTime( const time_t *pnTime,
  217. struct tm *poBrokenTime );
  218. struct tm CPL_DLL *VSILocalTime( const time_t *pnTime,
  219. struct tm *poBrokenTime );
  220. /* -------------------------------------------------------------------- */
  221. /* the following can be turned on for detailed logging of */
  222. /* almost all IO calls. */
  223. /* -------------------------------------------------------------------- */
  224. #ifdef VSI_DEBUG
  225. #ifndef DEBUG
  226. # define DEBUG
  227. #endif
  228. #include "cpl_error.h"
  229. #define VSIDebug4(f,a1,a2,a3,a4) CPLDebug( "VSI", f, a1, a2, a3, a4 );
  230. #define VSIDebug3( f, a1, a2, a3 ) CPLDebug( "VSI", f, a1, a2, a3 );
  231. #define VSIDebug2( f, a1, a2 ) CPLDebug( "VSI", f, a1, a2 );
  232. #define VSIDebug1( f, a1 ) CPLDebug( "VSI", f, a1 );
  233. #else
  234. #define VSIDebug4( f, a1, a2, a3, a4 ) {}
  235. #define VSIDebug3( f, a1, a2, a3 ) {}
  236. #define VSIDebug2( f, a1, a2 ) {}
  237. #define VSIDebug1( f, a1 ) {}
  238. #endif
  239. CPL_C_END
  240. #endif /* ndef CPL_VSI_H_INCLUDED */