Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: gdal "vsi move" 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_move.h" 14 : 15 : #include "cpl_vsi.h" 16 : #include "cpl_vsi_error.h" 17 : 18 : //! @cond Doxygen_Suppress 19 : 20 : #ifndef _ 21 : #define _(x) (x) 22 : #endif 23 : 24 : /************************************************************************/ 25 : /* GDALVSIMoveAlgorithm::GDALVSIMoveAlgorithm() */ 26 : /************************************************************************/ 27 : 28 47 : GDALVSIMoveAlgorithm::GDALVSIMoveAlgorithm() 29 47 : : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) 30 : { 31 47 : AddProgressArg(); 32 : { 33 : auto &arg = 34 94 : AddArg("source", 0, _("Source file or directory name"), &m_source) 35 47 : .SetPositional() 36 47 : .SetMinCharCount(1) 37 47 : .SetRequired(); 38 47 : SetAutoCompleteFunctionForFilename(arg, 0); 39 : } 40 : { 41 : auto &arg = 42 : AddArg("destination", 0, _("Destination file or directory name"), 43 94 : &m_destination) 44 47 : .SetPositional() 45 47 : .SetMinCharCount(1) 46 47 : .SetRequired(); 47 47 : SetAutoCompleteFunctionForFilename(arg, 0); 48 : } 49 47 : } 50 : 51 : /************************************************************************/ 52 : /* GDALVSIMoveAlgorithm::RunImpl() */ 53 : /************************************************************************/ 54 : 55 4 : bool GDALVSIMoveAlgorithm::RunImpl(GDALProgressFunc pfnProgress, 56 : void *pProgressData) 57 : { 58 4 : if (VSIMove(m_source.c_str(), m_destination.c_str(), nullptr, pfnProgress, 59 4 : pProgressData) != 0) 60 : { 61 3 : const auto nOldErrorNum = VSIGetLastErrorNo(); 62 3 : VSIErrorReset(); 63 : 64 : VSIStatBufL statBufSrc; 65 3 : const bool srcExists = VSIStatL(m_source.c_str(), &statBufSrc) == 0; 66 3 : if (!srcExists) 67 : { 68 2 : if (nOldErrorNum != VSIGetLastErrorNo()) 69 : { 70 1 : ReportError(CE_Failure, CPLE_FileIO, 71 : "'%s' cannot be accessed. %s: %s", m_source.c_str(), 72 : VSIErrorNumToString(VSIGetLastErrorNo()), 73 : VSIGetLastErrorMsg()); 74 : } 75 : else 76 : { 77 1 : ReportError(CE_Failure, CPLE_FileIO, 78 : "'%s' does not exist or cannot be accessed", 79 : m_source.c_str()); 80 : } 81 : } 82 : else 83 : { 84 1 : ReportError(CE_Failure, CPLE_FileIO, "%s could not be moved to %s", 85 : m_source.c_str(), m_destination.c_str()); 86 : } 87 3 : return false; 88 : } 89 1 : return true; 90 : } 91 : 92 : //! @endcond