Skip to content

Commit

Permalink
Add VSIRemovePluginHandler() to enable removal of virtual filesystems (
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxxen authored Dec 3, 2023
1 parent 1872179 commit fba7ec4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
10 changes: 10 additions & 0 deletions autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4197,6 +4197,16 @@ TEST_F(test_cpl, VSI_plugin_minimal_testing)

VSIFCloseL(fp);
EXPECT_TRUE(VSIFOpenL("/vsimyplugin/i_dont_exist", "rb") == nullptr);

// Check that we can remove the handler
VSIRemovePluginHandler("/vsimyplugin/");

EXPECT_TRUE(VSIFOpenL("/vsimyplugin/test", "rb") == nullptr);
EXPECT_TRUE(VSIFOpenL("/vsimyplugin/i_dont_exist", "rb") == nullptr);

// Removing a non-existing handler is a no-op
VSIRemovePluginHandler("/vsimyplugin/");
VSIRemovePluginHandler("/vsifoobar/");
}

TEST_F(test_cpl, VSI_plugin_advise_read)
Expand Down
11 changes: 11 additions & 0 deletions port/cpl_vsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,17 @@ void CPL_DLL VSIFreeFilesystemPluginCallbacksStruct(
int CPL_DLL VSIInstallPluginHandler(
const char *pszPrefix, const VSIFilesystemPluginCallbacksStruct *poCb);

/**
* Unregister a handler previously installed with VSIInstallPluginHandler() on
* the given prefix.
* Note: it is generally unsafe to remove a handler while there are still file
* handles opened that are managed by that handler. It is the responsibility of
* the caller to ensure that it calls this function in a situation where it is
* safe to do so.
* @since GDAL 3.9
*/
int CPL_DLL VSIRemovePluginHandler(const char *pszPrefix);

/* ==================================================================== */
/* Time querying. */
/* ==================================================================== */
Expand Down
3 changes: 1 addition & 2 deletions port/cpl_vsi_virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ class CPL_DLL VSIFileManager
static VSIFilesystemHandler *GetHandler(const char *);
static void InstallHandler(const std::string &osPrefix,
VSIFilesystemHandler *);
/* RemoveHandler is never defined. */
/* static void RemoveHandler( const std::string& osPrefix ); */
static void RemoveHandler(const std::string &osPrefix);

static char **GetPrefixes();
};
Expand Down
12 changes: 12 additions & 0 deletions port/cpl_vsil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,18 @@ void VSIFileManager::InstallHandler(const std::string &osPrefix,
Get()->oHandlers[osPrefix] = poHandler;
}

/************************************************************************/
/* RemoveHandler() */
/************************************************************************/

void VSIFileManager::RemoveHandler(const std::string &osPrefix)
{
if (osPrefix == "")
Get()->poDefaultHandler = nullptr;
else
Get()->oHandlers.erase(osPrefix);
}

/************************************************************************/
/* VSICleanupFileManager() */
/************************************************************************/
Expand Down
6 changes: 6 additions & 0 deletions port/cpl_vsil_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ int VSIInstallPluginHandler(const char *pszPrefix,
return 0;
}

int VSIRemovePluginHandler(const char *pszPrefix)
{
VSIFileManager::RemoveHandler(pszPrefix);
return 0;
}

VSIFilesystemPluginCallbacksStruct *
VSIAllocFilesystemPluginCallbacksStruct(void)
{
Expand Down

0 comments on commit fba7ec4

Please sign in to comment.