diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2020-01-13 22:17:08 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2020-01-14 22:42:18 +0100 |
| commit | 034238e5014ae965a7dbd1e2bf26f7227196e987 (patch) | |
| tree | 6e98016cddec4ac673ae03064491ff69b37e3516 /src | |
| parent | 0961d15ac13620beef2f7600974afe8e4d011972 (diff) | |
| download | PROJ-034238e5014ae965a7dbd1e2bf26f7227196e987.tar.gz PROJ-034238e5014ae965a7dbd1e2bf26f7227196e987.zip | |
Fix reading resources from user-writable directory & test it
Diffstat (limited to 'src')
| -rw-r--r-- | src/filemanager.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/filemanager.cpp b/src/filemanager.cpp index 277578d1..d2fce822 100644 --- a/src/filemanager.cpp +++ b/src/filemanager.cpp @@ -3294,9 +3294,39 @@ static void *pj_open_file_with_manager(projCtx ctx, const char *name, std::unique_ptr<NS_PROJ::File> NS_PROJ::FileManager::open_resource_file(projCtx ctx, const char *name) { + + if (ctx == nullptr) { + ctx = pj_get_default_ctx(); + } + auto file = std::unique_ptr<NS_PROJ::File>( reinterpret_cast<NS_PROJ::File *>(pj_open_lib_internal( ctx, name, "rb", pj_open_file_with_manager, nullptr, 0))); + + // Retry with a .tif extension if the file name doesn't end with .tif + if (file == nullptr && !is_tilde_slash(name) && + !is_rel_or_absolute_filename(name) && !starts_with(name, "http://") && + !starts_with(name, "https://") && strcmp(name, "proj.db") != 0 && + strstr(name, ".tif") == nullptr) { + std::string filename(name); + auto pos = filename.rfind('.'); + if (pos + 4 == filename.size()) { + filename = filename.substr(0, pos) + ".tif"; + file.reset(reinterpret_cast<NS_PROJ::File *>( + pj_open_lib_internal(ctx, filename.c_str(), "rb", + pj_open_file_with_manager, nullptr, 0))); + } else { + // For example for resource files like 'alaska' + filename += ".tif"; + file.reset(reinterpret_cast<NS_PROJ::File *>( + pj_open_lib_internal(ctx, filename.c_str(), "rb", + pj_open_file_with_manager, nullptr, 0))); + } + if (file) { + pj_ctx_set_errno(ctx, 0); + } + } + if (file == nullptr && !is_tilde_slash(name) && !is_rel_or_absolute_filename(name) && !starts_with(name, "http://") && !starts_with(name, "https://") && pj_context_is_network_enabled(ctx)) { |
