Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_vsi_sync.cpp
Line
Count
Source
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_string.h"
16
#include "cpl_vsi.h"
17
#include "cpl_vsi_error.h"
18
19
//! @cond Doxygen_Suppress
20
21
#ifndef _
22
0
#define _(x) (x)
23
#endif
24
25
/************************************************************************/
26
/*             GDALVSISyncAlgorithm::GDALVSISyncAlgorithm()             */
27
/************************************************************************/
28
29
GDALVSISyncAlgorithm::GDALVSISyncAlgorithm()
30
0
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
31
0
{
32
0
    AddProgressArg();
33
0
    {
34
0
        auto &arg =
35
0
            AddArg("source", 0, _("Source file or directory name"), &m_source)
36
0
                .SetPositional()
37
0
                .SetMinCharCount(1)
38
0
                .SetRequired();
39
0
        SetAutoCompleteFunctionForFilename(arg, 0);
40
0
    }
41
0
    {
42
0
        auto &arg =
43
0
            AddArg("destination", 0, _("Destination file or directory name"),
44
0
                   &m_destination)
45
0
                .SetPositional()
46
0
                .SetMinCharCount(1)
47
0
                .SetRequired();
48
0
        SetAutoCompleteFunctionForFilename(arg, 0);
49
0
    }
50
51
0
    AddArg("recursive", 'r', _("Synchronize recursively"), &m_recursive);
52
53
0
    AddArg("strategy", 0, _("Synchronization strategy"), &m_strategy)
54
0
        .SetDefault(m_strategy)
55
0
        .SetChoices("timestamp", "ETag", "overwrite");
56
57
0
    AddNumThreadsArg(&m_numThreads, &m_numThreadsStr);
58
0
}
59
60
/************************************************************************/
61
/*                   GDALVSISyncAlgorithm::RunImpl()                    */
62
/************************************************************************/
63
64
bool GDALVSISyncAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
65
                                   void *pProgressData)
66
0
{
67
0
    CPLStringList aosOptions;
68
0
    aosOptions.SetNameValue("RECURSIVE", m_recursive ? "YES" : "NO");
69
0
    aosOptions.SetNameValue("STRATEGY", m_strategy.c_str());
70
0
    aosOptions.SetNameValue("NUM_THREADS", CPLSPrintf("%d", m_numThreads));
71
72
0
    if (!VSISync(m_source.c_str(), m_destination.c_str(), aosOptions.List(),
73
0
                 pfnProgress, pProgressData, nullptr))
74
0
    {
75
0
        VSIStatBufL sStat;
76
0
        VSIErrorReset();
77
0
        const auto nOldErrorNum = VSIGetLastErrorNo();
78
0
        if (VSIStatL(m_source.c_str(), &sStat) != 0)
79
0
        {
80
0
            if (nOldErrorNum != VSIGetLastErrorNo())
81
0
            {
82
0
                ReportError(CE_Failure, CPLE_FileIO,
83
0
                            "'%s' cannot be accessed. %s: %s", m_source.c_str(),
84
0
                            VSIErrorNumToString(VSIGetLastErrorNo()),
85
0
                            VSIGetLastErrorMsg());
86
0
            }
87
0
            else
88
0
            {
89
0
                ReportError(CE_Failure, CPLE_FileIO,
90
0
                            "'%s' does not exist or cannot be accessed",
91
0
                            m_source.c_str());
92
0
            }
93
0
        }
94
0
        else
95
0
        {
96
0
            ReportError(CE_Failure, CPLE_FileIO,
97
0
                        "%s could not be synchronised with %s",
98
0
                        m_source.c_str(), m_destination.c_str());
99
0
        }
100
0
        return false;
101
0
    }
102
0
    return true;
103
0
}
104
105
//! @endcond