@@ -1805,9 +1805,11 @@ CPLErr GDALDriver::DefaultRename(const char *pszNewName, const char *pszOldName)
18051805 /* -------------------------------------------------------------------- */
18061806 /* Collect file list. */
18071807 /* -------------------------------------------------------------------- */
1808- GDALDatasetH hDS = GDALOpen (pszOldName, GA_ReadOnly);
1808+ auto poDS = std::unique_ptr<GDALDataset>(
1809+ GDALDataset::Open (pszOldName, GDAL_OF_ALL | GDAL_OF_VERBOSE_ERROR,
1810+ nullptr , nullptr , nullptr ));
18091811
1810- if (hDS == nullptr )
1812+ if (!poDS )
18111813 {
18121814 if (CPLGetLastErrorNo () == 0 )
18131815 CPLError (CE_Failure, CPLE_OpenFailed,
@@ -1816,11 +1818,11 @@ CPLErr GDALDriver::DefaultRename(const char *pszNewName, const char *pszOldName)
18161818 return CE_Failure;
18171819 }
18181820
1819- char **papszFileList = GDALGetFileList (hDS );
1821+ const CPLStringList aosFileList (poDS-> GetFileList () );
18201822
1821- GDALClose (hDS );
1823+ poDS. reset ( );
18221824
1823- if (CSLCount (papszFileList) == 0 )
1825+ if (aosFileList. empty () )
18241826 {
18251827 CPLError (CE_Failure, CPLE_NotSupported,
18261828 " Unable to determine files associated with %s,\n "
@@ -1835,31 +1837,44 @@ CPLErr GDALDriver::DefaultRename(const char *pszNewName, const char *pszOldName)
18351837 /* names. */
18361838 /* -------------------------------------------------------------------- */
18371839 CPLErr eErr = CE_None;
1838- char **papszNewFileList =
1839- CPLCorrespondingPaths (pszOldName, pszNewName, papszFileList );
1840+ const CPLStringList aosNewFileList (
1841+ CPLCorrespondingPaths (pszOldName, pszNewName, aosFileList. List ()) );
18401842
1841- if (papszNewFileList == nullptr )
1843+ if (aosNewFileList. empty () )
18421844 return CE_Failure;
18431845
1844- for (int i = 0 ; papszFileList[i] != nullptr ; ++i)
1846+ // Guaranteed by CPLCorrespondingPaths()
1847+ CPLAssert (aosNewFileList.size () == aosFileList.size ());
1848+
1849+ VSIStatBufL sStatBuf ;
1850+ if (VSIStatL (pszOldName, &sStatBuf ) == 0 && VSI_ISDIR (sStatBuf .st_mode ) &&
1851+ VSIStatL (pszNewName, &sStatBuf ) != 0 )
18451852 {
1846- if (CPLMoveFile (papszNewFileList[i], papszFileList[i] ) != 0 )
1853+ if (VSIMkdirRecursive (pszNewName, 0755 ) != 0 )
18471854 {
1855+ CPLError (CE_Failure, CPLE_AppDefined,
1856+ " Cannot create directory '%s'" , pszNewName);
1857+ return CE_Failure;
1858+ }
1859+ }
1860+
1861+ for (int i = 0 ; i < aosFileList.size (); ++i)
1862+ {
1863+ if (CPLMoveFile (aosNewFileList[i], aosFileList[i]) != 0 )
1864+ {
1865+ // Above method will have emitted an error in case of failure.
18481866 eErr = CE_Failure;
18491867 // Try to put the ones we moved back.
18501868 for (--i; i >= 0 ; i--)
18511869 {
18521870 // Nothing we can do if the moving back doesn't work...
18531871 CPL_IGNORE_RET_VAL (
1854- CPLMoveFile (papszFileList [i], papszNewFileList [i]));
1872+ CPLMoveFile (aosFileList [i], aosNewFileList [i]));
18551873 }
18561874 break ;
18571875 }
18581876 }
18591877
1860- CSLDestroy (papszNewFileList);
1861- CSLDestroy (papszFileList);
1862-
18631878 return eErr;
18641879}
18651880
@@ -1939,9 +1954,11 @@ CPLErr GDALDriver::DefaultCopyFiles(const char *pszNewName,
19391954 /* -------------------------------------------------------------------- */
19401955 /* Collect file list. */
19411956 /* -------------------------------------------------------------------- */
1942- GDALDatasetH hDS = GDALOpen (pszOldName, GA_ReadOnly);
1957+ auto poDS = std::unique_ptr<GDALDataset>(
1958+ GDALDataset::Open (pszOldName, GDAL_OF_ALL | GDAL_OF_VERBOSE_ERROR,
1959+ nullptr , nullptr , nullptr ));
19431960
1944- if (hDS == nullptr )
1961+ if (!poDS )
19451962 {
19461963 if (CPLGetLastErrorNo () == 0 )
19471964 CPLError (CE_Failure, CPLE_OpenFailed,
@@ -1950,16 +1967,15 @@ CPLErr GDALDriver::DefaultCopyFiles(const char *pszNewName,
19501967 return CE_Failure;
19511968 }
19521969
1953- char **papszFileList = GDALGetFileList (hDS );
1970+ const CPLStringList aosFileList (poDS-> GetFileList () );
19541971
1955- GDALClose (hDS);
1956- hDS = nullptr ;
1972+ poDS.reset ();
19571973
1958- if (CSLCount (papszFileList) == 0 )
1974+ if (aosFileList. empty () )
19591975 {
19601976 CPLError (CE_Failure, CPLE_NotSupported,
19611977 " Unable to determine files associated with %s,\n "
1962- " rename fails." ,
1978+ " copy fails." ,
19631979 pszOldName);
19641980
19651981 return CE_Failure;
@@ -1970,27 +1986,46 @@ CPLErr GDALDriver::DefaultCopyFiles(const char *pszNewName,
19701986 /* names. */
19711987 /* -------------------------------------------------------------------- */
19721988 CPLErr eErr = CE_None;
1973- char **papszNewFileList =
1974- CPLCorrespondingPaths (pszOldName, pszNewName, papszFileList );
1989+ const CPLStringList aosNewFileList (
1990+ CPLCorrespondingPaths (pszOldName, pszNewName, aosFileList. List ()) );
19751991
1976- if (papszNewFileList == nullptr )
1992+ if (aosNewFileList. empty () )
19771993 return CE_Failure;
19781994
1979- for (int i = 0 ; papszFileList[i] != nullptr ; ++i)
1995+ // Guaranteed by CPLCorrespondingPaths()
1996+ CPLAssert (aosNewFileList.size () == aosFileList.size ());
1997+
1998+ VSIStatBufL sStatBuf ;
1999+ if (VSIStatL (pszOldName, &sStatBuf ) == 0 && VSI_ISDIR (sStatBuf .st_mode ) &&
2000+ VSIStatL (pszNewName, &sStatBuf ) != 0 )
19802001 {
1981- if (CPLCopyFile (papszNewFileList[i], papszFileList[i] ) != 0 )
2002+ if (VSIMkdirRecursive (pszNewName, 0755 ) != 0 )
19822003 {
2004+ CPLError (CE_Failure, CPLE_AppDefined,
2005+ " Cannot create directory '%s'" , pszNewName);
2006+ return CE_Failure;
2007+ }
2008+ }
2009+
2010+ for (int i = 0 ; i < aosFileList.size (); ++i)
2011+ {
2012+ if (CPLCopyFile (aosNewFileList[i], aosFileList[i]) != 0 )
2013+ {
2014+ // Above method will have emitted an error in case of failure.
19832015 eErr = CE_Failure;
19842016 // Try to put the ones we moved back.
19852017 for (--i; i >= 0 ; --i)
1986- VSIUnlink (papszNewFileList[i]);
2018+ {
2019+ if (VSIUnlink (aosNewFileList[i]) != 0 )
2020+ {
2021+ CPLError (CE_Warning, CPLE_AppDefined, " Cannot delete '%s'" ,
2022+ aosNewFileList[i]);
2023+ }
2024+ }
19872025 break ;
19882026 }
19892027 }
19902028
1991- CSLDestroy (papszNewFileList);
1992- CSLDestroy (papszFileList);
1993-
19942029 return eErr;
19952030}
19962031
0 commit comments