Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL
4 : * Purpose: gdal "vsi sync" subcommand
5 : * Author: Even Rouault <even dot rouault at spatialys.com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "gdalalg_vsi_sync.h"
14 :
15 : #include "cpl_vsi.h"
16 : #include "cpl_string.h"
17 :
18 : //! @cond Doxygen_Suppress
19 :
20 : #ifndef _
21 : #define _(x) (x)
22 : #endif
23 :
24 : /************************************************************************/
25 : /* GDALVSISyncAlgorithm::GDALVSISyncAlgorithm() */
26 : /************************************************************************/
27 :
28 4 : GDALVSISyncAlgorithm::GDALVSISyncAlgorithm()
29 4 : : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
30 : {
31 4 : AddProgressArg();
32 : {
33 : auto &arg =
34 8 : AddArg("source", 0, _("Source file or directory name"), &m_source)
35 4 : .SetPositional()
36 4 : .SetMinCharCount(1)
37 4 : .SetRequired();
38 4 : SetAutoCompleteFunctionForFilename(arg, 0);
39 : }
40 : {
41 : auto &arg =
42 : AddArg("destination", 0, _("Destination file or directory name"),
43 8 : &m_destination)
44 4 : .SetPositional()
45 4 : .SetMinCharCount(1)
46 4 : .SetRequired();
47 4 : SetAutoCompleteFunctionForFilename(arg, 0);
48 : }
49 :
50 4 : AddArg("recursive", 'r', _("Synchronize recursively"), &m_recursive);
51 :
52 8 : AddArg("strategy", 0, _("Synchronization strategy"), &m_strategy)
53 4 : .SetDefault(m_strategy)
54 4 : .SetChoices("timestamp", "ETag", "overwrite");
55 :
56 4 : AddNumThreadsArg(&m_numThreads, &m_numThreadsStr);
57 4 : }
58 :
59 : /************************************************************************/
60 : /* GDALVSISyncAlgorithm::RunImpl() */
61 : /************************************************************************/
62 :
63 3 : bool GDALVSISyncAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
64 : void *pProgressData)
65 : {
66 6 : CPLStringList aosOptions;
67 3 : aosOptions.SetNameValue("RECURSIVE", m_recursive ? "YES" : "NO");
68 3 : aosOptions.SetNameValue("STRATEGY", m_strategy.c_str());
69 3 : aosOptions.SetNameValue("NUM_THREADS", CPLSPrintf("%d", m_numThreads));
70 :
71 3 : if (!VSISync(m_source.c_str(), m_destination.c_str(), aosOptions.List(),
72 : pfnProgress, pProgressData, nullptr))
73 : {
74 : VSIStatBufL sStat;
75 2 : if (VSIStatExL(m_source.c_str(), &sStat,
76 2 : VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) != 0)
77 : {
78 1 : ReportError(CE_Failure, CPLE_FileIO, "%s does not exist",
79 : m_source.c_str());
80 : }
81 : else
82 : {
83 1 : ReportError(CE_Failure, CPLE_FileIO,
84 : "%s could not be synchronised with %s",
85 : m_source.c_str(), m_destination.c_str());
86 : }
87 2 : return false;
88 : }
89 1 : return true;
90 : }
91 :
92 : //! @endcond
|