forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgdalalg_vfs_copy.cpp
316 lines (291 loc) · 11.7 KB
/
gdalalg_vfs_copy.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/******************************************************************************
*
* Project: GDAL
* Purpose: gdal "vfs copy" 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_vfs_copy.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include <algorithm>
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
/************************************************************************/
/* GDALVFSCopyAlgorithm::GDALVFSCopyAlgorithm() */
/************************************************************************/
GDALVFSCopyAlgorithm::GDALVFSCopyAlgorithm()
: GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
{
auto &arg =
AddArg("source", 0, _("Source file or directory name"), &m_source)
.SetPositional()
.SetRequired();
SetAutoCompleteFunctionForFilename(arg, 0);
arg.AddValidationAction(
[this]()
{
if (m_source.empty())
{
ReportError(CE_Failure, CPLE_IllegalArg,
"Source filename cannot be empty");
return false;
}
return true;
});
}
{
auto &arg =
AddArg("destination", 0, _("Destination file or directory name"),
&m_destination)
.SetPositional()
.SetRequired();
SetAutoCompleteFunctionForFilename(arg, 0);
arg.AddValidationAction(
[this]()
{
if (m_destination.empty())
{
ReportError(CE_Failure, CPLE_IllegalArg,
"Destination filename cannot be empty");
return false;
}
return true;
});
}
AddArg("recursive", 'r', _("Copy subdirectories recursively"),
&m_recursive);
AddArg("skip-errors", 0, _("Skip errors"), &m_skip);
AddProgressArg();
}
/************************************************************************/
/* GDALVFSCopyAlgorithm::RunImpl() */
/************************************************************************/
bool GDALVFSCopyAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
if (m_recursive || cpl::ends_with(m_source, "/*") ||
cpl::ends_with(m_source, "\\*"))
{
// Make sure that copy -r [srcdir/]lastsubdir targetdir' creates
// targetdir/lastsubdir if targetdir already exists (like cp -r does).
if (m_source.back() == '/')
m_source.pop_back();
if (!cpl::ends_with(m_source, "/*") && !cpl::ends_with(m_source, "\\*"))
{
VSIStatBufL statBufSrc;
bool srcExists =
VSIStatExL(m_source.c_str(), &statBufSrc,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
if (!srcExists)
{
srcExists =
VSIStatExL(
std::string(m_source).append("/").c_str(), &statBufSrc,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
}
VSIStatBufL statBufDst;
const bool dstExists =
VSIStatExL(m_destination.c_str(), &statBufDst,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
if (srcExists && VSI_ISDIR(statBufSrc.st_mode) && dstExists &&
VSI_ISDIR(statBufDst.st_mode))
{
if (m_destination.back() == '/')
m_destination.pop_back();
const auto srcLastSlashPos = m_source.rfind('/');
if (srcLastSlashPos != std::string::npos)
m_destination += m_source.substr(srcLastSlashPos);
else
m_destination = CPLFormFilenameSafe(
m_destination.c_str(), m_source.c_str(), nullptr);
}
}
else
{
m_source.resize(m_source.size() - 2);
}
uint64_t curAmount = 0;
return CopyRecursive(m_source, m_destination, 0, m_recursive ? -1 : 0,
curAmount, 0, pfnProgress, pProgressData);
}
else
{
VSIStatBufL statBufSrc;
bool srcExists =
VSIStatExL(m_source.c_str(), &statBufSrc,
VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
if (!srcExists)
{
ReportError(CE_Failure, CPLE_FileIO, "%s does not exist",
m_source.c_str());
return false;
}
if (VSI_ISDIR(statBufSrc.st_mode))
{
ReportError(CE_Failure, CPLE_FileIO,
"%s is a directory. Use -r/--recursive option",
m_source.c_str());
return false;
}
return CopySingle(m_source, m_destination, ~(static_cast<uint64_t>(0)),
pfnProgress, pProgressData);
}
}
/************************************************************************/
/* GDALVFSCopyAlgorithm::CopySingle() */
/************************************************************************/
bool GDALVFSCopyAlgorithm::CopySingle(const std::string &src,
const std::string &dstIn, uint64_t size,
GDALProgressFunc pfnProgress,
void *pProgressData) const
{
CPLDebug("gdal_vfs_copy", "Copying file %s...", src.c_str());
VSIStatBufL sStat;
std::string dst = dstIn;
const bool bExists =
VSIStatExL(dst.back() == '/' ? dst.c_str()
: std::string(dst).append("/").c_str(),
&sStat, VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
if ((!bExists && dst.back() == '/') ||
(bExists && VSI_ISDIR(sStat.st_mode)))
{
const std::string filename = CPLGetFilename(src.c_str());
dst = CPLFormFilenameSafe(dst.c_str(), filename.c_str(), nullptr);
}
return VSICopyFile(src.c_str(), dst.c_str(), nullptr, size, nullptr,
pfnProgress, pProgressData) == 0 ||
m_skip;
}
/************************************************************************/
/* GDALVFSCopyAlgorithm::CopyRecursive() */
/************************************************************************/
bool GDALVFSCopyAlgorithm::CopyRecursive(const std::string &srcIn,
const std::string &dst, int depth,
int maxdepth, uint64_t &curAmount,
uint64_t totalAmount,
GDALProgressFunc pfnProgress,
void *pProgressData) const
{
std::string src(srcIn);
if (src.back() == '/')
src.pop_back();
if (pfnProgress && depth == 0)
{
CPLDebug("gdal_vfs_copy", "Listing source files...");
std::unique_ptr<VSIDIR, decltype(&VSICloseDir)> dir(
VSIOpenDir(src.c_str(), maxdepth, nullptr), VSICloseDir);
if (dir)
{
while (const auto entry = VSIGetNextDirEntry(dir.get()))
{
if (!(entry->pszName[0] == '.' &&
(entry->pszName[1] == '.' || entry->pszName[1] == 0)))
{
totalAmount += entry->nSize + 1;
if (!pfnProgress(0.0, "", pProgressData))
return false;
}
}
}
}
CPLDebug("gdal_vfs_copy", "Copying directory %s...", src.c_str());
std::unique_ptr<VSIDIR, decltype(&VSICloseDir)> dir(
VSIOpenDir(src.c_str(), 0, nullptr), VSICloseDir);
if (dir)
{
VSIStatBufL sStat;
if (VSIStatL(dst.c_str(), &sStat) != 0)
{
if (VSIMkdir(dst.c_str(), 0755) != 0)
{
ReportError(m_skip ? CE_Warning : CE_Failure, CPLE_FileIO,
"Cannot create directory %s", dst.c_str());
return m_skip;
}
}
while (const auto entry = VSIGetNextDirEntry(dir.get()))
{
if (!(entry->pszName[0] == '.' &&
(entry->pszName[1] == '.' || entry->pszName[1] == 0)))
{
const std::string subsrc =
CPLFormFilenameSafe(src.c_str(), entry->pszName, nullptr);
if (VSI_ISDIR(entry->nMode))
{
const std::string subdest = CPLFormFilenameSafe(
dst.c_str(), entry->pszName, nullptr);
if (maxdepth < 0 || depth < maxdepth)
{
if (!CopyRecursive(subsrc, subdest, depth + 1, maxdepth,
curAmount, totalAmount, pfnProgress,
pProgressData) &&
!m_skip)
{
return false;
}
}
else
{
if (VSIStatL(subdest.c_str(), &sStat) != 0)
{
if (VSIMkdir(subdest.c_str(), 0755) != 0)
{
ReportError(m_skip ? CE_Warning : CE_Failure,
CPLE_FileIO,
"Cannot create directory %s",
subdest.c_str());
if (!m_skip)
return false;
}
}
}
curAmount += 1;
if (pfnProgress &&
!pfnProgress(
std::min(1.0, static_cast<double>(curAmount) /
static_cast<double>(totalAmount)),
"", pProgressData))
{
return false;
}
}
else
{
void *pScaledProgressData = GDALCreateScaledProgress(
static_cast<double>(curAmount) /
static_cast<double>(totalAmount),
std::min(1.0, static_cast<double>(curAmount +
entry->nSize + 1) /
static_cast<double>(totalAmount)),
pfnProgress, pProgressData);
const bool bRet = CopySingle(
subsrc, dst, entry->nSize,
pScaledProgressData ? GDALScaledProgress : nullptr,
pScaledProgressData);
GDALDestroyScaledProgress(pScaledProgressData);
curAmount += entry->nSize + 1;
if (!bRet)
return false;
}
}
}
}
else
{
ReportError(m_skip ? CE_Warning : CE_Failure, CPLE_AppDefined,
"%s is not a directory or cannot be opened", src.c_str());
if (!m_skip)
return false;
}
return true;
}
//! @endcond