/src/gdal/port/cpl_vsi_virtual.h
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: VSI Virtual File System |
4 | | * Purpose: Declarations for classes related to the virtual filesystem. |
5 | | * These would only be normally required by applications implementing |
6 | | * their own virtual file system classes which should be rare. |
7 | | * The class interface may be fragile through versions. |
8 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
9 | | * |
10 | | ****************************************************************************** |
11 | | * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com> |
12 | | * Copyright (c) 2010-2014, Even Rouault <even dot rouault at spatialys.com> |
13 | | * |
14 | | * SPDX-License-Identifier: MIT |
15 | | ****************************************************************************/ |
16 | | |
17 | | #ifndef CPL_VSI_VIRTUAL_H_INCLUDED |
18 | | #define CPL_VSI_VIRTUAL_H_INCLUDED |
19 | | |
20 | | #include "cpl_progress.h" |
21 | | #include "cpl_vsi.h" |
22 | | #include "cpl_vsi_error.h" |
23 | | #include "cpl_string.h" |
24 | | |
25 | | #include <cstdint> |
26 | | #include <map> |
27 | | #include <memory> |
28 | | #include <mutex> |
29 | | #include <vector> |
30 | | #include <string> |
31 | | |
32 | | // To avoid aliasing to GetDiskFreeSpace to GetDiskFreeSpaceA on Windows |
33 | | #ifdef GetDiskFreeSpace |
34 | | #undef GetDiskFreeSpace |
35 | | #endif |
36 | | |
37 | | // To avoid aliasing to CopyFile to CopyFileA on Windows |
38 | | #ifdef CopyFile |
39 | | #undef CopyFile |
40 | | #endif |
41 | | |
42 | | /************************************************************************/ |
43 | | /* VSIVirtualHandle */ |
44 | | /************************************************************************/ |
45 | | |
46 | | /** Virtual file handle */ |
47 | | struct CPL_DLL VSIVirtualHandle |
48 | | { |
49 | | public: |
50 | | virtual int Seek(vsi_l_offset nOffset, int nWhence) = 0; |
51 | | virtual vsi_l_offset Tell() = 0; |
52 | | virtual size_t Read(void *pBuffer, size_t nSize, size_t nCount) = 0; |
53 | | virtual int ReadMultiRange(int nRanges, void **ppData, |
54 | | const vsi_l_offset *panOffsets, |
55 | | const size_t *panSizes); |
56 | | |
57 | | /** This method is called when code plans to access soon one or several |
58 | | * ranges in a file. Some file systems may be able to use this hint to |
59 | | * for example asynchronously start such requests. |
60 | | * |
61 | | * Offsets may be given in a non-increasing order, and may potentially |
62 | | * overlap. |
63 | | * |
64 | | * @param nRanges Size of the panOffsets and panSizes arrays. |
65 | | * @param panOffsets Array containing the start offset of each range. |
66 | | * @param panSizes Array containing the size (in bytes) of each range. |
67 | | * @since GDAL 3.7 |
68 | | */ |
69 | | virtual void AdviseRead(CPL_UNUSED int nRanges, |
70 | | CPL_UNUSED const vsi_l_offset *panOffsets, |
71 | | CPL_UNUSED const size_t *panSizes) |
72 | 0 | { |
73 | 0 | } |
74 | | |
75 | | /** Return the total maximum number of bytes that AdviseRead() can handle |
76 | | * at once. |
77 | | * |
78 | | * Some AdviseRead() implementations may give up if the sum of the values |
79 | | * in the panSizes[] array provided to AdviseRead() exceeds a limit. |
80 | | * |
81 | | * Callers might use that threshold to optimize the efficiency of |
82 | | * AdviseRead(). |
83 | | * |
84 | | * A returned value of 0 indicates a unknown limit. |
85 | | * @since GDAL 3.9 |
86 | | */ |
87 | | virtual size_t GetAdviseReadTotalBytesLimit() const |
88 | 0 | { |
89 | 0 | return 0; |
90 | 0 | } |
91 | | |
92 | | virtual size_t Write(const void *pBuffer, size_t nSize, size_t nCount) = 0; |
93 | | |
94 | | int Printf(CPL_FORMAT_STRING(const char *pszFormat), ...) |
95 | | CPL_PRINT_FUNC_FORMAT(2, 3); |
96 | | |
97 | | virtual void ClearErr() = 0; |
98 | | |
99 | | virtual int Eof() = 0; |
100 | | |
101 | | virtual int Error() = 0; |
102 | | |
103 | | virtual int Flush() |
104 | 0 | { |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | | virtual int Close() = 0; |
109 | | // Base implementation that only supports file extension. |
110 | | virtual int Truncate(vsi_l_offset nNewSize); |
111 | | |
112 | | virtual void *GetNativeFileDescriptor() |
113 | 0 | { |
114 | 0 | return nullptr; |
115 | 0 | } |
116 | | |
117 | | virtual VSIRangeStatus GetRangeStatus(CPL_UNUSED vsi_l_offset nOffset, |
118 | | CPL_UNUSED vsi_l_offset nLength) |
119 | 0 | { |
120 | 0 | return VSI_RANGE_STATUS_UNKNOWN; |
121 | 0 | } |
122 | | |
123 | | virtual bool HasPRead() const; |
124 | | virtual size_t PRead(void *pBuffer, size_t nSize, |
125 | | vsi_l_offset nOffset) const; |
126 | | |
127 | | /** Ask current operations to be interrupted. |
128 | | * Implementations must be thread-safe, as this will typically be called |
129 | | * from another thread than the active one for this file. |
130 | | */ |
131 | | virtual void Interrupt() |
132 | 0 | { |
133 | 0 | } |
134 | | |
135 | | /** For a file created with CreateOnlyVisibleAtCloseTime(), ask for the |
136 | | * file to not be created at all (if possible) |
137 | | */ |
138 | | virtual void CancelCreation() |
139 | 0 | { |
140 | 0 | } |
141 | | |
142 | | // NOTE: when adding new methods, besides the "actual" implementations, |
143 | | // also consider the VSICachedFile and VSIVirtualHandleOnlyVisibleAtCloseTime one. |
144 | | |
145 | | virtual ~VSIVirtualHandle() |
146 | 13.4k | { |
147 | 13.4k | } |
148 | | }; |
149 | | |
150 | | /************************************************************************/ |
151 | | /* VSIVirtualHandleCloser */ |
152 | | /************************************************************************/ |
153 | | |
154 | | /** Helper close to use with a std:unique_ptr<VSIVirtualHandle>, |
155 | | * such as VSIVirtualHandleUniquePtr. */ |
156 | | struct VSIVirtualHandleCloser |
157 | | |
158 | | { |
159 | | /** Operator () that closes and deletes the file handle. */ |
160 | | void operator()(VSIVirtualHandle *poHandle) |
161 | 4.08k | { |
162 | 4.08k | if (poHandle) |
163 | 4.08k | { |
164 | 4.08k | poHandle->Close(); |
165 | 4.08k | delete poHandle; |
166 | 4.08k | } |
167 | 4.08k | } |
168 | | }; |
169 | | |
170 | | /** Unique pointer of VSIVirtualHandle that calls the Close() method */ |
171 | | typedef std::unique_ptr<VSIVirtualHandle, VSIVirtualHandleCloser> |
172 | | VSIVirtualHandleUniquePtr; |
173 | | |
174 | | /************************************************************************/ |
175 | | /* VSIProxyFileHandle */ |
176 | | /************************************************************************/ |
177 | | |
178 | | #ifndef DOXYGEN_SKIP |
179 | | class VSIProxyFileHandle /* non final */ : public VSIVirtualHandle |
180 | | { |
181 | | protected: |
182 | | VSIVirtualHandleUniquePtr m_nativeHandle{}; |
183 | | |
184 | | public: |
185 | | explicit VSIProxyFileHandle(VSIVirtualHandleUniquePtr &&nativeHandle) |
186 | 0 | : m_nativeHandle(std::move(nativeHandle)) |
187 | 0 | { |
188 | 0 | } |
189 | | |
190 | | int Seek(vsi_l_offset nOffset, int nWhence) override |
191 | 0 | { |
192 | 0 | return m_nativeHandle->Seek(nOffset, nWhence); |
193 | 0 | } |
194 | | |
195 | | vsi_l_offset Tell() override |
196 | 0 | { |
197 | 0 | return m_nativeHandle->Tell(); |
198 | 0 | } |
199 | | |
200 | | size_t Read(void *pBuffer, size_t nSize, size_t nCount) override |
201 | 0 | { |
202 | 0 | return m_nativeHandle->Read(pBuffer, nSize, nCount); |
203 | 0 | } |
204 | | |
205 | | int ReadMultiRange(int nRanges, void **ppData, |
206 | | const vsi_l_offset *panOffsets, |
207 | | const size_t *panSizes) override |
208 | 0 | { |
209 | 0 | return m_nativeHandle->ReadMultiRange(nRanges, ppData, panOffsets, |
210 | 0 | panSizes); |
211 | 0 | } |
212 | | |
213 | | void AdviseRead(int nRanges, const vsi_l_offset *panOffsets, |
214 | | const size_t *panSizes) override |
215 | 0 | { |
216 | 0 | return m_nativeHandle->AdviseRead(nRanges, panOffsets, panSizes); |
217 | 0 | } |
218 | | |
219 | | size_t GetAdviseReadTotalBytesLimit() const override |
220 | 0 | { |
221 | 0 | return m_nativeHandle->GetAdviseReadTotalBytesLimit(); |
222 | 0 | } |
223 | | |
224 | | size_t Write(const void *pBuffer, size_t nSize, size_t nCount) override |
225 | 0 | { |
226 | 0 | return m_nativeHandle->Write(pBuffer, nSize, nCount); |
227 | 0 | } |
228 | | |
229 | | void ClearErr() override |
230 | 0 | { |
231 | 0 | return m_nativeHandle->ClearErr(); |
232 | 0 | } |
233 | | |
234 | | int Eof() override |
235 | 0 | { |
236 | 0 | return m_nativeHandle->Eof(); |
237 | 0 | } |
238 | | |
239 | | int Error() override |
240 | 0 | { |
241 | 0 | return m_nativeHandle->Error(); |
242 | 0 | } |
243 | | |
244 | | int Flush() override |
245 | 0 | { |
246 | 0 | return m_nativeHandle->Flush(); |
247 | 0 | } |
248 | | |
249 | | int Close() override |
250 | 0 | { |
251 | 0 | return m_nativeHandle->Close(); |
252 | 0 | } |
253 | | |
254 | | int Truncate(vsi_l_offset nNewSize) override |
255 | 0 | { |
256 | 0 | return m_nativeHandle->Truncate(nNewSize); |
257 | 0 | } |
258 | | |
259 | | void *GetNativeFileDescriptor() override |
260 | 0 | { |
261 | 0 | return m_nativeHandle->GetNativeFileDescriptor(); |
262 | 0 | } |
263 | | |
264 | | VSIRangeStatus GetRangeStatus(vsi_l_offset nOffset, |
265 | | vsi_l_offset nLength) override |
266 | 0 | { |
267 | 0 | return m_nativeHandle->GetRangeStatus(nOffset, nLength); |
268 | 0 | } |
269 | | |
270 | | bool HasPRead() const override |
271 | 0 | { |
272 | 0 | return m_nativeHandle->HasPRead(); |
273 | 0 | } |
274 | | |
275 | | size_t PRead(void *pBuffer, size_t nSize, |
276 | | vsi_l_offset nOffset) const override |
277 | 0 | { |
278 | 0 | return m_nativeHandle->PRead(pBuffer, nSize, nOffset); |
279 | 0 | } |
280 | | |
281 | | void Interrupt() override |
282 | 0 | { |
283 | 0 | m_nativeHandle->Interrupt(); |
284 | 0 | } |
285 | | |
286 | | void CancelCreation() override; |
287 | | }; |
288 | | #endif |
289 | | |
290 | | /************************************************************************/ |
291 | | /* VSIFilesystemHandler */ |
292 | | /************************************************************************/ |
293 | | |
294 | | #ifndef DOXYGEN_SKIP |
295 | | class CPL_DLL VSIFilesystemHandler |
296 | | { |
297 | | |
298 | | public: |
299 | | virtual ~VSIFilesystemHandler() |
300 | 0 | { |
301 | 0 | } |
302 | | |
303 | | static VSIVirtualHandleUniquePtr |
304 | | OpenStatic(const char *pszFilename, const char *pszAccess, |
305 | | bool bSetError = false, CSLConstList papszOptions = nullptr); |
306 | | |
307 | | virtual VSIVirtualHandleUniquePtr |
308 | | Open(const char *pszFilename, const char *pszAccess, bool bSetError = false, |
309 | | CSLConstList papszOptions = nullptr) = 0; |
310 | | |
311 | | virtual VSIVirtualHandleUniquePtr |
312 | | CreateOnlyVisibleAtCloseTime(const char *pszFilename, |
313 | | bool bEmulationAllowed, |
314 | | CSLConstList papszOptions); |
315 | | |
316 | | virtual int Stat(const char *pszFilename, VSIStatBufL *pStatBuf, |
317 | | int nFlags) = 0; |
318 | | |
319 | | virtual int Unlink(const char *pszFilename) |
320 | 0 | { |
321 | 0 | (void)pszFilename; |
322 | 0 | errno = ENOENT; |
323 | 0 | return -1; |
324 | 0 | } |
325 | | |
326 | | virtual int *UnlinkBatch(CSLConstList papszFiles); |
327 | | |
328 | | virtual int Mkdir(const char *pszDirname, long nMode) |
329 | 0 | { |
330 | 0 | (void)pszDirname; |
331 | 0 | (void)nMode; |
332 | 0 | errno = ENOENT; |
333 | 0 | return -1; |
334 | 0 | } |
335 | | |
336 | | virtual int Rmdir(const char *pszDirname) |
337 | 0 | { |
338 | 0 | (void)pszDirname; |
339 | 0 | errno = ENOENT; |
340 | 0 | return -1; |
341 | 0 | } |
342 | | |
343 | | virtual int RmdirRecursive(const char *pszDirname); |
344 | | |
345 | | char **ReadDir(const char *pszDirname) |
346 | 0 | { |
347 | 0 | return ReadDirEx(pszDirname, 0); |
348 | 0 | } |
349 | | |
350 | | virtual char **ReadDirEx(const char * /*pszDirname*/, int /* nMaxFiles */) |
351 | 0 | { |
352 | 0 | return nullptr; |
353 | 0 | } |
354 | | |
355 | | virtual char **SiblingFiles(const char * /*pszFilename*/) |
356 | 0 | { |
357 | 0 | return nullptr; |
358 | 0 | } |
359 | | |
360 | | virtual int Rename(const char *oldpath, const char *newpath, |
361 | | GDALProgressFunc pProgressFunc, void *pProgressData) |
362 | 0 | { |
363 | 0 | (void)oldpath; |
364 | 0 | (void)newpath; |
365 | 0 | (void)pProgressFunc; |
366 | 0 | (void)pProgressData; |
367 | 0 | errno = ENOENT; |
368 | 0 | return -1; |
369 | 0 | } |
370 | | |
371 | | virtual int IsCaseSensitive(const char *pszFilename) |
372 | 0 | { |
373 | 0 | (void)pszFilename; |
374 | 0 | return TRUE; |
375 | 0 | } |
376 | | |
377 | | virtual GIntBig GetDiskFreeSpace(const char * /* pszDirname */) |
378 | 0 | { |
379 | 0 | return -1; |
380 | 0 | } |
381 | | |
382 | | virtual int SupportsSparseFiles(const char * /* pszPath */) |
383 | 0 | { |
384 | 0 | return FALSE; |
385 | 0 | } |
386 | | |
387 | | virtual int HasOptimizedReadMultiRange(const char * /* pszPath */) |
388 | 0 | { |
389 | 0 | return FALSE; |
390 | 0 | } |
391 | | |
392 | | virtual const char *GetActualURL(const char * /*pszFilename*/) |
393 | 0 | { |
394 | 0 | return nullptr; |
395 | 0 | } |
396 | | |
397 | | virtual const char *GetOptions() |
398 | 0 | { |
399 | 0 | return nullptr; |
400 | 0 | } |
401 | | |
402 | | virtual char *GetSignedURL(const char * /*pszFilename*/, |
403 | | CSLConstList /* papszOptions */) |
404 | 0 | { |
405 | 0 | return nullptr; |
406 | 0 | } |
407 | | |
408 | | virtual bool Sync(const char *pszSource, const char *pszTarget, |
409 | | const char *const *papszOptions, |
410 | | GDALProgressFunc pProgressFunc, void *pProgressData, |
411 | | char ***ppapszOutputs); |
412 | | |
413 | | virtual int CopyFile(const char *pszSource, const char *pszTarget, |
414 | | VSILFILE *fpSource, vsi_l_offset nSourceSize, |
415 | | const char *const *papszOptions, |
416 | | GDALProgressFunc pProgressFunc, void *pProgressData); |
417 | | |
418 | | virtual int |
419 | | CopyFileRestartable(const char *pszSource, const char *pszTarget, |
420 | | const char *pszInputPayload, char **ppszOutputPayload, |
421 | | CSLConstList papszOptions, |
422 | | GDALProgressFunc pProgressFunc, void *pProgressData); |
423 | | |
424 | | virtual VSIDIR *OpenDir(const char *pszPath, int nRecurseDepth, |
425 | | const char *const *papszOptions); |
426 | | |
427 | | virtual char **GetFileMetadata(const char *pszFilename, |
428 | | const char *pszDomain, |
429 | | CSLConstList papszOptions); |
430 | | |
431 | | virtual bool SetFileMetadata(const char *pszFilename, |
432 | | CSLConstList papszMetadata, |
433 | | const char *pszDomain, |
434 | | CSLConstList papszOptions); |
435 | | |
436 | | virtual bool |
437 | | MultipartUploadGetCapabilities(int *pbNonSequentialUploadSupported, |
438 | | int *pbParallelUploadSupported, |
439 | | int *pbAbortSupported, size_t *pnMinPartSize, |
440 | | size_t *pnMaxPartSize, int *pnMaxPartCount); |
441 | | |
442 | | virtual char *MultipartUploadStart(const char *pszFilename, |
443 | | CSLConstList papszOptions); |
444 | | |
445 | | virtual char *MultipartUploadAddPart(const char *pszFilename, |
446 | | const char *pszUploadId, |
447 | | int nPartNumber, |
448 | | vsi_l_offset nFileOffset, |
449 | | const void *pData, size_t nDataLength, |
450 | | CSLConstList papszOptions); |
451 | | |
452 | | virtual bool |
453 | | MultipartUploadEnd(const char *pszFilename, const char *pszUploadId, |
454 | | size_t nPartIdsCount, const char *const *apszPartIds, |
455 | | vsi_l_offset nTotalSize, CSLConstList papszOptions); |
456 | | |
457 | | virtual bool MultipartUploadAbort(const char *pszFilename, |
458 | | const char *pszUploadId, |
459 | | CSLConstList papszOptions); |
460 | | |
461 | | virtual bool AbortPendingUploads(const char * /*pszFilename*/) |
462 | 0 | { |
463 | 0 | return true; |
464 | 0 | } |
465 | | |
466 | | virtual std::string |
467 | | GetStreamingFilename(const std::string &osFilename) const |
468 | 0 | { |
469 | 0 | return osFilename; |
470 | 0 | } |
471 | | |
472 | | virtual std::string |
473 | | GetNonStreamingFilename(const std::string &osFilename) const |
474 | 0 | { |
475 | 0 | return osFilename; |
476 | 0 | } |
477 | | |
478 | | /** Return the canonical filename. |
479 | | * |
480 | | * May be implemented by case-insensitive filesystems |
481 | | * (currently Win32 and MacOSX) |
482 | | * to return the filename with its actual case (i.e. the one that would |
483 | | * be used when listing the content of the directory). |
484 | | */ |
485 | | virtual std::string |
486 | | GetCanonicalFilename(const std::string &osFilename) const |
487 | 0 | { |
488 | 0 | return osFilename; |
489 | 0 | } |
490 | | |
491 | | virtual bool IsLocal(const char * /* pszPath */) const |
492 | 0 | { |
493 | 0 | return true; |
494 | 0 | } |
495 | | |
496 | | virtual bool IsArchive(const char * /* pszPath */) const |
497 | 0 | { |
498 | 0 | return false; |
499 | 0 | } |
500 | | |
501 | | virtual bool SupportsSequentialWrite(const char * /* pszPath */, |
502 | | bool /* bAllowLocalTempFile */) |
503 | 0 | { |
504 | 0 | return true; |
505 | 0 | } |
506 | | |
507 | | virtual bool SupportsRandomWrite(const char * /* pszPath */, |
508 | | bool /* bAllowLocalTempFile */) |
509 | 0 | { |
510 | 0 | return true; |
511 | 0 | } |
512 | | |
513 | | virtual bool SupportsRead(const char * /* pszPath */) |
514 | 0 | { |
515 | 0 | return true; |
516 | 0 | } |
517 | | |
518 | | virtual VSIFilesystemHandler *Duplicate(const char * /* pszPrefix */) |
519 | 0 | { |
520 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
521 | 0 | "Duplicate() not supported on this file " |
522 | 0 | "system"); |
523 | 0 | return nullptr; |
524 | 0 | } |
525 | | |
526 | | /** Return the directory separator. |
527 | | * |
528 | | * Default is forward slash. The only exception currently is the Windows |
529 | | * file system which returns anti-slash, unless the specified path is of the |
530 | | * form "{drive_letter}:/{rest_of_the_path}". |
531 | | */ |
532 | | virtual const char *GetDirectorySeparator(CPL_UNUSED const char *pszPath) |
533 | 22.1k | { |
534 | 22.1k | return "/"; |
535 | 22.1k | } |
536 | | }; |
537 | | #endif /* #ifndef DOXYGEN_SKIP */ |
538 | | |
539 | | /************************************************************************/ |
540 | | /* VSIFileManager */ |
541 | | /************************************************************************/ |
542 | | |
543 | | #ifndef DOXYGEN_SKIP |
544 | | class CPL_DLL VSIFileManager |
545 | | { |
546 | | private: |
547 | | VSIFilesystemHandler *poDefaultHandler = nullptr; |
548 | | std::map<std::string, VSIFilesystemHandler *> oHandlers{}; |
549 | | |
550 | | VSIFileManager(); |
551 | | |
552 | | static VSIFileManager *Get(); |
553 | | |
554 | | CPL_DISALLOW_COPY_ASSIGN(VSIFileManager) |
555 | | |
556 | | public: |
557 | | ~VSIFileManager(); |
558 | | |
559 | | static VSIFilesystemHandler *GetHandler(const char *); |
560 | | static void InstallHandler(const std::string &osPrefix, |
561 | | VSIFilesystemHandler *); |
562 | | static void RemoveHandler(const std::string &osPrefix); |
563 | | |
564 | | static char **GetPrefixes(); |
565 | | }; |
566 | | #endif /* #ifndef DOXYGEN_SKIP */ |
567 | | |
568 | | /************************************************************************/ |
569 | | /* ==================================================================== */ |
570 | | /* VSIArchiveFilesystemHandler */ |
571 | | /* ==================================================================== */ |
572 | | /************************************************************************/ |
573 | | |
574 | | #ifndef DOXYGEN_SKIP |
575 | | |
576 | | class VSIArchiveEntryFileOffset |
577 | | { |
578 | | public: |
579 | | virtual ~VSIArchiveEntryFileOffset(); |
580 | | }; |
581 | | |
582 | | class VSIArchiveEntry |
583 | | { |
584 | | public: |
585 | | std::string fileName{}; |
586 | | vsi_l_offset uncompressed_size = 0; |
587 | | std::unique_ptr<VSIArchiveEntryFileOffset> file_pos{}; |
588 | | bool bIsDir = false; |
589 | | GIntBig nModifiedTime = 0; |
590 | | }; |
591 | | |
592 | | class VSIArchiveContent |
593 | | { |
594 | | public: |
595 | | time_t mTime = 0; |
596 | | vsi_l_offset nFileSize = 0; |
597 | | std::vector<VSIArchiveEntry> entries{}; |
598 | | |
599 | | // Store list of child indices for each directory |
600 | | using DirectoryChildren = std::vector<int>; |
601 | | |
602 | | std::map<std::string, DirectoryChildren> dirIndex{}; |
603 | | |
604 | 0 | VSIArchiveContent() = default; |
605 | | |
606 | | ~VSIArchiveContent(); |
607 | | |
608 | | private: |
609 | | CPL_DISALLOW_COPY_ASSIGN(VSIArchiveContent) |
610 | | }; |
611 | | |
612 | | class VSIArchiveReader |
613 | | { |
614 | | public: |
615 | | virtual ~VSIArchiveReader(); |
616 | | |
617 | | virtual int GotoFirstFile() = 0; |
618 | | virtual int GotoNextFile() = 0; |
619 | | virtual VSIArchiveEntryFileOffset *GetFileOffset() = 0; |
620 | | virtual GUIntBig GetFileSize() = 0; |
621 | | virtual CPLString GetFileName() = 0; |
622 | | virtual GIntBig GetModifiedTime() = 0; |
623 | | virtual int GotoFileOffset(VSIArchiveEntryFileOffset *pOffset) = 0; |
624 | | }; |
625 | | |
626 | | class VSIArchiveFilesystemHandler /* non final */ : public VSIFilesystemHandler |
627 | | { |
628 | | CPL_DISALLOW_COPY_ASSIGN(VSIArchiveFilesystemHandler) |
629 | | |
630 | | bool FindFileInArchive(const char *archiveFilename, |
631 | | const char *fileInArchiveName, |
632 | | const VSIArchiveEntry **archiveEntry); |
633 | | |
634 | | protected: |
635 | | mutable std::recursive_mutex oMutex{}; |
636 | | |
637 | | /* We use a cache that contains the list of files contained in a VSIArchive |
638 | | * file as */ |
639 | | /* unarchive.c is quite inefficient in listing them. This speeds up access |
640 | | * to VSIArchive files */ |
641 | | /* containing ~1000 files like a CADRG product */ |
642 | | std::map<CPLString, std::unique_ptr<VSIArchiveContent>> oFileList{}; |
643 | | |
644 | | virtual const char *GetPrefix() const = 0; |
645 | | virtual std::vector<CPLString> GetExtensions() const = 0; |
646 | | virtual std::unique_ptr<VSIArchiveReader> |
647 | | CreateReader(const char *pszArchiveFileName) = 0; |
648 | | |
649 | | public: |
650 | | VSIArchiveFilesystemHandler(); |
651 | | virtual ~VSIArchiveFilesystemHandler(); |
652 | | |
653 | | int Stat(const char *pszFilename, VSIStatBufL *pStatBuf, |
654 | | int nFlags) override; |
655 | | char **ReadDirEx(const char *pszDirname, int nMaxFiles) override; |
656 | | |
657 | | virtual const VSIArchiveContent * |
658 | | GetContentOfArchive(const char *archiveFilename, |
659 | | VSIArchiveReader *poReader = nullptr); |
660 | | virtual char *SplitFilename(const char *pszFilename, |
661 | | CPLString &osFileInArchive, |
662 | | bool bCheckMainFileExists, |
663 | | bool bSetError) const; |
664 | | virtual std::unique_ptr<VSIArchiveReader> |
665 | | OpenArchiveFile(const char *archiveFilename, const char *fileInArchiveName); |
666 | | |
667 | | bool IsLocal(const char *pszPath) const override; |
668 | | |
669 | | bool IsArchive(const char *pszPath) const override; |
670 | | |
671 | | bool SupportsSequentialWrite(const char * /* pszPath */, |
672 | | bool /* bAllowLocalTempFile */) override |
673 | 0 | { |
674 | 0 | return false; |
675 | 0 | } |
676 | | |
677 | | bool SupportsRandomWrite(const char * /* pszPath */, |
678 | | bool /* bAllowLocalTempFile */) override |
679 | 0 | { |
680 | 0 | return false; |
681 | 0 | } |
682 | | }; |
683 | | |
684 | | /************************************************************************/ |
685 | | /* VSIDIR */ |
686 | | /************************************************************************/ |
687 | | |
688 | | struct CPL_DLL VSIDIR |
689 | | { |
690 | 0 | VSIDIR() = default; |
691 | | virtual ~VSIDIR(); |
692 | | |
693 | | virtual const VSIDIREntry *NextDirEntry() = 0; |
694 | | |
695 | | private: |
696 | | VSIDIR(const VSIDIR &) = delete; |
697 | | VSIDIR &operator=(const VSIDIR &) = delete; |
698 | | }; |
699 | | |
700 | | #endif /* #ifndef DOXYGEN_SKIP */ |
701 | | |
702 | | VSIVirtualHandle CPL_DLL * |
703 | | VSICreateBufferedReaderHandle(VSIVirtualHandle *poBaseHandle); |
704 | | VSIVirtualHandle * |
705 | | VSICreateBufferedReaderHandle(VSIVirtualHandle *poBaseHandle, |
706 | | const GByte *pabyBeginningContent, |
707 | | vsi_l_offset nCheatFileSize); |
708 | | constexpr int VSI_CACHED_DEFAULT_CHUNK_SIZE = 32768; |
709 | | VSIVirtualHandle CPL_DLL * |
710 | | VSICreateCachedFile(VSIVirtualHandle *poBaseHandle, |
711 | | size_t nChunkSize = VSI_CACHED_DEFAULT_CHUNK_SIZE, |
712 | | size_t nCacheSize = 0); |
713 | | |
714 | | const int CPL_DEFLATE_TYPE_GZIP = 0; |
715 | | const int CPL_DEFLATE_TYPE_ZLIB = 1; |
716 | | const int CPL_DEFLATE_TYPE_RAW_DEFLATE = 2; |
717 | | VSIVirtualHandle CPL_DLL *VSICreateGZipWritable(VSIVirtualHandle *poBaseHandle, |
718 | | int nDeflateType, |
719 | | int bAutoCloseBaseHandle); |
720 | | |
721 | | VSIVirtualHandle *VSICreateGZipWritable(VSIVirtualHandle *poBaseHandle, |
722 | | int nDeflateType, |
723 | | bool bAutoCloseBaseHandle, int nThreads, |
724 | | size_t nChunkSize, |
725 | | size_t nSOZIPIndexEltSize, |
726 | | std::vector<uint8_t> *panSOZIPIndex); |
727 | | |
728 | | VSIVirtualHandle * |
729 | | VSICreateUploadOnCloseFile(VSIVirtualHandleUniquePtr &&poWritableHandle, |
730 | | VSIVirtualHandleUniquePtr &&poTmpFile, |
731 | | const std::string &osTmpFilename); |
732 | | |
733 | | #endif /* ndef CPL_VSI_VIRTUAL_H_INCLUDED */ |