diff --git a/godal.cpp b/godal.cpp index f59b5fb..90eb4c6 100644 --- a/godal.cpp +++ b/godal.cpp @@ -1770,17 +1770,26 @@ namespace cpl } // namespace cpl -void VSIInstallGoHandler(cctx *ctx, const char *pszPrefix, size_t bufferSize, size_t cacheSize) +bool VSIHasGoHandler(const char *pszPrefix) { - godalWrap(ctx); CSLConstList papszPrefix = VSIFileManager::GetPrefixes(); for( size_t i = 0; papszPrefix && papszPrefix[i]; ++i ) { if(strcmp(papszPrefix[i],pszPrefix)==0) { - CPLError(CE_Failure, CPLE_AppDefined, "handler already registered on prefix"); - godalUnwrap(); - return; + return true; } } + return false; +} + +void VSIInstallGoHandler(cctx *ctx, const char *pszPrefix, size_t bufferSize, size_t cacheSize) +{ + bool alreadyExists = VSIHasGoHandler(pszPrefix); + godalWrap(ctx); + if (alreadyExists) { + CPLError(CE_Failure, CPLE_AppDefined, "handler already registered on prefix"); + godalUnwrap(); + return; + } VSIFilesystemHandler *poHandler = new cpl::VSIGoFilesystemHandler(bufferSize, cacheSize); const std::string sPrefix(pszPrefix); VSIFileManager::InstallHandler(sPrefix, poHandler); diff --git a/godal.go b/godal.go index 6e1e185..902c715 100644 --- a/godal.go +++ b/godal.go @@ -3900,7 +3900,7 @@ func RegisterVSIHandler(prefix string, handler KeySizerReaderAt, opts ...VSIHand // HasVSIHandler returns true if a VSIHandler is registered for this prefix func HasVSIHandler(prefix string) bool { - return handlers != nil && handlers[prefix].KeySizerReaderAt != nil + return bool(C.VSIHasGoHandler(C.CString(prefix))) } // BuildVRT runs the GDALBuildVRT function and creates a VRT dataset from a list of datasets diff --git a/godal.h b/godal.h index 95dd163..c834a8e 100644 --- a/godal.h +++ b/godal.h @@ -130,6 +130,7 @@ extern "C" { void godalStartTransaction(cctx *ctx, GDALDatasetH ds, int bForce); void godalDatasetRollbackTransaction(cctx *ctx, GDALDatasetH ds); void godalCommitTransaction(cctx *ctx, GDALDatasetH ds); + bool VSIHasGoHandler(const char *pszPrefix); void VSIInstallGoHandler(cctx *ctx, const char *pszPrefix, size_t bufferSize, size_t cacheSize); void godalGetColorTable(GDALRasterBandH bnd, GDALPaletteInterp *interp, int *nEntries, short **entries); diff --git a/godal_test.go b/godal_test.go index 8b4faf8..466fd99 100644 --- a/godal_test.go +++ b/godal_test.go @@ -3459,6 +3459,7 @@ func (mvp mvpHandler) ReadAtMulti(k string, buf [][]byte, off []int64) ([]int, e } return b.(KeyMultiReader).ReadAtMulti(k, buf, off) } + func TestHasVSIHandler(t *testing.T) { // stripPrefix false assert.False(t, HasVSIHandler("unregistered_prefix://")) @@ -3467,11 +3468,6 @@ func TestHasVSIHandler(t *testing.T) { // stripPrefix false assert.NoError(t, err) assert.True(t, HasVSIHandler("registered_prefix://")) - // nil handler - err = RegisterVSIHandler("nil_prefix://", nil) - assert.NoError(t, err) - assert.False(t, HasVSIHandler("nil_prefix://")) - // unregistered_prefix assert.False(t, HasVSIHandler("unregistered_prefix://")) }