/src/gdal/apps/gdalalg_vsi_copy.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: gdal "vsi copy" 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_copy.h" |
14 | | |
15 | | #include "cpl_conv.h" |
16 | | #include "cpl_string.h" |
17 | | #include "cpl_vsi.h" |
18 | | #include "cpl_vsi_error.h" |
19 | | |
20 | | #include <algorithm> |
21 | | |
22 | | //! @cond Doxygen_Suppress |
23 | | |
24 | | #ifndef _ |
25 | 0 | #define _(x) (x) |
26 | | #endif |
27 | | |
28 | | /************************************************************************/ |
29 | | /* GDALVSICopyAlgorithm::GDALVSICopyAlgorithm() */ |
30 | | /************************************************************************/ |
31 | | |
32 | | GDALVSICopyAlgorithm::GDALVSICopyAlgorithm() |
33 | 0 | : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) |
34 | 0 | { |
35 | 0 | { |
36 | 0 | auto &arg = |
37 | 0 | AddArg("source", 0, _("Source file or directory name"), &m_source) |
38 | 0 | .SetPositional() |
39 | 0 | .SetMinCharCount(1) |
40 | 0 | .SetRequired(); |
41 | 0 | SetAutoCompleteFunctionForFilename(arg, 0); |
42 | 0 | } |
43 | 0 | { |
44 | 0 | auto &arg = |
45 | 0 | AddArg("destination", 0, _("Destination file or directory name"), |
46 | 0 | &m_destination) |
47 | 0 | .SetPositional() |
48 | 0 | .SetMinCharCount(1) |
49 | 0 | .SetRequired() |
50 | 0 | .AddAction( |
51 | 0 | [this]() |
52 | 0 | { |
53 | | // If outputting to stdout, automatically turn off progress bar |
54 | 0 | if (m_destination == "/vsistdout/") |
55 | 0 | { |
56 | 0 | auto quietArg = GetArg(GDAL_ARG_NAME_QUIET); |
57 | 0 | if (quietArg && quietArg->GetType() == GAAT_BOOLEAN) |
58 | 0 | quietArg->Set(true); |
59 | 0 | } |
60 | 0 | }); |
61 | 0 | SetAutoCompleteFunctionForFilename(arg, 0); |
62 | 0 | } |
63 | |
|
64 | 0 | AddArg("recursive", 'r', _("Copy subdirectories recursively"), |
65 | 0 | &m_recursive); |
66 | |
|
67 | 0 | AddArg("skip-errors", 0, _("Skip errors"), &m_skip); |
68 | 0 | AddProgressArg(); |
69 | 0 | } |
70 | | |
71 | | /************************************************************************/ |
72 | | /* GDALVSICopyAlgorithm::RunImpl() */ |
73 | | /************************************************************************/ |
74 | | |
75 | | bool GDALVSICopyAlgorithm::RunImpl(GDALProgressFunc pfnProgress, |
76 | | void *pProgressData) |
77 | 0 | { |
78 | 0 | const auto ReportSourceNotAccessible = |
79 | 0 | [this](int nOldErrorNum, int nNewErrorNum, const std::string &filename) |
80 | 0 | { |
81 | 0 | if (nOldErrorNum != nNewErrorNum) |
82 | 0 | { |
83 | 0 | ReportError(CE_Failure, CPLE_FileIO, |
84 | 0 | "'%s' cannot be accessed. %s: %s", filename.c_str(), |
85 | 0 | VSIErrorNumToString(nNewErrorNum), |
86 | 0 | VSIGetLastErrorMsg()); |
87 | 0 | } |
88 | 0 | else |
89 | 0 | { |
90 | 0 | ReportError(CE_Failure, CPLE_FileIO, "'%s' cannot be accessed.", |
91 | 0 | filename.c_str()); |
92 | 0 | } |
93 | 0 | }; |
94 | |
|
95 | 0 | if (m_recursive || cpl::ends_with(m_source, "/*") || |
96 | 0 | cpl::ends_with(m_source, "\\*")) |
97 | 0 | { |
98 | | // Make sure that copy -r [srcdir/]lastsubdir targetdir' creates |
99 | | // targetdir/lastsubdir if targetdir already exists (like cp -r does). |
100 | 0 | if (m_source.back() == '/') |
101 | 0 | m_source.pop_back(); |
102 | |
|
103 | 0 | if (!cpl::ends_with(m_source, "/*") && !cpl::ends_with(m_source, "\\*")) |
104 | 0 | { |
105 | 0 | const auto nOldErrorNum = VSIGetLastErrorNo(); |
106 | 0 | VSIErrorReset(); |
107 | |
|
108 | 0 | VSIStatBufL statBufSrc; |
109 | 0 | bool srcExists = VSIStatL(m_source.c_str(), &statBufSrc) == 0; |
110 | 0 | if (!srcExists) |
111 | 0 | { |
112 | 0 | srcExists = VSIStatL(std::string(m_source).append("/").c_str(), |
113 | 0 | &statBufSrc) == 0; |
114 | 0 | } |
115 | 0 | const auto nNewErrorNum = VSIGetLastErrorNo(); |
116 | 0 | VSIStatBufL statBufDst; |
117 | 0 | const bool dstExists = |
118 | 0 | VSIStatExL(m_destination.c_str(), &statBufDst, |
119 | 0 | VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0; |
120 | 0 | if (srcExists && VSI_ISDIR(statBufSrc.st_mode) && dstExists && |
121 | 0 | VSI_ISDIR(statBufDst.st_mode)) |
122 | 0 | { |
123 | 0 | if (m_destination.back() == '/') |
124 | 0 | m_destination.pop_back(); |
125 | 0 | const auto srcLastSlashPos = m_source.rfind('/'); |
126 | 0 | if (srcLastSlashPos != std::string::npos) |
127 | 0 | m_destination += m_source.substr(srcLastSlashPos); |
128 | 0 | else |
129 | 0 | m_destination = CPLFormFilenameSafe( |
130 | 0 | m_destination.c_str(), m_source.c_str(), nullptr); |
131 | 0 | } |
132 | 0 | else if (!srcExists) |
133 | 0 | { |
134 | 0 | ReportSourceNotAccessible(nOldErrorNum, nNewErrorNum, m_source); |
135 | 0 | return false; |
136 | 0 | } |
137 | 0 | } |
138 | 0 | else |
139 | 0 | { |
140 | 0 | const auto nOldErrorNum = VSIGetLastErrorNo(); |
141 | 0 | VSIErrorReset(); |
142 | |
|
143 | 0 | m_source.resize(m_source.size() - 2); |
144 | 0 | VSIStatBufL statBufSrc; |
145 | 0 | bool srcExists = VSIStatL(m_source.c_str(), &statBufSrc) == 0; |
146 | 0 | if (!srcExists) |
147 | 0 | { |
148 | 0 | const auto nNewErrorNum = VSIGetLastErrorNo(); |
149 | 0 | ReportSourceNotAccessible(nOldErrorNum, nNewErrorNum, m_source); |
150 | 0 | return false; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | 0 | uint64_t curAmount = 0; |
155 | 0 | return CopyRecursive(m_source, m_destination, 0, m_recursive ? -1 : 0, |
156 | 0 | curAmount, 0, pfnProgress, pProgressData); |
157 | 0 | } |
158 | 0 | else |
159 | 0 | { |
160 | 0 | const auto nOldErrorNum = VSIGetLastErrorNo(); |
161 | 0 | VSIErrorReset(); |
162 | |
|
163 | 0 | VSIStatBufL statBufSrc; |
164 | 0 | bool srcExists = VSIStatL(m_source.c_str(), &statBufSrc) == 0; |
165 | 0 | if (!srcExists) |
166 | 0 | { |
167 | 0 | const auto nNewErrorNum = VSIGetLastErrorNo(); |
168 | 0 | ReportSourceNotAccessible(nOldErrorNum, nNewErrorNum, m_source); |
169 | 0 | return false; |
170 | 0 | } |
171 | 0 | if (VSI_ISDIR(statBufSrc.st_mode)) |
172 | 0 | { |
173 | 0 | ReportError(CE_Failure, CPLE_FileIO, |
174 | 0 | "%s is a directory. Use -r/--recursive option", |
175 | 0 | m_source.c_str()); |
176 | 0 | return false; |
177 | 0 | } |
178 | | |
179 | 0 | return CopySingle(m_source, m_destination, ~(static_cast<uint64_t>(0)), |
180 | 0 | pfnProgress, pProgressData); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | /************************************************************************/ |
185 | | /* GDALVSICopyAlgorithm::CopySingle() */ |
186 | | /************************************************************************/ |
187 | | |
188 | | bool GDALVSICopyAlgorithm::CopySingle(const std::string &src, |
189 | | const std::string &dstIn, uint64_t size, |
190 | | GDALProgressFunc pfnProgress, |
191 | | void *pProgressData) const |
192 | 0 | { |
193 | 0 | CPLDebug("gdal_vsi_copy", "Copying file %s...", src.c_str()); |
194 | 0 | VSIStatBufL sStat; |
195 | 0 | std::string dst = dstIn; |
196 | 0 | const bool bExists = |
197 | 0 | VSIStatExL(dst.back() == '/' ? dst.c_str() |
198 | 0 | : std::string(dst).append("/").c_str(), |
199 | 0 | &sStat, VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0; |
200 | 0 | if ((!bExists && dst.back() == '/') || |
201 | 0 | (bExists && VSI_ISDIR(sStat.st_mode))) |
202 | 0 | { |
203 | 0 | const std::string filename = CPLGetFilename(src.c_str()); |
204 | 0 | dst = CPLFormFilenameSafe(dst.c_str(), filename.c_str(), nullptr); |
205 | 0 | } |
206 | 0 | return VSICopyFile(src.c_str(), dst.c_str(), nullptr, size, nullptr, |
207 | 0 | pfnProgress, pProgressData) == 0 || |
208 | 0 | m_skip; |
209 | 0 | } |
210 | | |
211 | | /************************************************************************/ |
212 | | /* GDALVSICopyAlgorithm::CopyRecursive() */ |
213 | | /************************************************************************/ |
214 | | |
215 | | bool GDALVSICopyAlgorithm::CopyRecursive(const std::string &srcIn, |
216 | | const std::string &dst, int depth, |
217 | | int maxdepth, uint64_t &curAmount, |
218 | | uint64_t totalAmount, |
219 | | GDALProgressFunc pfnProgress, |
220 | | void *pProgressData) const |
221 | 0 | { |
222 | 0 | std::string src(srcIn); |
223 | 0 | if (src.back() == '/') |
224 | 0 | src.pop_back(); |
225 | |
|
226 | 0 | if (pfnProgress && depth == 0) |
227 | 0 | { |
228 | 0 | CPLDebug("gdal_vsi_copy", "Listing source files..."); |
229 | 0 | std::unique_ptr<VSIDIR, decltype(&VSICloseDir)> dir( |
230 | 0 | VSIOpenDir(src.c_str(), maxdepth, nullptr), VSICloseDir); |
231 | 0 | if (dir) |
232 | 0 | { |
233 | 0 | while (const auto entry = VSIGetNextDirEntry(dir.get())) |
234 | 0 | { |
235 | 0 | if (!(entry->pszName[0] == '.' && |
236 | 0 | (entry->pszName[1] == '.' || entry->pszName[1] == 0))) |
237 | 0 | { |
238 | 0 | totalAmount += entry->nSize + 1; |
239 | 0 | if (!pfnProgress(0.0, "", pProgressData)) |
240 | 0 | return false; |
241 | 0 | } |
242 | 0 | } |
243 | 0 | } |
244 | 0 | } |
245 | 0 | totalAmount = std::max<uint64_t>(1, totalAmount); |
246 | |
|
247 | 0 | CPLDebug("gdal_vsi_copy", "Copying directory %s...", src.c_str()); |
248 | 0 | std::unique_ptr<VSIDIR, decltype(&VSICloseDir)> dir( |
249 | 0 | VSIOpenDir(src.c_str(), 0, nullptr), VSICloseDir); |
250 | 0 | if (dir) |
251 | 0 | { |
252 | 0 | VSIStatBufL sStat; |
253 | 0 | if (VSIStatL(dst.c_str(), &sStat) != 0) |
254 | 0 | { |
255 | 0 | if (VSIMkdir(dst.c_str(), 0755) != 0) |
256 | 0 | { |
257 | 0 | ReportError(m_skip ? CE_Warning : CE_Failure, CPLE_FileIO, |
258 | 0 | "Cannot create directory %s", dst.c_str()); |
259 | 0 | return m_skip; |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | 0 | while (const auto entry = VSIGetNextDirEntry(dir.get())) |
264 | 0 | { |
265 | 0 | if (!(entry->pszName[0] == '.' && |
266 | 0 | (entry->pszName[1] == '.' || entry->pszName[1] == 0))) |
267 | 0 | { |
268 | 0 | const std::string subsrc = |
269 | 0 | CPLFormFilenameSafe(src.c_str(), entry->pszName, nullptr); |
270 | 0 | if (VSI_ISDIR(entry->nMode)) |
271 | 0 | { |
272 | 0 | const std::string subdest = CPLFormFilenameSafe( |
273 | 0 | dst.c_str(), entry->pszName, nullptr); |
274 | 0 | if (maxdepth < 0 || depth < maxdepth) |
275 | 0 | { |
276 | 0 | if (!CopyRecursive(subsrc, subdest, depth + 1, maxdepth, |
277 | 0 | curAmount, totalAmount, pfnProgress, |
278 | 0 | pProgressData) && |
279 | 0 | !m_skip) |
280 | 0 | { |
281 | 0 | return false; |
282 | 0 | } |
283 | 0 | } |
284 | 0 | else |
285 | 0 | { |
286 | 0 | if (VSIStatL(subdest.c_str(), &sStat) != 0) |
287 | 0 | { |
288 | 0 | if (VSIMkdir(subdest.c_str(), 0755) != 0) |
289 | 0 | { |
290 | 0 | ReportError(m_skip ? CE_Warning : CE_Failure, |
291 | 0 | CPLE_FileIO, |
292 | 0 | "Cannot create directory %s", |
293 | 0 | subdest.c_str()); |
294 | 0 | if (!m_skip) |
295 | 0 | return false; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } |
299 | 0 | curAmount += 1; |
300 | |
|
301 | 0 | if (pfnProgress && |
302 | 0 | !pfnProgress( |
303 | 0 | std::min(1.0, static_cast<double>(curAmount) / |
304 | 0 | static_cast<double>(totalAmount)), |
305 | 0 | "", pProgressData)) |
306 | 0 | { |
307 | 0 | return false; |
308 | 0 | } |
309 | 0 | } |
310 | 0 | else |
311 | 0 | { |
312 | 0 | void *pScaledProgressData = GDALCreateScaledProgress( |
313 | 0 | static_cast<double>(curAmount) / |
314 | 0 | static_cast<double>(totalAmount), |
315 | 0 | std::min(1.0, static_cast<double>(curAmount + |
316 | 0 | entry->nSize + 1) / |
317 | 0 | static_cast<double>(totalAmount)), |
318 | 0 | pfnProgress, pProgressData); |
319 | 0 | const bool bRet = CopySingle( |
320 | 0 | subsrc, dst, entry->nSize, |
321 | 0 | pScaledProgressData ? GDALScaledProgress : nullptr, |
322 | 0 | pScaledProgressData); |
323 | 0 | GDALDestroyScaledProgress(pScaledProgressData); |
324 | |
|
325 | 0 | curAmount += entry->nSize + 1; |
326 | |
|
327 | 0 | if (!bRet) |
328 | 0 | return false; |
329 | 0 | } |
330 | 0 | } |
331 | 0 | } |
332 | 0 | } |
333 | 0 | else |
334 | 0 | { |
335 | 0 | ReportError(m_skip ? CE_Warning : CE_Failure, CPLE_AppDefined, |
336 | 0 | "%s is not a directory or cannot be opened", src.c_str()); |
337 | 0 | if (!m_skip) |
338 | 0 | return false; |
339 | 0 | } |
340 | 0 | return true; |
341 | 0 | } |
342 | | |
343 | | //! @endcond |