LCOV - code coverage report
Current view: top level - port - cpl_vsil_curl_class.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 127 161 78.9 %
Date: 2026-06-20 20:44:25 Functions: 55 71 77.5 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  CPL - Common Portability Library
       4             :  * Purpose:  Declarations for /vsicurl/ and related file systems
       5             :  * Author:   Even Rouault, even.rouault at spatialys.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2010-2018, Even Rouault <even.rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef CPL_VSIL_CURL_CLASS_H_INCLUDED
      14             : #define CPL_VSIL_CURL_CLASS_H_INCLUDED
      15             : 
      16             : #ifdef HAVE_CURL
      17             : 
      18             : #include "cpl_aws.h"
      19             : #include "cpl_azure.h"
      20             : #include "cpl_port.h"
      21             : #include "cpl_json.h"
      22             : #include "cpl_http.h"
      23             : #include "cpl_string.h"
      24             : #include "cpl_vsil_curl_priv.h"
      25             : #include "cpl_mem_cache.h"
      26             : #include "cpl_multiproc.h"
      27             : 
      28             : #include "cpl_curl_priv.h"
      29             : 
      30             : #include <algorithm>
      31             : #include <atomic>
      32             : #include <condition_variable>
      33             : #include <set>
      34             : #include <map>
      35             : #include <memory>
      36             : #include <mutex>
      37             : #include <thread>
      38             : #include <utility>
      39             : 
      40             : // To avoid aliasing to CopyFile to CopyFileA on Windows
      41             : #ifdef CopyFile
      42             : #undef CopyFile
      43             : #endif
      44             : 
      45             : //! @cond Doxygen_Suppress
      46             : 
      47             : // Leave it for backward compatibility, but deprecate.
      48             : #define HAVE_CURLINFO_REDIRECT_URL
      49             : 
      50             : void VSICurlStreamingClearCache(void);  // from cpl_vsil_curl_streaming.cpp
      51             : 
      52             : struct curl_slist *VSICurlSetOptions(CURL *hCurlHandle, const char *pszURL,
      53             :                                      const char *const *papszOptions);
      54             : 
      55             : struct curl_slist *VSICurlSetContentTypeFromExt(struct curl_slist *polist,
      56             :                                                 const char *pszPath);
      57             : 
      58             : struct curl_slist *VSICurlSetCreationHeadersFromOptions(
      59             :     struct curl_slist *headers, CSLConstList papszOptions, const char *pszPath);
      60             : 
      61             : namespace cpl
      62             : {
      63             : 
      64             : typedef enum
      65             : {
      66             :     EXIST_UNKNOWN = -1,
      67             :     EXIST_NO,
      68             :     EXIST_YES,
      69             : } ExistStatus;
      70             : 
      71             : class FileProp
      72             : {
      73             :   public:
      74             :     unsigned int nGenerationAuthParameters = 0;
      75             :     ExistStatus eExists = EXIST_UNKNOWN;
      76             :     int nHTTPCode = 0;
      77             :     vsi_l_offset fileSize = 0;
      78             :     time_t mTime = 0;
      79             :     time_t nExpireTimestampLocal = 0;
      80             :     std::string osRedirectURL{};
      81             :     bool bHasComputedFileSize = false;
      82             :     bool bIsDirectory = false;
      83             :     bool bIsAzureFolder = false;
      84             :     int nMode = 0;  // st_mode member of struct stat
      85             :     bool bS3LikeRedirect = false;
      86             :     std::string ETag{};
      87             : };
      88             : 
      89             : struct CachedDirList
      90             : {
      91             :     bool bGotFileList = false;
      92             :     unsigned int nGenerationAuthParameters = 0;
      93             :     CPLStringList oFileList{}; /* only file name without path */
      94             : };
      95             : 
      96             : struct WriteFuncStruct
      97             : {
      98             :     char *pBuffer = nullptr;
      99             :     size_t nSize = 0;
     100             :     bool bIsHTTP = false;
     101             :     bool bMultiRange = false;
     102             :     vsi_l_offset nStartOffset = 0;
     103             :     vsi_l_offset nEndOffset = 0;
     104             :     int nHTTPCode = 0;       // potentially after redirect
     105             :     int nFirstHTTPCode = 0;  // the one of the redirect
     106             :     vsi_l_offset nContentLength = 0;
     107             :     bool bFoundContentRange = false;
     108             :     bool bError = false;
     109             :     bool bInterruptDownload = false;
     110             :     bool bDetectRangeDownloadingError = false;
     111             :     GIntBig nTimestampDate = 0;  // Corresponds to Date: header field
     112             : 
     113             :     VSILFILE *fp = nullptr;
     114             :     VSICurlReadCbkFunc pfnReadCbk = nullptr;
     115             :     void *pReadCbkUserData = nullptr;
     116             :     bool bInterrupted = false;
     117             : };
     118             : 
     119             : struct PutData
     120             : {
     121             :     const GByte *pabyData = nullptr;
     122             :     size_t nOff = 0;
     123             :     size_t nTotalSize = 0;
     124             : 
     125          34 :     static size_t ReadCallBackBuffer(char *buffer, size_t size, size_t nitems,
     126             :                                      void *instream)
     127             :     {
     128          34 :         PutData *poThis = static_cast<PutData *>(instream);
     129          34 :         const size_t nSizeMax = size * nitems;
     130             :         const size_t nSizeToWrite =
     131          34 :             std::min(nSizeMax, poThis->nTotalSize - poThis->nOff);
     132          35 :         memcpy(buffer, poThis->pabyData + poThis->nOff, nSizeToWrite);
     133          35 :         poThis->nOff += nSizeToWrite;
     134          35 :         return nSizeToWrite;
     135             :     }
     136             : };
     137             : 
     138             : /************************************************************************/
     139             : /*                       VSICurlFilesystemHandler                       */
     140             : /************************************************************************/
     141             : 
     142             : class VSICurlHandle;
     143             : 
     144             : class VSICurlFilesystemHandlerBase : public VSIFilesystemHandler
     145             : {
     146             :     CPL_DISALLOW_COPY_ASSIGN(VSICurlFilesystemHandlerBase)
     147             : 
     148             :     struct FilenameOffsetPair
     149             :     {
     150             :         std::string filename_;
     151             :         vsi_l_offset offset_;
     152             : 
     153      183689 :         FilenameOffsetPair(const std::string &filename, vsi_l_offset offset)
     154      183689 :             : filename_(filename), offset_(offset)
     155             :         {
     156      183689 :         }
     157             : 
     158      157510 :         bool operator==(const FilenameOffsetPair &other) const
     159             :         {
     160      157510 :             return filename_ == other.filename_ && offset_ == other.offset_;
     161             :         }
     162             :     };
     163             : 
     164             :     struct FilenameOffsetPairHasher
     165             :     {
     166      201691 :         std::size_t operator()(const FilenameOffsetPair &k) const
     167             :         {
     168      201691 :             return std::hash<std::string>()(k.filename_) ^
     169      201691 :                    std::hash<vsi_l_offset>()(k.offset_);
     170             :         }
     171             :     };
     172             : 
     173             :     using RegionCacheType = lru11::Cache<
     174             :         FilenameOffsetPair, std::shared_ptr<std::string>, lru11::NullLock,
     175             :         std::unordered_map<
     176             :             FilenameOffsetPair,
     177             :             typename std::list<lru11::KeyValuePair<
     178             :                 FilenameOffsetPair, std::shared_ptr<std::string>>>::iterator,
     179             :             FilenameOffsetPairHasher>>;
     180             : 
     181             :     std::unique_ptr<RegionCacheType>
     182             :         m_poRegionCacheDoNotUseDirectly{};  // do not access directly. Use
     183             :                                             // GetRegionCache();
     184             :     RegionCacheType *GetRegionCache();
     185             : 
     186             :     int nCachedFilesInDirList = 0;
     187             :     lru11::Cache<std::string, CachedDirList> oCacheDirList;
     188             : 
     189             :     char **ParseHTMLFileList(const char *pszFilename, int nMaxFiles,
     190             :                              char *pszData, bool *pbGotFileList);
     191             : 
     192             :     // Data structure and map to store regions that are in progress, to
     193             :     // avoid simultaneous downloads of the same region in different threads
     194             :     // Cf https://github.com/OSGeo/gdal/issues/8041
     195             :     struct RegionInDownload
     196             :     {
     197             :         std::mutex oMutex{};
     198             :         std::condition_variable oCond{};
     199             :         bool bDownloadInProgress = false;
     200             :         int nWaiters = 0;
     201             :         std::string osData{};
     202             :     };
     203             : 
     204             :     std::mutex m_oMutex{};
     205             :     std::map<std::string, std::unique_ptr<RegionInDownload>>
     206             :         m_oMapRegionInDownload{};
     207             : 
     208             :   protected:
     209             :     CPLMutex *hMutex = nullptr;
     210             : 
     211             :     virtual VSICurlHandle *CreateFileHandle(const char *pszFilename);
     212             :     virtual char **GetFileList(const char *pszFilename, int nMaxFiles,
     213             :                                bool *pbGotFileList);
     214             : 
     215             :     void RegisterEmptyDir(const std::string &osDirname);
     216             : 
     217             :     bool
     218             :     AnalyseS3FileList(const std::string &osBaseURL, const char *pszXML,
     219             :                       CPLStringList &osFileList, int nMaxFiles,
     220             :                       const std::set<std::string> &oSetIgnoredStorageClasses,
     221             :                       bool &bIsTruncated);
     222             : 
     223             :     void AnalyseSwiftFileList(const std::string &osBaseURL,
     224             :                               const std::string &osPrefix, const char *pszJson,
     225             :                               CPLStringList &osFileList, int nMaxFilesThisQuery,
     226             :                               int nMaxFiles, bool &bIsTruncated,
     227             :                               std::string &osNextMarker);
     228             : 
     229             :     VSICurlFilesystemHandlerBase();
     230             : 
     231             :   public:
     232             :     ~VSICurlFilesystemHandlerBase() override;
     233             : 
     234             :     static bool IsAllowedFilename(const char *pszFilename);
     235             : 
     236             :     VSIVirtualHandleUniquePtr Open(const char *pszFilename,
     237             :                                    const char *pszAccess, bool bSetError,
     238             :                                    CSLConstList /* papszOptions */) override;
     239             : 
     240             :     int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
     241             :              int nFlags) override;
     242             :     char **ReadDirEx(const char *pszDirname, int nMaxFiles) override;
     243             :     char **SiblingFiles(const char *pszFilename) override;
     244             : 
     245          18 :     int HasOptimizedReadMultiRange(const char * /* pszPath */) override
     246             :     {
     247          18 :         return true;
     248             :     }
     249             : 
     250             :     const char *GetActualURL(const char *pszFilename) override;
     251             : 
     252             :     const char *GetOptions() override;
     253             : 
     254             :     char **GetFileMetadata(const char *pszFilename, const char *pszDomain,
     255             :                            CSLConstList papszOptions) override;
     256             : 
     257             :     char **ReadDirInternal(const char *pszDirname, int nMaxFiles,
     258             :                            bool *pbGotFileList);
     259             :     void InvalidateDirContent(const std::string &osDirname);
     260             : 
     261             :     virtual const char *GetDebugKey() const = 0;
     262             : 
     263             :     virtual std::string GetFSPrefix() const = 0;
     264             :     virtual bool AllowCachedDataFor(const char *pszFilename);
     265             : 
     266           8 :     bool IsLocal(const char * /* pszPath */) const override
     267             :     {
     268           8 :         return false;
     269             :     }
     270             : 
     271             :     virtual bool
     272           2 :     SupportsSequentialWrite(const char * /* pszPath */,
     273             :                             bool /* bAllowLocalTempFile */) override
     274             :     {
     275           2 :         return false;
     276             :     }
     277             : 
     278           1 :     virtual bool SupportsRandomWrite(const char * /* pszPath */,
     279             :                                      bool /* bAllowLocalTempFile */) override
     280             :     {
     281           1 :         return false;
     282             :     }
     283             : 
     284             :     std::shared_ptr<std::string> GetRegion(const char *pszURL,
     285             :                                            vsi_l_offset nFileOffsetStart);
     286             : 
     287             :     void AddRegion(const char *pszURL, vsi_l_offset nFileOffsetStart,
     288             :                    size_t nSize, const char *pData);
     289             : 
     290             :     std::pair<bool, std::string>
     291             :     NotifyStartDownloadRegion(const std::string &osURL,
     292             :                               vsi_l_offset startOffset, int nBlocks);
     293             :     void NotifyStopDownloadRegion(const std::string &osURL,
     294             :                                   vsi_l_offset startOffset, int nBlocks,
     295             :                                   const std::string &osData);
     296             : 
     297             :     static bool GetCachedFileProp(const char *pszURL, FileProp &oFileProp);
     298             :     static void SetCachedFileProp(const char *pszURL, FileProp &oFileProp);
     299             :     void InvalidateCachedData(const char *pszURL);
     300             : 
     301             :     CURLM *GetCurlMultiHandleFor(const std::string &osURL);
     302             : 
     303             :     virtual void ClearCache();
     304             :     virtual void PartialClearCache(const char *pszFilename);
     305             : 
     306             :     bool GetCachedDirList(const char *pszURL, CachedDirList &oCachedDirList);
     307             :     void SetCachedDirList(const char *pszURL, CachedDirList &oCachedDirList);
     308             :     bool ExistsInCacheDirList(const std::string &osDirname, bool *pbIsDir);
     309             : 
     310             :     virtual std::string GetURLFromFilename(const std::string &osFilename) const;
     311             : 
     312             :     std::string
     313             :     GetStreamingFilename(const std::string &osFilename) const override = 0;
     314             : 
     315             :     static std::set<std::string> GetS3IgnoredStorageClasses();
     316             : 
     317             :     static const char *GetOptionsStatic();
     318             : };
     319             : 
     320             : class VSICurlFilesystemHandler final : public VSICurlFilesystemHandlerBase
     321             : {
     322             :     CPL_DISALLOW_COPY_ASSIGN(VSICurlFilesystemHandler)
     323             : 
     324             :   public:
     325        2098 :     VSICurlFilesystemHandler() = default;
     326             : 
     327        1219 :     const char *GetDebugKey() const override
     328             :     {
     329        1219 :         return "VSICURL";
     330             :     }
     331             : 
     332      175362 :     std::string GetFSPrefix() const override
     333             :     {
     334      175362 :         return "/vsicurl/";
     335             :     }
     336             : 
     337             :     std::string
     338             :     GetStreamingFilename(const std::string &osFilename) const override;
     339             : 
     340             :     std::string
     341             :     GetHintForPotentiallyRecognizedPath(const std::string &osPath) override;
     342             : };
     343             : 
     344             : /************************************************************************/
     345             : /*                            VSICurlHandle                             */
     346             : /************************************************************************/
     347             : 
     348             : class VSICurlHandle /* non final*/ : public VSIVirtualHandle
     349             : {
     350             :     CPL_DISALLOW_COPY_ASSIGN(VSICurlHandle)
     351             : 
     352             :   protected:
     353             :     VSICurlFilesystemHandlerBase *poFS = nullptr;
     354             : 
     355             :     bool m_bCached = true;
     356             : 
     357             :     mutable FileProp oFileProp{};
     358             : 
     359             :     mutable std::mutex m_oMutex{};
     360             :     std::string m_osFilename{};  // e.g "/vsicurl/http://example.com/foo"
     361             :     char *m_pszURL = nullptr;    // e.g "http://example.com/foo"
     362             :     mutable std::string m_osQueryString{};  // e.g. an Azure SAS
     363             : 
     364             :     CPLStringList m_aosHTTPOptions{};
     365             :     CPLHTTPRetryParameters
     366             :         m_oRetryParameters;  // must be initialized in constructor
     367             : 
     368             :     vsi_l_offset lastDownloadedOffset = VSI_L_OFFSET_MAX;
     369             :     int nBlocksToDownload = 1;
     370             : 
     371             :     bool bStopOnInterruptUntilUninstall = false;
     372             :     bool bInterrupted = false;
     373             :     VSICurlReadCbkFunc pfnReadCbk = nullptr;
     374             :     void *pReadCbkUserData = nullptr;
     375             : 
     376             :     CPLStringList m_aosHeaders{};
     377             : 
     378             :     void DownloadRegionPostProcess(const vsi_l_offset startOffset,
     379             :                                    const int nBlocks, const char *pBuffer,
     380             :                                    size_t nSize);
     381             : 
     382             :   private:
     383             :     vsi_l_offset curOffset = 0;
     384             : 
     385             :     bool bEOF = false;
     386             :     bool bError = false;
     387             : 
     388             :     virtual std::string DownloadRegion(vsi_l_offset startOffset, int nBlocks);
     389             : 
     390             :     bool m_bUseHead = false;
     391             :     bool m_bUseRedirectURLIfNoQueryStringParams = false;
     392             : 
     393             :     mutable std::atomic<bool> m_bInterrupt = false;
     394             : 
     395             :     // Specific to Planetary Computer signing:
     396             :     // https://planetarycomputer.microsoft.com/docs/concepts/sas/
     397             :     mutable bool m_bPlanetaryComputerURLSigning = false;
     398             :     mutable std::string m_osPlanetaryComputerCollection{};
     399             :     void ManagePlanetaryComputerSigning() const;
     400             : 
     401             :     void UpdateQueryString() const;
     402             : 
     403             :     int ReadMultiRangeSingleGet(int nRanges, void **ppData,
     404             :                                 const vsi_l_offset *panOffsets,
     405             :                                 const size_t *panSizes);
     406             :     std::string GetRedirectURLIfValid(bool &bHasExpired,
     407             :                                       CPLStringList &aosHTTPOptions) const;
     408             : 
     409             :     void UpdateRedirectInfo(CURL *hCurlHandle,
     410             :                             const WriteFuncStruct &sWriteFuncHeaderData);
     411             : 
     412             :     // Used by AdviseRead()
     413             :     struct AdviseReadRange
     414             :     {
     415             :         bool bDone = false;
     416             :         bool bToRetry = true;
     417             :         double dfSleepDelay = 0.0;
     418             :         std::mutex oMutex{};
     419             :         std::condition_variable oCV{};
     420             :         vsi_l_offset nStartOffset = 0;
     421             :         size_t nSize = 0;
     422             :         std::vector<GByte> abyData{};
     423             :         CPLHTTPRetryContext retryContext;
     424             : 
     425           6 :         explicit AdviseReadRange(const CPLHTTPRetryParameters &oRetryParameters)
     426           6 :             : retryContext(oRetryParameters)
     427             :         {
     428           6 :         }
     429             : 
     430             :         AdviseReadRange(const AdviseReadRange &) = delete;
     431             :         AdviseReadRange &operator=(const AdviseReadRange &) = delete;
     432             :         AdviseReadRange(AdviseReadRange &&) = delete;
     433             :         AdviseReadRange &operator=(AdviseReadRange &&) = delete;
     434             :     };
     435             : 
     436             :     std::vector<std::unique_ptr<AdviseReadRange>> m_aoAdviseReadRanges{};
     437             :     std::thread m_oThreadAdviseRead{};
     438             :     CURLM *m_hCurlMultiHandleForAdviseRead = nullptr;
     439             : 
     440             :   protected:
     441         689 :     virtual struct curl_slist *GetCurlHeaders(const std::string & /*osVerb*/,
     442             :                                               struct curl_slist *psHeaders)
     443             :     {
     444         689 :         return psHeaders;
     445             :     }
     446             : 
     447         754 :     virtual bool AllowAutomaticRedirection()
     448             :     {
     449         754 :         return true;
     450             :     }
     451             : 
     452         156 :     virtual bool CanRestartOnError(const char *, const char *, bool)
     453             :     {
     454         156 :         return false;
     455             :     }
     456             : 
     457         527 :     virtual bool UseLimitRangeGetInsteadOfHead()
     458             :     {
     459         527 :         return false;
     460             :     }
     461             : 
     462         325 :     virtual bool IsDirectoryFromExists(const char * /*pszVerb*/,
     463             :                                        int /*response_code*/)
     464             :     {
     465         325 :         return false;
     466             :     }
     467             : 
     468         153 :     virtual void ProcessGetFileSizeResult(const char * /* pszContent */)
     469             :     {
     470         153 :     }
     471             : 
     472             :     void SetURL(const char *pszURL);
     473             : 
     474           0 :     virtual bool Authenticate(const char * /* pszFilename */)
     475             :     {
     476           0 :         return false;
     477             :     }
     478             : 
     479             :   public:
     480             :     VSICurlHandle(VSICurlFilesystemHandlerBase *poFS, const char *pszFilename,
     481             :                   const char *pszURLIn = nullptr);
     482             :     ~VSICurlHandle() override;
     483             : 
     484             :     int Seek(vsi_l_offset nOffset, int nWhence) override;
     485             :     vsi_l_offset Tell() override;
     486             :     size_t Read(void *pBuffer, size_t nBytes) override;
     487             :     int ReadMultiRange(int nRanges, void **ppData,
     488             :                        const vsi_l_offset *panOffsets,
     489             :                        const size_t *panSizes) override;
     490             :     size_t Write(const void *pBuffer, size_t nBytes) override;
     491             :     void ClearErr() override;
     492             :     int Eof() override;
     493             :     int Error() override;
     494             :     int Flush() override;
     495             :     int Close() override;
     496             : 
     497           0 :     void Interrupt() override
     498             :     {
     499           0 :         m_bInterrupt = true;
     500           0 :     }
     501             : 
     502          18 :     bool HasPRead() const override
     503             :     {
     504          18 :         return true;
     505             :     }
     506             : 
     507             :     size_t PRead(void *pBuffer, size_t nSize,
     508             :                  vsi_l_offset nOffset) const override;
     509             : 
     510             :     void AdviseRead(int nRanges, const vsi_l_offset *panOffsets,
     511             :                     const size_t *panSizes) override;
     512             : 
     513             :     size_t GetAdviseReadTotalBytesLimit() const override;
     514             : 
     515        1126 :     bool IsKnownFileSize() const
     516             :     {
     517        1126 :         return oFileProp.bHasComputedFileSize;
     518             :     }
     519             : 
     520             :     vsi_l_offset GetFileSizeOrHeaders(bool bSetError, bool bGetHeaders);
     521             : 
     522        2023 :     virtual vsi_l_offset GetFileSize(bool bSetError)
     523             :     {
     524        2023 :         return GetFileSizeOrHeaders(bSetError, false);
     525             :     }
     526             : 
     527             :     bool Exists(bool bSetError);
     528             : 
     529        1280 :     bool IsDirectory() const
     530             :     {
     531        1280 :         return oFileProp.bIsDirectory;
     532             :     }
     533             : 
     534        1126 :     int GetMode() const
     535             :     {
     536        1126 :         return oFileProp.nMode;
     537             :     }
     538             : 
     539        1126 :     time_t GetMTime() const
     540             :     {
     541        1126 :         return oFileProp.mTime;
     542             :     }
     543             : 
     544           4 :     const CPLStringList &GetHeaders()
     545             :     {
     546           4 :         return m_aosHeaders;
     547             :     }
     548             : 
     549             :     int InstallReadCbk(VSICurlReadCbkFunc pfnReadCbk, void *pfnUserData,
     550             :                        int bStopOnInterruptUntilUninstall);
     551             :     int UninstallReadCbk();
     552             : 
     553           5 :     const char *GetURL() const
     554             :     {
     555           5 :         return m_pszURL;
     556             :     }
     557             : 
     558             :     //! Whether caching of file content and metadata should persist after file closing
     559         874 :     void SetCache(bool bCache)
     560             :     {
     561         874 :         m_bCached = bCache;
     562         874 :     }
     563             : };
     564             : 
     565             : /************************************************************************/
     566             : /*                 VSICurlFilesystemHandlerBaseWritable                 */
     567             : /************************************************************************/
     568             : 
     569             : class VSICurlFilesystemHandlerBaseWritable /* non final */
     570             :     : public VSICurlFilesystemHandlerBase
     571             : {
     572             :     CPL_DISALLOW_COPY_ASSIGN(VSICurlFilesystemHandlerBaseWritable)
     573             : 
     574             :   protected:
     575       14686 :     VSICurlFilesystemHandlerBaseWritable() = default;
     576             : 
     577             :     virtual VSIVirtualHandleUniquePtr
     578             :     CreateWriteHandle(const char *pszFilename, CSLConstList papszOptions) = 0;
     579             : 
     580             :   public:
     581             :     VSIVirtualHandleUniquePtr Open(const char *pszFilename,
     582             :                                    const char *pszAccess, bool bSetError,
     583             :                                    CSLConstList /* papszOptions */) override;
     584             : 
     585           1 :     bool SupportsSequentialWrite(const char * /* pszPath */,
     586             :                                  bool /* bAllowLocalTempFile */) override
     587             :     {
     588           1 :         return true;
     589             :     }
     590             : 
     591             :     bool SupportsRandomWrite(const char * /* pszPath */,
     592             :                              bool /* bAllowLocalTempFile */) override;
     593             : };
     594             : 
     595             : /************************************************************************/
     596             : /*                         IVSIS3LikeFSHandler                          */
     597             : /************************************************************************/
     598             : 
     599             : class IVSIS3LikeFSHandler /* non final */
     600             :     : public VSICurlFilesystemHandlerBaseWritable
     601             : {
     602             :     CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeFSHandler)
     603             : 
     604             :     virtual int MkdirInternal(const char *pszDirname, long nMode,
     605             :                               bool bDoStatCheck);
     606             : 
     607             :   protected:
     608             :     char **GetFileList(const char *pszFilename, int nMaxFiles,
     609             :                        bool *pbGotFileList) override;
     610             : 
     611             :     virtual IVSIS3LikeHandleHelper *CreateHandleHelper(const char *pszURI,
     612             :                                                        bool bAllowNoObject) = 0;
     613             : 
     614             :     virtual int CopyObject(const char *oldpath, const char *newpath,
     615             :                            CSLConstList papszMetadata);
     616             : 
     617             :     int RmdirRecursiveInternal(const char *pszDirname, int nBatchSize);
     618             : 
     619             :     virtual bool
     620           0 :     IsAllowedHeaderForObjectCreation(const char * /* pszHeaderName */)
     621             :     {
     622           0 :         return false;
     623             :     }
     624             : 
     625       12588 :     IVSIS3LikeFSHandler() = default;
     626             : 
     627             :   public:
     628             :     int Unlink(const char *pszFilename) override;
     629             :     int Mkdir(const char *pszDirname, long nMode) override;
     630             :     int Rmdir(const char *pszDirname) override;
     631             :     int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
     632             :              int nFlags) override;
     633             :     int Rename(const char *oldpath, const char *newpath, GDALProgressFunc,
     634             :                void *) override;
     635             : 
     636             :     virtual int CopyFile(const char *pszSource, const char *pszTarget,
     637             :                          VSILFILE *fpSource, vsi_l_offset nSourceSize,
     638             :                          const char *const *papszOptions,
     639             :                          GDALProgressFunc pProgressFunc,
     640             :                          void *pProgressData) override;
     641             : 
     642             :     virtual int DeleteObject(const char *pszFilename);
     643             : 
     644             :     virtual int *DeleteObjectBatch(CSLConstList papszFilesOrDirs);
     645             : 
     646             :     bool Sync(const char *pszSource, const char *pszTarget,
     647             :               const char *const *papszOptions, GDALProgressFunc pProgressFunc,
     648             :               void *pProgressData, char ***ppapszOutputs) override;
     649             : 
     650             :     VSIDIR *OpenDir(const char *pszPath, int nRecurseDepth,
     651             :                     const char *const *papszOptions) override;
     652             : };
     653             : 
     654             : /************************************************************************/
     655             : /*                IVSIS3LikeFSHandlerWithMultipartUpload                */
     656             : /************************************************************************/
     657             : 
     658             : class IVSIS3LikeFSHandlerWithMultipartUpload /* non final */
     659             :     : public IVSIS3LikeFSHandler
     660             : {
     661             :     CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeFSHandlerWithMultipartUpload)
     662             : 
     663             :   protected:
     664       10490 :     IVSIS3LikeFSHandlerWithMultipartUpload() = default;
     665             : 
     666             :   public:
     667           5 :     virtual bool SupportsNonSequentialMultipartUpload() const
     668             :     {
     669           5 :         return true;
     670             :     }
     671             : 
     672          36 :     virtual bool SupportsParallelMultipartUpload() const
     673             :     {
     674          36 :         return true;
     675             :     }
     676             : 
     677             :     virtual bool SupportsMultipartAbort() const = 0;
     678             : 
     679             :     size_t GetUploadChunkSizeInBytes(const char *pszFilename,
     680             :                                      const char *pszSpecifiedValInBytes);
     681             : 
     682             :     virtual int CopyFileRestartable(const char *pszSource,
     683             :                                     const char *pszTarget,
     684             :                                     const char *pszInputPayload,
     685             :                                     char **ppszOutputPayload,
     686             :                                     CSLConstList papszOptions,
     687             :                                     GDALProgressFunc pProgressFunc,
     688             :                                     void *pProgressData) override;
     689             : 
     690             :     //! Maximum number of parts for multipart upload
     691             :     // Limit currently used by S3 and GS.
     692             :     // Cf https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
     693             :     // and https://cloud.google.com/storage/quotas#requests
     694          11 :     virtual int GetMaximumPartCount()
     695             :     {
     696          11 :         return 10000;
     697             :     }
     698             : 
     699             :     //! Minimum size of a part for multipart upload (except last one), in MiB.
     700             :     // Limit currently used by S3 and GS.
     701             :     // Cf https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
     702             :     // and https://cloud.google.com/storage/quotas#requests
     703           5 :     virtual int GetMinimumPartSizeInMiB()
     704             :     {
     705           5 :         return 5;
     706             :     }
     707             : 
     708             :     //! Maximum size of a part for multipart upload, in MiB.
     709             :     // Limit currently used by S3 and GS.
     710             :     // Cf https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
     711             :     // and https://cloud.google.com/storage/quotas#requests
     712          50 :     virtual int GetMaximumPartSizeInMiB()
     713             :     {
     714             : #if SIZEOF_VOIDP == 8
     715          50 :         return 5 * 1024;
     716             : #else
     717             :         // Cannot be larger than 4, otherwise integer overflow would occur
     718             :         // 1 GiB is the maximum reasonable value on a 32-bit machine
     719             :         return 1 * 1024;
     720             : #endif
     721             :     }
     722             : 
     723             :     //! Default size of a part for multipart upload, in MiB.
     724          47 :     virtual int GetDefaultPartSizeInMiB()
     725             :     {
     726          47 :         return 50;
     727             :     }
     728             : 
     729             :     virtual std::string
     730             :     InitiateMultipartUpload(const std::string &osFilename,
     731             :                             IVSIS3LikeHandleHelper *poS3HandleHelper,
     732             :                             const CPLHTTPRetryParameters &oRetryParameters,
     733             :                             CSLConstList papszOptions);
     734             : 
     735             :     virtual std::string
     736             :     UploadPart(const std::string &osFilename, int nPartNumber,
     737             :                const std::string &osUploadID, vsi_l_offset nPosition,
     738             :                const void *pabyBuffer, size_t nBufferSize,
     739             :                IVSIS3LikeHandleHelper *poS3HandleHelper,
     740             :                const CPLHTTPRetryParameters &oRetryParameters,
     741             :                CSLConstList papszOptions);
     742             : 
     743             :     virtual bool CompleteMultipart(
     744             :         const std::string &osFilename, const std::string &osUploadID,
     745             :         const std::vector<std::string> &aosEtags, vsi_l_offset nTotalSize,
     746             :         IVSIS3LikeHandleHelper *poS3HandleHelper,
     747             :         const CPLHTTPRetryParameters &oRetryParameters);
     748             : 
     749             :     virtual bool AbortMultipart(const std::string &osFilename,
     750             :                                 const std::string &osUploadID,
     751             :                                 IVSIS3LikeHandleHelper *poS3HandleHelper,
     752             :                                 const CPLHTTPRetryParameters &oRetryParameters);
     753             : 
     754             :     bool AbortPendingUploads(const char *pszFilename) override;
     755             : 
     756             :     bool MultipartUploadGetCapabilities(int *pbNonSequentialUploadSupported,
     757             :                                         int *pbParallelUploadSupported,
     758             :                                         int *pbAbortSupported,
     759             :                                         size_t *pnMinPartSize,
     760             :                                         size_t *pnMaxPartSize,
     761             :                                         int *pnMaxPartCount) override;
     762             : 
     763             :     char *MultipartUploadStart(const char *pszFilename,
     764             :                                CSLConstList papszOptions) override;
     765             : 
     766             :     char *MultipartUploadAddPart(const char *pszFilename,
     767             :                                  const char *pszUploadId, int nPartNumber,
     768             :                                  vsi_l_offset nFileOffset, const void *pData,
     769             :                                  size_t nDataLength,
     770             :                                  CSLConstList papszOptions) override;
     771             : 
     772             :     bool MultipartUploadEnd(const char *pszFilename, const char *pszUploadId,
     773             :                             size_t nPartIdsCount,
     774             :                             const char *const *apszPartIds,
     775             :                             vsi_l_offset nTotalSize,
     776             :                             CSLConstList papszOptions) override;
     777             : 
     778             :     bool MultipartUploadAbort(const char *pszFilename, const char *pszUploadId,
     779             :                               CSLConstList papszOptions) override;
     780             : };
     781             : 
     782             : /************************************************************************/
     783             : /*                           IVSIS3LikeHandle                           */
     784             : /************************************************************************/
     785             : 
     786         368 : class IVSIS3LikeHandle /* non final */ : public VSICurlHandle
     787             : {
     788             :     CPL_DISALLOW_COPY_ASSIGN(IVSIS3LikeHandle)
     789             : 
     790             :   protected:
     791         162 :     bool UseLimitRangeGetInsteadOfHead() override
     792             :     {
     793         162 :         return true;
     794             :     }
     795             : 
     796         117 :     bool IsDirectoryFromExists(const char *pszVerb, int response_code) override
     797             :     {
     798             :         // A bit dirty, but on S3, a GET on a existing directory returns a 416
     799         121 :         return response_code == 416 && EQUAL(pszVerb, "GET") &&
     800         121 :                std::string(m_pszURL).back() == '/';
     801             :     }
     802             : 
     803          55 :     void ProcessGetFileSizeResult(const char *pszContent) override
     804             :     {
     805          55 :         oFileProp.bIsDirectory =
     806          55 :             strstr(pszContent, "ListBucketResult") != nullptr;
     807          55 :     }
     808             : 
     809             :   public:
     810         368 :     IVSIS3LikeHandle(VSICurlFilesystemHandlerBase *poFSIn,
     811             :                      const char *pszFilename, const char *pszURLIn)
     812         368 :         : VSICurlHandle(poFSIn, pszFilename, pszURLIn)
     813             :     {
     814         368 :     }
     815             : 
     816             :     ~IVSIS3LikeHandle() override;
     817             : };
     818             : 
     819             : /************************************************************************/
     820             : /*                       VSIMultipartWriteHandle                        */
     821             : /************************************************************************/
     822             : 
     823             : class VSIMultipartWriteHandle final : public VSIVirtualHandle
     824             : {
     825             :     CPL_DISALLOW_COPY_ASSIGN(VSIMultipartWriteHandle)
     826             : 
     827             :     IVSIS3LikeFSHandlerWithMultipartUpload *m_poFS = nullptr;
     828             :     std::string m_osFilename{};
     829             :     IVSIS3LikeHandleHelper *m_poS3HandleHelper = nullptr;
     830             :     CPLStringList m_aosOptions{};
     831             :     CPLStringList m_aosHTTPOptions{};
     832             :     CPLHTTPRetryParameters m_oRetryParameters;
     833             : 
     834             :     vsi_l_offset m_nCurOffset = 0;
     835             :     size_t m_nBufferOff = 0;
     836             :     size_t m_nBufferSize = 0;
     837             :     bool m_bClosed = false;
     838             :     GByte *m_pabyBuffer = nullptr;
     839             :     std::string m_osUploadID{};
     840             :     int m_nPartNumber = 0;
     841             :     std::vector<std::string> m_aosEtags{};
     842             :     bool m_bError = false;
     843             : 
     844             :     WriteFuncStruct m_sWriteFuncHeaderData{};
     845             : 
     846             :     bool UploadPart();
     847             :     bool DoSinglePartPUT();
     848             : 
     849             :     void InvalidateParentDirectory();
     850             : 
     851             :   public:
     852             :     VSIMultipartWriteHandle(IVSIS3LikeFSHandlerWithMultipartUpload *poFS,
     853             :                             const char *pszFilename,
     854             :                             IVSIS3LikeHandleHelper *poS3HandleHelper,
     855             :                             CSLConstList papszOptions);
     856             :     ~VSIMultipartWriteHandle() override;
     857             : 
     858             :     int Seek(vsi_l_offset nOffset, int nWhence) override;
     859             :     vsi_l_offset Tell() override;
     860             :     size_t Read(void *pBuffer, size_t nBytes) override;
     861             :     size_t Write(const void *pBuffer, size_t nBytes) override;
     862             : 
     863           0 :     void ClearErr() override
     864             :     {
     865           0 :     }
     866             : 
     867           0 :     int Error() override
     868             :     {
     869           0 :         return FALSE;
     870             :     }
     871             : 
     872           0 :     int Eof() override
     873             :     {
     874           0 :         return FALSE;
     875             :     }
     876             : 
     877             :     int Close() override;
     878             : 
     879          31 :     bool IsOK()
     880             :     {
     881          31 :         return m_pabyBuffer != nullptr;
     882             :     }
     883             : };
     884             : 
     885             : /************************************************************************/
     886             : /*                       VSIChunkedWriteHandle()                        */
     887             : /************************************************************************/
     888             : 
     889             : /** Class with Write() append-only implementation using
     890             :  * "Transfer-Encoding: chunked" writing
     891             :  */
     892             : class VSIChunkedWriteHandle final : public VSIVirtualHandle
     893             : {
     894             :     CPL_DISALLOW_COPY_ASSIGN(VSIChunkedWriteHandle)
     895             : 
     896             :     IVSIS3LikeFSHandler *m_poFS = nullptr;
     897             :     std::string m_osFilename{};
     898             :     IVSIS3LikeHandleHelper *m_poS3HandleHelper = nullptr;
     899             :     CPLStringList m_aosOptions{};
     900             :     CPLStringList m_aosHTTPOptions{};
     901             :     CPLHTTPRetryParameters m_oRetryParameters;
     902             : 
     903             :     vsi_l_offset m_nCurOffset = 0;
     904             :     size_t m_nBufferOff = 0;
     905             :     bool m_bError = false;
     906             :     bool m_bClosed = false;
     907             : 
     908             :     CURLM *m_hCurlMulti = nullptr;
     909             :     CURL *m_hCurl = nullptr;
     910             :     const void *m_pBuffer = nullptr;
     911             :     std::string m_osCurlErrBuf{};
     912             :     size_t m_nChunkedBufferOff = 0;
     913             :     size_t m_nChunkedBufferSize = 0;
     914             :     size_t m_nWrittenInPUT = 0;
     915             : 
     916             :     WriteFuncStruct m_sWriteFuncHeaderData{};
     917             : 
     918             :     static size_t ReadCallBackBufferChunked(char *buffer, size_t size,
     919             :                                             size_t nitems, void *instream);
     920             :     int FinishChunkedTransfer();
     921             : 
     922             :     bool DoEmptyPUT();
     923             : 
     924             :     void InvalidateParentDirectory();
     925             : 
     926             :   public:
     927             :     VSIChunkedWriteHandle(IVSIS3LikeFSHandler *poFS, const char *pszFilename,
     928             :                           IVSIS3LikeHandleHelper *poS3HandleHelper,
     929             :                           CSLConstList papszOptions);
     930             :     ~VSIChunkedWriteHandle() override;
     931             : 
     932             :     int Seek(vsi_l_offset nOffset, int nWhence) override;
     933             :     vsi_l_offset Tell() override;
     934             :     size_t Read(void *pBuffer, size_t nBytes) override;
     935             :     size_t Write(const void *pBuffer, size_t nBytes) override;
     936             : 
     937           0 :     void ClearErr() override
     938             :     {
     939           0 :     }
     940             : 
     941           0 :     int Error() override
     942             :     {
     943           0 :         return FALSE;
     944             :     }
     945             : 
     946           0 :     int Eof() override
     947             :     {
     948           0 :         return FALSE;
     949             :     }
     950             : 
     951             :     int Close() override;
     952             : };
     953             : 
     954             : /************************************************************************/
     955             : /*                         VSIAppendWriteHandle                         */
     956             : /************************************************************************/
     957             : 
     958             : class VSIAppendWriteHandle CPL_NON_FINAL : public VSIVirtualHandle
     959             : {
     960             :     CPL_DISALLOW_COPY_ASSIGN(VSIAppendWriteHandle)
     961             : 
     962             :   protected:
     963             :     VSICurlFilesystemHandlerBase *m_poFS = nullptr;
     964             :     std::string m_osFSPrefix{};
     965             :     std::string m_osFilename{};
     966             :     CPLHTTPRetryParameters m_oRetryParameters{};
     967             : 
     968             :     vsi_l_offset m_nCurOffset = 0;
     969             :     int m_nBufferOff = 0;
     970             :     int m_nBufferSize = 0;
     971             :     int m_nBufferOffReadCallback = 0;
     972             :     bool m_bClosed = false;
     973             :     GByte *m_pabyBuffer = nullptr;
     974             :     bool m_bError = false;
     975             : 
     976             :     static size_t ReadCallBackBuffer(char *buffer, size_t size, size_t nitems,
     977             :                                      void *instream);
     978             :     virtual bool Send(bool bIsLastBlock) = 0;
     979             : 
     980             :   public:
     981             :     VSIAppendWriteHandle(VSICurlFilesystemHandlerBase *poFS,
     982             :                          const char *pszFSPrefix, const char *pszFilename,
     983             :                          int nChunkSize);
     984             :     ~VSIAppendWriteHandle() override;
     985             : 
     986             :     int Seek(vsi_l_offset nOffset, int nWhence) override;
     987             :     vsi_l_offset Tell() override;
     988             :     size_t Read(void *pBuffer, size_t nBytes) override;
     989             :     size_t Write(const void *pBuffer, size_t nBytes) override;
     990             : 
     991           0 :     void ClearErr() override
     992             :     {
     993           0 :     }
     994             : 
     995           0 :     int Error() override
     996             :     {
     997           0 :         return FALSE;
     998             :     }
     999             : 
    1000           0 :     int Eof() override
    1001             :     {
    1002           0 :         return FALSE;
    1003             :     }
    1004             : 
    1005             :     int Close() override;
    1006             : 
    1007          10 :     bool IsOK()
    1008             :     {
    1009          10 :         return m_pabyBuffer != nullptr;
    1010             :     }
    1011             : };
    1012             : 
    1013             : /************************************************************************/
    1014             : /*                    VSIDIRWithMissingDirSynthesis                     */
    1015             : /************************************************************************/
    1016             : 
    1017         138 : struct VSIDIRWithMissingDirSynthesis /* non final */ : public VSIDIR
    1018             : {
    1019             :     std::vector<std::unique_ptr<VSIDIREntry>> aoEntries{};
    1020             : 
    1021             :   protected:
    1022             :     ~VSIDIRWithMissingDirSynthesis() override;
    1023             : 
    1024             :     std::vector<std::string> m_aosSubpathsStack{};
    1025             : 
    1026             :     void SynthetizeMissingDirectories(const std::string &osCurSubdir,
    1027             :                                       bool bAddEntryForThisSubdir);
    1028             : };
    1029             : 
    1030             : /************************************************************************/
    1031             : /*                             VSIDIRS3Like                             */
    1032             : /************************************************************************/
    1033             : 
    1034             : struct VSIDIRS3Like /* non final */ : public VSIDIRWithMissingDirSynthesis
    1035             : {
    1036             :     const std::string m_osDirName;
    1037             : 
    1038             :     int nRecurseDepth = 0;
    1039             : 
    1040             :     std::string osNextMarker{};
    1041             :     int nPos = 0;
    1042             : 
    1043             :     std::string osBucket{};
    1044             :     std::string osObjectKey{};
    1045             :     VSICurlFilesystemHandlerBase *poFS = nullptr;
    1046             :     IVSIS3LikeFSHandler *poS3FS = nullptr;
    1047             :     std::unique_ptr<IVSIS3LikeHandleHelper> poHandleHelper{};
    1048             :     int nMaxFiles = 0;
    1049             :     bool bCacheEntries = true;
    1050             :     bool m_bSynthetizeMissingDirectories = false;
    1051             :     std::string m_osFilterPrefix{};
    1052             : 
    1053             :     // used when listing only the file system prefix
    1054             :     std::unique_ptr<VSIDIR, decltype(&VSICloseDir)> m_subdir{nullptr,
    1055         138 :                                                              VSICloseDir};
    1056             : 
    1057         138 :     VSIDIRS3Like(const std::string &osDirName, IVSIS3LikeFSHandler *poFSIn)
    1058         138 :         : m_osDirName(osDirName), poFS(poFSIn), poS3FS(poFSIn)
    1059             :     {
    1060         138 :     }
    1061             : 
    1062           0 :     VSIDIRS3Like(const std::string &osDirName,
    1063             :                  VSICurlFilesystemHandlerBase *poFSIn)
    1064           0 :         : m_osDirName(osDirName), poFS(poFSIn)
    1065             :     {
    1066           0 :     }
    1067             : 
    1068             :     VSIDIRS3Like(const VSIDIRS3Like &) = delete;
    1069             :     VSIDIRS3Like &operator=(const VSIDIRS3Like &) = delete;
    1070             : 
    1071             :     const VSIDIREntry *NextDirEntry() override;
    1072             : 
    1073             :     virtual bool IssueListDir() = 0;
    1074             :     void clear();
    1075             : };
    1076             : 
    1077             : /************************************************************************/
    1078             : /*                          CurlRequestHelper                           */
    1079             : /************************************************************************/
    1080             : 
    1081             : struct CurlRequestHelper
    1082             : {
    1083             :     WriteFuncStruct sWriteFuncData{};
    1084             :     WriteFuncStruct sWriteFuncHeaderData{};
    1085             :     char szCurlErrBuf[CURL_ERROR_SIZE + 1] = {};
    1086             : 
    1087             :     CurlRequestHelper();
    1088             :     ~CurlRequestHelper();
    1089             :     long perform(CURL *hCurlHandle,
    1090             :                  struct curl_slist *headers,  // ownership transferred
    1091             :                  VSICurlFilesystemHandlerBase *poFS,
    1092             :                  IVSIS3LikeHandleHelper *poS3HandleHelper);
    1093             : };
    1094             : 
    1095             : /************************************************************************/
    1096             : /*                       NetworkStatisticsLogger                        */
    1097             : /************************************************************************/
    1098             : 
    1099             : class NetworkStatisticsLogger
    1100             : {
    1101             :     static int gnEnabled;
    1102             :     static NetworkStatisticsLogger gInstance;
    1103             : 
    1104        1919 :     NetworkStatisticsLogger() = default;
    1105             : 
    1106             :     std::mutex m_mutex{};
    1107             : 
    1108             :     struct Counters
    1109             :     {
    1110             :         GIntBig nHEAD = 0;
    1111             :         GIntBig nGET = 0;
    1112             :         GIntBig nPUT = 0;
    1113             :         GIntBig nPOST = 0;
    1114             :         GIntBig nDELETE = 0;
    1115             :         GIntBig nGETDownloadedBytes = 0;
    1116             :         GIntBig nPUTUploadedBytes = 0;
    1117             :         GIntBig nPOSTDownloadedBytes = 0;
    1118             :         GIntBig nPOSTUploadedBytes = 0;
    1119             :     };
    1120             : 
    1121             :     enum class ContextPathType
    1122             :     {
    1123             :         FILESYSTEM,
    1124             :         FILE,
    1125             :         ACTION,
    1126             :     };
    1127             : 
    1128             :     struct ContextPathItem
    1129             :     {
    1130             :         ContextPathType eType;
    1131             :         std::string osName;
    1132             : 
    1133           3 :         ContextPathItem(ContextPathType eTypeIn, const std::string &osNameIn)
    1134           3 :             : eType(eTypeIn), osName(osNameIn)
    1135             :         {
    1136           3 :         }
    1137             : 
    1138           0 :         bool operator<(const ContextPathItem &other) const
    1139             :         {
    1140           0 :             if (static_cast<int>(eType) < static_cast<int>(other.eType))
    1141           0 :                 return true;
    1142           0 :             if (static_cast<int>(eType) > static_cast<int>(other.eType))
    1143           0 :                 return false;
    1144           0 :             return osName < other.osName;
    1145             :         }
    1146             :     };
    1147             : 
    1148             :     struct Stats
    1149             :     {
    1150             :         Counters counters{};
    1151             :         std::map<ContextPathItem, Stats> children{};
    1152             : 
    1153             :         void AsJSON(CPLJSONObject &oJSON) const;
    1154             :     };
    1155             : 
    1156             :     // Workaround bug in Coverity Scan
    1157             :     // coverity[generated_default_constructor_used_in_field_initializer]
    1158             :     Stats m_stats{};
    1159             :     std::map<GIntBig, std::vector<ContextPathItem>>
    1160             :         m_mapThreadIdToContextPath{};
    1161             : 
    1162             :     static void ReadEnabled();
    1163             : 
    1164             :     std::vector<Counters *> GetCountersForContext();
    1165             : 
    1166             :   public:
    1167      904814 :     static inline bool IsEnabled()
    1168             :     {
    1169      904814 :         if (gnEnabled < 0)
    1170             :         {
    1171           7 :             ReadEnabled();
    1172             :         }
    1173      904814 :         return gnEnabled == TRUE;
    1174             :     }
    1175             : 
    1176             :     static void EnterFileSystem(const char *pszName);
    1177             : 
    1178             :     static void LeaveFileSystem();
    1179             : 
    1180             :     static void EnterFile(const char *pszName);
    1181             : 
    1182             :     static void LeaveFile();
    1183             : 
    1184             :     static void EnterAction(const char *pszName);
    1185             : 
    1186             :     static void LeaveAction();
    1187             : 
    1188             :     static void LogHEAD();
    1189             : 
    1190             :     static void LogGET(size_t nDownloadedBytes);
    1191             : 
    1192             :     static void LogPUT(size_t nUploadedBytes);
    1193             : 
    1194             :     static void LogPOST(size_t nUploadedBytes, size_t nDownloadedBytes);
    1195             : 
    1196             :     static void LogDELETE();
    1197             : 
    1198             :     static void Reset();
    1199             : 
    1200             :     static std::string GetReportAsSerializedJSON();
    1201             : };
    1202             : 
    1203             : struct NetworkStatisticsFileSystem
    1204             : {
    1205      151310 :     inline explicit NetworkStatisticsFileSystem(const char *pszName)
    1206             :     {
    1207      151310 :         NetworkStatisticsLogger::EnterFileSystem(pszName);
    1208      151310 :     }
    1209             : 
    1210      151310 :     inline ~NetworkStatisticsFileSystem()
    1211             :     {
    1212      151310 :         NetworkStatisticsLogger::LeaveFileSystem();
    1213      151310 :     }
    1214             : };
    1215             : 
    1216             : struct NetworkStatisticsFile
    1217             : {
    1218      149047 :     inline explicit NetworkStatisticsFile(const char *pszName)
    1219             :     {
    1220      149047 :         NetworkStatisticsLogger::EnterFile(pszName);
    1221      149047 :     }
    1222             : 
    1223      149047 :     inline ~NetworkStatisticsFile()
    1224             :     {
    1225      149047 :         NetworkStatisticsLogger::LeaveFile();
    1226      149047 :     }
    1227             : };
    1228             : 
    1229             : struct NetworkStatisticsAction
    1230             : {
    1231      151310 :     inline explicit NetworkStatisticsAction(const char *pszName)
    1232             :     {
    1233      151310 :         NetworkStatisticsLogger::EnterAction(pszName);
    1234      151310 :     }
    1235             : 
    1236      151310 :     inline ~NetworkStatisticsAction()
    1237             :     {
    1238      151310 :         NetworkStatisticsLogger::LeaveAction();
    1239      151310 :     }
    1240             : };
    1241             : 
    1242             : }  // namespace cpl
    1243             : 
    1244             : int VSICURLGetDownloadChunkSize();
    1245             : 
    1246             : void VSICURLInitWriteFuncStruct(cpl::WriteFuncStruct *psStruct, VSILFILE *fp,
    1247             :                                 VSICurlReadCbkFunc pfnReadCbk,
    1248             :                                 void *pReadCbkUserData);
    1249             : size_t VSICurlHandleWriteFunc(void *buffer, size_t count, size_t nmemb,
    1250             :                               void *req);
    1251             : void VSICURLMultiPerform(CURLM *hCurlMultiHandle, CURL *hEasyHandle = nullptr,
    1252             :                          std::atomic<bool> *pbInterrupt = nullptr);
    1253             : void VSICURLResetHeaderAndWriterFunctions(CURL *hCurlHandle);
    1254             : 
    1255             : int VSICurlParseUnixPermissions(const char *pszPermissions);
    1256             : 
    1257             : // Cache of file properties (size, etc.)
    1258             : bool VSICURLGetCachedFileProp(const char *pszURL, cpl::FileProp &oFileProp);
    1259             : void VSICURLSetCachedFileProp(const char *pszURL, cpl::FileProp &oFileProp);
    1260             : void VSICURLInvalidateCachedFileProp(const char *pszURL);
    1261             : void VSICURLInvalidateCachedFilePropPrefix(const char *pszURL);
    1262             : void VSICURLDestroyCacheFileProp();
    1263             : 
    1264             : void VSICURLMultiCleanup(CURLM *hCurlMultiHandle);
    1265             : 
    1266             : //! @endcond
    1267             : 
    1268             : #endif  // HAVE_CURL
    1269             : 
    1270             : #endif  // CPL_VSIL_CURL_CLASS_H_INCLUDED

Generated by: LCOV version 1.14