LCOV - code coverage report
Current view: top level - apps - gdalalg_vsi_move.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 27 27 100.0 %
Date: 2025-06-19 12:30:01 Functions: 2 2 100.0 %

          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           5 : GDALVSIMoveAlgorithm::GDALVSIMoveAlgorithm()
      29           5 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      30             : {
      31           5 :     AddProgressArg();
      32             :     {
      33             :         auto &arg =
      34          10 :             AddArg("source", 0, _("Source file or directory name"), &m_source)
      35           5 :                 .SetPositional()
      36           5 :                 .SetMinCharCount(1)
      37           5 :                 .SetRequired();
      38           5 :         SetAutoCompleteFunctionForFilename(arg, 0);
      39             :     }
      40             :     {
      41             :         auto &arg =
      42             :             AddArg("destination", 0, _("Destination file or directory name"),
      43          10 :                    &m_destination)
      44           5 :                 .SetPositional()
      45           5 :                 .SetMinCharCount(1)
      46           5 :                 .SetRequired();
      47           5 :         SetAutoCompleteFunctionForFilename(arg, 0);
      48             :     }
      49           5 : }
      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             :         VSIStatBufL statBufSrc;
      62           3 :         VSIErrorReset();
      63           3 :         const auto nOldErrorNum = VSIGetLastErrorNo();
      64           3 :         const bool srcExists = VSIStatL(m_source.c_str(), &statBufSrc) == 0;
      65           3 :         if (!srcExists)
      66             :         {
      67           2 :             if (nOldErrorNum != VSIGetLastErrorNo())
      68             :             {
      69           1 :                 ReportError(CE_Failure, CPLE_FileIO,
      70             :                             "'%s' cannot be accessed. %s: %s", m_source.c_str(),
      71             :                             VSIErrorNumToString(VSIGetLastErrorNo()),
      72             :                             VSIGetLastErrorMsg());
      73             :             }
      74             :             else
      75             :             {
      76           1 :                 ReportError(CE_Failure, CPLE_FileIO,
      77             :                             "'%s' does not exist or cannot be accessed",
      78             :                             m_source.c_str());
      79             :             }
      80             :         }
      81             :         else
      82             :         {
      83           1 :             ReportError(CE_Failure, CPLE_FileIO, "%s could not be moved to %s",
      84             :                         m_source.c_str(), m_destination.c_str());
      85             :         }
      86           3 :         return false;
      87             :     }
      88           1 :     return true;
      89             : }
      90             : 
      91             : //! @endcond

Generated by: LCOV version 1.14