LCOV - code coverage report
Current view: top level - port - cpl_vsil.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 855 963 88.8 %
Date: 2025-01-18 12:42:00 Functions: 101 110 91.8 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  VSI Virtual File System
       4             :  * Purpose:  Implementation VSI*L File API and other file system access
       5             :  *           methods going through file virtualization.
       6             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
      10             :  * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
      11             :  *
      12             :  * SPDX-License-Identifier: MIT
      13             :  ****************************************************************************/
      14             : 
      15             : #include "cpl_port.h"
      16             : #include "cpl_vsi.h"
      17             : 
      18             : #include <cassert>
      19             : #include <cinttypes>
      20             : #include <cstdarg>
      21             : #include <cstddef>
      22             : #include <cstring>
      23             : #if HAVE_FCNTL_H
      24             : #include <fcntl.h>
      25             : #endif
      26             : 
      27             : #include <algorithm>
      28             : #include <limits>
      29             : #include <map>
      30             : #include <memory>
      31             : #include <mutex>
      32             : #include <set>
      33             : #include <string>
      34             : #include <utility>
      35             : #include <vector>
      36             : 
      37             : #include "cpl_conv.h"
      38             : #include "cpl_error.h"
      39             : #include "cpl_multiproc.h"
      40             : #include "cpl_string.h"
      41             : #include "cpl_vsi_virtual.h"
      42             : #include "cpl_vsil_curl_class.h"
      43             : 
      44             : // To avoid aliasing to GetDiskFreeSpace to GetDiskFreeSpaceA on Windows
      45             : #ifdef GetDiskFreeSpace
      46             : #undef GetDiskFreeSpace
      47             : #endif
      48             : 
      49             : /************************************************************************/
      50             : /*                             VSIReadDir()                             */
      51             : /************************************************************************/
      52             : 
      53             : /**
      54             :  * \brief Read names in a directory.
      55             :  *
      56             :  * This function abstracts access to directory contains.  It returns a
      57             :  * list of strings containing the names of files, and directories in this
      58             :  * directory.  The resulting string list becomes the responsibility of the
      59             :  * application and should be freed with CSLDestroy() when no longer needed.
      60             :  *
      61             :  * Note that no error is issued via CPLError() if the directory path is
      62             :  * invalid, though NULL is returned.
      63             :  *
      64             :  * This function used to be known as CPLReadDir(), but the old name is now
      65             :  * deprecated.
      66             :  *
      67             :  * @param pszPath the relative, or absolute path of a directory to read.
      68             :  * UTF-8 encoded.
      69             :  * @return The list of entries in the directory, or NULL if the directory
      70             :  * doesn't exist.  Filenames are returned in UTF-8 encoding.
      71             :  */
      72             : 
      73       14281 : char **VSIReadDir(const char *pszPath)
      74             : {
      75       14281 :     return VSIReadDirEx(pszPath, 0);
      76             : }
      77             : 
      78             : /************************************************************************/
      79             : /*                             VSIReadDirEx()                           */
      80             : /************************************************************************/
      81             : 
      82             : /**
      83             :  * \brief Read names in a directory.
      84             :  *
      85             :  * This function abstracts access to directory contains.  It returns a
      86             :  * list of strings containing the names of files, and directories in this
      87             :  * directory.  The resulting string list becomes the responsibility of the
      88             :  * application and should be freed with CSLDestroy() when no longer needed.
      89             :  *
      90             :  * Note that no error is issued via CPLError() if the directory path is
      91             :  * invalid, though NULL is returned.
      92             :  *
      93             :  * If nMaxFiles is set to a positive number, directory listing will stop after
      94             :  * that limit has been reached. Note that to indicate truncate, at least one
      95             :  * element more than the nMaxFiles limit will be returned. If CSLCount() on the
      96             :  * result is lesser or equal to nMaxFiles, then no truncation occurred.
      97             :  *
      98             :  * @param pszPath the relative, or absolute path of a directory to read.
      99             :  * UTF-8 encoded.
     100             :  * @param nMaxFiles maximum number of files after which to stop, or 0 for no
     101             :  * limit.
     102             :  * @return The list of entries in the directory, or NULL if the directory
     103             :  * doesn't exist.  Filenames are returned in UTF-8 encoding.
     104             :  * @since GDAL 2.1
     105             :  */
     106             : 
     107       48250 : char **VSIReadDirEx(const char *pszPath, int nMaxFiles)
     108             : {
     109       48250 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
     110             : 
     111       48250 :     return poFSHandler->ReadDirEx(pszPath, nMaxFiles);
     112             : }
     113             : 
     114             : /************************************************************************/
     115             : /*                             VSISiblingFiles()                        */
     116             : /************************************************************************/
     117             : 
     118             : /**
     119             :  * \brief Return related filenames
     120             :  *
     121             :  * This function is essentially meant at being used by GDAL internals.
     122             :  *
     123             :  * @param pszFilename the path of a filename to inspect
     124             :  * UTF-8 encoded.
     125             :  * @return The list of entries, relative to the directory, of all sidecar
     126             :  * files available or NULL if the list is not known.
     127             :  * Filenames are returned in UTF-8 encoding.
     128             :  * Most implementations will return NULL, and a subsequent ReadDir will
     129             :  * list all files available in the file's directory. This function will be
     130             :  * overridden by VSI FilesystemHandlers that wish to force e.g. an empty list
     131             :  * to avoid opening non-existent files on slow filesystems. The return value
     132             :  * shall be destroyed with CSLDestroy()
     133             :  * @since GDAL 3.2
     134             :  */
     135       71957 : char **VSISiblingFiles(const char *pszFilename)
     136             : {
     137       71957 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
     138             : 
     139       71962 :     return poFSHandler->SiblingFiles(pszFilename);
     140             : }
     141             : 
     142             : /************************************************************************/
     143             : /*                           VSIFnMatch()                               */
     144             : /************************************************************************/
     145             : 
     146         533 : static bool VSIFnMatch(const char *pszPattern, const char *pszStr)
     147             : {
     148         533 :     for (; *pszPattern && *pszStr; pszPattern++, pszStr++)
     149             :     {
     150         527 :         if (*pszPattern == '*')
     151             :         {
     152          14 :             if (pszPattern[1] == 0)
     153           4 :                 return true;
     154          46 :             for (; *pszStr; ++pszStr)
     155             :             {
     156          40 :                 if (VSIFnMatch(pszPattern + 1, pszStr))
     157           4 :                     return true;
     158             :             }
     159           6 :             return false;
     160             :         }
     161         513 :         else if (*pszPattern == '?')
     162             :         {
     163             :             // match single any char
     164             :         }
     165         509 :         else if (*pszPattern == '[')
     166             :         {
     167             :             // match character classes and ranges
     168             :             // "[abcd]" will match a character that is a, b, c or d
     169             :             // "[a-z]" will match a character that is a to z
     170             :             // "[!abcd] will match a character that is *not* a, b, c or d
     171             :             // "[]]" will match character ]
     172             :             // "[]-]" will match character ] or -
     173             :             // "[!]a-]" will match a character that is *not* ], a or -
     174             : 
     175          10 :             const char *pszOpenBracket = pszPattern;
     176          10 :             ++pszPattern;
     177          10 :             const bool isNot = (*pszPattern == '!');
     178          10 :             if (isNot)
     179             :             {
     180           3 :                 ++pszOpenBracket;
     181           3 :                 ++pszPattern;
     182             :             }
     183          10 :             bool res = false;
     184          22 :             for (; *pszPattern; ++pszPattern)
     185             :             {
     186          21 :                 if ((*pszPattern == ']' || *pszPattern == '-') &&
     187          13 :                     pszPattern == pszOpenBracket + 1)
     188             :                 {
     189           3 :                     if (*pszStr == *pszPattern)
     190             :                     {
     191           2 :                         res = true;
     192             :                     }
     193             :                 }
     194          18 :                 else if (*pszPattern == ']')
     195             :                 {
     196           9 :                     break;
     197             :                 }
     198           9 :                 else if (pszPattern[1] == '-' && pszPattern[2] != 0 &&
     199           2 :                          pszPattern[2] != ']')
     200             :                 {
     201           1 :                     if (*pszStr >= pszPattern[0] && *pszStr <= pszPattern[2])
     202             :                     {
     203           1 :                         res = true;
     204             :                     }
     205           1 :                     pszPattern += 2;
     206             :                 }
     207           8 :                 else if (*pszStr == *pszPattern)
     208             :                 {
     209           2 :                     res = true;
     210             :                 }
     211             :             }
     212          10 :             if (*pszPattern == 0)
     213           1 :                 return false;
     214           9 :             if (!res && !isNot)
     215           1 :                 return false;
     216           8 :             if (res && isNot)
     217           0 :                 return false;
     218             :         }
     219         499 :         else if (*pszPattern != *pszStr)
     220             :         {
     221         448 :             return false;
     222             :         }
     223             :     }
     224           6 :     return *pszPattern == 0 && *pszStr == 0;
     225             : }
     226             : 
     227             : /************************************************************************/
     228             : /*                             VSIGlob()                                */
     229             : /************************************************************************/
     230             : 
     231             : /**
     232             :  \brief Return a list of file and directory names matching
     233             :  a pattern that can contain wildcards.
     234             : 
     235             :  This function has similar behavior to the POSIX glob() function:
     236             :  https://man7.org/linux/man-pages/man7/glob.7.html
     237             : 
     238             :  In particular it supports the following wildcards:
     239             :  <ul>
     240             :  <li>'*': match any string</li>
     241             :  <li>'?': match any single character</li>
     242             :  <li>'[': match character class or range, with '!' immediately after '['
     243             :  to indicate negation.</li>
     244             :  </ul>
     245             :  Refer to to the above man page for more details.
     246             : 
     247             :  It also supports the "**" recursive wildcard, behaving similarly to Python
     248             :  glob.glob() with recursive=True. Be careful of the amount of memory and time
     249             :  required when using that recursive wildcard on directories with a large
     250             :  amount of files and subdirectories.
     251             : 
     252             :  Examples, given a file hierarchy:
     253             :  - one.tif
     254             :  - my_subdir/two.tif
     255             :  - my_subdir/subsubdir/three.tif
     256             : 
     257             :  \code{.cpp}
     258             :  VSIGlob("one.tif",NULL,NULL,NULL) returns ["one.tif", NULL]
     259             :  VSIGlob("*.tif",NULL,NULL,NULL) returns ["one.tif", NULL]
     260             :  VSIGlob("on?.tif",NULL,NULL,NULL) returns ["one.tif", NULL]
     261             :  VSIGlob("on[a-z].tif",NULL,NULL,NULL) returns ["one.tif", NULL]
     262             :  VSIGlob("on[ef].tif",NULL,NULL,NULL) returns ["one.tif", NULL]
     263             :  VSIGlob("on[!e].tif",NULL,NULL,NULL) returns NULL
     264             :  VSIGlob("my_subdir" "/" "*.tif",NULL,NULL,NULL) returns ["my_subdir/two.tif", NULL]
     265             :  VSIGlob("**" "/" "*.tif",NULL,NULL,NULL) returns ["one.tif", "my_subdir/two.tif", "my_subdir/subsubdir/three.tif", NULL]
     266             :  \endcode
     267             : 
     268             :  In the current implementation, matching is done based on the assumption that
     269             :  a character fits into a single byte, which will not work properly on
     270             :  non-ASCII UTF-8 filenames.
     271             : 
     272             :  VSIGlob() works with any virtual file systems supported by GDAL, including
     273             :  network file systems such as /vsis3/, /vsigs/, /vsiaz/, etc. But note that
     274             :  for those ones, the pattern is not passed to the remote server, and thus large
     275             :  amount of filenames can be transferred from the remote server to the host
     276             :  where the filtering is done.
     277             : 
     278             :  @param pszPattern the relative, or absolute path of a directory to read.
     279             :  UTF-8 encoded.
     280             :  @param papszOptions NULL-terminate list of options, or NULL. None supported
     281             :  currently.
     282             :  @param pProgressFunc Progress function, or NULL. This is only used as a way
     283             :  for the user to cancel operation if it takes too much time. The percentage
     284             :  passed to the callback is not significant (always at 0).
     285             :  @param pProgressData User data passed to the progress function, or NULL.
     286             :  @return The list of matched filenames, which must be freed with CSLDestroy().
     287             :  Filenames are returned in UTF-8 encoding.
     288             : 
     289             :  @since GDAL 3.11
     290             : */
     291             : 
     292          17 : char **VSIGlob(const char *pszPattern, const char *const *papszOptions,
     293             :                GDALProgressFunc pProgressFunc, void *pProgressData)
     294             : {
     295          17 :     CPL_IGNORE_RET_VAL(papszOptions);
     296             : 
     297          34 :     CPLStringList aosRes;
     298          34 :     std::vector<std::pair<std::string, size_t>> candidates;
     299          17 :     candidates.emplace_back(pszPattern, 0);
     300          48 :     while (!candidates.empty())
     301             :     {
     302          31 :         auto [osPattern, nPosStart] = candidates.back();
     303          31 :         pszPattern = osPattern.c_str() + nPosStart;
     304          31 :         candidates.pop_back();
     305             : 
     306          31 :         std::string osPath = osPattern.substr(0, nPosStart);
     307          31 :         std::string osCurPath;
     308         515 :         for (;; ++pszPattern)
     309             :         {
     310         546 :             if (*pszPattern == 0 || *pszPattern == '/' || *pszPattern == '\\')
     311             :             {
     312             :                 struct VSIDirCloser
     313             :                 {
     314          15 :                     void operator()(VSIDIR *dir)
     315             :                     {
     316          15 :                         VSICloseDir(dir);
     317          15 :                     }
     318             :                 };
     319             : 
     320         104 :                 if (osCurPath == "**")
     321             :                 {
     322             :                     std::unique_ptr<VSIDIR, VSIDirCloser> psDir(
     323           1 :                         VSIOpenDir(osPath.c_str(), -1, nullptr));
     324           1 :                     if (!psDir)
     325           0 :                         return nullptr;
     326             :                     while (const VSIDIREntry *psEntry =
     327           5 :                                VSIGetNextDirEntry(psDir.get()))
     328             :                     {
     329           4 :                         if (pProgressFunc &&
     330           0 :                             !pProgressFunc(0, "", pProgressData))
     331             :                         {
     332           0 :                             return nullptr;
     333             :                         }
     334             :                         {
     335           8 :                             std::string osCandidate(osPath);
     336           4 :                             osCandidate += psEntry->pszName;
     337           4 :                             nPosStart = osCandidate.size();
     338           4 :                             if (*pszPattern)
     339             :                             {
     340           4 :                                 osCandidate += pszPattern;
     341             :                             }
     342           4 :                             candidates.emplace_back(std::move(osCandidate),
     343           4 :                                                     nPosStart);
     344             :                         }
     345           4 :                     }
     346           1 :                     osPath.clear();
     347           1 :                     break;
     348             :                 }
     349         103 :                 else if (osCurPath.find_first_of("*?[") != std::string::npos)
     350             :                 {
     351             :                     std::unique_ptr<VSIDIR, VSIDirCloser> psDir(
     352          14 :                         VSIOpenDir(osPath.c_str(), 0, nullptr));
     353          14 :                     if (!psDir)
     354           0 :                         return nullptr;
     355             :                     while (const VSIDIREntry *psEntry =
     356         444 :                                VSIGetNextDirEntry(psDir.get()))
     357             :                     {
     358         430 :                         if (pProgressFunc &&
     359           0 :                             !pProgressFunc(0, "", pProgressData))
     360             :                         {
     361           0 :                             return nullptr;
     362             :                         }
     363         430 :                         if (VSIFnMatch(osCurPath.c_str(), psEntry->pszName))
     364             :                         {
     365          20 :                             std::string osCandidate(osPath);
     366          10 :                             osCandidate += psEntry->pszName;
     367          10 :                             nPosStart = osCandidate.size();
     368          10 :                             if (*pszPattern)
     369             :                             {
     370           2 :                                 osCandidate += pszPattern;
     371             :                             }
     372          10 :                             candidates.emplace_back(std::move(osCandidate),
     373          10 :                                                     nPosStart);
     374             :                         }
     375         430 :                     }
     376          14 :                     osPath.clear();
     377          14 :                     break;
     378             :                 }
     379          89 :                 else if (*pszPattern == 0)
     380             :                 {
     381          16 :                     osPath += osCurPath;
     382          16 :                     break;
     383             :                 }
     384             :                 else
     385             :                 {
     386          73 :                     osPath += osCurPath;
     387          73 :                     osPath += *pszPattern;
     388          73 :                     osCurPath.clear();
     389          73 :                 }
     390             :             }
     391             :             else
     392             :             {
     393         442 :                 osCurPath += *pszPattern;
     394             :             }
     395         515 :         }
     396          31 :         if (!osPath.empty())
     397             :         {
     398             :             VSIStatBufL sStat;
     399          16 :             if (VSIStatL(osPath.c_str(), &sStat) == 0)
     400          11 :                 aosRes.AddString(osPath.c_str());
     401             :         }
     402             :     }
     403             : 
     404          17 :     return aosRes.StealList();
     405             : }
     406             : 
     407             : /************************************************************************/
     408             : /*                      VSIGetDirectorySeparator()                      */
     409             : /************************************************************************/
     410             : 
     411             : /** Return the directory separator for the specified path.
     412             :  *
     413             :  * Default is forward slash. The only exception currently is the Windows
     414             :  * file system which returns backslash, unless the specified path is of the
     415             :  * form "{drive_letter}:/{rest_of_the_path}".
     416             :  *
     417             :  * @since 3.9
     418             :  */
     419      878300 : const char *VSIGetDirectorySeparator(const char *pszPath)
     420             : {
     421      878300 :     if (STARTS_WITH(pszPath, "http://") || STARTS_WITH(pszPath, "https://"))
     422         391 :         return "/";
     423             : 
     424      877909 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
     425      877946 :     return poFSHandler->GetDirectorySeparator(pszPath);
     426             : }
     427             : 
     428             : /************************************************************************/
     429             : /*                             VSIReadRecursive()                       */
     430             : /************************************************************************/
     431             : 
     432             : /**
     433             :  * \brief Read names in a directory recursively.
     434             :  *
     435             :  * This function abstracts access to directory contents and subdirectories.
     436             :  * It returns a list of strings containing the names of files and directories
     437             :  * in this directory and all subdirectories.  The resulting string list becomes
     438             :  * the responsibility of the application and should be freed with CSLDestroy()
     439             :  * when no longer needed.
     440             :  *
     441             :  * Note that no error is issued via CPLError() if the directory path is
     442             :  * invalid, though NULL is returned.
     443             :  *
     444             :  * Note: since GDAL 3.9, for recursive mode, the directory separator will no
     445             :  * longer be always forward slash, but will be the one returned by
     446             :  * VSIGetDirectorySeparator(pszPathIn), so potentially backslash on Windows
     447             :  * file systems.
     448             :  *
     449             :  * @param pszPathIn the relative, or absolute path of a directory to read.
     450             :  * UTF-8 encoded.
     451             :  *
     452             :  * @return The list of entries in the directory and subdirectories
     453             :  * or NULL if the directory doesn't exist.  Filenames are returned in UTF-8
     454             :  * encoding.
     455             :  * @since GDAL 1.10.0
     456             :  *
     457             :  */
     458             : 
     459        1246 : char **VSIReadDirRecursive(const char *pszPathIn)
     460             : {
     461        1246 :     const char SEP = VSIGetDirectorySeparator(pszPathIn)[0];
     462             : 
     463        1246 :     const char *const apszOptions[] = {"NAME_AND_TYPE_ONLY=YES", nullptr};
     464        1246 :     VSIDIR *psDir = VSIOpenDir(pszPathIn, -1, apszOptions);
     465        1246 :     if (!psDir)
     466           3 :         return nullptr;
     467        2486 :     CPLStringList oFiles;
     468        3845 :     while (auto psEntry = VSIGetNextDirEntry(psDir))
     469             :     {
     470        2602 :         if (VSI_ISDIR(psEntry->nMode) && psEntry->pszName[0] &&
     471        1131 :             psEntry->pszName[strlen(psEntry->pszName) - 1] != SEP)
     472             :         {
     473        1131 :             oFiles.AddString((std::string(psEntry->pszName) + SEP).c_str());
     474             :         }
     475             :         else
     476             :         {
     477        1471 :             oFiles.AddString(psEntry->pszName);
     478             :         }
     479        2602 :     }
     480        1243 :     VSICloseDir(psDir);
     481             : 
     482        1243 :     return oFiles.StealList();
     483             : }
     484             : 
     485             : /************************************************************************/
     486             : /*                             CPLReadDir()                             */
     487             : /*                                                                      */
     488             : /*      This is present only to provide ABI compatibility with older    */
     489             : /*      versions.                                                       */
     490             : /************************************************************************/
     491             : #undef CPLReadDir
     492             : 
     493             : CPL_C_START
     494             : char CPL_DLL **CPLReadDir(const char *pszPath);
     495             : CPL_C_END
     496             : 
     497           0 : char **CPLReadDir(const char *pszPath)
     498             : {
     499           0 :     return VSIReadDir(pszPath);
     500             : }
     501             : 
     502             : /************************************************************************/
     503             : /*                             VSIOpenDir()                             */
     504             : /************************************************************************/
     505             : 
     506             : /**
     507             :  * \brief Open a directory to read its entries.
     508             :  *
     509             :  * This function is close to the POSIX opendir() function.
     510             :  *
     511             :  * For /vsis3/, /vsigs/, /vsioss/, /vsiaz/ and /vsiadls/, this function has an
     512             :  * efficient implementation, minimizing the number of network requests, when
     513             :  * invoked with nRecurseDepth <= 0.
     514             :  *
     515             :  * Entries are read by calling VSIGetNextDirEntry() on the handled returned by
     516             :  * that function, until it returns NULL. VSICloseDir() must be called once done
     517             :  * with the returned directory handle.
     518             :  *
     519             :  * @param pszPath the relative, or absolute path of a directory to read.
     520             :  * UTF-8 encoded.
     521             :  * @param nRecurseDepth 0 means do not recurse in subdirectories, 1 means
     522             :  * recurse only in the first level of subdirectories, etc. -1 means unlimited
     523             :  * recursion level
     524             :  * @param papszOptions NULL terminated list of options, or NULL. The following
     525             :  * options are implemented:
     526             :  * <ul>
     527             :  * <li>PREFIX=string: (GDAL >= 3.4) Filter to select filenames only starting
     528             :  *     with the specified prefix. Implemented efficiently for /vsis3/, /vsigs/,
     529             :  *     and /vsiaz/ (but not /vsiadls/)
     530             :  * </li>
     531             :  * <li>NAME_AND_TYPE_ONLY=YES/NO: (GDAL >= 3.4) Defaults to NO. If set to YES,
     532             :  *     only the pszName and nMode members of VSIDIR are guaranteed to be set.
     533             :  *     This is implemented efficiently for the Unix virtual file system.
     534             :  * </li>
     535             :  * </ul>
     536             :  *
     537             :  * @return a handle, or NULL in case of error
     538             :  * @since GDAL 2.4
     539             :  *
     540             :  */
     541             : 
     542        1395 : VSIDIR *VSIOpenDir(const char *pszPath, int nRecurseDepth,
     543             :                    const char *const *papszOptions)
     544             : {
     545        1395 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
     546             : 
     547        1395 :     return poFSHandler->OpenDir(pszPath, nRecurseDepth, papszOptions);
     548             : }
     549             : 
     550             : /************************************************************************/
     551             : /*                          VSIGetNextDirEntry()                        */
     552             : /************************************************************************/
     553             : 
     554             : /**
     555             :  * \brief Return the next entry of the directory
     556             :  *
     557             :  * This function is close to the POSIX readdir() function. It actually returns
     558             :  * more information (file size, last modification time), which on 'real' file
     559             :  * systems involve one 'stat' call per file.
     560             :  *
     561             :  * For filesystems that can have both a regular file and a directory name of
     562             :  * the same name (typically /vsis3/), when this situation of duplicate happens,
     563             :  * the directory name will be suffixed by a slash character. Otherwise directory
     564             :  * names are not suffixed by slash.
     565             :  *
     566             :  * The returned entry remains valid until the next call to VSINextDirEntry()
     567             :  * or VSICloseDir() with the same handle.
     568             :  *
     569             :  * Note: since GDAL 3.9, for recursive mode, the directory separator will no
     570             :  * longer be always forward slash, but will be the one returned by
     571             :  * VSIGetDirectorySeparator(pszPathIn), so potentially backslash on Windows
     572             :  * file systems.
     573             :  *
     574             :  * @param dir Directory handled returned by VSIOpenDir(). Must not be NULL.
     575             :  *
     576             :  * @return a entry, or NULL if there is no more entry in the directory. This
     577             :  * return value must not be freed.
     578             :  * @since GDAL 2.4
     579             :  *
     580             :  */
     581             : 
     582        4891 : const VSIDIREntry *VSIGetNextDirEntry(VSIDIR *dir)
     583             : {
     584        4891 :     return dir->NextDirEntry();
     585             : }
     586             : 
     587             : /************************************************************************/
     588             : /*                             VSICloseDir()                            */
     589             : /************************************************************************/
     590             : 
     591             : /**
     592             :  * \brief Close a directory
     593             :  *
     594             :  * This function is close to the POSIX closedir() function.
     595             :  *
     596             :  * @param dir Directory handled returned by VSIOpenDir().
     597             :  *
     598             :  * @since GDAL 2.4
     599             :  */
     600             : 
     601        1368 : void VSICloseDir(VSIDIR *dir)
     602             : {
     603        1368 :     delete dir;
     604        1368 : }
     605             : 
     606             : /************************************************************************/
     607             : /*                              VSIMkdir()                              */
     608             : /************************************************************************/
     609             : 
     610             : /**
     611             :  * \brief Create a directory.
     612             :  *
     613             :  * Create a new directory with the indicated mode. For POSIX-style systems,
     614             :  * the mode is modified by the file creation mask (umask). However, some
     615             :  * file systems and platforms may not use umask, or they may ignore the mode
     616             :  * completely. So a reasonable cross-platform default mode value is 0755.
     617             :  *
     618             :  * Analog of the POSIX mkdir() function.
     619             :  *
     620             :  * @param pszPathname the path to the directory to create. UTF-8 encoded.
     621             :  * @param mode the permissions mode.
     622             :  *
     623             :  * @return 0 on success or -1 on an error.
     624             :  */
     625             : 
     626       93647 : int VSIMkdir(const char *pszPathname, long mode)
     627             : 
     628             : {
     629       93647 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPathname);
     630             : 
     631       93647 :     return poFSHandler->Mkdir(pszPathname, mode);
     632             : }
     633             : 
     634             : /************************************************************************/
     635             : /*                       VSIMkdirRecursive()                            */
     636             : /************************************************************************/
     637             : 
     638             : /**
     639             :  * \brief Create a directory and all its ancestors
     640             :  *
     641             :  * @param pszPathname the path to the directory to create. UTF-8 encoded.
     642             :  * @param mode the permissions mode.
     643             :  *
     644             :  * @return 0 on success or -1 on an error.
     645             :  * @since GDAL 2.3
     646             :  */
     647             : 
     648      133871 : int VSIMkdirRecursive(const char *pszPathname, long mode)
     649             : {
     650      133871 :     if (pszPathname == nullptr || pszPathname[0] == '\0' ||
     651      133871 :         strncmp("/", pszPathname, 2) == 0)
     652             :     {
     653           1 :         return -1;
     654             :     }
     655             : 
     656      267741 :     const CPLString osPathname(pszPathname);
     657             :     VSIStatBufL sStat;
     658      133871 :     if (VSIStatL(osPathname, &sStat) == 0)
     659             :     {
     660       44606 :         return VSI_ISDIR(sStat.st_mode) ? 0 : -1;
     661             :     }
     662      178530 :     const std::string osParentPath(CPLGetPathSafe(osPathname));
     663             : 
     664             :     // Prevent crazy paths from recursing forever.
     665      178529 :     if (osParentPath == osPathname ||
     666       89264 :         osParentPath.length() >= osPathname.length())
     667             :     {
     668           0 :         return -1;
     669             :     }
     670             : 
     671       89265 :     if (VSIStatL(osParentPath.c_str(), &sStat) != 0)
     672             :     {
     673       44742 :         if (VSIMkdirRecursive(osParentPath.c_str(), mode) != 0)
     674           1 :             return -1;
     675             :     }
     676             : 
     677       89264 :     return VSIMkdir(osPathname, mode);
     678             : }
     679             : 
     680             : /************************************************************************/
     681             : /*                             VSIUnlink()                              */
     682             : /************************************************************************/
     683             : 
     684             : /**
     685             :  * \brief Delete a file.
     686             :  *
     687             :  * Deletes a file object from the file system.
     688             :  *
     689             :  * This method goes through the VSIFileHandler virtualization and may
     690             :  * work on unusual filesystems such as in memory.
     691             :  *
     692             :  * Analog of the POSIX unlink() function.
     693             :  *
     694             :  * @param pszFilename the path of the file to be deleted. UTF-8 encoded.
     695             :  *
     696             :  * @return 0 on success or -1 on an error.
     697             :  */
     698             : 
     699      104877 : int VSIUnlink(const char *pszFilename)
     700             : 
     701             : {
     702      104877 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
     703             : 
     704      104877 :     return poFSHandler->Unlink(pszFilename);
     705             : }
     706             : 
     707             : /************************************************************************/
     708             : /*                           VSIUnlinkBatch()                           */
     709             : /************************************************************************/
     710             : 
     711             : /**
     712             :  * \brief Delete several files, possibly in a batch.
     713             :  *
     714             :  * All files should belong to the same file system handler.
     715             :  *
     716             :  * This is implemented efficiently for /vsis3/ and /vsigs/ (provided for /vsigs/
     717             :  * that OAuth2 authentication is used).
     718             :  *
     719             :  * @param papszFiles NULL terminated list of files. UTF-8 encoded.
     720             :  *
     721             :  * @return an array of size CSLCount(papszFiles), whose values are TRUE or FALSE
     722             :  * depending on the success of deletion of the corresponding file. The array
     723             :  * should be freed with VSIFree().
     724             :  * NULL might be return in case of a more general error (for example,
     725             :  * files belonging to different file system handlers)
     726             :  *
     727             :  * @since GDAL 3.1
     728             :  */
     729             : 
     730          10 : int *VSIUnlinkBatch(CSLConstList papszFiles)
     731             : {
     732          10 :     VSIFilesystemHandler *poFSHandler = nullptr;
     733          28 :     for (CSLConstList papszIter = papszFiles; papszIter && *papszIter;
     734             :          ++papszIter)
     735             :     {
     736          19 :         auto poFSHandlerThisFile = VSIFileManager::GetHandler(*papszIter);
     737          19 :         if (poFSHandler == nullptr)
     738           9 :             poFSHandler = poFSHandlerThisFile;
     739          10 :         else if (poFSHandler != poFSHandlerThisFile)
     740             :         {
     741           1 :             CPLError(CE_Failure, CPLE_AppDefined,
     742             :                      "Files belong to different file system handlers");
     743           1 :             poFSHandler = nullptr;
     744           1 :             break;
     745             :         }
     746             :     }
     747          10 :     if (poFSHandler == nullptr)
     748           2 :         return nullptr;
     749           8 :     return poFSHandler->UnlinkBatch(papszFiles);
     750             : }
     751             : 
     752             : /************************************************************************/
     753             : /*                             VSIRename()                              */
     754             : /************************************************************************/
     755             : 
     756             : /**
     757             :  * \brief Rename a file.
     758             :  *
     759             :  * Renames a file object in the file system.  It should be possible
     760             :  * to rename a file onto a new filesystem, but it is safest if this
     761             :  * function is only used to rename files that remain in the same directory.
     762             :  *
     763             :  * This method goes through the VSIFileHandler virtualization and may
     764             :  * work on unusual filesystems such as in memory.
     765             :  *
     766             :  * Analog of the POSIX rename() function.
     767             :  *
     768             :  * @param oldpath the name of the file to be renamed.  UTF-8 encoded.
     769             :  * @param newpath the name the file should be given.  UTF-8 encoded.
     770             :  *
     771             :  * @return 0 on success or -1 on an error.
     772             :  */
     773             : 
     774         674 : int VSIRename(const char *oldpath, const char *newpath)
     775             : 
     776             : {
     777         674 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(oldpath);
     778             : 
     779         674 :     return poFSHandler->Rename(oldpath, newpath);
     780             : }
     781             : 
     782             : /************************************************************************/
     783             : /*                             VSICopyFile()                            */
     784             : /************************************************************************/
     785             : 
     786             : /**
     787             :  * \brief Copy a source file into a target file.
     788             :  *
     789             :  * For a /vsizip/foo.zip/bar target, the options available are those of
     790             :  * CPLAddFileInZip()
     791             :  *
     792             :  * The following copies are made fully on the target server, without local
     793             :  * download from source and upload to target:
     794             :  * - /vsis3/ -> /vsis3/
     795             :  * - /vsigs/ -> /vsigs/
     796             :  * - /vsiaz/ -> /vsiaz/
     797             :  * - /vsiadls/ -> /vsiadls/
     798             :  * - any of the above or /vsicurl/ -> /vsiaz/ (starting with GDAL 3.8)
     799             :  *
     800             :  * @param pszSource Source filename. UTF-8 encoded. May be NULL if fpSource is
     801             :  * not NULL.
     802             :  * @param pszTarget Target filename.  UTF-8 encoded. Must not be NULL
     803             :  * @param fpSource File handle on the source file. May be NULL if pszSource is
     804             :  * not NULL.
     805             :  * @param nSourceSize Size of the source file. Pass -1 if unknown.
     806             :  * If set to -1, and progress callback is used, VSIStatL() will be used on
     807             :  * pszSource to retrieve the source size.
     808             :  * @param papszOptions Null terminated list of options, or NULL.
     809             :  * @param pProgressFunc Progress callback, or NULL.
     810             :  * @param pProgressData User data of progress callback, or NULL.
     811             :  *
     812             :  * @return 0 on success.
     813             :  * @since GDAL 3.7
     814             :  */
     815             : 
     816        2230 : int VSICopyFile(const char *pszSource, const char *pszTarget,
     817             :                 VSILFILE *fpSource, vsi_l_offset nSourceSize,
     818             :                 const char *const *papszOptions, GDALProgressFunc pProgressFunc,
     819             :                 void *pProgressData)
     820             : 
     821             : {
     822        2230 :     if (!pszSource && !fpSource)
     823             :     {
     824           1 :         CPLError(CE_Failure, CPLE_AppDefined,
     825             :                  "pszSource == nullptr && fpSource == nullptr");
     826           1 :         return -1;
     827             :     }
     828        2229 :     if (!pszTarget || pszTarget[0] == '\0')
     829             :     {
     830           0 :         return -1;
     831             :     }
     832             : 
     833             :     VSIFilesystemHandler *poFSHandlerTarget =
     834        2229 :         VSIFileManager::GetHandler(pszTarget);
     835        2229 :     return poFSHandlerTarget->CopyFile(pszSource, pszTarget, fpSource,
     836             :                                        nSourceSize, papszOptions, pProgressFunc,
     837        2229 :                                        pProgressData);
     838             : }
     839             : 
     840             : /************************************************************************/
     841             : /*                       VSICopyFileRestartable()                       */
     842             : /************************************************************************/
     843             : 
     844             : /**
     845             :  \brief Copy a source file into a target file in a way that can (potentially)
     846             :  be restarted.
     847             : 
     848             :  This function provides the possibility of efficiently restarting upload of
     849             :  large files to cloud storage that implements upload in a chunked way,
     850             :  such as /vsis3/ and /vsigs/.
     851             :  For other destination file systems, this function may fallback to
     852             :  VSICopyFile() and not provide any smart restartable implementation.
     853             : 
     854             :  Example of a potential workflow:
     855             : 
     856             :  @code{.cpp}
     857             :  char* pszOutputPayload = NULL;
     858             :  int ret = VSICopyFileRestartable(pszSource, pszTarget, NULL,
     859             :                                   &pszOutputPayload, NULL, NULL, NULL);
     860             :  while( ret == 1 ) // add also a limiting counter to avoid potentiall endless looping
     861             :  {
     862             :      // TODO: wait for some time
     863             : 
     864             :      char* pszOutputPayloadNew = NULL;
     865             :      const char* pszInputPayload = pszOutputPayload;
     866             :      ret = VSICopyFileRestartable(pszSource, pszTarget, pszInputPayload,
     867             :                                   &pszOutputPayloadNew, NULL, NULL, NULL);
     868             :      VSIFree(pszOutputPayload);
     869             :      pszOutputPayload = pszOutputPayloadNew;
     870             :  }
     871             :  VSIFree(pszOutputPayload);
     872             :  @endcode
     873             : 
     874             :  @param pszSource Source filename. UTF-8 encoded. Must not be NULL
     875             :  @param pszTarget Target filename. UTF-8 encoded. Must not be NULL
     876             :  @param pszInputPayload NULL at the first invocation. When doing a retry,
     877             :                         should be the content of *ppszOutputPayload from a
     878             :                         previous invocation.
     879             :  @param[out] ppszOutputPayload Pointer to an output string that will be set to
     880             :                                a value that can be provided as pszInputPayload
     881             :                                for a next call to VSICopyFileRestartable().
     882             :                                ppszOutputPayload must not be NULL.
     883             :                                The string set in *ppszOutputPayload, if not NULL,
     884             :                                is JSON-encoded, and can be re-used in another
     885             :                                process instance. It must be freed with VSIFree()
     886             :                                when no longer needed.
     887             :  @param papszOptions Null terminated list of options, or NULL.
     888             :  Currently accepted options are:
     889             :  <ul>
     890             :  <li>NUM_THREADS=integer or ALL_CPUS. Number of threads to use for parallel
     891             :  file copying. Only use for when /vsis3/, /vsigs/, /vsiaz/ or /vsiadls/ is in
     892             :  source or target. The default is 10.
     893             :  </li>
     894             :  <li>CHUNK_SIZE=integer. Maximum size of chunk (in bytes) to use
     895             :  to split large objects. For upload to /vsis3/, this chunk size must be set at
     896             :  least to 5 MB. The default is 50 MB.
     897             :  </li>
     898             :  </ul>
     899             :  @param pProgressFunc Progress callback, or NULL.
     900             :  @param pProgressData User data of progress callback, or NULL.
     901             :  @return 0 on success,
     902             :          -1 on (non-restartable) failure,
     903             :          1 if VSICopyFileRestartable() can be called again in a restartable way
     904             :  @since GDAL 3.10
     905             : 
     906             :  @see VSIAbortPendingUploads()
     907             : */
     908             : 
     909          20 : int VSICopyFileRestartable(const char *pszSource, const char *pszTarget,
     910             :                            const char *pszInputPayload,
     911             :                            char **ppszOutputPayload,
     912             :                            const char *const *papszOptions,
     913             :                            GDALProgressFunc pProgressFunc, void *pProgressData)
     914             : 
     915             : {
     916          20 :     if (!pszSource)
     917             :     {
     918           0 :         return -1;
     919             :     }
     920          20 :     if (!pszTarget || pszTarget[0] == '\0')
     921             :     {
     922           0 :         return -1;
     923             :     }
     924          20 :     if (!ppszOutputPayload)
     925             :     {
     926           0 :         return -1;
     927             :     }
     928             : 
     929             :     VSIFilesystemHandler *poFSHandlerTarget =
     930          20 :         VSIFileManager::GetHandler(pszTarget);
     931          20 :     return poFSHandlerTarget->CopyFileRestartable(
     932             :         pszSource, pszTarget, pszInputPayload, ppszOutputPayload, papszOptions,
     933          20 :         pProgressFunc, pProgressData);
     934             : }
     935             : 
     936             : /************************************************************************/
     937             : /*                             VSISync()                                */
     938             : /************************************************************************/
     939             : 
     940             : /**
     941             :  * \brief Synchronize a source file/directory with a target file/directory.
     942             :  *
     943             :  * This is a analog of the 'rsync' utility. In the current implementation,
     944             :  * rsync would be more efficient for local file copying, but VSISync() main
     945             :  * interest is when the source or target is a remote
     946             :  * file system like /vsis3/ or /vsigs/, in which case it can take into account
     947             :  * the timestamps of the files (or optionally the ETag/MD5Sum) to avoid
     948             :  * unneeded copy operations.
     949             :  *
     950             :  * This is only implemented efficiently for:
     951             :  * <ul>
     952             :  * <li> local filesystem <--> remote filesystem.</li>
     953             :  * <li> remote filesystem <--> remote filesystem (starting with GDAL 3.1).
     954             :  * Where the source and target remote filesystems are the same and one of
     955             :  * /vsis3/, /vsigs/ or /vsiaz/. Or when the target is /vsiaz/ and the source
     956             :  * is /vsis3/, /vsigs/, /vsiadls/ or /vsicurl/ (starting with GDAL 3.8)</li>
     957             :  * </ul>
     958             :  *
     959             :  * Similarly to rsync behavior, if the source filename ends with a slash,
     960             :  * it means that the content of the directory must be copied, but not the
     961             :  * directory name. For example, assuming "/home/even/foo" contains a file "bar",
     962             :  * VSISync("/home/even/foo/", "/mnt/media", ...) will create a "/mnt/media/bar"
     963             :  * file. Whereas VSISync("/home/even/foo", "/mnt/media", ...) will create a
     964             :  * "/mnt/media/foo" directory which contains a bar file.
     965             :  *
     966             :  * @param pszSource Source file or directory.  UTF-8 encoded.
     967             :  * @param pszTarget Target file or directory.  UTF-8 encoded.
     968             :  * @param papszOptions Null terminated list of options, or NULL.
     969             :  * Currently accepted options are:
     970             :  * <ul>
     971             :  * <li>RECURSIVE=NO (the default is YES)</li>
     972             :  * <li>SYNC_STRATEGY=TIMESTAMP/ETAG/OVERWRITE.
     973             :  *
     974             :  *     Determines which criterion is used to determine if a target file must be
     975             :  *     replaced when it already exists and has the same file size as the source.
     976             :  *     Only applies for a source or target being a network filesystem.
     977             :  *
     978             :  *     The default is TIMESTAMP (similarly to how 'aws s3 sync' works), that is
     979             :  *     to say that for an upload operation, a remote file is
     980             :  *     replaced if it has a different size or if it is older than the source.
     981             :  *     For a download operation, a local file is  replaced if it has a different
     982             :  *     size or if it is newer than the remote file.
     983             :  *
     984             :  *     The ETAG strategy assumes that the ETag metadata of the remote file is
     985             :  *     the MD5Sum of the file content, which is only true in the case of /vsis3/
     986             :  *     for files not using KMS server side encryption and uploaded in a single
     987             :  *     PUT operation (so smaller than 50 MB given the default used by GDAL).
     988             :  *     Only to be used for /vsis3/, /vsigs/ or other filesystems using a
     989             :  *     MD5Sum as ETAG.
     990             :  *
     991             :  *     The OVERWRITE strategy (GDAL >= 3.2) will always overwrite the target
     992             :  *     file with the source one.
     993             :  * </li>
     994             :  * <li>NUM_THREADS=integer. (GDAL >= 3.1) Number of threads to use for parallel
     995             :  * file copying. Only use for when /vsis3/, /vsigs/, /vsiaz/ or /vsiadls/ is in
     996             :  * source or target. The default is 10 since GDAL 3.3</li>
     997             :  * <li>CHUNK_SIZE=integer. (GDAL >= 3.1) Maximum size of chunk (in bytes) to use
     998             :  * to split large objects when downloading them from /vsis3/, /vsigs/, /vsiaz/
     999             :  * or /vsiadls/ to local file system, or for upload to /vsis3/, /vsiaz/ or
    1000             :  * /vsiadls/ from local file system. Only used if NUM_THREADS > 1. For upload to
    1001             :  * /vsis3/, this chunk size must be set at least to 5 MB. The default is 8 MB
    1002             :  * since GDAL 3.3</li> <li>x-amz-KEY=value. (GDAL >= 3.5) MIME header to pass
    1003             :  * during creation of a /vsis3/ object.</li> <li>x-goog-KEY=value. (GDAL >= 3.5)
    1004             :  * MIME header to pass during creation of a /vsigs/ object.</li>
    1005             :  * <li>x-ms-KEY=value. (GDAL >= 3.5) MIME header to pass during creation of a
    1006             :  * /vsiaz/ or /vsiadls/ object.</li>
    1007             :  * </ul>
    1008             :  * @param pProgressFunc Progress callback, or NULL.
    1009             :  * @param pProgressData User data of progress callback, or NULL.
    1010             :  * @param ppapszOutputs Unused. Should be set to NULL for now.
    1011             :  *
    1012             :  * @return TRUE on success or FALSE on an error.
    1013             :  * @since GDAL 2.4
    1014             :  */
    1015             : 
    1016          45 : int VSISync(const char *pszSource, const char *pszTarget,
    1017             :             const char *const *papszOptions, GDALProgressFunc pProgressFunc,
    1018             :             void *pProgressData, char ***ppapszOutputs)
    1019             : 
    1020             : {
    1021          45 :     if (pszSource[0] == '\0' || pszTarget[0] == '\0')
    1022             :     {
    1023           0 :         return FALSE;
    1024             :     }
    1025             : 
    1026             :     VSIFilesystemHandler *poFSHandlerSource =
    1027          45 :         VSIFileManager::GetHandler(pszSource);
    1028             :     VSIFilesystemHandler *poFSHandlerTarget =
    1029          45 :         VSIFileManager::GetHandler(pszTarget);
    1030          45 :     VSIFilesystemHandler *poFSHandlerLocal = VSIFileManager::GetHandler("");
    1031             :     VSIFilesystemHandler *poFSHandlerMem =
    1032          45 :         VSIFileManager::GetHandler("/vsimem/");
    1033          45 :     VSIFilesystemHandler *poFSHandler = poFSHandlerSource;
    1034          45 :     if (poFSHandlerTarget != poFSHandlerLocal &&
    1035             :         poFSHandlerTarget != poFSHandlerMem)
    1036             :     {
    1037          22 :         poFSHandler = poFSHandlerTarget;
    1038             :     }
    1039             : 
    1040          90 :     return poFSHandler->Sync(pszSource, pszTarget, papszOptions, pProgressFunc,
    1041          45 :                              pProgressData, ppapszOutputs)
    1042          45 :                ? TRUE
    1043          45 :                : FALSE;
    1044             : }
    1045             : 
    1046             : /************************************************************************/
    1047             : /*                    VSIMultipartUploadGetCapabilities()               */
    1048             : /************************************************************************/
    1049             : 
    1050             : /**
    1051             :  * \brief Return capabilities for multiple part file upload.
    1052             :  *
    1053             :  * @param pszFilename Filename, or virtual file system prefix, onto which
    1054             :  * capabilities should apply.
    1055             :  * @param[out] pbNonSequentialUploadSupported If not null,
    1056             :  * the pointed value is set if parts can be uploaded in a non-sequential way.
    1057             :  * @param[out] pbParallelUploadSupported If not null,
    1058             :  * the pointed value is set if parts can be uploaded in a parallel way.
    1059             :  * (implies *pbNonSequentialUploadSupported = true)
    1060             :  * @param[out] pbAbortSupported If not null,
    1061             :  * the pointed value is set if VSIMultipartUploadAbort() is implemented.
    1062             :  * @param[out] pnMinPartSize If not null, the pointed value is set to the minimum
    1063             :  * size of parts (but the last one), in MiB.
    1064             :  * @param[out] pnMaxPartSize If not null, the pointed value is set to the maximum
    1065             :  * size of parts, in MiB.
    1066             :  * @param[out] pnMaxPartCount  If not null, the pointed value is set to the
    1067             :  * maximum number of parts that can be uploaded.
    1068             :  *
    1069             :  * @return TRUE in case of success, FALSE otherwise.
    1070             :  *
    1071             :  * @since 3.10
    1072             :  */
    1073           7 : int VSIMultipartUploadGetCapabilities(
    1074             :     const char *pszFilename, int *pbNonSequentialUploadSupported,
    1075             :     int *pbParallelUploadSupported, int *pbAbortSupported,
    1076             :     size_t *pnMinPartSize, size_t *pnMaxPartSize, int *pnMaxPartCount)
    1077             : {
    1078           7 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1079             : 
    1080          14 :     return poFSHandler->MultipartUploadGetCapabilities(
    1081             :         pbNonSequentialUploadSupported, pbParallelUploadSupported,
    1082           7 :         pbAbortSupported, pnMinPartSize, pnMaxPartSize, pnMaxPartCount);
    1083             : }
    1084             : 
    1085             : /************************************************************************/
    1086             : /*                     VSIMultipartUploadStart()                        */
    1087             : /************************************************************************/
    1088             : 
    1089             : /**
    1090             :  * \brief Initiates the upload a (big) file in a piece-wise way.
    1091             :  *
    1092             :  * Using this API directly is generally not needed, but in very advanced cases,
    1093             :  * as VSIFOpenL(..., "wb") + VSIFWriteL(), VSISync(), VSICopyFile() or
    1094             :  * VSICopyFileRestartable() may be able to leverage it when needed.
    1095             :  *
    1096             :  * This is only implemented for the /vsis3/, /vsigs/, /vsiaz/, /vsiadls/ and
    1097             :  * /vsioss/ virtual file systems.
    1098             :  *
    1099             :  * The typical workflow is to do :
    1100             :  * - VSIMultipartUploadStart()
    1101             :  * - VSIMultipartUploadAddPart(): several times
    1102             :  * - VSIMultipartUploadEnd()
    1103             :  *
    1104             :  * If VSIMultipartUploadAbort() is supported by the filesystem (VSIMultipartUploadGetCapabilities()
    1105             :  * can be used to determine it), this function should be called to cancel an
    1106             :  * upload. This can be needed to avoid extra billing for some cloud storage
    1107             :  * providers.
    1108             :  *
    1109             :  * The following options are supported:
    1110             :  * <ul>
    1111             :  * <li>MIME headers such as Content-Type and Content-Encoding
    1112             :  * are supported for the /vsis3/, /vsigs/, /vsiaz/, /vsiadls/ file systems.</li>
    1113             :  * </ul>
    1114             :  *
    1115             :  * @param pszFilename Filename to create
    1116             :  * @param papszOptions NULL or null-terminated list of options.
    1117             :  * @return an upload ID to pass to other VSIMultipartUploadXXXXX() functions,
    1118             :  * and to free with CPLFree() once done, or nullptr in case of error.
    1119             :  *
    1120             :  * @since 3.10
    1121             :  */
    1122           4 : char *VSIMultipartUploadStart(const char *pszFilename,
    1123             :                               CSLConstList papszOptions)
    1124             : {
    1125           4 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1126             : 
    1127           4 :     return poFSHandler->MultipartUploadStart(pszFilename, papszOptions);
    1128             : }
    1129             : 
    1130             : /************************************************************************/
    1131             : /*                     VSIMultipartUploadAddPart()                      */
    1132             : /************************************************************************/
    1133             : 
    1134             : /**
    1135             :  * \brief Uploads a new part to a multi-part uploaded file.
    1136             :  *
    1137             :  * Cf VSIMultipartUploadStart().
    1138             :  *
    1139             :  * VSIMultipartUploadGetCapabilities() returns hints on the constraints that
    1140             :  * apply to the upload, in terms of minimum/maximum size of each part, maximum
    1141             :  * number of parts, and whether non-sequential or parallel uploads are
    1142             :  * supported.
    1143             :  *
    1144             :  * @param pszFilename Filename to which to append the new part. Should be the
    1145             :  *                    same as the one used for VSIMultipartUploadStart()
    1146             :  * @param pszUploadId Value returned by VSIMultipartUploadStart()
    1147             :  * @param nPartNumber Part number, starting at 1.
    1148             :  * @param nFileOffset Offset within the file at which (starts at 0) the passed
    1149             :  *                    data starts.
    1150             :  * @param pData       Pointer to an array of nDataLength bytes.
    1151             :  * @param nDataLength Size in bytes of pData.
    1152             :  * @param papszOptions Unused. Should be nullptr.
    1153             :  *
    1154             :  * @return a part identifier that must be passed into the apszPartIds[] array of
    1155             :  * VSIMultipartUploadEnd(), and to free with CPLFree() once done, or nullptr in
    1156             :  * case of error.
    1157             :  *
    1158             :  * @since 3.10
    1159             :  */
    1160           5 : char *VSIMultipartUploadAddPart(const char *pszFilename,
    1161             :                                 const char *pszUploadId, int nPartNumber,
    1162             :                                 vsi_l_offset nFileOffset, const void *pData,
    1163             :                                 size_t nDataLength, CSLConstList papszOptions)
    1164             : {
    1165           5 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1166             : 
    1167           5 :     return poFSHandler->MultipartUploadAddPart(pszFilename, pszUploadId,
    1168             :                                                nPartNumber, nFileOffset, pData,
    1169           5 :                                                nDataLength, papszOptions);
    1170             : }
    1171             : 
    1172             : /************************************************************************/
    1173             : /*                       VSIMultipartUploadEnd()                        */
    1174             : /************************************************************************/
    1175             : 
    1176             : /**
    1177             :  * \brief Completes a multi-part file upload.
    1178             :  *
    1179             :  * Cf VSIMultipartUploadStart().
    1180             :  *
    1181             :  * @param pszFilename Filename for which multipart upload should be completed.
    1182             :  *                    Should be the same as the one used for
    1183             :  *                    VSIMultipartUploadStart()
    1184             :  * @param pszUploadId Value returned by VSIMultipartUploadStart()
    1185             :  * @param nPartIdsCount Number of parts,  andsize of apszPartIds
    1186             :  * @param apszPartIds Array of part identifiers (as returned by
    1187             :  *                    VSIMultipartUploadAddPart()), that must be ordered in
    1188             :  *                    the sequential order of parts, and of size nPartIdsCount.
    1189             :  * @param nTotalSize  Total size of the file in bytes (must be equal to the sum
    1190             :  *                    of nDataLength passed to VSIMultipartUploadAddPart())
    1191             :  * @param papszOptions Unused. Should be nullptr.
    1192             :  *
    1193             :  * @return TRUE in case of success, FALSE in case of failure.
    1194             :  *
    1195             :  * @since 3.10
    1196             :  */
    1197           5 : int VSIMultipartUploadEnd(const char *pszFilename, const char *pszUploadId,
    1198             :                           size_t nPartIdsCount, const char *const *apszPartIds,
    1199             :                           vsi_l_offset nTotalSize, CSLConstList papszOptions)
    1200             : {
    1201           5 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1202             : 
    1203          10 :     return poFSHandler->MultipartUploadEnd(pszFilename, pszUploadId,
    1204             :                                            nPartIdsCount, apszPartIds,
    1205           5 :                                            nTotalSize, papszOptions);
    1206             : }
    1207             : 
    1208             : /************************************************************************/
    1209             : /*                       VSIMultipartUploadAbort()                      */
    1210             : /************************************************************************/
    1211             : 
    1212             : /**
    1213             :  * \brief Aborts a multi-part file upload.
    1214             :  *
    1215             :  * Cf VSIMultipartUploadStart().
    1216             :  *
    1217             :  * This function is not implemented for all virtual file systems.
    1218             :  * Use VSIMultipartUploadGetCapabilities() to determine if it is supported.
    1219             :  *
    1220             :  * This can be needed to avoid extra billing for some cloud storage providers.
    1221             :  *
    1222             :  * @param pszFilename Filename for which multipart upload should be completed.
    1223             :  *                    Should be the same as the one used for
    1224             :  *                    VSIMultipartUploadStart()
    1225             :  * @param pszUploadId Value returned by VSIMultipartUploadStart()
    1226             :  * @param papszOptions Unused. Should be nullptr.
    1227             :  *
    1228             :  * @return TRUE in case of success, FALSE in case of failure.
    1229             :  *
    1230             :  * @since 3.10
    1231             :  */
    1232           6 : int VSIMultipartUploadAbort(const char *pszFilename, const char *pszUploadId,
    1233             :                             CSLConstList papszOptions)
    1234             : {
    1235           6 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1236             : 
    1237          12 :     return poFSHandler->MultipartUploadAbort(pszFilename, pszUploadId,
    1238           6 :                                              papszOptions);
    1239             : }
    1240             : 
    1241             : #ifndef DOXYGEN_SKIP
    1242             : 
    1243             : /************************************************************************/
    1244             : /*                     MultipartUploadGetCapabilities()                 */
    1245             : /************************************************************************/
    1246             : 
    1247           2 : bool VSIFilesystemHandler::MultipartUploadGetCapabilities(int *, int *, int *,
    1248             :                                                           size_t *, size_t *,
    1249             :                                                           int *)
    1250             : {
    1251           2 :     CPLError(
    1252             :         CE_Failure, CPLE_NotSupported,
    1253             :         "MultipartUploadGetCapabilities() not supported by this file system");
    1254           2 :     return false;
    1255             : }
    1256             : 
    1257             : /************************************************************************/
    1258             : /*                         MultipartUploadStart()                       */
    1259             : /************************************************************************/
    1260             : 
    1261           1 : char *VSIFilesystemHandler::MultipartUploadStart(const char *, CSLConstList)
    1262             : {
    1263           1 :     CPLError(CE_Failure, CPLE_NotSupported,
    1264             :              "MultipartUploadStart() not supported by this file system");
    1265           1 :     return nullptr;
    1266             : }
    1267             : 
    1268             : /************************************************************************/
    1269             : /*                       MultipartUploadAddPart()                       */
    1270             : /************************************************************************/
    1271             : 
    1272           1 : char *VSIFilesystemHandler::MultipartUploadAddPart(const char *, const char *,
    1273             :                                                    int, vsi_l_offset,
    1274             :                                                    const void *, size_t,
    1275             :                                                    CSLConstList)
    1276             : {
    1277           1 :     CPLError(CE_Failure, CPLE_NotSupported,
    1278             :              "MultipartUploadAddPart() not supported by this file system");
    1279           1 :     return nullptr;
    1280             : }
    1281             : 
    1282             : /************************************************************************/
    1283             : /*                         MultipartUploadEnd()                         */
    1284             : /************************************************************************/
    1285             : 
    1286           1 : bool VSIFilesystemHandler::MultipartUploadEnd(const char *, const char *,
    1287             :                                               size_t, const char *const *,
    1288             :                                               vsi_l_offset, CSLConstList)
    1289             : {
    1290           1 :     CPLError(CE_Failure, CPLE_NotSupported,
    1291             :              "MultipartUploadEnd() not supported by this file system");
    1292           1 :     return FALSE;
    1293             : }
    1294             : 
    1295             : /************************************************************************/
    1296             : /*                         MultipartUploadAbort()                       */
    1297             : /************************************************************************/
    1298             : 
    1299           1 : bool VSIFilesystemHandler::MultipartUploadAbort(const char *, const char *,
    1300             :                                                 CSLConstList)
    1301             : {
    1302           1 :     CPLError(CE_Failure, CPLE_NotSupported,
    1303             :              "MultipartUploadAbort() not supported by this file system");
    1304           1 :     return FALSE;
    1305             : }
    1306             : 
    1307             : #endif
    1308             : 
    1309             : /************************************************************************/
    1310             : /*                         VSIAbortPendingUploads()                     */
    1311             : /************************************************************************/
    1312             : 
    1313             : /**
    1314             :  * \brief Abort all ongoing multi-part uploads.
    1315             :  *
    1316             :  * Abort ongoing multi-part uploads on AWS S3 and Google Cloud Storage. This
    1317             :  * can be used in case a process doing such uploads was killed in a unclean way.
    1318             :  *
    1319             :  * This can be needed to avoid extra billing for some cloud storage providers.
    1320             :  *
    1321             :  * Without effect on other virtual file systems.
    1322             :  *
    1323             :  * VSIMultipartUploadAbort() can also be used to cancel a given upload, if the
    1324             :  * upload ID is known.
    1325             :  *
    1326             :  * @param pszFilename filename or prefix of a directory into which multipart
    1327             :  * uploads must be aborted. This can be the root directory of a bucket.  UTF-8
    1328             :  * encoded.
    1329             :  *
    1330             :  * @return TRUE on success or FALSE on an error.
    1331             :  * @since GDAL 3.4
    1332             :  */
    1333             : 
    1334           1 : int VSIAbortPendingUploads(const char *pszFilename)
    1335             : {
    1336           1 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1337             : 
    1338           1 :     return poFSHandler->AbortPendingUploads(pszFilename);
    1339             : }
    1340             : 
    1341             : /************************************************************************/
    1342             : /*                              VSIRmdir()                              */
    1343             : /************************************************************************/
    1344             : 
    1345             : /**
    1346             :  * \brief Delete a directory.
    1347             :  *
    1348             :  * Deletes a directory object from the file system.  On some systems
    1349             :  * the directory must be empty before it can be deleted.
    1350             :  *
    1351             :  * This method goes through the VSIFileHandler virtualization and may
    1352             :  * work on unusual filesystems such as in memory.
    1353             :  *
    1354             :  * Analog of the POSIX rmdir() function.
    1355             :  *
    1356             :  * @param pszDirname the path of the directory to be deleted.  UTF-8 encoded.
    1357             :  *
    1358             :  * @return 0 on success or -1 on an error.
    1359             :  */
    1360             : 
    1361         319 : int VSIRmdir(const char *pszDirname)
    1362             : 
    1363             : {
    1364         319 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszDirname);
    1365             : 
    1366         319 :     return poFSHandler->Rmdir(pszDirname);
    1367             : }
    1368             : 
    1369             : /************************************************************************/
    1370             : /*                         VSIRmdirRecursive()                          */
    1371             : /************************************************************************/
    1372             : 
    1373             : /**
    1374             :  * \brief Delete a directory recursively
    1375             :  *
    1376             :  * Deletes a directory object and its content from the file system.
    1377             :  *
    1378             :  * Starting with GDAL 3.1, /vsis3/ has an efficient implementation of this
    1379             :  * function.
    1380             :  * Starting with GDAL 3.4, /vsigs/ has an efficient implementation of this
    1381             :  * function, provided that OAuth2 authentication is used.
    1382             :  *
    1383             :  * @return 0 on success or -1 on an error.
    1384             :  * @since GDAL 2.3
    1385             :  */
    1386             : 
    1387        2921 : int VSIRmdirRecursive(const char *pszDirname)
    1388             : {
    1389        2921 :     if (pszDirname == nullptr || pszDirname[0] == '\0' ||
    1390        2921 :         strncmp("/", pszDirname, 2) == 0)
    1391             :     {
    1392           0 :         return -1;
    1393             :     }
    1394        2921 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszDirname);
    1395        2921 :     return poFSHandler->RmdirRecursive(pszDirname);
    1396             : }
    1397             : 
    1398             : /************************************************************************/
    1399             : /*                              VSIStatL()                              */
    1400             : /************************************************************************/
    1401             : 
    1402             : /**
    1403             :  * \brief Get filesystem object info.
    1404             :  *
    1405             :  * Fetches status information about a filesystem object (file, directory, etc).
    1406             :  * The returned information is placed in the VSIStatBufL structure.   For
    1407             :  * portability, only use the st_size (size in bytes) and st_mode (file type).
    1408             :  * This method is similar to VSIStat(), but will work on large files on
    1409             :  * systems where this requires special calls.
    1410             :  *
    1411             :  * This method goes through the VSIFileHandler virtualization and may
    1412             :  * work on unusual filesystems such as in memory.
    1413             :  *
    1414             :  * Analog of the POSIX stat() function.
    1415             :  *
    1416             :  * @param pszFilename the path of the filesystem object to be queried.
    1417             :  * UTF-8 encoded.
    1418             :  * @param psStatBuf the structure to load with information.
    1419             :  *
    1420             :  * @return 0 on success or -1 on an error.
    1421             :  */
    1422             : 
    1423      420404 : int VSIStatL(const char *pszFilename, VSIStatBufL *psStatBuf)
    1424             : 
    1425             : {
    1426      420404 :     return VSIStatExL(pszFilename, psStatBuf, 0);
    1427             : }
    1428             : 
    1429             : /************************************************************************/
    1430             : /*                            VSIStatExL()                              */
    1431             : /************************************************************************/
    1432             : 
    1433             : /**
    1434             :  * \brief Get filesystem object info.
    1435             :  *
    1436             :  * Fetches status information about a filesystem object (file, directory, etc).
    1437             :  * The returned information is placed in the VSIStatBufL structure.   For
    1438             :  * portability, only use the st_size (size in bytes) and st_mode (file type).
    1439             :  * This method is similar to VSIStat(), but will work on large files on
    1440             :  * systems where this requires special calls.
    1441             :  *
    1442             :  * This method goes through the VSIFileHandler virtualization and may
    1443             :  * work on unusual filesystems such as in memory.
    1444             :  *
    1445             :  * Analog of the POSIX stat() function, with an extra parameter to
    1446             :  * specify which information is needed, which offers a potential for
    1447             :  * speed optimizations on specialized and potentially slow virtual
    1448             :  * filesystem objects (/vsigzip/, /vsicurl/)
    1449             :  *
    1450             :  * @param pszFilename the path of the filesystem object to be queried.
    1451             :  * UTF-8 encoded.
    1452             :  * @param psStatBuf the structure to load with information.
    1453             :  * @param nFlags 0 to get all information, or VSI_STAT_EXISTS_FLAG,
    1454             :  *                 VSI_STAT_NATURE_FLAG, VSI_STAT_SIZE_FLAG,
    1455             :  * VSI_STAT_SET_ERROR_FLAG, VSI_STAT_CACHE_ONLY or a combination of those to get
    1456             :  * partial info.
    1457             :  *
    1458             :  * @return 0 on success or -1 on an error.
    1459             :  *
    1460             :  * @since GDAL 1.8.0
    1461             :  */
    1462             : 
    1463      778192 : int VSIStatExL(const char *pszFilename, VSIStatBufL *psStatBuf, int nFlags)
    1464             : 
    1465             : {
    1466      778192 :     char szAltPath[4] = {'\0'};
    1467             : 
    1468             :     // Enable to work on "C:" as if it were "C:\".
    1469      778192 :     if (pszFilename[0] != '\0' && pszFilename[1] == ':' &&
    1470          20 :         pszFilename[2] == '\0')
    1471             :     {
    1472           0 :         szAltPath[0] = pszFilename[0];
    1473           0 :         szAltPath[1] = pszFilename[1];
    1474           0 :         szAltPath[2] = '\\';
    1475           0 :         szAltPath[3] = '\0';
    1476             : 
    1477           0 :         pszFilename = szAltPath;
    1478             :     }
    1479             : 
    1480      778192 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1481             : 
    1482      778193 :     if (nFlags == 0)
    1483      424072 :         nFlags =
    1484             :             VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG | VSI_STAT_SIZE_FLAG;
    1485             : 
    1486     1556390 :     return poFSHandler->Stat(pszFilename, psStatBuf, nFlags);
    1487             : }
    1488             : 
    1489             : /************************************************************************/
    1490             : /*                       VSIGetFileMetadata()                           */
    1491             : /************************************************************************/
    1492             : 
    1493             : /**
    1494             :  * \brief Get metadata on files.
    1495             :  *
    1496             :  * Implemented currently only for network-like filesystems, or starting
    1497             :  * with GDAL 3.7 for /vsizip/
    1498             :  *
    1499             :  * Starting with GDAL 3.11, calling it with pszFilename being the root of a
    1500             :  * /vsigs/ bucket and pszDomain == nullptr, and when authenticated through
    1501             :  * OAuth2, will result in returning the result of a "Buckets: get"
    1502             :  * operation (https://cloud.google.com/storage/docs/json_api/v1/buckets/get),
    1503             :  * with the keys of the top-level JSON document as keys of the key=value pairs
    1504             :  * returned by this function.
    1505             :  *
    1506             :  * @param pszFilename the path of the filesystem object to be queried.
    1507             :  * UTF-8 encoded.
    1508             :  * @param pszDomain Metadata domain to query. Depends on the file system.
    1509             :  * The following ones are supported:
    1510             :  * <ul>
    1511             :  * <li>HEADERS: to get HTTP headers for network-like filesystems (/vsicurl/,
    1512             :  * /vsis3/, /vsgis/, etc)</li>
    1513             :  * <li>TAGS:
    1514             :  *   <ul>
    1515             :  *     <li>/vsis3/: to get S3 Object tagging information</li>
    1516             :  *     <li>/vsiaz/: to get blob tags. Refer to
    1517             :  *     https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-tags
    1518             :  *     </li>
    1519             :  *   </ul>
    1520             :  * </li>
    1521             :  * <li>STATUS: specific to /vsiadls/: returns all system defined properties for
    1522             :  * a path (seems in practice to be a subset of HEADERS)</li> <li>ACL: specific
    1523             :  * to /vsiadls/ and /vsigs/: returns the access control list for a path. For
    1524             :  * /vsigs/, a single XML=xml_content string is returned. Refer to
    1525             :  * https://cloud.google.com/storage/docs/xml-api/get-object-acls
    1526             :  * </li>
    1527             :  * <li>METADATA: specific to /vsiaz/: to get blob metadata. Refer to
    1528             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-metadata.
    1529             :  * Note: this will be a subset of what pszDomain=HEADERS returns</li>
    1530             :  * <li>ZIP: specific to /vsizip/: to obtain ZIP specific metadata, in particular
    1531             :  * if a file is SOZIP-enabled (SOZIP_VALID=YES)</li>
    1532             :  * </ul>
    1533             :  * @param papszOptions Unused. Should be set to NULL.
    1534             :  *
    1535             :  * @return a NULL-terminated list of key=value strings, to be freed with
    1536             :  * CSLDestroy() or NULL in case of error / empty list.
    1537             :  *
    1538             :  * @since GDAL 3.1.0
    1539             :  */
    1540             : 
    1541          76 : char **VSIGetFileMetadata(const char *pszFilename, const char *pszDomain,
    1542             :                           CSLConstList papszOptions)
    1543             : {
    1544          76 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1545          76 :     return poFSHandler->GetFileMetadata(pszFilename, pszDomain, papszOptions);
    1546             : }
    1547             : 
    1548             : /************************************************************************/
    1549             : /*                       VSISetFileMetadata()                           */
    1550             : /************************************************************************/
    1551             : 
    1552             : /**
    1553             :  * \brief Set metadata on files.
    1554             :  *
    1555             :  * Implemented currently only for /vsis3/, /vsigs/, /vsiaz/ and /vsiadls/
    1556             :  *
    1557             :  * @param pszFilename the path of the filesystem object to be set.
    1558             :  * UTF-8 encoded.
    1559             :  * @param papszMetadata NULL-terminated list of key=value strings.
    1560             :  * @param pszDomain Metadata domain to set. Depends on the file system.
    1561             :  * The following are supported:
    1562             :  * <ul>
    1563             :  * <li>HEADERS: specific to /vsis3/ and /vsigs/: to set HTTP headers, such as
    1564             :  * "Content-Type", or other file system specific header.
    1565             :  * For /vsigs/, this also includes: x-goog-meta-{key}={value}. Note that you
    1566             :  * should specify all metadata to be set, as existing metadata will be
    1567             :  * overridden.
    1568             :  * </li>
    1569             :  * <li>TAGS: Content of papszMetadata should be KEY=VALUE pairs.
    1570             :  *    <ul>
    1571             :  *      <li>/vsis3/: to set S3 Object tagging information</li>
    1572             :  *      <li>/vsiaz/: to set blob tags. Refer to
    1573             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-tags.
    1574             :  * Note: storageV2 must be enabled on the account</li>
    1575             :  *    </ul>
    1576             :  * </li>
    1577             :  * <li>PROPERTIES:
    1578             :  *    <ul>
    1579             :  *      <li>to /vsiaz/: to set properties. Refer to
    1580             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-properties.</li>
    1581             :  *      <li>to /vsiadls/: to set properties. Refer to
    1582             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update
    1583             :  * for headers valid for action=setProperties.</li>
    1584             :  *    </ul>
    1585             :  * </li>
    1586             :  * <li>ACL: specific to /vsiadls/ and /vsigs/: to set access control list.
    1587             :  * For /vsiadls/, refer to
    1588             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update
    1589             :  * for headers valid for action=setAccessControl or setAccessControlRecursive.
    1590             :  * In setAccessControlRecursive, x-ms-acl must be specified in papszMetadata.
    1591             :  * For /vsigs/, refer to
    1592             :  * https://cloud.google.com/storage/docs/xml-api/put-object-acls. A single
    1593             :  * XML=xml_content string should be specified as in papszMetadata.
    1594             :  * </li>
    1595             :  * <li>METADATA: specific to /vsiaz/: to set blob metadata. Refer to
    1596             :  * https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-metadata.
    1597             :  * Content of papszMetadata should be strings in the form
    1598             :  * x-ms-meta-name=value</li>
    1599             :  * </ul>
    1600             :  * @param papszOptions NULL or NULL terminated list of options.
    1601             :  *                     For /vsiadls/ and pszDomain=ACL, "RECURSIVE=TRUE" can be
    1602             :  *                     set to set the access control list recursively. When
    1603             :  *                     RECURSIVE=TRUE is set, MODE should also be set to one of
    1604             :  *                     "set", "modify" or "remove".
    1605             :  *
    1606             :  * @return TRUE in case of success.
    1607             :  *
    1608             :  * @since GDAL 3.1.0
    1609             :  */
    1610             : 
    1611          17 : int VSISetFileMetadata(const char *pszFilename, CSLConstList papszMetadata,
    1612             :                        const char *pszDomain, CSLConstList papszOptions)
    1613             : {
    1614          17 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1615          34 :     return poFSHandler->SetFileMetadata(pszFilename, papszMetadata, pszDomain,
    1616          17 :                                         papszOptions)
    1617          17 :                ? 1
    1618          17 :                : 0;
    1619             : }
    1620             : 
    1621             : /************************************************************************/
    1622             : /*                       VSIIsCaseSensitiveFS()                         */
    1623             : /************************************************************************/
    1624             : 
    1625             : /**
    1626             :  * \brief Returns if the filenames of the filesystem are case sensitive.
    1627             :  *
    1628             :  * This method retrieves to which filesystem belongs the passed filename
    1629             :  * and return TRUE if the filenames of that filesystem are case sensitive.
    1630             :  *
    1631             :  * Currently, this will return FALSE only for Windows real filenames. Other
    1632             :  * VSI virtual filesystems are case sensitive.
    1633             :  *
    1634             :  * This methods avoid ugly \#ifndef _WIN32 / \#endif code, that is wrong when
    1635             :  * dealing with virtual filenames.
    1636             :  *
    1637             :  * @param pszFilename the path of the filesystem object to be tested.
    1638             :  * UTF-8 encoded.
    1639             :  *
    1640             :  * @return TRUE if the filenames of the filesystem are case sensitive.
    1641             :  *
    1642             :  * @since GDAL 1.8.0
    1643             :  */
    1644             : 
    1645       26038 : int VSIIsCaseSensitiveFS(const char *pszFilename)
    1646             : {
    1647       26038 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1648             : 
    1649       26038 :     return poFSHandler->IsCaseSensitive(pszFilename);
    1650             : }
    1651             : 
    1652             : /************************************************************************/
    1653             : /*                       VSISupportsSparseFiles()                       */
    1654             : /************************************************************************/
    1655             : 
    1656             : /**
    1657             :  * \brief Returns if the filesystem supports sparse files.
    1658             :  *
    1659             :  * Only supported on Linux (and no other Unix derivatives) and
    1660             :  * Windows.  On Linux, the answer depends on a few hardcoded
    1661             :  * signatures for common filesystems. Other filesystems will be
    1662             :  * considered as not supporting sparse files.
    1663             :  *
    1664             :  * @param pszPath the path of the filesystem object to be tested.
    1665             :  * UTF-8 encoded.
    1666             :  *
    1667             :  * @return TRUE if the file system is known to support sparse files. FALSE may
    1668             :  *              be returned both in cases where it is known to not support them,
    1669             :  *              or when it is unknown.
    1670             :  *
    1671             :  * @since GDAL 2.2
    1672             :  */
    1673             : 
    1674           2 : int VSISupportsSparseFiles(const char *pszPath)
    1675             : {
    1676           2 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1677             : 
    1678           2 :     return poFSHandler->SupportsSparseFiles(pszPath);
    1679             : }
    1680             : 
    1681             : /************************************************************************/
    1682             : /*                           VSIIsLocal()                               */
    1683             : /************************************************************************/
    1684             : 
    1685             : /**
    1686             :  * \brief Returns if the file/filesystem is "local".
    1687             :  *
    1688             :  * The concept of local is mostly by opposition with a network / remote
    1689             :  * file system whose access time can be long.
    1690             :  *
    1691             :  * /vsimem/ is considered to be a local file system, although a non-persistent
    1692             :  * one.
    1693             :  *
    1694             :  * @param pszPath the path of the filesystem object to be tested.
    1695             :  * UTF-8 encoded.
    1696             :  *
    1697             :  * @return TRUE or FALSE
    1698             :  *
    1699             :  * @since GDAL 3.6
    1700             :  */
    1701             : 
    1702         231 : bool VSIIsLocal(const char *pszPath)
    1703             : {
    1704         231 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1705             : 
    1706         231 :     return poFSHandler->IsLocal(pszPath);
    1707             : }
    1708             : 
    1709             : /************************************************************************/
    1710             : /*                       VSIGetCanonicalFilename()                      */
    1711             : /************************************************************************/
    1712             : 
    1713             : /**
    1714             :  * \brief Returns the canonical filename.
    1715             :  *
    1716             :  * May be implemented by case-insensitive filesystems
    1717             :  * (currently Win32 and MacOSX) to return the filename with its actual case
    1718             :  * (i.e. the one that would be used when listing the content of the directory).
    1719             :  *
    1720             :  * @param pszPath UTF-8 encoded path
    1721             :  *
    1722             :  * @return UTF-8 encoded string, to free with VSIFree()
    1723             :  *
    1724             :  * @since GDAL 3.8
    1725             :  */
    1726             : 
    1727         248 : char *VSIGetCanonicalFilename(const char *pszPath)
    1728             : {
    1729         248 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1730             : 
    1731         248 :     return CPLStrdup(poFSHandler->GetCanonicalFilename(pszPath).c_str());
    1732             : }
    1733             : 
    1734             : /************************************************************************/
    1735             : /*                      VSISupportsSequentialWrite()                    */
    1736             : /************************************************************************/
    1737             : 
    1738             : /**
    1739             :  * \brief Returns if the filesystem supports sequential write.
    1740             :  *
    1741             :  * @param pszPath the path of the filesystem object to be tested.
    1742             :  * UTF-8 encoded.
    1743             :  * @param bAllowLocalTempFile whether the file system is allowed to use a
    1744             :  * local temporary file before uploading to the target location.
    1745             :  *
    1746             :  * @return TRUE or FALSE
    1747             :  *
    1748             :  * @since GDAL 3.6
    1749             :  */
    1750             : 
    1751         103 : bool VSISupportsSequentialWrite(const char *pszPath, bool bAllowLocalTempFile)
    1752             : {
    1753         103 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1754             : 
    1755         103 :     return poFSHandler->SupportsSequentialWrite(pszPath, bAllowLocalTempFile);
    1756             : }
    1757             : 
    1758             : /************************************************************************/
    1759             : /*                      VSISupportsRandomWrite()                        */
    1760             : /************************************************************************/
    1761             : 
    1762             : /**
    1763             :  * \brief Returns if the filesystem supports random write.
    1764             :  *
    1765             :  * @param pszPath the path of the filesystem object to be tested.
    1766             :  * UTF-8 encoded.
    1767             :  * @param bAllowLocalTempFile whether the file system is allowed to use a
    1768             :  * local temporary file before uploading to the target location.
    1769             :  *
    1770             :  * @return TRUE or FALSE
    1771             :  *
    1772             :  * @since GDAL 3.6
    1773             :  */
    1774             : 
    1775         317 : bool VSISupportsRandomWrite(const char *pszPath, bool bAllowLocalTempFile)
    1776             : {
    1777         317 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1778             : 
    1779         317 :     return poFSHandler->SupportsRandomWrite(pszPath, bAllowLocalTempFile);
    1780             : }
    1781             : 
    1782             : /************************************************************************/
    1783             : /*                     VSIHasOptimizedReadMultiRange()                  */
    1784             : /************************************************************************/
    1785             : 
    1786             : /**
    1787             :  * \brief Returns if the filesystem supports efficient multi-range reading.
    1788             :  *
    1789             :  * Currently only returns TRUE for /vsicurl/ and derived file systems.
    1790             :  *
    1791             :  * @param pszPath the path of the filesystem object to be tested.
    1792             :  * UTF-8 encoded.
    1793             :  *
    1794             :  * @return TRUE if the file system is known to have an efficient multi-range
    1795             :  * reading.
    1796             :  *
    1797             :  * @since GDAL 2.3
    1798             :  */
    1799             : 
    1800       12578 : int VSIHasOptimizedReadMultiRange(const char *pszPath)
    1801             : {
    1802       12578 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszPath);
    1803             : 
    1804       12578 :     return poFSHandler->HasOptimizedReadMultiRange(pszPath);
    1805             : }
    1806             : 
    1807             : /************************************************************************/
    1808             : /*                        VSIGetActualURL()                             */
    1809             : /************************************************************************/
    1810             : 
    1811             : /**
    1812             :  * \brief Returns the actual URL of a supplied filename.
    1813             :  *
    1814             :  * Currently only returns a non-NULL value for network-based virtual file
    1815             :  * systems. For example "/vsis3/bucket/filename" will be expanded as
    1816             :  * "https://bucket.s3.amazon.com/filename"
    1817             :  *
    1818             :  * Note that the lifetime of the returned string, is short, and may be
    1819             :  * invalidated by any following GDAL functions.
    1820             :  *
    1821             :  * @param pszFilename the path of the filesystem object. UTF-8 encoded.
    1822             :  *
    1823             :  * @return the actual URL corresponding to the supplied filename, or NULL.
    1824             :  * Should not be freed.
    1825             :  *
    1826             :  * @since GDAL 2.3
    1827             :  */
    1828             : 
    1829           9 : const char *VSIGetActualURL(const char *pszFilename)
    1830             : {
    1831           9 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1832             : 
    1833           9 :     return poFSHandler->GetActualURL(pszFilename);
    1834             : }
    1835             : 
    1836             : /************************************************************************/
    1837             : /*                        VSIGetSignedURL()                             */
    1838             : /************************************************************************/
    1839             : 
    1840             : /**
    1841             :  * \brief Returns a signed URL of a supplied filename.
    1842             :  *
    1843             :  * Currently only returns a non-NULL value for /vsis3/, /vsigs/, /vsiaz/ and
    1844             :  * /vsioss/ For example "/vsis3/bucket/filename" will be expanded as
    1845             :  * "https://bucket.s3.amazon.com/filename?X-Amz-Algorithm=AWS4-HMAC-SHA256..."
    1846             :  * Configuration options that apply for file opening (typically to provide
    1847             :  * credentials), and are returned by VSIGetFileSystemOptions(), are also valid
    1848             :  * in that context.
    1849             :  *
    1850             :  * @param pszFilename the path of the filesystem object. UTF-8 encoded.
    1851             :  * @param papszOptions list of options, or NULL. Depend on file system handler.
    1852             :  * For /vsis3/, /vsigs/, /vsiaz/ and /vsioss/, the following options are
    1853             :  * supported: <ul> <li>START_DATE=YYMMDDTHHMMSSZ: date and time in UTC following
    1854             :  * ISO 8601 standard, corresponding to the start of validity of the URL. If not
    1855             :  * specified, current date time.</li> <li>EXPIRATION_DELAY=number_of_seconds:
    1856             :  * number between 1 and 604800 (seven days) for the validity of the signed URL.
    1857             :  * Defaults to 3600 (one hour)</li> <li>VERB=GET/HEAD/DELETE/PUT/POST: HTTP VERB
    1858             :  * for which the request will be used. Default to GET.</li>
    1859             :  * </ul>
    1860             :  *
    1861             :  * /vsiaz/ supports additional options:
    1862             :  * <ul>
    1863             :  * <li>SIGNEDIDENTIFIER=value: to relate the given shared access signature
    1864             :  * to a corresponding stored access policy.</li>
    1865             :  * <li>SIGNEDPERMISSIONS=r|w: permissions associated with the shared access
    1866             :  * signature. Normally deduced from VERB.</li>
    1867             :  * </ul>
    1868             :  *
    1869             :  * @return a signed URL, or NULL. Should be freed with CPLFree().
    1870             :  * @since GDAL 2.3
    1871             :  */
    1872             : 
    1873          25 : char *VSIGetSignedURL(const char *pszFilename, CSLConstList papszOptions)
    1874             : {
    1875          25 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    1876             : 
    1877          25 :     return poFSHandler->GetSignedURL(pszFilename, papszOptions);
    1878             : }
    1879             : 
    1880             : /************************************************************************/
    1881             : /*                             VSIFOpenL()                              */
    1882             : /************************************************************************/
    1883             : 
    1884             : /**
    1885             :  * \brief Open file.
    1886             :  *
    1887             :  * This function opens a file with the desired access.  Large files (larger
    1888             :  * than 2GB) should be supported.  Binary access is always implied and
    1889             :  * the "b" does not need to be included in the pszAccess string.
    1890             :  *
    1891             :  * Note that the "VSILFILE *" returned since GDAL 1.8.0 by this function is
    1892             :  * *NOT* a standard C library FILE *, and cannot be used with any functions
    1893             :  * other than the "VSI*L" family of functions.  They aren't "real" FILE objects.
    1894             :  *
    1895             :  * On windows it is possible to define the configuration option
    1896             :  * GDAL_FILE_IS_UTF8 to have pszFilename treated as being in the local
    1897             :  * encoding instead of UTF-8, restoring the pre-1.8.0 behavior of VSIFOpenL().
    1898             :  *
    1899             :  * This method goes through the VSIFileHandler virtualization and may
    1900             :  * work on unusual filesystems such as in memory.
    1901             :  *
    1902             :  * Analog of the POSIX fopen() function.
    1903             :  *
    1904             :  * @param pszFilename the file to open.  UTF-8 encoded.
    1905             :  * @param pszAccess access requested (i.e. "r", "r+", "w")
    1906             :  *
    1907             :  * @return NULL on failure, or the file handle.
    1908             :  */
    1909             : 
    1910      225710 : VSILFILE *VSIFOpenL(const char *pszFilename, const char *pszAccess)
    1911             : 
    1912             : {
    1913      225710 :     return VSIFOpenExL(pszFilename, pszAccess, false);
    1914             : }
    1915             : 
    1916             : /************************************************************************/
    1917             : /*                               Open()                                 */
    1918             : /************************************************************************/
    1919             : 
    1920             : #ifndef DOXYGEN_SKIP
    1921             : 
    1922        5758 : VSIVirtualHandle *VSIFilesystemHandler::Open(const char *pszFilename,
    1923             :                                              const char *pszAccess)
    1924             : {
    1925        5758 :     return Open(pszFilename, pszAccess, false, nullptr);
    1926             : }
    1927             : 
    1928             : /************************************************************************/
    1929             : /*                             CopyFile()                               */
    1930             : /************************************************************************/
    1931             : 
    1932        2209 : int VSIFilesystemHandler::CopyFile(const char *pszSource, const char *pszTarget,
    1933             :                                    VSILFILE *fpSource, vsi_l_offset nSourceSize,
    1934             :                                    CSLConstList papszOptions,
    1935             :                                    GDALProgressFunc pProgressFunc,
    1936             :                                    void *pProgressData)
    1937             : {
    1938        2209 :     VSIVirtualHandleUniquePtr poFileHandleAutoClose;
    1939        2209 :     if (!fpSource)
    1940             :     {
    1941        2192 :         CPLAssert(pszSource);
    1942        2192 :         fpSource = VSIFOpenExL(pszSource, "rb", TRUE);
    1943        2192 :         if (!fpSource)
    1944             :         {
    1945           1 :             CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", pszSource);
    1946           1 :             return -1;
    1947             :         }
    1948        2191 :         poFileHandleAutoClose.reset(fpSource);
    1949             :     }
    1950        2208 :     if (nSourceSize == static_cast<vsi_l_offset>(-1) &&
    1951           2 :         pProgressFunc != nullptr && pszSource != nullptr)
    1952             :     {
    1953             :         VSIStatBufL sStat;
    1954           2 :         if (VSIStatL(pszSource, &sStat) == 0)
    1955             :         {
    1956           2 :             nSourceSize = sStat.st_size;
    1957             :         }
    1958             :     }
    1959             : 
    1960        2208 :     VSILFILE *fpOut = VSIFOpenEx2L(pszTarget, "wb", TRUE, papszOptions);
    1961        2208 :     if (!fpOut)
    1962             :     {
    1963           2 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s", pszTarget);
    1964           2 :         return -1;
    1965             :     }
    1966             : 
    1967        4412 :     CPLString osMsg;
    1968        2206 :     if (pszSource)
    1969        2204 :         osMsg.Printf("Copying of %s", pszSource);
    1970             :     else
    1971           2 :         pszSource = "(unknown filename)";
    1972             : 
    1973        2206 :     int ret = 0;
    1974        2206 :     constexpr size_t nBufferSize = 10 * 4096;
    1975        2206 :     std::vector<GByte> abyBuffer(nBufferSize, 0);
    1976        2206 :     GUIntBig nOffset = 0;
    1977             :     while (true)
    1978             :     {
    1979        2508 :         const size_t nRead = VSIFReadL(&abyBuffer[0], 1, nBufferSize, fpSource);
    1980        2508 :         if (nRead < nBufferSize && VSIFErrorL(fpSource))
    1981             :         {
    1982           8 :             CPLError(
    1983             :                 CE_Failure, CPLE_FileIO,
    1984             :                 "Copying of %s to %s failed: error while reading source file",
    1985             :                 pszSource, pszTarget);
    1986           8 :             ret = -1;
    1987           8 :             break;
    1988             :         }
    1989        2500 :         if (nRead > 0)
    1990             :         {
    1991        2499 :             const size_t nWritten = VSIFWriteL(&abyBuffer[0], 1, nRead, fpOut);
    1992        2499 :             if (nWritten != nRead)
    1993             :             {
    1994          20 :                 CPLError(CE_Failure, CPLE_FileIO,
    1995             :                          "Copying of %s to %s failed: error while writing into "
    1996             :                          "target file",
    1997             :                          pszSource, pszTarget);
    1998          20 :                 ret = -1;
    1999          20 :                 break;
    2000             :             }
    2001        2479 :             nOffset += nRead;
    2002        2523 :             if (pProgressFunc &&
    2003          88 :                 !pProgressFunc(
    2004             :                     nSourceSize == 0 ? 1.0
    2005          44 :                     : nSourceSize > 0 &&
    2006             :                             nSourceSize != static_cast<vsi_l_offset>(-1)
    2007          88 :                         ? double(nOffset) / nSourceSize
    2008             :                         : 0.0,
    2009          44 :                     !osMsg.empty() ? osMsg.c_str() : nullptr, pProgressData))
    2010             :             {
    2011           1 :                 ret = -1;
    2012           1 :                 break;
    2013             :             }
    2014             :         }
    2015        2479 :         if (nRead < nBufferSize)
    2016             :         {
    2017        2177 :             break;
    2018             :         }
    2019         302 :     }
    2020             : 
    2021        2206 :     if (nSourceSize != static_cast<vsi_l_offset>(-1) && nOffset != nSourceSize)
    2022             :     {
    2023           2 :         CPLError(CE_Failure, CPLE_FileIO,
    2024             :                  "Copying of %s to %s failed: %" PRIu64 " bytes were copied "
    2025             :                  "whereas %" PRIu64 " were expected",
    2026             :                  pszSource, pszTarget, static_cast<uint64_t>(nOffset),
    2027             :                  static_cast<uint64_t>(nSourceSize));
    2028           2 :         ret = -1;
    2029             :     }
    2030             : 
    2031        2206 :     if (VSIFCloseL(fpOut) != 0)
    2032             :     {
    2033           1 :         ret = -1;
    2034             :     }
    2035             : 
    2036        2206 :     if (ret != 0)
    2037          30 :         VSIUnlink(pszTarget);
    2038             : 
    2039        2206 :     return ret;
    2040             : }
    2041             : 
    2042             : /************************************************************************/
    2043             : /*                       CopyFileRestartable()                          */
    2044             : /************************************************************************/
    2045             : 
    2046           2 : int VSIFilesystemHandler::CopyFileRestartable(
    2047             :     const char *pszSource, const char *pszTarget,
    2048             :     const char * /* pszInputPayload */, char **ppszOutputPayload,
    2049             :     CSLConstList papszOptions, GDALProgressFunc pProgressFunc,
    2050             :     void *pProgressData)
    2051             : {
    2052           2 :     *ppszOutputPayload = nullptr;
    2053           2 :     return CopyFile(pszSource, pszTarget, nullptr,
    2054             :                     static_cast<vsi_l_offset>(-1), papszOptions, pProgressFunc,
    2055           2 :                     pProgressData);
    2056             : }
    2057             : 
    2058             : /************************************************************************/
    2059             : /*                               Sync()                                 */
    2060             : /************************************************************************/
    2061             : 
    2062          28 : bool VSIFilesystemHandler::Sync(const char *pszSource, const char *pszTarget,
    2063             :                                 const char *const *papszOptions,
    2064             :                                 GDALProgressFunc pProgressFunc,
    2065             :                                 void *pProgressData, char ***ppapszOutputs)
    2066             : {
    2067          28 :     const char SOURCE_SEP = VSIGetDirectorySeparator(pszSource)[0];
    2068             : 
    2069          28 :     if (ppapszOutputs)
    2070             :     {
    2071           0 :         *ppapszOutputs = nullptr;
    2072             :     }
    2073             : 
    2074             :     VSIStatBufL sSource;
    2075          56 :     CPLString osSource(pszSource);
    2076          56 :     CPLString osSourceWithoutSlash(pszSource);
    2077          34 :     if (osSourceWithoutSlash.back() == '/' ||
    2078           6 :         osSourceWithoutSlash.back() == '\\')
    2079             :     {
    2080          22 :         osSourceWithoutSlash.pop_back();
    2081             :     }
    2082          28 :     if (VSIStatL(osSourceWithoutSlash, &sSource) < 0)
    2083             :     {
    2084           1 :         CPLError(CE_Failure, CPLE_FileIO, "%s does not exist", pszSource);
    2085           1 :         return false;
    2086             :     }
    2087             : 
    2088          27 :     if (VSI_ISDIR(sSource.st_mode))
    2089             :     {
    2090          22 :         std::string osTargetDir(pszTarget);
    2091          11 :         if (osSource.back() != '/' && osSource.back() != '\\')
    2092             :         {
    2093           2 :             osTargetDir = CPLFormFilenameSafe(
    2094           1 :                 osTargetDir.c_str(), CPLGetFilename(pszSource), nullptr);
    2095             :         }
    2096             : 
    2097             :         VSIStatBufL sTarget;
    2098          11 :         bool ret = true;
    2099          11 :         if (VSIStatL(osTargetDir.c_str(), &sTarget) < 0)
    2100             :         {
    2101           9 :             if (VSIMkdirRecursive(osTargetDir.c_str(), 0755) < 0)
    2102             :             {
    2103           1 :                 CPLError(CE_Failure, CPLE_FileIO, "Cannot create directory %s",
    2104             :                          osTargetDir.c_str());
    2105           1 :                 return false;
    2106             :             }
    2107             :         }
    2108             : 
    2109          10 :         if (!CPLFetchBool(papszOptions, "STOP_ON_DIR", false))
    2110             :         {
    2111          20 :             CPLStringList aosChildOptions(CSLDuplicate(papszOptions));
    2112          10 :             if (!CPLFetchBool(papszOptions, "RECURSIVE", true))
    2113             :             {
    2114           0 :                 aosChildOptions.SetNameValue("RECURSIVE", nullptr);
    2115           0 :                 aosChildOptions.AddString("STOP_ON_DIR=TRUE");
    2116             :             }
    2117             : 
    2118          10 :             char **papszSrcFiles = VSIReadDir(osSourceWithoutSlash);
    2119          10 :             int nFileCount = 0;
    2120          27 :             for (auto iter = papszSrcFiles; iter && *iter; ++iter)
    2121             :             {
    2122          17 :                 if (strcmp(*iter, ".") != 0 && strcmp(*iter, "..") != 0)
    2123             :                 {
    2124          17 :                     nFileCount++;
    2125             :                 }
    2126             :             }
    2127          10 :             int iFile = 0;
    2128          27 :             for (auto iter = papszSrcFiles; iter && *iter; ++iter, ++iFile)
    2129             :             {
    2130          17 :                 if (strcmp(*iter, ".") == 0 || strcmp(*iter, "..") == 0)
    2131             :                 {
    2132           0 :                     continue;
    2133             :                 }
    2134             :                 const std::string osSubSource(CPLFormFilenameSafe(
    2135          17 :                     osSourceWithoutSlash.c_str(), *iter, nullptr));
    2136             :                 const std::string osSubTarget(
    2137          17 :                     CPLFormFilenameSafe(osTargetDir.c_str(), *iter, nullptr));
    2138             :                 // coverity[divide_by_zero]
    2139          34 :                 void *pScaledProgress = GDALCreateScaledProgress(
    2140          17 :                     double(iFile) / nFileCount, double(iFile + 1) / nFileCount,
    2141             :                     pProgressFunc, pProgressData);
    2142          17 :                 ret = Sync((osSubSource + SOURCE_SEP).c_str(),
    2143          17 :                            osSubTarget.c_str(), aosChildOptions.List(),
    2144          17 :                            GDALScaledProgress, pScaledProgress, nullptr);
    2145          17 :                 GDALDestroyScaledProgress(pScaledProgress);
    2146          17 :                 if (!ret)
    2147             :                 {
    2148           0 :                     break;
    2149             :                 }
    2150             :             }
    2151          10 :             CSLDestroy(papszSrcFiles);
    2152             :         }
    2153          10 :         return ret;
    2154             :     }
    2155             : 
    2156             :     VSIStatBufL sTarget;
    2157          32 :     std::string osTarget(pszTarget);
    2158          16 :     if (VSIStatL(osTarget.c_str(), &sTarget) == 0)
    2159             :     {
    2160           4 :         bool bTargetIsFile = true;
    2161           4 :         if (VSI_ISDIR(sTarget.st_mode))
    2162             :         {
    2163           4 :             osTarget = CPLFormFilenameSafe(osTarget.c_str(),
    2164           2 :                                            CPLGetFilename(pszSource), nullptr);
    2165           3 :             bTargetIsFile = VSIStatL(osTarget.c_str(), &sTarget) == 0 &&
    2166           1 :                             !CPL_TO_BOOL(VSI_ISDIR(sTarget.st_mode));
    2167             :         }
    2168           4 :         if (bTargetIsFile)
    2169             :         {
    2170           3 :             if (sSource.st_size == sTarget.st_size &&
    2171           3 :                 sSource.st_mtime == sTarget.st_mtime && sSource.st_mtime != 0)
    2172             :             {
    2173           2 :                 CPLDebug("VSI",
    2174             :                          "%s and %s have same size and modification "
    2175             :                          "date. Skipping copying",
    2176             :                          osSourceWithoutSlash.c_str(), osTarget.c_str());
    2177           2 :                 return true;
    2178             :             }
    2179             :         }
    2180             :     }
    2181             : 
    2182          14 :     VSILFILE *fpIn = VSIFOpenExL(osSourceWithoutSlash, "rb", TRUE);
    2183          14 :     if (fpIn == nullptr)
    2184             :     {
    2185           0 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
    2186             :                  osSourceWithoutSlash.c_str());
    2187           0 :         return false;
    2188             :     }
    2189             : 
    2190          14 :     VSILFILE *fpOut = VSIFOpenExL(osTarget.c_str(), "wb", TRUE);
    2191          14 :     if (fpOut == nullptr)
    2192             :     {
    2193           1 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s", osTarget.c_str());
    2194           1 :         VSIFCloseL(fpIn);
    2195           1 :         return false;
    2196             :     }
    2197             : 
    2198          13 :     bool ret = true;
    2199          13 :     constexpr size_t nBufferSize = 10 * 4096;
    2200          26 :     std::vector<GByte> abyBuffer(nBufferSize, 0);
    2201          13 :     GUIntBig nOffset = 0;
    2202          13 :     CPLString osMsg;
    2203          13 :     osMsg.Printf("Copying of %s", osSourceWithoutSlash.c_str());
    2204             :     while (true)
    2205             :     {
    2206          16 :         size_t nRead = VSIFReadL(&abyBuffer[0], 1, nBufferSize, fpIn);
    2207          16 :         size_t nWritten = VSIFWriteL(&abyBuffer[0], 1, nRead, fpOut);
    2208          16 :         if (nWritten != nRead)
    2209             :         {
    2210           0 :             CPLError(CE_Failure, CPLE_FileIO, "Copying of %s to %s failed",
    2211             :                      osSourceWithoutSlash.c_str(), osTarget.c_str());
    2212           0 :             ret = false;
    2213           0 :             break;
    2214             :         }
    2215          16 :         nOffset += nRead;
    2216          16 :         if (pProgressFunc && !pProgressFunc(double(nOffset) / sSource.st_size,
    2217             :                                             osMsg.c_str(), pProgressData))
    2218             :         {
    2219           0 :             ret = false;
    2220           0 :             break;
    2221             :         }
    2222          16 :         if (nRead < nBufferSize)
    2223             :         {
    2224          13 :             break;
    2225             :         }
    2226           3 :     }
    2227             : 
    2228          13 :     VSIFCloseL(fpIn);
    2229          13 :     if (VSIFCloseL(fpOut) != 0)
    2230             :     {
    2231           0 :         ret = false;
    2232             :     }
    2233          13 :     return ret;
    2234             : }
    2235             : 
    2236             : /************************************************************************/
    2237             : /*                            VSIDIREntry()                             */
    2238             : /************************************************************************/
    2239             : 
    2240        3218 : VSIDIREntry::VSIDIREntry()
    2241             :     : pszName(nullptr), nMode(0), nSize(0), nMTime(0), bModeKnown(false),
    2242        3218 :       bSizeKnown(false), bMTimeKnown(false), papszExtra(nullptr)
    2243             : {
    2244        3218 : }
    2245             : 
    2246             : /************************************************************************/
    2247             : /*                            VSIDIREntry()                             */
    2248             : /************************************************************************/
    2249             : 
    2250           6 : VSIDIREntry::VSIDIREntry(const VSIDIREntry &other)
    2251           6 :     : pszName(VSIStrdup(other.pszName)), nMode(other.nMode), nSize(other.nSize),
    2252           6 :       nMTime(other.nMTime), bModeKnown(other.bModeKnown),
    2253           6 :       bSizeKnown(other.bSizeKnown), bMTimeKnown(other.bMTimeKnown),
    2254           6 :       papszExtra(CSLDuplicate(other.papszExtra))
    2255             : {
    2256           6 : }
    2257             : 
    2258             : /************************************************************************/
    2259             : /*                           ~VSIDIREntry()                             */
    2260             : /************************************************************************/
    2261             : 
    2262        6448 : VSIDIREntry::~VSIDIREntry()
    2263             : {
    2264        3224 :     CPLFree(pszName);
    2265        3224 :     CSLDestroy(papszExtra);
    2266        3224 : }
    2267             : 
    2268             : /************************************************************************/
    2269             : /*                              ~VSIDIR()                               */
    2270             : /************************************************************************/
    2271             : 
    2272        2828 : VSIDIR::~VSIDIR()
    2273             : {
    2274        2828 : }
    2275             : 
    2276             : /************************************************************************/
    2277             : /*                            VSIDIRGeneric                             */
    2278             : /************************************************************************/
    2279             : 
    2280             : namespace
    2281             : {
    2282             : struct VSIDIRGeneric : public VSIDIR
    2283             : {
    2284             :     CPLString osRootPath{};
    2285             :     CPLString osBasePath{};
    2286             :     char **papszContent = nullptr;
    2287             :     int nRecurseDepth = 0;
    2288             :     int nPos = 0;
    2289             :     VSIDIREntry entry{};
    2290             :     std::vector<VSIDIRGeneric *> aoStackSubDir{};
    2291             :     VSIFilesystemHandler *poFS = nullptr;
    2292             :     std::string m_osFilterPrefix{};
    2293             : 
    2294        2462 :     explicit VSIDIRGeneric(VSIFilesystemHandler *poFSIn) : poFS(poFSIn)
    2295             :     {
    2296        2462 :     }
    2297             : 
    2298             :     ~VSIDIRGeneric();
    2299             : 
    2300             :     const VSIDIREntry *NextDirEntry() override;
    2301             : 
    2302             :     VSIDIRGeneric(const VSIDIRGeneric &) = delete;
    2303             :     VSIDIRGeneric &operator=(const VSIDIRGeneric &) = delete;
    2304             : };
    2305             : 
    2306             : /************************************************************************/
    2307             : /*                         ~VSIDIRGeneric()                             */
    2308             : /************************************************************************/
    2309             : 
    2310        7386 : VSIDIRGeneric::~VSIDIRGeneric()
    2311             : {
    2312        2467 :     while (!aoStackSubDir.empty())
    2313             :     {
    2314           5 :         delete aoStackSubDir.back();
    2315           5 :         aoStackSubDir.pop_back();
    2316             :     }
    2317        2462 :     CSLDestroy(papszContent);
    2318        4924 : }
    2319             : 
    2320             : }  // namespace
    2321             : 
    2322             : /************************************************************************/
    2323             : /*                            OpenDir()                                 */
    2324             : /************************************************************************/
    2325             : 
    2326        2468 : VSIDIR *VSIFilesystemHandler::OpenDir(const char *pszPath, int nRecurseDepth,
    2327             :                                       const char *const *papszOptions)
    2328             : {
    2329        2468 :     char **papszContent = VSIReadDir(pszPath);
    2330             :     VSIStatBufL sStatL;
    2331        3005 :     if (papszContent == nullptr &&
    2332         537 :         (VSIStatL(pszPath, &sStatL) != 0 || !VSI_ISDIR(sStatL.st_mode)))
    2333             :     {
    2334           6 :         return nullptr;
    2335             :     }
    2336        2462 :     VSIDIRGeneric *dir = new VSIDIRGeneric(this);
    2337        2462 :     dir->osRootPath = pszPath;
    2338        4924 :     if (!dir->osRootPath.empty() &&
    2339        2462 :         (dir->osRootPath.back() == '/' || dir->osRootPath.back() == '\\'))
    2340          15 :         dir->osRootPath.pop_back();
    2341        2462 :     dir->nRecurseDepth = nRecurseDepth;
    2342        2462 :     dir->papszContent = papszContent;
    2343        2462 :     dir->m_osFilterPrefix = CSLFetchNameValueDef(papszOptions, "PREFIX", "");
    2344        2462 :     return dir;
    2345             : }
    2346             : 
    2347             : /************************************************************************/
    2348             : /*                           NextDirEntry()                             */
    2349             : /************************************************************************/
    2350             : 
    2351      506670 : const VSIDIREntry *VSIDIRGeneric::NextDirEntry()
    2352             : {
    2353      506670 :     const char SEP = VSIGetDirectorySeparator(osRootPath.c_str())[0];
    2354             : 
    2355      506671 : begin:
    2356      506671 :     if (VSI_ISDIR(entry.nMode) && nRecurseDepth != 0)
    2357             :     {
    2358        1137 :         CPLString osCurFile(osRootPath);
    2359        1137 :         if (!osCurFile.empty())
    2360        1137 :             osCurFile += SEP;
    2361        1137 :         osCurFile += entry.pszName;
    2362             :         auto subdir =
    2363        1137 :             static_cast<VSIDIRGeneric *>(poFS->VSIFilesystemHandler::OpenDir(
    2364        1137 :                 osCurFile, nRecurseDepth - 1, nullptr));
    2365        1137 :         if (subdir)
    2366             :         {
    2367        1137 :             subdir->osRootPath = osRootPath;
    2368        1137 :             subdir->osBasePath = entry.pszName;
    2369        1137 :             subdir->m_osFilterPrefix = m_osFilterPrefix;
    2370        1137 :             aoStackSubDir.push_back(subdir);
    2371             :         }
    2372        1137 :         entry.nMode = 0;
    2373             :     }
    2374             : 
    2375      507803 :     while (!aoStackSubDir.empty())
    2376             :     {
    2377      502728 :         auto l_entry = aoStackSubDir.back()->NextDirEntry();
    2378      502728 :         if (l_entry)
    2379             :         {
    2380      501596 :             return l_entry;
    2381             :         }
    2382        1132 :         delete aoStackSubDir.back();
    2383        1132 :         aoStackSubDir.pop_back();
    2384             :     }
    2385             : 
    2386        5075 :     if (papszContent == nullptr)
    2387             :     {
    2388         531 :         return nullptr;
    2389             :     }
    2390             : 
    2391             :     while (true)
    2392             :     {
    2393        4571 :         if (!papszContent[nPos])
    2394             :         {
    2395        1921 :             return nullptr;
    2396             :         }
    2397             :         // Skip . and ..entries
    2398        2650 :         if (papszContent[nPos][0] == '.' &&
    2399          98 :             (papszContent[nPos][1] == '\0' ||
    2400          98 :              (papszContent[nPos][1] == '.' && papszContent[nPos][2] == '\0')))
    2401             :         {
    2402          22 :             nPos++;
    2403             :         }
    2404             :         else
    2405             :         {
    2406        2628 :             CPLFree(entry.pszName);
    2407        2628 :             CPLString osName(osBasePath);
    2408        2628 :             if (!osName.empty())
    2409        1520 :                 osName += SEP;
    2410        2628 :             osName += papszContent[nPos];
    2411        2628 :             nPos++;
    2412             : 
    2413        2628 :             entry.pszName = CPLStrdup(osName);
    2414        2628 :             entry.nMode = 0;
    2415        2628 :             CPLString osCurFile(osRootPath);
    2416        2628 :             if (!osCurFile.empty())
    2417        2628 :                 osCurFile += SEP;
    2418        2628 :             osCurFile += entry.pszName;
    2419             : 
    2420        5246 :             const auto StatFile = [&osCurFile, this]()
    2421             :             {
    2422             :                 VSIStatBufL sStatL;
    2423        2623 :                 if (VSIStatL(osCurFile, &sStatL) == 0)
    2424             :                 {
    2425        2619 :                     entry.nMode = sStatL.st_mode;
    2426        2619 :                     entry.nSize = sStatL.st_size;
    2427        2619 :                     entry.nMTime = sStatL.st_mtime;
    2428        2619 :                     entry.bModeKnown = true;
    2429        2619 :                     entry.bSizeKnown = true;
    2430        2619 :                     entry.bMTimeKnown = true;
    2431             :                 }
    2432             :                 else
    2433             :                 {
    2434           4 :                     entry.nMode = 0;
    2435           4 :                     entry.nSize = 0;
    2436           4 :                     entry.nMTime = 0;
    2437           4 :                     entry.bModeKnown = false;
    2438           4 :                     entry.bSizeKnown = false;
    2439           4 :                     entry.bMTimeKnown = false;
    2440             :                 }
    2441        2623 :             };
    2442             : 
    2443        2640 :             if (!m_osFilterPrefix.empty() &&
    2444          12 :                 m_osFilterPrefix.size() > osName.size())
    2445             :             {
    2446           6 :                 if (STARTS_WITH(m_osFilterPrefix.c_str(), osName.c_str()) &&
    2447           2 :                     m_osFilterPrefix[osName.size()] == SEP)
    2448             :                 {
    2449           1 :                     StatFile();
    2450           1 :                     if (VSI_ISDIR(entry.nMode))
    2451             :                     {
    2452           1 :                         goto begin;
    2453             :                     }
    2454             :                 }
    2455           3 :                 continue;
    2456             :             }
    2457        2632 :             if (!m_osFilterPrefix.empty() &&
    2458           8 :                 !STARTS_WITH(osName.c_str(), m_osFilterPrefix.c_str()))
    2459             :             {
    2460           2 :                 continue;
    2461             :             }
    2462             : 
    2463        2622 :             StatFile();
    2464             : 
    2465        2622 :             break;
    2466             :         }
    2467          27 :     }
    2468             : 
    2469        2622 :     return &(entry);
    2470             : }
    2471             : 
    2472             : /************************************************************************/
    2473             : /*                           UnlinkBatch()                              */
    2474             : /************************************************************************/
    2475             : 
    2476           1 : int *VSIFilesystemHandler::UnlinkBatch(CSLConstList papszFiles)
    2477             : {
    2478             :     int *panRet =
    2479           1 :         static_cast<int *>(CPLMalloc(sizeof(int) * CSLCount(papszFiles)));
    2480           3 :     for (int i = 0; papszFiles && papszFiles[i]; ++i)
    2481             :     {
    2482           2 :         panRet[i] = VSIUnlink(papszFiles[i]) == 0;
    2483             :     }
    2484           1 :     return panRet;
    2485             : }
    2486             : 
    2487             : /************************************************************************/
    2488             : /*                          RmdirRecursive()                            */
    2489             : /************************************************************************/
    2490             : 
    2491          37 : int VSIFilesystemHandler::RmdirRecursive(const char *pszDirname)
    2492             : {
    2493          74 :     CPLString osDirnameWithoutEndSlash(pszDirname);
    2494          74 :     if (!osDirnameWithoutEndSlash.empty() &&
    2495          37 :         (osDirnameWithoutEndSlash.back() == '/' ||
    2496          37 :          osDirnameWithoutEndSlash.back() == '\\'))
    2497             :     {
    2498           0 :         osDirnameWithoutEndSlash.pop_back();
    2499             :     }
    2500             : 
    2501          37 :     const char SEP = VSIGetDirectorySeparator(pszDirname)[0];
    2502             : 
    2503          74 :     CPLStringList aosOptions;
    2504             :     auto poDir =
    2505          74 :         std::unique_ptr<VSIDIR>(OpenDir(pszDirname, -1, aosOptions.List()));
    2506          37 :     if (!poDir)
    2507           8 :         return -1;
    2508          58 :     std::vector<std::string> aosDirs;
    2509             :     while (true)
    2510             :     {
    2511       21637 :         auto entry = poDir->NextDirEntry();
    2512       21637 :         if (!entry)
    2513          29 :             break;
    2514             : 
    2515       43216 :         const CPLString osFilename(osDirnameWithoutEndSlash + SEP +
    2516       43216 :                                    entry->pszName);
    2517       21608 :         if ((entry->nMode & S_IFDIR))
    2518             :         {
    2519         150 :             aosDirs.push_back(osFilename);
    2520             :         }
    2521             :         else
    2522             :         {
    2523       21458 :             if (VSIUnlink(osFilename) != 0)
    2524           0 :                 return -1;
    2525             :         }
    2526       21608 :     }
    2527             : 
    2528             :     // Sort in reverse order, so that inner-most directories are deleted first
    2529          29 :     std::sort(aosDirs.begin(), aosDirs.end(),
    2530         888 :               [](const std::string &a, const std::string &b) { return a > b; });
    2531             : 
    2532         179 :     for (const auto &osDir : aosDirs)
    2533             :     {
    2534         150 :         if (VSIRmdir(osDir.c_str()) != 0)
    2535           0 :             return -1;
    2536             :     }
    2537             : 
    2538          29 :     return VSIRmdir(pszDirname);
    2539             : }
    2540             : 
    2541             : /************************************************************************/
    2542             : /*                          GetFileMetadata()                           */
    2543             : /************************************************************************/
    2544             : 
    2545           0 : char **VSIFilesystemHandler::GetFileMetadata(const char * /* pszFilename*/,
    2546             :                                              const char * /*pszDomain*/,
    2547             :                                              CSLConstList /*papszOptions*/)
    2548             : {
    2549           0 :     return nullptr;
    2550             : }
    2551             : 
    2552             : /************************************************************************/
    2553             : /*                          SetFileMetadata()                           */
    2554             : /************************************************************************/
    2555             : 
    2556           0 : bool VSIFilesystemHandler::SetFileMetadata(const char * /* pszFilename*/,
    2557             :                                            CSLConstList /* papszMetadata */,
    2558             :                                            const char * /* pszDomain */,
    2559             :                                            CSLConstList /* papszOptions */)
    2560             : {
    2561           0 :     CPLError(CE_Failure, CPLE_NotSupported, "SetFileMetadata() not supported");
    2562           0 :     return false;
    2563             : }
    2564             : 
    2565             : #endif
    2566             : 
    2567             : /************************************************************************/
    2568             : /*                             VSIFOpenExL()                            */
    2569             : /************************************************************************/
    2570             : 
    2571             : /**
    2572             :  * \brief Open/create file.
    2573             :  *
    2574             :  * This function opens (or creates) a file with the desired access.
    2575             :  * Binary access is always implied and
    2576             :  * the "b" does not need to be included in the pszAccess string.
    2577             :  *
    2578             :  * Note that the "VSILFILE *" returned by this function is
    2579             :  * *NOT* a standard C library FILE *, and cannot be used with any functions
    2580             :  * other than the "VSI*L" family of functions.  They aren't "real" FILE objects.
    2581             :  *
    2582             :  * On windows it is possible to define the configuration option
    2583             :  * GDAL_FILE_IS_UTF8 to have pszFilename treated as being in the local
    2584             :  * encoding instead of UTF-8, restoring the pre-1.8.0 behavior of VSIFOpenL().
    2585             :  *
    2586             :  * This method goes through the VSIFileHandler virtualization and may
    2587             :  * work on unusual filesystems such as in memory.
    2588             :  *
    2589             :  * Analog of the POSIX fopen() function.
    2590             :  *
    2591             :  * @param pszFilename the file to open.  UTF-8 encoded.
    2592             :  * @param pszAccess access requested (i.e. "r", "r+", "w")
    2593             :  * @param bSetError flag determining whether or not this open call
    2594             :  * should set VSIErrors on failure.
    2595             :  *
    2596             :  * @return NULL on failure, or the file handle.
    2597             :  *
    2598             :  * @since GDAL 2.1
    2599             :  */
    2600             : 
    2601      362162 : VSILFILE *VSIFOpenExL(const char *pszFilename, const char *pszAccess,
    2602             :                       int bSetError)
    2603             : 
    2604             : {
    2605      362162 :     return VSIFOpenEx2L(pszFilename, pszAccess, bSetError, nullptr);
    2606             : }
    2607             : 
    2608             : /************************************************************************/
    2609             : /*                            VSIFOpenEx2L()                            */
    2610             : /************************************************************************/
    2611             : 
    2612             : /**
    2613             :  * \brief Open/create file.
    2614             :  *
    2615             :  * This function opens (or creates) a file with the desired access.
    2616             :  * Binary access is always implied and
    2617             :  * the "b" does not need to be included in the pszAccess string.
    2618             :  *
    2619             :  * Note that the "VSILFILE *" returned by this function is
    2620             :  * *NOT* a standard C library FILE *, and cannot be used with any functions
    2621             :  * other than the "VSI*L" family of functions.  They aren't "real" FILE objects.
    2622             :  *
    2623             :  * On windows it is possible to define the configuration option
    2624             :  * GDAL_FILE_IS_UTF8 to have pszFilename treated as being in the local
    2625             :  * encoding instead of UTF-8, restoring the pre-1.8.0 behavior of VSIFOpenL().
    2626             :  *
    2627             :  * This method goes through the VSIFileHandler virtualization and may
    2628             :  * work on unusual filesystems such as in memory.
    2629             :  *
    2630             :  * The following options are supported:
    2631             :  * <ul>
    2632             :  * <li>MIME headers such as Content-Type and Content-Encoding
    2633             :  * are supported for the /vsis3/, /vsigs/, /vsiaz/, /vsiadls/ file systems.</li>
    2634             :  * <li>DISABLE_READDIR_ON_OPEN=YES/NO (GDAL >= 3.6) for /vsicurl/ and other
    2635             :  * network-based file systems. By default, directory file listing is done,
    2636             :  * unless YES is specified.</li>
    2637             :  * <li>WRITE_THROUGH=YES (GDAL >= 3.8) for the Windows regular files to
    2638             :  * set the FILE_FLAG_WRITE_THROUGH flag to the CreateFile() function. In that
    2639             :  * mode, the data is written to the system cache but is flushed to disk without
    2640             :  * delay.</li>
    2641             :  * </ul>
    2642             :  *
    2643             :  * Options specifics to /vsis3/, /vsigs/, /vsioss/ and /vsiaz/ in "w" mode:
    2644             :  * <ul>
    2645             :  * <li>CHUNK_SIZE=val in MiB. (GDAL >= 3.10) Size of a block. Default is 50 MiB.
    2646             :  * For /vsis3/, /vsigz/, /vsioss/, it can be up to 5000 MiB.
    2647             :  * For /vsiaz/, only taken into account when BLOB_TYPE=BLOCK. It can be up to 4000 MiB.
    2648             :  * </li>
    2649             :  * </ul>
    2650             :  *
    2651             :  * Options specifics to /vsiaz/ in "w" mode:
    2652             :  * <ul>
    2653             :  * <li>BLOB_TYPE=APPEND/BLOCK. (GDAL >= 3.10) Type of blob. Defaults to APPEND.
    2654             :  * Append blocks are limited to 195 GiB
    2655             :  * (however if the file size is below 4 MiB, a block blob will be created in a
    2656             :  * single PUT operation)
    2657             :  * </li>
    2658             :  * </ul>
    2659             :  *
    2660             :  * Analog of the POSIX fopen() function.
    2661             :  *
    2662             :  * @param pszFilename the file to open.  UTF-8 encoded.
    2663             :  * @param pszAccess access requested (i.e. "r", "r+", "w")
    2664             :  * @param bSetError flag determining whether or not this open call
    2665             :  * should set VSIErrors on failure.
    2666             :  * @param papszOptions NULL or NULL-terminated list of strings. The content is
    2667             :  *                     highly file system dependent.
    2668             :  *
    2669             :  *
    2670             :  * @return NULL on failure, or the file handle.
    2671             :  *
    2672             :  * @since GDAL 3.3
    2673             :  */
    2674             : 
    2675      391092 : VSILFILE *VSIFOpenEx2L(const char *pszFilename, const char *pszAccess,
    2676             :                        int bSetError, CSLConstList papszOptions)
    2677             : 
    2678             : {
    2679             :     // Too long filenames can cause excessive memory allocation due to
    2680             :     // recursion in some filesystem handlers
    2681      391092 :     constexpr size_t knMaxPath = 8192;
    2682      391092 :     if (CPLStrnlen(pszFilename, knMaxPath) == knMaxPath)
    2683           0 :         return nullptr;
    2684             : 
    2685      391142 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    2686             : 
    2687      391158 :     VSILFILE *fp = poFSHandler->Open(pszFilename, pszAccess,
    2688      391186 :                                      CPL_TO_BOOL(bSetError), papszOptions);
    2689             : 
    2690             :     VSIDebug4("VSIFOpenEx2L(%s,%s,%d) = %p", pszFilename, pszAccess, bSetError,
    2691             :               fp);
    2692             : 
    2693      391161 :     return fp;
    2694             : }
    2695             : 
    2696             : /************************************************************************/
    2697             : /*                             VSIFCloseL()                             */
    2698             : /************************************************************************/
    2699             : 
    2700             : /**
    2701             :  * \fn VSIVirtualHandle::Close()
    2702             :  * \brief Close file.
    2703             :  *
    2704             :  * This function closes the indicated file.
    2705             :  *
    2706             :  * This method goes through the VSIFileHandler virtualization and may
    2707             :  * work on unusual filesystems such as in memory.
    2708             :  *
    2709             :  * Analog of the POSIX fclose() function.
    2710             :  *
    2711             :  * @return 0 on success or -1 on failure.
    2712             :  */
    2713             : 
    2714             : /**
    2715             :  * \brief Close file.
    2716             :  *
    2717             :  * This function closes the indicated file.
    2718             :  *
    2719             :  * This method goes through the VSIFileHandler virtualization and may
    2720             :  * work on unusual filesystems such as in memory.
    2721             :  *
    2722             :  * Analog of the POSIX fclose() function.
    2723             :  *
    2724             :  * @param fp file handle opened with VSIFOpenL().  Passing a nullptr produces
    2725             :  * undefined behavior.
    2726             :  *
    2727             :  * @return 0 on success or -1 on failure.
    2728             :  */
    2729             : 
    2730      282198 : int VSIFCloseL(VSILFILE *fp)
    2731             : 
    2732             : {
    2733             :     VSIDebug1("VSIFCloseL(%p)", fp);
    2734             : 
    2735      282198 :     const int nResult = fp->Close();
    2736             : 
    2737      281845 :     delete fp;
    2738             : 
    2739      282378 :     return nResult;
    2740             : }
    2741             : 
    2742             : /************************************************************************/
    2743             : /*                             VSIFSeekL()                              */
    2744             : /************************************************************************/
    2745             : 
    2746             : /**
    2747             :  * \fn int VSIVirtualHandle::Seek( vsi_l_offset nOffset, int nWhence )
    2748             :  * \brief Seek to requested offset.
    2749             :  *
    2750             :  * Seek to the desired offset (nOffset) in the indicated file.
    2751             :  *
    2752             :  * This method goes through the VSIFileHandler virtualization and may
    2753             :  * work on unusual filesystems such as in memory.
    2754             :  *
    2755             :  * Analog of the POSIX fseek() call.
    2756             :  *
    2757             :  * Caution: vsi_l_offset is a unsigned type, so SEEK_CUR can only be used
    2758             :  * for positive seek. If negative seek is needed, use
    2759             :  * handle->Seek( handle->Tell() + negative_offset, SEEK_SET ).
    2760             :  *
    2761             :  * @param nOffset offset in bytes.
    2762             :  * @param nWhence one of SEEK_SET, SEEK_CUR or SEEK_END.
    2763             :  *
    2764             :  * @return 0 on success or -1 one failure.
    2765             :  */
    2766             : 
    2767             : /**
    2768             :  * \brief Seek to requested offset.
    2769             :  *
    2770             :  * Seek to the desired offset (nOffset) in the indicated file.
    2771             :  *
    2772             :  * This method goes through the VSIFileHandler virtualization and may
    2773             :  * work on unusual filesystems such as in memory.
    2774             :  *
    2775             :  * Analog of the POSIX fseek() call.
    2776             :  *
    2777             :  * Caution: vsi_l_offset is a unsigned type, so SEEK_CUR can only be used
    2778             :  * for positive seek. If negative seek is needed, use
    2779             :  * VSIFSeekL( fp, VSIFTellL(fp) + negative_offset, SEEK_SET ).
    2780             :  *
    2781             :  * @param fp file handle opened with VSIFOpenL().
    2782             :  * @param nOffset offset in bytes.
    2783             :  * @param nWhence one of SEEK_SET, SEEK_CUR or SEEK_END.
    2784             :  *
    2785             :  * @return 0 on success or -1 one failure.
    2786             :  */
    2787             : 
    2788     8232090 : int VSIFSeekL(VSILFILE *fp, vsi_l_offset nOffset, int nWhence)
    2789             : 
    2790             : {
    2791     8232090 :     return fp->Seek(nOffset, nWhence);
    2792             : }
    2793             : 
    2794             : /************************************************************************/
    2795             : /*                             VSIFTellL()                              */
    2796             : /************************************************************************/
    2797             : 
    2798             : /**
    2799             :  * \fn VSIVirtualHandle::Tell()
    2800             :  * \brief Tell current file offset.
    2801             :  *
    2802             :  * Returns the current file read/write offset in bytes from the beginning of
    2803             :  * the file.
    2804             :  *
    2805             :  * This method goes through the VSIFileHandler virtualization and may
    2806             :  * work on unusual filesystems such as in memory.
    2807             :  *
    2808             :  * Analog of the POSIX ftell() call.
    2809             :  *
    2810             :  * @return file offset in bytes.
    2811             :  */
    2812             : 
    2813             : /**
    2814             :  * \brief Tell current file offset.
    2815             :  *
    2816             :  * Returns the current file read/write offset in bytes from the beginning of
    2817             :  * the file.
    2818             :  *
    2819             :  * This method goes through the VSIFileHandler virtualization and may
    2820             :  * work on unusual filesystems such as in memory.
    2821             :  *
    2822             :  * Analog of the POSIX ftell() call.
    2823             :  *
    2824             :  * @param fp file handle opened with VSIFOpenL().
    2825             :  *
    2826             :  * @return file offset in bytes.
    2827             :  */
    2828             : 
    2829     5563660 : vsi_l_offset VSIFTellL(VSILFILE *fp)
    2830             : 
    2831             : {
    2832     5563660 :     return fp->Tell();
    2833             : }
    2834             : 
    2835             : /************************************************************************/
    2836             : /*                             VSIRewindL()                             */
    2837             : /************************************************************************/
    2838             : 
    2839             : /**
    2840             :  * \brief Rewind the file pointer to the beginning of the file.
    2841             :  *
    2842             :  * This is equivalent to VSIFSeekL( fp, 0, SEEK_SET )
    2843             :  *
    2844             :  * Analog of the POSIX rewind() call.
    2845             :  *
    2846             :  * @param fp file handle opened with VSIFOpenL().
    2847             :  */
    2848             : 
    2849       80346 : void VSIRewindL(VSILFILE *fp)
    2850             : 
    2851             : {
    2852       80346 :     CPL_IGNORE_RET_VAL(VSIFSeekL(fp, 0, SEEK_SET));
    2853       80351 : }
    2854             : 
    2855             : /************************************************************************/
    2856             : /*                             VSIFFlushL()                             */
    2857             : /************************************************************************/
    2858             : 
    2859             : /**
    2860             :  * \fn VSIVirtualHandle::Flush()
    2861             :  * \brief Flush pending writes to disk.
    2862             :  *
    2863             :  * For files in write or update mode and on filesystem types where it is
    2864             :  * applicable, all pending output on the file is flushed to the physical disk.
    2865             :  *
    2866             :  * This method goes through the VSIFileHandler virtualization and may
    2867             :  * work on unusual filesystems such as in memory.
    2868             :  *
    2869             :  * Analog of the POSIX fflush() call.
    2870             :  *
    2871             :  * On Windows regular files, this method does nothing, unless the
    2872             :  * VSI_FLUSH configuration option is set to YES (and only when the file has
    2873             :  * *not* been opened with the WRITE_THROUGH option).
    2874             :  *
    2875             :  * @return 0 on success or -1 on error.
    2876             :  */
    2877             : 
    2878             : /**
    2879             :  * \brief Flush pending writes to disk.
    2880             :  *
    2881             :  * For files in write or update mode and on filesystem types where it is
    2882             :  * applicable, all pending output on the file is flushed to the physical disk.
    2883             :  *
    2884             :  * This method goes through the VSIFileHandler virtualization and may
    2885             :  * work on unusual filesystems such as in memory.
    2886             :  *
    2887             :  * Analog of the POSIX fflush() call.
    2888             :  *
    2889             :  * On Windows regular files, this method does nothing, unless the
    2890             :  * VSI_FLUSH configuration option is set to YES (and only when the file has
    2891             :  * *not* been opened with the WRITE_THROUGH option).
    2892             :  *
    2893             :  * @param fp file handle opened with VSIFOpenL().
    2894             :  *
    2895             :  * @return 0 on success or -1 on error.
    2896             :  */
    2897             : 
    2898       69102 : int VSIFFlushL(VSILFILE *fp)
    2899             : 
    2900             : {
    2901       69102 :     return fp->Flush();
    2902             : }
    2903             : 
    2904             : /************************************************************************/
    2905             : /*                             VSIFReadL()                              */
    2906             : /************************************************************************/
    2907             : 
    2908             : /**
    2909             :  * \fn VSIVirtualHandle::Read( void *pBuffer, size_t nSize, size_t nCount )
    2910             :  * \brief Read bytes from file.
    2911             :  *
    2912             :  * Reads nCount objects of nSize bytes from the indicated file at the
    2913             :  * current offset into the indicated buffer.
    2914             :  *
    2915             :  * This method goes through the VSIFileHandler virtualization and may
    2916             :  * work on unusual filesystems such as in memory.
    2917             :  *
    2918             :  * Analog of the POSIX fread() call.
    2919             :  *
    2920             :  * @param pBuffer the buffer into which the data should be read (at least
    2921             :  * nCount * nSize bytes in size.
    2922             :  * @param nSize size of objects to read in bytes.
    2923             :  * @param nCount number of objects to read.
    2924             :  *
    2925             :  * @return number of objects successfully read. If that number is less than
    2926             :  * nCount, VSIFEofL() or VSIFErrorL() can be used to determine the reason for
    2927             :  * the short read.
    2928             :  */
    2929             : 
    2930             : /**
    2931             :  * \brief Read bytes from file.
    2932             :  *
    2933             :  * Reads nCount objects of nSize bytes from the indicated file at the
    2934             :  * current offset into the indicated buffer.
    2935             :  *
    2936             :  * This method goes through the VSIFileHandler virtualization and may
    2937             :  * work on unusual filesystems such as in memory.
    2938             :  *
    2939             :  * Analog of the POSIX fread() call.
    2940             :  *
    2941             :  * @param pBuffer the buffer into which the data should be read (at least
    2942             :  * nCount * nSize bytes in size.
    2943             :  * @param nSize size of objects to read in bytes.
    2944             :  * @param nCount number of objects to read.
    2945             :  * @param fp file handle opened with VSIFOpenL().
    2946             :  *
    2947             :  * @return number of objects successfully read. If that number is less than
    2948             :  * nCount, VSIFEofL() or VSIFErrorL() can be used to determine the reason for
    2949             :  * the short read.
    2950             :  */
    2951             : 
    2952    14966100 : size_t VSIFReadL(void *pBuffer, size_t nSize, size_t nCount, VSILFILE *fp)
    2953             : 
    2954             : {
    2955    14966100 :     return fp->Read(pBuffer, nSize, nCount);
    2956             : }
    2957             : 
    2958             : /************************************************************************/
    2959             : /*                       VSIFReadMultiRangeL()                          */
    2960             : /************************************************************************/
    2961             : 
    2962             : /**
    2963             :  * \fn VSIVirtualHandle::ReadMultiRange( int nRanges, void ** ppData,
    2964             :  *                                       const vsi_l_offset* panOffsets,
    2965             :  *                                       const size_t* panSizes )
    2966             :  * \brief Read several ranges of bytes from file.
    2967             :  *
    2968             :  * Reads nRanges objects of panSizes[i] bytes from the indicated file at the
    2969             :  * offset panOffsets[i] into the buffer ppData[i].
    2970             :  *
    2971             :  * Ranges must be sorted in ascending start offset, and must not overlap each
    2972             :  * other.
    2973             :  *
    2974             :  * This method goes through the VSIFileHandler virtualization and may
    2975             :  * work on unusual filesystems such as in memory or /vsicurl/.
    2976             :  *
    2977             :  * @param nRanges number of ranges to read.
    2978             :  * @param ppData array of nRanges buffer into which the data should be read
    2979             :  *               (ppData[i] must be at list panSizes[i] bytes).
    2980             :  * @param panOffsets array of nRanges offsets at which the data should be read.
    2981             :  * @param panSizes array of nRanges sizes of objects to read (in bytes).
    2982             :  *
    2983             :  * @return 0 in case of success, -1 otherwise.
    2984             :  * @since GDAL 1.9.0
    2985             :  */
    2986             : 
    2987             : /**
    2988             :  * \brief Read several ranges of bytes from file.
    2989             :  *
    2990             :  * Reads nRanges objects of panSizes[i] bytes from the indicated file at the
    2991             :  * offset panOffsets[i] into the buffer ppData[i].
    2992             :  *
    2993             :  * Ranges must be sorted in ascending start offset, and must not overlap each
    2994             :  * other.
    2995             :  *
    2996             :  * This method goes through the VSIFileHandler virtualization and may
    2997             :  * work on unusual filesystems such as in memory or /vsicurl/.
    2998             :  *
    2999             :  * @param nRanges number of ranges to read.
    3000             :  * @param ppData array of nRanges buffer into which the data should be read
    3001             :  *               (ppData[i] must be at list panSizes[i] bytes).
    3002             :  * @param panOffsets array of nRanges offsets at which the data should be read.
    3003             :  * @param panSizes array of nRanges sizes of objects to read (in bytes).
    3004             :  * @param fp file handle opened with VSIFOpenL().
    3005             :  *
    3006             :  * @return 0 in case of success, -1 otherwise.
    3007             :  * @since GDAL 1.9.0
    3008             :  */
    3009             : 
    3010         604 : int VSIFReadMultiRangeL(int nRanges, void **ppData,
    3011             :                         const vsi_l_offset *panOffsets, const size_t *panSizes,
    3012             :                         VSILFILE *fp)
    3013             : {
    3014         604 :     return fp->ReadMultiRange(nRanges, ppData, panOffsets, panSizes);
    3015             : }
    3016             : 
    3017             : /************************************************************************/
    3018             : /*                             VSIFWriteL()                             */
    3019             : /************************************************************************/
    3020             : 
    3021             : /**
    3022             :  * \fn VSIVirtualHandle::Write( const void *pBuffer,
    3023             :  *                              size_t nSize, size_t nCount )
    3024             :  * \brief Write bytes to file.
    3025             :  *
    3026             :  * Writes nCount objects of nSize bytes to the indicated file at the
    3027             :  * current offset into the indicated buffer.
    3028             :  *
    3029             :  * This method goes through the VSIFileHandler virtualization and may
    3030             :  * work on unusual filesystems such as in memory.
    3031             :  *
    3032             :  * Analog of the POSIX fwrite() call.
    3033             :  *
    3034             :  * @param pBuffer the buffer from which the data should be written (at least
    3035             :  * nCount * nSize bytes in size.
    3036             :  * @param nSize size of objects to write in bytes.
    3037             :  * @param nCount number of objects to write.
    3038             :  *
    3039             :  * @return number of objects successfully written.
    3040             :  */
    3041             : 
    3042             : /**
    3043             :  * \brief Write bytes to file.
    3044             :  *
    3045             :  * Writes nCount objects of nSize bytes to the indicated file at the
    3046             :  * current offset into the indicated buffer.
    3047             :  *
    3048             :  * This method goes through the VSIFileHandler virtualization and may
    3049             :  * work on unusual filesystems such as in memory.
    3050             :  *
    3051             :  * Analog of the POSIX fwrite() call.
    3052             :  *
    3053             :  * @param pBuffer the buffer from which the data should be written (at least
    3054             :  * nCount * nSize bytes in size.
    3055             :  * @param nSize size of objects to write in bytes.
    3056             :  * @param nCount number of objects to write.
    3057             :  * @param fp file handle opened with VSIFOpenL().
    3058             :  *
    3059             :  * @return number of objects successfully written.
    3060             :  */
    3061             : 
    3062     4269970 : size_t VSIFWriteL(const void *pBuffer, size_t nSize, size_t nCount,
    3063             :                   VSILFILE *fp)
    3064             : 
    3065             : {
    3066     4269970 :     return fp->Write(pBuffer, nSize, nCount);
    3067             : }
    3068             : 
    3069             : /************************************************************************/
    3070             : /*                              VSIFEofL()                              */
    3071             : /************************************************************************/
    3072             : 
    3073             : /**
    3074             :  * \fn VSIVirtualHandle::Eof()
    3075             :  * \brief Test for end of file.
    3076             :  *
    3077             :  * Returns TRUE (non-zero) if an end-of-file condition occurred during the
    3078             :  * previous read operation. The end-of-file flag is cleared by a successful
    3079             :  * VSIFSeekL() call, or a call to VSIFClearErrL().
    3080             :  *
    3081             :  * This method goes through the VSIFileHandler virtualization and may
    3082             :  * work on unusual filesystems such as in memory.
    3083             :  *
    3084             :  * Analog of the POSIX feof() call.
    3085             :  *
    3086             :  * @return TRUE if at EOF, else FALSE.
    3087             :  */
    3088             : 
    3089             : /**
    3090             :  * \brief Test for end of file.
    3091             :  *
    3092             :  * Returns TRUE (non-zero) if an end-of-file condition occurred during the
    3093             :  * previous read operation. The end-of-file flag is cleared by a successful
    3094             :  * VSIFSeekL() call, or a call to VSIFClearErrL().
    3095             :  *
    3096             :  * This method goes through the VSIFileHandler virtualization and may
    3097             :  * work on unusual filesystems such as in memory.
    3098             :  *
    3099             :  * Analog of the POSIX feof() call.
    3100             :  *
    3101             :  * @param fp file handle opened with VSIFOpenL().
    3102             :  *
    3103             :  * @return TRUE if at EOF, else FALSE.
    3104             :  */
    3105             : 
    3106      279184 : int VSIFEofL(VSILFILE *fp)
    3107             : 
    3108             : {
    3109      279184 :     return fp->Eof();
    3110             : }
    3111             : 
    3112             : /************************************************************************/
    3113             : /*                            VSIFErrorL()                              */
    3114             : /************************************************************************/
    3115             : 
    3116             : /**
    3117             :  * \fn VSIVirtualHandle::Error()
    3118             :  * \brief Test the error indicator.
    3119             :  *
    3120             :  * Returns TRUE (non-zero) if an error condition occurred during the
    3121             :  * previous read operation. The error indicator is cleared by a call to
    3122             :  * VSIFClearErrL(). Note that a end-of-file situation, reported by VSIFEofL(),
    3123             :  * is *not* an error reported by VSIFErrorL().
    3124             :  *
    3125             :  * This method goes through the VSIFileHandler virtualization and may
    3126             :  * work on unusual filesystems such as in memory.
    3127             :  *
    3128             :  * Analog of the POSIX ferror() call.
    3129             :  *
    3130             :  * @return TRUE if the error indicator is set, else FALSE.
    3131             :  * @since 3.10
    3132             :  */
    3133             : 
    3134             : /**
    3135             :  * \brief Test the error indicator.
    3136             :  *
    3137             :  * Returns TRUE (non-zero) if an error condition occurred during the
    3138             :  * previous read operation. The error indicator is cleared by a call to
    3139             :  * VSIFClearErrL(). Note that a end-of-file situation, reported by VSIFEofL(),
    3140             :  * is *not* an error reported by VSIFErrorL().
    3141             :  *
    3142             :  * This method goes through the VSIFileHandler virtualization and may
    3143             :  * work on unusual filesystems such as in memory.
    3144             :  *
    3145             :  * Analog of the POSIX feof() call.
    3146             :  *
    3147             :  * @param fp file handle opened with VSIFOpenL().
    3148             :  *
    3149             :  * @return TRUE if the error indicator is set, else FALSE.
    3150             :  * @since 3.10
    3151             :  */
    3152             : 
    3153       85964 : int VSIFErrorL(VSILFILE *fp)
    3154             : 
    3155             : {
    3156       85964 :     return fp->Error();
    3157             : }
    3158             : 
    3159             : /************************************************************************/
    3160             : /*                           VSIFClearErrL()                            */
    3161             : /************************************************************************/
    3162             : 
    3163             : /**
    3164             :  * \fn VSIVirtualHandle::ClearErr()
    3165             :  * \brief Reset the error and end-of-file indicators.
    3166             :  *
    3167             :  * This method goes through the VSIFileHandler virtualization and may
    3168             :  * work on unusual filesystems such as in memory.
    3169             :  *
    3170             :  * Analog of the POSIX clearerr() call.
    3171             :  *
    3172             :  * @since 3.10
    3173             :  */
    3174             : 
    3175             : /**
    3176             :  * \brief Reset the error and end-of-file indicators.
    3177             :  *
    3178             :  * This method goes through the VSIFileHandler virtualization and may
    3179             :  * work on unusual filesystems such as in memory.
    3180             :  *
    3181             :  * Analog of the POSIX clearerr() call.
    3182             :  *
    3183             :  * @param fp file handle opened with VSIFOpenL().
    3184             :  *
    3185             :  * @since 3.10
    3186             :  */
    3187             : 
    3188       30037 : void VSIFClearErrL(VSILFILE *fp)
    3189             : 
    3190             : {
    3191       30037 :     fp->ClearErr();
    3192       30037 : }
    3193             : 
    3194             : /************************************************************************/
    3195             : /*                            VSIFTruncateL()                           */
    3196             : /************************************************************************/
    3197             : 
    3198             : /**
    3199             :  * \fn VSIVirtualHandle::Truncate( vsi_l_offset nNewSize )
    3200             :  * \brief Truncate/expand the file to the specified size
    3201             : 
    3202             :  * This method goes through the VSIFileHandler virtualization and may
    3203             :  * work on unusual filesystems such as in memory.
    3204             :  *
    3205             :  * Analog of the POSIX ftruncate() call.
    3206             :  *
    3207             :  * @param nNewSize new size in bytes.
    3208             :  *
    3209             :  * @return 0 on success
    3210             :  * @since GDAL 1.9.0
    3211             :  */
    3212             : 
    3213             : /**
    3214             :  * \brief Truncate/expand the file to the specified size
    3215             : 
    3216             :  * This method goes through the VSIFileHandler virtualization and may
    3217             :  * work on unusual filesystems such as in memory.
    3218             :  *
    3219             :  * Analog of the POSIX ftruncate() call.
    3220             :  *
    3221             :  * @param fp file handle opened with VSIFOpenL().
    3222             :  * @param nNewSize new size in bytes.
    3223             :  *
    3224             :  * @return 0 on success
    3225             :  * @since GDAL 1.9.0
    3226             :  */
    3227             : 
    3228        1215 : int VSIFTruncateL(VSILFILE *fp, vsi_l_offset nNewSize)
    3229             : 
    3230             : {
    3231        1215 :     return fp->Truncate(nNewSize);
    3232             : }
    3233             : 
    3234             : /************************************************************************/
    3235             : /*                            VSIFPrintfL()                             */
    3236             : /************************************************************************/
    3237             : 
    3238             : /**
    3239             :  * \brief Formatted write to file.
    3240             :  *
    3241             :  * Provides fprintf() style formatted output to a VSI*L file.  This formats
    3242             :  * an internal buffer which is written using VSIFWriteL().
    3243             :  *
    3244             :  * Analog of the POSIX fprintf() call.
    3245             :  *
    3246             :  * @param fp file handle opened with VSIFOpenL().
    3247             :  * @param pszFormat the printf() style format string.
    3248             :  *
    3249             :  * @return the number of bytes written or -1 on an error.
    3250             :  */
    3251             : 
    3252       86627 : int VSIFPrintfL(VSILFILE *fp, CPL_FORMAT_STRING(const char *pszFormat), ...)
    3253             : 
    3254             : {
    3255             :     va_list args;
    3256             : 
    3257       86627 :     va_start(args, pszFormat);
    3258       86627 :     CPLString osResult;
    3259       86627 :     osResult.vPrintf(pszFormat, args);
    3260       86627 :     va_end(args);
    3261             : 
    3262             :     return static_cast<int>(
    3263      173254 :         VSIFWriteL(osResult.c_str(), 1, osResult.length(), fp));
    3264             : }
    3265             : 
    3266             : /************************************************************************/
    3267             : /*                 VSIVirtualHandle::Printf()                           */
    3268             : /************************************************************************/
    3269             : 
    3270             : /**
    3271             :  * \brief Formatted write to file.
    3272             :  *
    3273             :  * Provides fprintf() style formatted output to a VSI*L file.  This formats
    3274             :  * an internal buffer which is written using VSIFWriteL().
    3275             :  *
    3276             :  * Analog of the POSIX fprintf() call.
    3277             :  *
    3278             :  * @param pszFormat the printf() style format string.
    3279             :  *
    3280             :  * @return the number of bytes written or -1 on an error.
    3281             :  */
    3282             : 
    3283         770 : int VSIVirtualHandle::Printf(CPL_FORMAT_STRING(const char *pszFormat), ...)
    3284             : {
    3285             :     va_list args;
    3286             : 
    3287         770 :     va_start(args, pszFormat);
    3288         770 :     CPLString osResult;
    3289         770 :     osResult.vPrintf(pszFormat, args);
    3290         770 :     va_end(args);
    3291             : 
    3292        1540 :     return static_cast<int>(Write(osResult.c_str(), 1, osResult.length()));
    3293             : }
    3294             : 
    3295             : /************************************************************************/
    3296             : /*                              VSIFPutcL()                              */
    3297             : /************************************************************************/
    3298             : 
    3299             : // TODO: should we put in conformance with POSIX regarding the return
    3300             : // value. As of today (2015-08-29), no code in GDAL sources actually
    3301             : // check the return value.
    3302             : 
    3303             : /**
    3304             :  * \brief Write a single byte to the file
    3305             :  *
    3306             :  * Writes the character nChar, cast to an unsigned char, to file.
    3307             :  *
    3308             :  * Almost an analog of the POSIX  fputc() call, except that it returns
    3309             :  * the  number of  character  written (1  or 0),  and  not the  (cast)
    3310             :  * character itself or EOF.
    3311             :  *
    3312             :  * @param nChar character to write.
    3313             :  * @param fp file handle opened with VSIFOpenL().
    3314             :  *
    3315             :  * @return 1 in case of success, 0 on error.
    3316             :  */
    3317             : 
    3318        2082 : int VSIFPutcL(int nChar, VSILFILE *fp)
    3319             : 
    3320             : {
    3321        2082 :     const unsigned char cChar = static_cast<unsigned char>(nChar);
    3322        2082 :     return static_cast<int>(VSIFWriteL(&cChar, 1, 1, fp));
    3323             : }
    3324             : 
    3325             : /************************************************************************/
    3326             : /*                        VSIFGetRangeStatusL()                        */
    3327             : /************************************************************************/
    3328             : 
    3329             : /**
    3330             :  * \fn VSIVirtualHandle::GetRangeStatus( vsi_l_offset nOffset,
    3331             :  *                                       vsi_l_offset nLength )
    3332             :  * \brief Return if a given file range contains data or holes filled with zeroes
    3333             :  *
    3334             :  * This uses the filesystem capabilities of querying which regions of
    3335             :  * a sparse file are allocated or not. This is currently only
    3336             :  * implemented for Linux (and no other Unix derivatives) and Windows.
    3337             :  *
    3338             :  * Note: A return of VSI_RANGE_STATUS_DATA doesn't exclude that the
    3339             :  * extent is filled with zeroes! It must be interpreted as "may
    3340             :  * contain non-zero data".
    3341             :  *
    3342             :  * @param nOffset offset of the start of the extent.
    3343             :  * @param nLength extent length.
    3344             :  *
    3345             :  * @return extent status: VSI_RANGE_STATUS_UNKNOWN, VSI_RANGE_STATUS_DATA or
    3346             :  *         VSI_RANGE_STATUS_HOLE
    3347             :  * @since GDAL 2.2
    3348             :  */
    3349             : 
    3350             : /**
    3351             :  * \brief Return if a given file range contains data or holes filled with zeroes
    3352             :  *
    3353             :  * This uses the filesystem capabilities of querying which regions of
    3354             :  * a sparse file are allocated or not. This is currently only
    3355             :  * implemented for Linux (and no other Unix derivatives) and Windows.
    3356             :  *
    3357             :  * Note: A return of VSI_RANGE_STATUS_DATA doesn't exclude that the
    3358             :  * extent is filled with zeroes! It must be interpreted as "may
    3359             :  * contain non-zero data".
    3360             :  *
    3361             :  * @param fp file handle opened with VSIFOpenL().
    3362             :  * @param nOffset offset of the start of the extent.
    3363             :  * @param nLength extent length.
    3364             :  *
    3365             :  * @return extent status: VSI_RANGE_STATUS_UNKNOWN, VSI_RANGE_STATUS_DATA or
    3366             :  *         VSI_RANGE_STATUS_HOLE
    3367             :  * @since GDAL 2.2
    3368             :  */
    3369             : 
    3370         636 : VSIRangeStatus VSIFGetRangeStatusL(VSILFILE *fp, vsi_l_offset nOffset,
    3371             :                                    vsi_l_offset nLength)
    3372             : {
    3373         636 :     return fp->GetRangeStatus(nOffset, nLength);
    3374             : }
    3375             : 
    3376             : /************************************************************************/
    3377             : /*                           VSIIngestFile()                            */
    3378             : /************************************************************************/
    3379             : 
    3380             : /**
    3381             :  * \brief Ingest a file into memory.
    3382             :  *
    3383             :  * Read the whole content of a file into a memory buffer.
    3384             :  *
    3385             :  * Either fp or pszFilename can be NULL, but not both at the same time.
    3386             :  *
    3387             :  * If fp is passed non-NULL, it is the responsibility of the caller to
    3388             :  * close it.
    3389             :  *
    3390             :  * If non-NULL, the returned buffer is guaranteed to be NUL-terminated.
    3391             :  *
    3392             :  * @param fp file handle opened with VSIFOpenL().
    3393             :  * @param pszFilename filename.
    3394             :  * @param ppabyRet pointer to the target buffer. *ppabyRet must be freed with
    3395             :  *                 VSIFree()
    3396             :  * @param pnSize pointer to variable to store the file size. May be NULL.
    3397             :  * @param nMaxSize maximum size of file allowed. If no limit, set to a negative
    3398             :  *                 value.
    3399             :  *
    3400             :  * @return TRUE in case of success.
    3401             :  *
    3402             :  * @since GDAL 1.11
    3403             :  */
    3404             : 
    3405        9533 : int VSIIngestFile(VSILFILE *fp, const char *pszFilename, GByte **ppabyRet,
    3406             :                   vsi_l_offset *pnSize, GIntBig nMaxSize)
    3407             : {
    3408        9533 :     if (fp == nullptr && pszFilename == nullptr)
    3409           0 :         return FALSE;
    3410        9533 :     if (ppabyRet == nullptr)
    3411           0 :         return FALSE;
    3412             : 
    3413        9533 :     *ppabyRet = nullptr;
    3414        9533 :     if (pnSize != nullptr)
    3415        2818 :         *pnSize = 0;
    3416             : 
    3417        9533 :     bool bFreeFP = false;
    3418        9533 :     if (nullptr == fp)
    3419             :     {
    3420        8363 :         fp = VSIFOpenL(pszFilename, "rb");
    3421        8363 :         if (nullptr == fp)
    3422             :         {
    3423         366 :             CPLError(CE_Failure, CPLE_FileIO, "Cannot open file '%s'",
    3424             :                      pszFilename);
    3425         366 :             return FALSE;
    3426             :         }
    3427        7997 :         bFreeFP = true;
    3428             :     }
    3429             :     else
    3430             :     {
    3431        1170 :         if (VSIFSeekL(fp, 0, SEEK_SET) != 0)
    3432           0 :             return FALSE;
    3433             :     }
    3434             : 
    3435        9167 :     vsi_l_offset nDataLen = 0;
    3436             : 
    3437        9167 :     if (pszFilename == nullptr || strcmp(pszFilename, "/vsistdin/") == 0)
    3438             :     {
    3439         100 :         vsi_l_offset nDataAlloc = 0;
    3440         100 :         if (VSIFSeekL(fp, 0, SEEK_SET) != 0)
    3441             :         {
    3442           0 :             if (bFreeFP)
    3443           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3444           0 :             return FALSE;
    3445             :         }
    3446             :         while (true)
    3447             :         {
    3448         507 :             if (nDataLen + 8192 + 1 > nDataAlloc)
    3449             :             {
    3450         233 :                 nDataAlloc = (nDataAlloc * 4) / 3 + 8192 + 1;
    3451             :                 if (nDataAlloc >
    3452             :                     static_cast<vsi_l_offset>(static_cast<size_t>(nDataAlloc)))
    3453             :                 {
    3454             :                     CPLError(CE_Failure, CPLE_AppDefined,
    3455             :                              "Input file too large to be opened");
    3456             :                     VSIFree(*ppabyRet);
    3457             :                     *ppabyRet = nullptr;
    3458             :                     if (bFreeFP)
    3459             :                         CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3460             :                     return FALSE;
    3461             :                 }
    3462             :                 GByte *pabyNew = static_cast<GByte *>(
    3463         233 :                     VSIRealloc(*ppabyRet, static_cast<size_t>(nDataAlloc)));
    3464         233 :                 if (pabyNew == nullptr)
    3465             :                 {
    3466           0 :                     CPLError(CE_Failure, CPLE_OutOfMemory,
    3467             :                              "Cannot allocate " CPL_FRMT_GIB " bytes",
    3468             :                              nDataAlloc);
    3469           0 :                     VSIFree(*ppabyRet);
    3470           0 :                     *ppabyRet = nullptr;
    3471           0 :                     if (bFreeFP)
    3472           0 :                         CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3473           0 :                     return FALSE;
    3474             :                 }
    3475         233 :                 *ppabyRet = pabyNew;
    3476             :             }
    3477             :             const int nRead =
    3478         507 :                 static_cast<int>(VSIFReadL(*ppabyRet + nDataLen, 1, 8192, fp));
    3479         507 :             nDataLen += nRead;
    3480             : 
    3481         507 :             if (nMaxSize >= 0 && nDataLen > static_cast<vsi_l_offset>(nMaxSize))
    3482             :             {
    3483           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
    3484             :                          "Input file too large to be opened");
    3485           0 :                 VSIFree(*ppabyRet);
    3486           0 :                 *ppabyRet = nullptr;
    3487           0 :                 if (pnSize != nullptr)
    3488           0 :                     *pnSize = 0;
    3489           0 :                 if (bFreeFP)
    3490           0 :                     CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3491           0 :                 return FALSE;
    3492             :             }
    3493             : 
    3494         507 :             if (pnSize != nullptr)
    3495         245 :                 *pnSize += nRead;
    3496         507 :             (*ppabyRet)[nDataLen] = '\0';
    3497         507 :             if (nRead == 0)
    3498         100 :                 break;
    3499         507 :         }
    3500             :     }
    3501             :     else
    3502             :     {
    3503        9067 :         if (VSIFSeekL(fp, 0, SEEK_END) != 0)
    3504             :         {
    3505           0 :             if (bFreeFP)
    3506           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3507           0 :             return FALSE;
    3508             :         }
    3509        9067 :         nDataLen = VSIFTellL(fp);
    3510             : 
    3511             :         // With "large" VSI I/O API we can read data chunks larger than
    3512             :         // VSIMalloc could allocate. Catch it here.
    3513        9067 :         if (nDataLen !=
    3514             :                 static_cast<vsi_l_offset>(static_cast<size_t>(nDataLen)) ||
    3515             :             nDataLen + 1 < nDataLen
    3516             :             // opening a directory returns nDataLen = INT_MAX (on 32bit) or
    3517             :             // INT64_MAX (on 64bit)
    3518       12872 :             || nDataLen + 1 > std::numeric_limits<size_t>::max() / 2 ||
    3519        3805 :             (nMaxSize >= 0 && nDataLen > static_cast<vsi_l_offset>(nMaxSize)))
    3520             :         {
    3521           0 :             CPLError(CE_Failure, CPLE_AppDefined,
    3522             :                      "Input file too large to be opened");
    3523           0 :             if (bFreeFP)
    3524           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3525           0 :             return FALSE;
    3526             :         }
    3527             : 
    3528        9067 :         if (VSIFSeekL(fp, 0, SEEK_SET) != 0)
    3529             :         {
    3530           0 :             if (bFreeFP)
    3531           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3532           0 :             return FALSE;
    3533             :         }
    3534             : 
    3535        9067 :         *ppabyRet =
    3536        9067 :             static_cast<GByte *>(VSIMalloc(static_cast<size_t>(nDataLen + 1)));
    3537        9067 :         if (nullptr == *ppabyRet)
    3538             :         {
    3539           0 :             CPLError(CE_Failure, CPLE_OutOfMemory,
    3540             :                      "Cannot allocate " CPL_FRMT_GIB " bytes", nDataLen + 1);
    3541           0 :             if (bFreeFP)
    3542           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3543           0 :             return FALSE;
    3544             :         }
    3545             : 
    3546        9067 :         (*ppabyRet)[nDataLen] = '\0';
    3547        9067 :         if (nDataLen !=
    3548        9067 :             VSIFReadL(*ppabyRet, 1, static_cast<size_t>(nDataLen), fp))
    3549             :         {
    3550           0 :             CPLError(CE_Failure, CPLE_FileIO,
    3551             :                      "Cannot read " CPL_FRMT_GIB " bytes", nDataLen);
    3552           0 :             VSIFree(*ppabyRet);
    3553           0 :             *ppabyRet = nullptr;
    3554           0 :             if (bFreeFP)
    3555           0 :                 CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3556           0 :             return FALSE;
    3557             :         }
    3558        9067 :         if (pnSize != nullptr)
    3559        2462 :             *pnSize = nDataLen;
    3560             :     }
    3561        9167 :     if (bFreeFP)
    3562        7997 :         CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
    3563        9167 :     return TRUE;
    3564             : }
    3565             : 
    3566             : /************************************************************************/
    3567             : /*                         VSIOverwriteFile()                           */
    3568             : /************************************************************************/
    3569             : 
    3570             : /**
    3571             :  * \brief Overwrite an existing file with content from another one
    3572             :  *
    3573             :  * @param fpTarget file handle opened with VSIFOpenL() with "rb+" flag.
    3574             :  * @param pszSourceFilename source filename
    3575             :  *
    3576             :  * @return TRUE in case of success.
    3577             :  *
    3578             :  * @since GDAL 3.1
    3579             :  */
    3580             : 
    3581           4 : int VSIOverwriteFile(VSILFILE *fpTarget, const char *pszSourceFilename)
    3582             : {
    3583           4 :     VSILFILE *fpSource = VSIFOpenL(pszSourceFilename, "rb");
    3584           4 :     if (fpSource == nullptr)
    3585             :     {
    3586           0 :         CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s", pszSourceFilename);
    3587           0 :         return false;
    3588             :     }
    3589             : 
    3590           4 :     const size_t nBufferSize = 4096;
    3591           4 :     void *pBuffer = CPLMalloc(nBufferSize);
    3592           4 :     VSIFSeekL(fpTarget, 0, SEEK_SET);
    3593           4 :     bool bRet = true;
    3594             :     while (true)
    3595             :     {
    3596           4 :         size_t nRead = VSIFReadL(pBuffer, 1, nBufferSize, fpSource);
    3597           4 :         size_t nWritten = VSIFWriteL(pBuffer, 1, nRead, fpTarget);
    3598           4 :         if (nWritten != nRead)
    3599             :         {
    3600           0 :             bRet = false;
    3601           0 :             break;
    3602             :         }
    3603           4 :         if (nRead < nBufferSize)
    3604           4 :             break;
    3605           0 :     }
    3606             : 
    3607           4 :     if (bRet)
    3608             :     {
    3609           4 :         bRet = VSIFTruncateL(fpTarget, VSIFTellL(fpTarget)) == 0;
    3610           4 :         if (!bRet)
    3611             :         {
    3612           0 :             CPLError(CE_Failure, CPLE_FileIO, "Truncation failed");
    3613             :         }
    3614             :     }
    3615             : 
    3616           4 :     CPLFree(pBuffer);
    3617           4 :     VSIFCloseL(fpSource);
    3618           4 :     return bRet;
    3619             : }
    3620             : 
    3621             : /************************************************************************/
    3622             : /*                        VSIFGetNativeFileDescriptorL()                */
    3623             : /************************************************************************/
    3624             : 
    3625             : /**
    3626             :  * \fn VSIVirtualHandle::GetNativeFileDescriptor()
    3627             :  * \brief Returns the "native" file descriptor for the virtual handle.
    3628             :  *
    3629             :  * This will only return a non-NULL value for "real" files handled by the
    3630             :  * operating system (to be opposed to GDAL virtual file systems).
    3631             :  *
    3632             :  * On POSIX systems, this will be a integer value ("fd") cast as a void*.
    3633             :  * On Windows systems, this will be the HANDLE.
    3634             :  *
    3635             :  * @return the native file descriptor, or NULL.
    3636             :  */
    3637             : 
    3638             : /**
    3639             :  * \brief Returns the "native" file descriptor for the virtual handle.
    3640             :  *
    3641             :  * This will only return a non-NULL value for "real" files handled by the
    3642             :  * operating system (to be opposed to GDAL virtual file systems).
    3643             :  *
    3644             :  * On POSIX systems, this will be a integer value ("fd") cast as a void*.
    3645             :  * On Windows systems, this will be the HANDLE.
    3646             :  *
    3647             :  * @param fp file handle opened with VSIFOpenL().
    3648             :  *
    3649             :  * @return the native file descriptor, or NULL.
    3650             :  */
    3651             : 
    3652          64 : void *VSIFGetNativeFileDescriptorL(VSILFILE *fp)
    3653             : {
    3654          64 :     return fp->GetNativeFileDescriptor();
    3655             : }
    3656             : 
    3657             : /************************************************************************/
    3658             : /*                      VSIGetDiskFreeSpace()                           */
    3659             : /************************************************************************/
    3660             : 
    3661             : /**
    3662             :  * \brief Return free disk space available on the filesystem.
    3663             :  *
    3664             :  * This function returns the free disk space available on the filesystem.
    3665             :  *
    3666             :  * @param pszDirname a directory of the filesystem to query.
    3667             :  * @return The free space in bytes. Or -1 in case of error.
    3668             :  * @since GDAL 2.1
    3669             :  */
    3670             : 
    3671          73 : GIntBig VSIGetDiskFreeSpace(const char *pszDirname)
    3672             : {
    3673          73 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszDirname);
    3674             : 
    3675          73 :     return poFSHandler->GetDiskFreeSpace(pszDirname);
    3676             : }
    3677             : 
    3678             : /************************************************************************/
    3679             : /*                    VSIGetFileSystemsPrefixes()                       */
    3680             : /************************************************************************/
    3681             : 
    3682             : /**
    3683             :  * \brief Return the list of prefixes for virtual file system handlers
    3684             :  * currently registered.
    3685             :  *
    3686             :  * Typically: "", "/vsimem/", "/vsicurl/", etc
    3687             :  *
    3688             :  * @return a NULL terminated list of prefixes. Must be freed with CSLDestroy()
    3689             :  * @since GDAL 2.3
    3690             :  */
    3691             : 
    3692           2 : char **VSIGetFileSystemsPrefixes(void)
    3693             : {
    3694           2 :     return VSIFileManager::GetPrefixes();
    3695             : }
    3696             : 
    3697             : /************************************************************************/
    3698             : /*                     VSIGetFileSystemOptions()                        */
    3699             : /************************************************************************/
    3700             : 
    3701             : /**
    3702             :  * \brief Return the list of options associated with a virtual file system
    3703             :  * handler as a serialized XML string.
    3704             :  *
    3705             :  * Those options may be set as configuration options with CPLSetConfigOption().
    3706             :  *
    3707             :  * @param pszFilename a filename, or prefix of a virtual file system handler.
    3708             :  * @return a string, which must not be freed, or NULL if no options is declared.
    3709             :  * @since GDAL 2.3
    3710             :  */
    3711             : 
    3712          33 : const char *VSIGetFileSystemOptions(const char *pszFilename)
    3713             : {
    3714          33 :     VSIFilesystemHandler *poFSHandler = VSIFileManager::GetHandler(pszFilename);
    3715             : 
    3716          33 :     return poFSHandler->GetOptions();
    3717             : }
    3718             : 
    3719             : /************************************************************************/
    3720             : /*                       VSISetPathSpecificOption()                     */
    3721             : /************************************************************************/
    3722             : 
    3723             : static std::mutex oMutexPathSpecificOptions;
    3724             : 
    3725             : // key is a path prefix
    3726             : // value is a map of key, value pair
    3727             : static std::map<std::string, std::map<std::string, std::string>>
    3728             :     oMapPathSpecificOptions;
    3729             : 
    3730             : /**
    3731             :  * \brief Set a credential (or more generally an option related to a
    3732             :  *        virtual file system) for a given path prefix.
    3733             :  * @deprecated in GDAL 3.6 for the better named VSISetPathSpecificOption()
    3734             :  * @see VSISetPathSpecificOption()
    3735             :  */
    3736           0 : void VSISetCredential(const char *pszPathPrefix, const char *pszKey,
    3737             :                       const char *pszValue)
    3738             : {
    3739           0 :     VSISetPathSpecificOption(pszPathPrefix, pszKey, pszValue);
    3740           0 : }
    3741             : 
    3742             : /**
    3743             :  * \brief Set a path specific option for a given path prefix.
    3744             :  *
    3745             :  * Such option is typically, but not limited to, a credential setting for a
    3746             :  * virtual file system.
    3747             :  *
    3748             :  * That option may also be set as a configuration option with
    3749             :  * CPLSetConfigOption(), but this function allows to specify them with a
    3750             :  * granularity at the level of a file path, which makes it easier if using the
    3751             :  * same virtual file system but with different credentials (e.g. different
    3752             :  * credentials for bucket "/vsis3/foo" and "/vsis3/bar")
    3753             :  *
    3754             :  * This is supported for the following virtual file systems:
    3755             :  * /vsis3/, /vsigs/, /vsiaz/, /vsioss/, /vsiwebhdfs, /vsiswift.
    3756             :  * Note: setting them for a path starting with /vsiXXX/ will also apply for
    3757             :  * /vsiXXX_streaming/ requests.
    3758             :  *
    3759             :  * Note that no particular care is taken to store them in RAM in a secure way.
    3760             :  * So they might accidentally hit persistent storage if swapping occurs, or
    3761             :  * someone with access to the memory allocated by the process may be able to
    3762             :  * read them.
    3763             :  *
    3764             :  * @param pszPathPrefix a path prefix of a virtual file system handler.
    3765             :  *                      Typically of the form "/vsiXXX/bucket". Must NOT be
    3766             :  * NULL. Should not include trailing slashes.
    3767             :  * @param pszKey        Option name. Must NOT be NULL.
    3768             :  * @param pszValue      Option value. May be NULL to erase it.
    3769             :  *
    3770             :  * @since GDAL 3.6
    3771             :  */
    3772             : 
    3773          90 : void VSISetPathSpecificOption(const char *pszPathPrefix, const char *pszKey,
    3774             :                               const char *pszValue)
    3775             : {
    3776         180 :     std::lock_guard<std::mutex> oLock(oMutexPathSpecificOptions);
    3777          90 :     auto oIter = oMapPathSpecificOptions.find(pszPathPrefix);
    3778         180 :     CPLString osKey(pszKey);
    3779          90 :     osKey.toupper();
    3780          90 :     if (oIter == oMapPathSpecificOptions.end())
    3781             :     {
    3782          23 :         if (pszValue != nullptr)
    3783          23 :             oMapPathSpecificOptions[pszPathPrefix][osKey] = pszValue;
    3784             :     }
    3785          67 :     else if (pszValue != nullptr)
    3786          59 :         oIter->second[osKey] = pszValue;
    3787             :     else
    3788           8 :         oIter->second.erase(osKey);
    3789          90 : }
    3790             : 
    3791             : /************************************************************************/
    3792             : /*                       VSIClearPathSpecificOptions()                  */
    3793             : /************************************************************************/
    3794             : 
    3795             : /**
    3796             :  * \brief Clear path specific options set with VSISetPathSpecificOption()
    3797             :  * @deprecated in GDAL 3.6 for the better named VSIClearPathSpecificOptions()
    3798             :  * @see VSIClearPathSpecificOptions()
    3799             :  */
    3800           0 : void VSIClearCredentials(const char *pszPathPrefix)
    3801             : {
    3802           0 :     return VSIClearPathSpecificOptions(pszPathPrefix);
    3803             : }
    3804             : 
    3805             : /**
    3806             :  * \brief Clear path specific options set with VSISetPathSpecificOption()
    3807             :  *
    3808             :  * Note that no particular care is taken to remove them from RAM in a secure
    3809             :  * way.
    3810             :  *
    3811             :  * @param pszPathPrefix If set to NULL, all path specific options are cleared.
    3812             :  *                      If set to not-NULL, only those set with
    3813             :  *                      VSISetPathSpecificOption(pszPathPrefix, ...) will be
    3814             :  * cleared.
    3815             :  *
    3816             :  * @since GDAL 3.6
    3817             :  */
    3818          18 : void VSIClearPathSpecificOptions(const char *pszPathPrefix)
    3819             : {
    3820          36 :     std::lock_guard<std::mutex> oLock(oMutexPathSpecificOptions);
    3821          18 :     if (pszPathPrefix == nullptr)
    3822             :     {
    3823           2 :         oMapPathSpecificOptions.clear();
    3824             :     }
    3825             :     else
    3826             :     {
    3827          16 :         oMapPathSpecificOptions.erase(pszPathPrefix);
    3828             :     }
    3829          18 : }
    3830             : 
    3831             : /************************************************************************/
    3832             : /*                        VSIGetPathSpecificOption()                    */
    3833             : /************************************************************************/
    3834             : 
    3835             : /**
    3836             :  * \brief Get the value of a credential (or more generally an option related to
    3837             :  * a virtual file system) for a given path.
    3838             :  * @deprecated in GDAL 3.6 for the better named VSIGetPathSpecificOption()
    3839             :  * @see VSIGetPathSpecificOption()
    3840             :  */
    3841           0 : const char *VSIGetCredential(const char *pszPath, const char *pszKey,
    3842             :                              const char *pszDefault)
    3843             : {
    3844           0 :     return VSIGetPathSpecificOption(pszPath, pszKey, pszDefault);
    3845             : }
    3846             : 
    3847             : /**
    3848             :  * \brief Get the value a path specific option.
    3849             :  *
    3850             :  * Such option is typically, but not limited to, a credential setting for a
    3851             :  * virtual file system.
    3852             :  *
    3853             :  * If no match occurs, CPLGetConfigOption(pszKey, pszDefault) is returned.
    3854             :  *
    3855             :  * Mostly to be used by virtual file system implementations.
    3856             :  *
    3857             :  * @since GDAL 3.6
    3858             :  * @see VSISetPathSpecificOption()
    3859             :  */
    3860      134893 : const char *VSIGetPathSpecificOption(const char *pszPath, const char *pszKey,
    3861             :                                      const char *pszDefault)
    3862             : {
    3863             :     {
    3864      134893 :         std::lock_guard<std::mutex> oLock(oMutexPathSpecificOptions);
    3865      166145 :         for (auto it = oMapPathSpecificOptions.rbegin();
    3866      197397 :              it != oMapPathSpecificOptions.rend(); ++it)
    3867             :         {
    3868       31517 :             if (STARTS_WITH(pszPath, it->first.c_str()))
    3869             :             {
    3870        3177 :                 auto oIter = it->second.find(CPLString(pszKey).toupper());
    3871        3177 :                 if (oIter != it->second.end())
    3872         265 :                     return oIter->second.c_str();
    3873             :             }
    3874             :         }
    3875             :     }
    3876      134628 :     return CPLGetConfigOption(pszKey, pszDefault);
    3877             : }
    3878             : 
    3879             : /************************************************************************/
    3880             : /*                      VSIDuplicateFileSystemHandler()                 */
    3881             : /************************************************************************/
    3882             : 
    3883             : /**
    3884             :  * \brief Duplicate an existing file system handler.
    3885             :  *
    3886             :  * A number of virtual file system for remote object stores use protocols
    3887             :  * identical or close to popular ones (typically AWS S3), but with slightly
    3888             :  * different settings (at the very least the endpoint).
    3889             :  *
    3890             :  * This functions allows to duplicate the source virtual file system handler
    3891             :  * as a new one with a different prefix (when the source virtual file system
    3892             :  * handler supports the duplication operation).
    3893             :  *
    3894             :  * VSISetPathSpecificOption() will typically be called afterwards to change
    3895             :  * configurable settings on the cloned file system handler (e.g. AWS_S3_ENDPOINT
    3896             :  * for a clone of /vsis3/).
    3897             :  *
    3898             :  * @since GDAL 3.7
    3899             :  */
    3900           4 : bool VSIDuplicateFileSystemHandler(const char *pszSourceFSName,
    3901             :                                    const char *pszNewFSName)
    3902             : {
    3903             :     VSIFilesystemHandler *poTargetFSHandler =
    3904           4 :         VSIFileManager::GetHandler(pszNewFSName);
    3905           4 :     if (poTargetFSHandler != VSIFileManager::GetHandler("/"))
    3906             :     {
    3907           1 :         CPLError(CE_Failure, CPLE_AppDefined,
    3908             :                  "%s is already a known virtual file system", pszNewFSName);
    3909           1 :         return false;
    3910             :     }
    3911             : 
    3912             :     VSIFilesystemHandler *poSourceFSHandler =
    3913           3 :         VSIFileManager::GetHandler(pszSourceFSName);
    3914           3 :     if (!poSourceFSHandler)
    3915             :     {
    3916           0 :         CPLError(CE_Failure, CPLE_AppDefined,
    3917             :                  "%s is not a known virtual file system", pszSourceFSName);
    3918           0 :         return false;
    3919             :     }
    3920             : 
    3921           3 :     poTargetFSHandler = poSourceFSHandler->Duplicate(pszNewFSName);
    3922           3 :     if (!poTargetFSHandler)
    3923           2 :         return false;
    3924             : 
    3925           1 :     VSIFileManager::InstallHandler(pszNewFSName, poTargetFSHandler);
    3926           1 :     return true;
    3927             : }
    3928             : 
    3929             : /************************************************************************/
    3930             : /* ==================================================================== */
    3931             : /*                           VSIFileManager()                           */
    3932             : /* ==================================================================== */
    3933             : /************************************************************************/
    3934             : 
    3935             : #ifndef DOXYGEN_SKIP
    3936             : 
    3937             : /*
    3938             : ** Notes on Multithreading:
    3939             : **
    3940             : ** The VSIFileManager maintains a list of file type handlers (mem, large
    3941             : ** file, etc).  It should be thread safe.
    3942             : **/
    3943             : 
    3944             : /************************************************************************/
    3945             : /*                           VSIFileManager()                           */
    3946             : /************************************************************************/
    3947             : 
    3948        1392 : VSIFileManager::VSIFileManager() : poDefaultHandler(nullptr)
    3949             : {
    3950        1392 : }
    3951             : 
    3952             : /************************************************************************/
    3953             : /*                          ~VSIFileManager()                           */
    3954             : /************************************************************************/
    3955             : 
    3956         941 : VSIFileManager::~VSIFileManager()
    3957             : {
    3958        1882 :     std::set<VSIFilesystemHandler *> oSetAlreadyDeleted;
    3959       26350 :     for (std::map<std::string, VSIFilesystemHandler *>::const_iterator iter =
    3960         941 :              oHandlers.begin();
    3961       53641 :          iter != oHandlers.end(); ++iter)
    3962             :     {
    3963       26350 :         if (oSetAlreadyDeleted.find(iter->second) == oSetAlreadyDeleted.end())
    3964             :         {
    3965       24468 :             oSetAlreadyDeleted.insert(iter->second);
    3966       24468 :             delete iter->second;
    3967             :         }
    3968             :     }
    3969             : 
    3970         941 :     delete poDefaultHandler;
    3971         941 : }
    3972             : 
    3973             : /************************************************************************/
    3974             : /*                                Get()                                 */
    3975             : /************************************************************************/
    3976             : 
    3977             : static VSIFileManager *poManager = nullptr;
    3978             : static CPLMutex *hVSIFileManagerMutex = nullptr;
    3979             : 
    3980     2576980 : VSIFileManager *VSIFileManager::Get()
    3981             : {
    3982     5154170 :     CPLMutexHolder oHolder(&hVSIFileManagerMutex);
    3983     2577190 :     if (poManager != nullptr)
    3984             :     {
    3985     2575800 :         return poManager;
    3986             :     }
    3987             : 
    3988        1392 :     poManager = new VSIFileManager;
    3989        1392 :     VSIInstallLargeFileHandler();
    3990        1392 :     VSIInstallSubFileHandler();
    3991        1392 :     VSIInstallMemFileHandler();
    3992             : #ifdef HAVE_LIBZ
    3993        1392 :     VSIInstallGZipFileHandler();
    3994        1392 :     VSIInstallZipFileHandler();
    3995             : #endif
    3996             : #ifdef HAVE_LIBARCHIVE
    3997             :     VSIInstall7zFileHandler();
    3998             :     VSIInstallRarFileHandler();
    3999             : #endif
    4000             : #ifdef HAVE_CURL
    4001        1392 :     VSIInstallCurlFileHandler();
    4002        1392 :     VSIInstallCurlStreamingFileHandler();
    4003        1392 :     VSIInstallS3FileHandler();
    4004        1392 :     VSIInstallS3StreamingFileHandler();
    4005        1392 :     VSIInstallGSFileHandler();
    4006        1392 :     VSIInstallGSStreamingFileHandler();
    4007        1392 :     VSIInstallAzureFileHandler();
    4008        1392 :     VSIInstallAzureStreamingFileHandler();
    4009        1392 :     VSIInstallADLSFileHandler();
    4010        1392 :     VSIInstallOSSFileHandler();
    4011        1392 :     VSIInstallOSSStreamingFileHandler();
    4012        1392 :     VSIInstallSwiftFileHandler();
    4013        1392 :     VSIInstallSwiftStreamingFileHandler();
    4014        1392 :     VSIInstallWebHdfsHandler();
    4015             : #endif
    4016        1392 :     VSIInstallStdinHandler();
    4017        1392 :     VSIInstallHdfsHandler();
    4018        1392 :     VSIInstallStdoutHandler();
    4019        1392 :     VSIInstallSparseFileHandler();
    4020        1392 :     VSIInstallTarFileHandler();
    4021        1392 :     VSIInstallCachedFileHandler();
    4022        1392 :     VSIInstallCryptFileHandler();
    4023             : 
    4024        1392 :     return poManager;
    4025             : }
    4026             : 
    4027             : /************************************************************************/
    4028             : /*                           GetPrefixes()                              */
    4029             : /************************************************************************/
    4030             : 
    4031         628 : char **VSIFileManager::GetPrefixes()
    4032             : {
    4033        1256 :     CPLMutexHolder oHolder(&hVSIFileManagerMutex);
    4034        1256 :     CPLStringList aosList;
    4035       18212 :     for (const auto &oIter : Get()->oHandlers)
    4036             :     {
    4037       17584 :         if (oIter.first != "/vsicurl?")
    4038             :         {
    4039       16956 :             aosList.AddString(oIter.first.c_str());
    4040             :         }
    4041             :     }
    4042        1256 :     return aosList.StealList();
    4043             : }
    4044             : 
    4045             : /************************************************************************/
    4046             : /*                             GetHandler()                             */
    4047             : /************************************************************************/
    4048             : 
    4049     2536070 : VSIFilesystemHandler *VSIFileManager::GetHandler(const char *pszPath)
    4050             : 
    4051             : {
    4052     2536070 :     VSIFileManager *poThis = Get();
    4053     2536200 :     const size_t nPathLen = strlen(pszPath);
    4054             : 
    4055    49587500 :     for (std::map<std::string, VSIFilesystemHandler *>::const_iterator iter =
    4056     2536200 :              poThis->oHandlers.begin();
    4057   101710000 :          iter != poThis->oHandlers.end(); ++iter)
    4058             :     {
    4059    51316500 :         const char *pszIterKey = iter->first.c_str();
    4060    51322900 :         const size_t nIterKeyLen = iter->first.size();
    4061    51330800 :         if (strncmp(pszPath, pszIterKey, nIterKeyLen) == 0)
    4062     1744890 :             return iter->second;
    4063             : 
    4064             :         // "/vsimem\foo" should be handled as "/vsimem/foo".
    4065    49699200 :         if (nIterKeyLen && nPathLen > nIterKeyLen &&
    4066    44819100 :             pszIterKey[nIterKeyLen - 1] == '/' &&
    4067    39018500 :             pszPath[nIterKeyLen - 1] == '\\' &&
    4068          27 :             strncmp(pszPath, pszIterKey, nIterKeyLen - 1) == 0)
    4069           0 :             return iter->second;
    4070             : 
    4071             :         // /vsimem should be treated as a match for /vsimem/.
    4072    49699200 :         if (nPathLen + 1 == nIterKeyLen &&
    4073      504712 :             strncmp(pszPath, pszIterKey, nPathLen) == 0)
    4074      113320 :             return iter->second;
    4075             :     }
    4076             : 
    4077      791197 :     return poThis->poDefaultHandler;
    4078             : }
    4079             : 
    4080             : /************************************************************************/
    4081             : /*                           InstallHandler()                           */
    4082             : /************************************************************************/
    4083             : 
    4084       40360 : void VSIFileManager::InstallHandler(const std::string &osPrefix,
    4085             :                                     VSIFilesystemHandler *poHandler)
    4086             : 
    4087             : {
    4088       40360 :     if (osPrefix == "")
    4089        1392 :         Get()->poDefaultHandler = poHandler;
    4090             :     else
    4091       38968 :         Get()->oHandlers[osPrefix] = poHandler;
    4092       40360 : }
    4093             : 
    4094             : /************************************************************************/
    4095             : /*                          RemoveHandler()                             */
    4096             : /************************************************************************/
    4097             : 
    4098           3 : void VSIFileManager::RemoveHandler(const std::string &osPrefix)
    4099             : {
    4100           3 :     if (osPrefix == "")
    4101           0 :         Get()->poDefaultHandler = nullptr;
    4102             :     else
    4103           3 :         Get()->oHandlers.erase(osPrefix);
    4104           3 : }
    4105             : 
    4106             : /************************************************************************/
    4107             : /*                       VSICleanupFileManager()                        */
    4108             : /************************************************************************/
    4109             : 
    4110         941 : void VSICleanupFileManager()
    4111             : 
    4112             : {
    4113         941 :     if (poManager)
    4114             :     {
    4115         941 :         delete poManager;
    4116         941 :         poManager = nullptr;
    4117             :     }
    4118             : 
    4119         941 :     if (hVSIFileManagerMutex != nullptr)
    4120             :     {
    4121         941 :         CPLDestroyMutex(hVSIFileManagerMutex);
    4122         941 :         hVSIFileManagerMutex = nullptr;
    4123             :     }
    4124             : 
    4125             : #ifdef HAVE_CURL
    4126         941 :     VSICURLDestroyCacheFileProp();
    4127             : #endif
    4128         941 : }
    4129             : 
    4130             : /************************************************************************/
    4131             : /*                            Truncate()                                */
    4132             : /************************************************************************/
    4133             : 
    4134           2 : int VSIVirtualHandle::Truncate(vsi_l_offset nNewSize)
    4135             : {
    4136           2 :     const vsi_l_offset nOriginalPos = Tell();
    4137           2 :     if (Seek(0, SEEK_END) == 0 && nNewSize >= Tell())
    4138             :     {
    4139             :         // Fill with zeroes
    4140           2 :         std::vector<GByte> aoBytes(4096, 0);
    4141           1 :         vsi_l_offset nCurOffset = nOriginalPos;
    4142           3 :         while (nCurOffset < nNewSize)
    4143             :         {
    4144           2 :             constexpr vsi_l_offset nMaxOffset = 4096;
    4145             :             const int nSize =
    4146           2 :                 static_cast<int>(std::min(nMaxOffset, nNewSize - nCurOffset));
    4147           2 :             if (Write(&aoBytes[0], nSize, 1) != 1)
    4148             :             {
    4149           0 :                 Seek(nOriginalPos, SEEK_SET);
    4150           0 :                 return -1;
    4151             :             }
    4152           2 :             nCurOffset += nSize;
    4153             :         }
    4154           1 :         return Seek(nOriginalPos, SEEK_SET) == 0 ? 0 : -1;
    4155             :     }
    4156             : 
    4157           1 :     CPLDebug("VSI", "Truncation is not supported in generic implementation "
    4158             :                     "of Truncate()");
    4159           1 :     Seek(nOriginalPos, SEEK_SET);
    4160           1 :     return -1;
    4161             : }
    4162             : 
    4163             : /************************************************************************/
    4164             : /*                           ReadMultiRange()                           */
    4165             : /************************************************************************/
    4166             : 
    4167         603 : int VSIVirtualHandle::ReadMultiRange(int nRanges, void **ppData,
    4168             :                                      const vsi_l_offset *panOffsets,
    4169             :                                      const size_t *panSizes)
    4170             : {
    4171         603 :     int nRet = 0;
    4172         603 :     const vsi_l_offset nCurOffset = Tell();
    4173       46075 :     for (int i = 0; i < nRanges; i++)
    4174             :     {
    4175       45493 :         if (Seek(panOffsets[i], SEEK_SET) < 0)
    4176             :         {
    4177           0 :             nRet = -1;
    4178           0 :             break;
    4179             :         }
    4180             : 
    4181       45493 :         const size_t nRead = Read(ppData[i], 1, panSizes[i]);
    4182       45493 :         if (panSizes[i] != nRead)
    4183             :         {
    4184          21 :             nRet = -1;
    4185          21 :             break;
    4186             :         }
    4187             :     }
    4188             : 
    4189         603 :     Seek(nCurOffset, SEEK_SET);
    4190             : 
    4191         603 :     return nRet;
    4192             : }
    4193             : 
    4194             : #endif  // #ifndef DOXYGEN_SKIP
    4195             : 
    4196             : /************************************************************************/
    4197             : /*                            HasPRead()                                */
    4198             : /************************************************************************/
    4199             : 
    4200             : /** Returns whether this file handle supports the PRead() method.
    4201             :  *
    4202             :  * @since GDAL 3.6
    4203             :  */
    4204           0 : bool VSIVirtualHandle::HasPRead() const
    4205             : {
    4206           0 :     return false;
    4207             : }
    4208             : 
    4209             : /************************************************************************/
    4210             : /*                             PRead()                                  */
    4211             : /************************************************************************/
    4212             : 
    4213             : /** Do a parallel-compatible read operation.
    4214             :  *
    4215             :  * This methods reads into pBuffer up to nSize bytes starting at offset nOffset
    4216             :  * in the file. The current file offset is not affected by this method.
    4217             :  *
    4218             :  * The implementation is thread-safe: several threads can issue PRead()
    4219             :  * concurrently on the same VSIVirtualHandle object.
    4220             :  *
    4221             :  * This method has the same semantics as pread() Linux operation. It is only
    4222             :  * available if HasPRead() returns true.
    4223             :  *
    4224             :  * @param pBuffer output buffer (must be at least nSize bytes large).
    4225             :  * @param nSize   number of bytes to read in the file.
    4226             :  * @param nOffset file offset from which to read.
    4227             :  * @return number of bytes read.
    4228             :  * @since GDAL 3.6
    4229             :  */
    4230           0 : size_t VSIVirtualHandle::PRead(CPL_UNUSED void *pBuffer,
    4231             :                                CPL_UNUSED size_t nSize,
    4232             :                                CPL_UNUSED vsi_l_offset nOffset) const
    4233             : {
    4234           0 :     return 0;
    4235             : }

Generated by: LCOV version 1.14