Skip to content

Commit

Permalink
make HasVSIHandler test externally registered handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvaroquauxads authored Sep 12, 2024
1 parent 203295b commit ea45bb1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
19 changes: 14 additions & 5 deletions godal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions godal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://"))

Expand All @@ -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://"))
}
Expand Down

0 comments on commit ea45bb1

Please sign in to comment.