File: gdalalg_vsi_sync.cpp

package info (click to toggle)
gdal 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,292 kB
  • sloc: cpp: 1,223,498; ansic: 206,434; python: 26,278; java: 6,001; xml: 4,769; sh: 3,855; cs: 2,513; yacc: 1,306; makefile: 214
file content (105 lines) | stat: -rw-r--r-- 3,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
 *
 * Project:  GDAL
 * Purpose:  gdal "vsi sync" subcommand
 * Author:   Even Rouault <even dot rouault at spatialys.com>
 *
 ******************************************************************************
 * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
 *
 * SPDX-License-Identifier: MIT
 ****************************************************************************/

#include "gdalalg_vsi_sync.h"

#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_vsi_error.h"

//! @cond Doxygen_Suppress

#ifndef _
#define _(x) (x)
#endif

/************************************************************************/
/*              GDALVSISyncAlgorithm::GDALVSISyncAlgorithm()            */
/************************************************************************/

GDALVSISyncAlgorithm::GDALVSISyncAlgorithm()
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
    AddProgressArg();
    {
        auto &arg =
            AddArg("source", 0, _("Source file or directory name"), &m_source)
                .SetPositional()
                .SetMinCharCount(1)
                .SetRequired();
        SetAutoCompleteFunctionForFilename(arg, 0);
    }
    {
        auto &arg =
            AddArg("destination", 0, _("Destination file or directory name"),
                   &m_destination)
                .SetPositional()
                .SetMinCharCount(1)
                .SetRequired();
        SetAutoCompleteFunctionForFilename(arg, 0);
    }

    AddArg("recursive", 'r', _("Synchronize recursively"), &m_recursive);

    AddArg("strategy", 0, _("Synchronization strategy"), &m_strategy)
        .SetDefault(m_strategy)
        .SetChoices("timestamp", "ETag", "overwrite");

    AddNumThreadsArg(&m_numThreads, &m_numThreadsStr);
}

/************************************************************************/
/*                    GDALVSISyncAlgorithm::RunImpl()                   */
/************************************************************************/

bool GDALVSISyncAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
                                   void *pProgressData)
{
    CPLStringList aosOptions;
    aosOptions.SetNameValue("RECURSIVE", m_recursive ? "YES" : "NO");
    aosOptions.SetNameValue("STRATEGY", m_strategy.c_str());
    aosOptions.SetNameValue("NUM_THREADS", CPLSPrintf("%d", m_numThreads));

    if (!VSISync(m_source.c_str(), m_destination.c_str(), aosOptions.List(),
                 pfnProgress, pProgressData, nullptr))
    {
        VSIStatBufL sStat;
        VSIErrorReset();
        const auto nOldErrorNum = VSIGetLastErrorNo();
        if (VSIStatL(m_source.c_str(), &sStat) != 0)
        {
            if (nOldErrorNum != VSIGetLastErrorNo())
            {
                ReportError(CE_Failure, CPLE_FileIO,
                            "'%s' cannot be accessed. %s: %s", m_source.c_str(),
                            VSIErrorNumToString(VSIGetLastErrorNo()),
                            VSIGetLastErrorMsg());
            }
            else
            {
                ReportError(CE_Failure, CPLE_FileIO,
                            "'%s' does not exist or cannot be accessed",
                            m_source.c_str());
            }
        }
        else
        {
            ReportError(CE_Failure, CPLE_FileIO,
                        "%s could not be synchronised with %s",
                        m_source.c_str(), m_destination.c_str());
        }
        return false;
    }
    return true;
}

//! @endcond