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