Skip to content

Commit

Permalink
gdalinfo_lib.cpp: do not list files of a raster FileGDB unless -json …
Browse files Browse the repository at this point in the history
…is enabled
  • Loading branch information
rouault committed Mar 9, 2023
1 parent d9ea927 commit 8823b91
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 168 deletions.
7 changes: 6 additions & 1 deletion apps/gdalinfo_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions)
GDALGetDriverShortName(hDriver), GDALGetDriverLongName(hDriver));
}

char **papszFileList = GDALGetFileList(hDataset);
// The list of files of a raster FileGDB is not super useful and potentially
// super long, so omit it, unless the -json mode is enabled
char **papszFileList =
(!bJson && EQUAL(GDALGetDriverShortName(hDriver), "OpenFileGDB"))
? nullptr
: GDALGetFileList(hDataset);

if (papszFileList == nullptr || *papszFileList == nullptr)
{
Expand Down
4 changes: 4 additions & 0 deletions autotest/ogr/ogr_openfilegdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def test_ogr_openfilegdb_1(gdb_source):
srs = osr.SpatialReference()
srs.SetFromUserInput("WGS84")

assert gdal.OpenEx(filename, gdal.OF_RASTER) is None

assert gdal.OpenEx(filename, gdal.OF_RASTER | gdal.OF_VECTOR) is not None

ds = ogr.Open(filename)

for data in ogrtest.openfilegdb_datalist:
Expand Down
37 changes: 19 additions & 18 deletions autotest/ogr/ogr_openfilegdb_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_ogr_openfilegdb_write_empty():
assert ds is not None
ds = None

ds = ogr.Open(dirname)
ds = ogr.Open(dirname, update=1)
assert ds is not None
assert ds.GetLayerCount() == 0
ds = None
Expand Down Expand Up @@ -3232,23 +3232,6 @@ def test_ogr_openfilegdb_write_emulated_transactions():
# Implicit rollback
ds = None

gdal.Mkdir(dirname + "/.ogrtransaction_backup", 0o755)
with gdaltest.error_handler():
# Cannot open in update mode with an existing backup directory
assert ogr.Open(dirname, update=1) is None

# Emit warning in read-only mode when opening with an existing backup directory
gdal.ErrorReset()
assert ogr.Open(dirname) is not None
assert "A previous backup directory" in gdal.GetLastErrorMsg()
gdal.Rmdir(dirname + "/.ogrtransaction_backup")

# Transaction not supported in read-only mode
ds = ogr.Open(dirname)
assert ds.TestCapability(ogr.ODsCEmulatedTransactions) == 0
with gdaltest.error_handler():
assert ds.StartTransaction(True) == ogr.OGRERR_FAILURE

ds = ogr.Open(dirname, update=1)
assert ds.StartTransaction(True) == ogr.OGRERR_NONE
gdal.Rmdir(dirname + "/.ogrtransaction_backup")
Expand Down Expand Up @@ -3283,6 +3266,24 @@ def test_ogr_openfilegdb_write_emulated_transactions():
assert gdal.VSIStatL(dirname + "/a0000000a.gdbtable") is not None
ds = None

gdal.Mkdir(dirname + "/.ogrtransaction_backup", 0o755)
with gdaltest.error_handler():
# Cannot open in update mode with an existing backup directory
assert ogr.Open(dirname, update=1) is None

# Emit warning in read-only mode when opening with an existing backup directory
gdal.ErrorReset()
assert ogr.Open(dirname) is not None
assert "A previous backup directory" in gdal.GetLastErrorMsg()
gdal.Rmdir(dirname + "/.ogrtransaction_backup")

# Transaction not supported in read-only mode
ds = ogr.Open(dirname)
assert ds.TestCapability(ogr.ODsCEmulatedTransactions) == 0
with gdaltest.error_handler():
assert ds.StartTransaction(True) == ogr.OGRERR_FAILURE
ds = None

ds = ogr.Open(dirname, update=1)
assert ds.GetLayerCount() == 1
lyr = ds.GetLayerByName("foo2")
Expand Down
1 change: 1 addition & 0 deletions doc/source/drivers/raster/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Raster drivers
ntv2
nwtgrd
ogcapi
openfilegdb
ozi
palsar
paux
Expand Down
7 changes: 5 additions & 2 deletions doc/source/drivers/vector/openfilegdb.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _vector.openfilegdb:

ESRI File Geodatabase (OpenFileGDB)
===================================
ESRI File Geodatabase vector (OpenFileGDB)
==========================================

.. shortname:: OpenFileGDB

Expand All @@ -23,6 +23,8 @@ Curve in geometries are supported with GDAL >= 2.2.

Write and update capabilities are supported since GDAL >= 3.6

The driver also supports :ref:`raster layers<raster.openfilegdb>` since GDAL 3.7

Driver capabilities
-------------------

Expand Down Expand Up @@ -243,6 +245,7 @@ Examples
Links
-----

- :ref:`OpenFileGDB raster <raster.openfilegdb>` documentation page
- :ref:`FileGDB driver <vector.filegdb>`, relying on the FileGDB API SDK
- Reverse-engineered specification of the `FileGDB
format <https://github.com/rouault/dump_gdbtable/wiki/FGDB-Spec>`__
Expand Down
16 changes: 16 additions & 0 deletions ogr/ogrsf_frmts/filegdb/FGdbDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ static GDALDataset *OGRFileGDBDriverOpen(GDALOpenInfo *poOpenInfo)
GDAL_IDENTIFY_FALSE)
return nullptr;

// If this is a raster-only GDB, do not try to open it, to be consistent
// with OpenFileGDB behavior.
const char *const apszOpenFileGDBDriver[] = {"OpenFileGDB", nullptr};
auto poOpenFileGDBDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
pszFilename, GDAL_OF_RASTER, apszOpenFileGDBDriver, nullptr, nullptr));
if (poOpenFileGDBDS)
{
poOpenFileGDBDS.reset();
poOpenFileGDBDS = std::unique_ptr<GDALDataset>(
GDALDataset::Open(pszFilename, GDAL_OF_VECTOR,
apszOpenFileGDBDriver, nullptr, nullptr));
if (!poOpenFileGDBDS)
return nullptr;
}
poOpenFileGDBDS.reset();

const bool bUpdate = poOpenInfo->eAccess == GA_Update;
long hr;

Expand Down
2 changes: 2 additions & 0 deletions ogr/ogrsf_frmts/openfilegdb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ add_gdal_driver(
ogropenfilegdbdriver.cpp
ogropenfilegdblayer.cpp
ogropenfilegdblayer_write.cpp
gdalopenfilegdbrasterband.cpp
PLUGIN_CAPABLE NO_DEPS STRONG_CXX_WFLAGS)

gdal_standard_includes(ogr_OpenFileGDB)
target_include_directories(ogr_OpenFileGDB PRIVATE $<TARGET_PROPERTY:ogr_MEM,SOURCE_DIR>)

Expand Down
16 changes: 0 additions & 16 deletions ogr/ogrsf_frmts/openfilegdb/filegdbtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,22 +684,6 @@ int FileGDBTable::ReadTableXHeader()
return TRUE;
}

/************************************************************************/
/* ReadUTF16String() */
/************************************************************************/

static std::string ReadUTF16String(const GByte *pabyIter, int nCarCount)
{
std::wstring osWideStr;
for (int j = 0; j < nCarCount; j++)
osWideStr += pabyIter[2 * j] | (pabyIter[2 * j + 1] << 8);
char *pszStr =
CPLRecodeFromWChar(osWideStr.c_str(), CPL_ENC_UCS2, CPL_ENC_UTF8);
std::string osRet(pszStr);
CPLFree(pszStr);
return osRet;
}

/************************************************************************/
/* Open() */
/************************************************************************/
Expand Down
18 changes: 18 additions & 0 deletions ogr/ogrsf_frmts/openfilegdb/filegdbtable_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define FILEGDBTABLE_PRIV_H_INCLUDED

#include "filegdbtable.h"

#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_time.h"

Expand Down Expand Up @@ -334,6 +336,22 @@ inline void WriteVarInt(std::vector<GByte> &abyBuffer, int64_t nVal)
WriteVarUInt(abyBuffer, nUVal);
}

/************************************************************************/
/* ReadUTF16String() */
/************************************************************************/

inline std::string ReadUTF16String(const GByte *pabyIter, int nCarCount)
{
std::wstring osWideStr;
for (int j = 0; j < nCarCount; j++)
osWideStr += pabyIter[2 * j] | (pabyIter[2 * j + 1] << 8);
char *pszStr =
CPLRecodeFromWChar(osWideStr.c_str(), CPL_ENC_UCS2, CPL_ENC_UTF8);
std::string osRet(pszStr);
CPLFree(pszStr);
return osRet;
}

/************************************************************************/
/* WriteUTF16String() */
/************************************************************************/
Expand Down
Loading

0 comments on commit 8823b91

Please sign in to comment.