Skip to content

Commit d4835a1

Browse files
committed
ZARR: emit an error when reading from a JSON/Kerchunk reference store and one of the pointed file cannot be opened
Fixes #13126
1 parent f0113a4 commit d4835a1

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed
6 Bytes
Binary file not shown.

autotest/gdrivers/zarr_kerchunk_json.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,14 @@ def test_zarr_kerchunk_json_fail_exception(
245245
"""{".zgroup": {"zarr_format":2}, "foo":["https://localhost:1/",0,1]}""",
246246
],
247247
)
248-
def test_zarr_kerchunk_json_open_fail_no_exception(tmp_vsimem, content):
248+
def test_zarr_kerchunk_json_open_fail(tmp_vsimem, content):
249249

250250
json_filename = str(tmp_vsimem / "test.json")
251251
gdal.FileFromMemBuffer(json_filename, content)
252252

253-
with gdal.quiet_errors():
254-
assert (
255-
gdal.VSIFOpenL("/vsikerchunk_json_ref/{" + json_filename + "}/foo", "rb")
256-
is None
257-
)
253+
with pytest.raises(Exception):
254+
with gdal.VSIFile("/vsikerchunk_json_ref/{" + json_filename + "}/foo", "rb"):
255+
pass
258256

259257

260258
###############################################################################

autotest/gdrivers/zarr_kerchunk_parquet.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
# SPDX-License-Identifier: MIT
1212
###############################################################################
1313

14+
import os
15+
import shutil
16+
import sys
17+
1418
import pytest
1519

1620
from osgeo import gdal
@@ -318,6 +322,27 @@ def test_zarr_kerchunk_parquet_gdal_open_dim2():
318322
###############################################################################
319323

320324

325+
@pytest.mark.skipif(sys.platform == "win32", reason="Fails for some reason on Windows")
326+
def test_zarr_kerchunk_parquet_gdal_open_missing_bin_file(tmp_path):
327+
328+
shutil.copytree(
329+
"data/zarr/kerchunk_parquet/parquet_ref_2_dim", tmp_path / "parquet_ref_2_dim"
330+
)
331+
os.unlink(tmp_path / "parquet_ref_2_dim/ar/4.bin")
332+
333+
with gdal.OpenEx(
334+
f"ZARR:/vsikerchunk_parquet_ref/{{{tmp_path}/parquet_ref_2_dim}}",
335+
gdal.OF_MULTIDIM_RASTER,
336+
) as ds:
337+
rg = ds.GetRootGroup()
338+
ar = rg.OpenMDArray("ar")
339+
with pytest.raises(Exception):
340+
ar.Read()
341+
342+
343+
###############################################################################
344+
345+
321346
@pytest.mark.require_driver("PARQUET")
322347
def test_zarr_kerchunk_parquet_invalid_parquet_struct():
323348
with gdal.OpenEx(

frmts/zarr/vsikerchunk_json_ref.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "cpl_json_streaming_parser.h"
2222
#include "cpl_json_streaming_writer.h"
2323
#include "cpl_mem_cache.h"
24+
#include "cpl_vsi_error.h"
2425
#include "cpl_vsi_virtual.h"
2526

2627
#include "gdal_priv.h"
@@ -1583,7 +1584,14 @@ VSIKerchunkJSONRefFileSystem::Open(const char *pszFilename,
15831584
osPath.c_str());
15841585
CPLConfigOptionSetter oSetter("GDAL_DISABLE_READDIR_ON_OPEN",
15851586
"EMPTY_DIR", false);
1586-
return VSIFilesystemHandler::OpenStatic(osPath.c_str(), "rb");
1587+
auto fp = VSIFilesystemHandler::OpenStatic(osPath.c_str(), "rb", true);
1588+
if (!fp)
1589+
{
1590+
if (!VSIToCPLError(CE_Failure, CPLE_FileIO))
1591+
CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
1592+
osPath.c_str());
1593+
}
1594+
return fp;
15871595
}
15881596
}
15891597

frmts/zarr/vsikerchunk_parquet_ref.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "cpl_json.h"
1717
#include "cpl_mem_cache.h"
18+
#include "cpl_vsi_error.h"
1819
#include "cpl_vsi_virtual.h"
1920

2021
#include "gdal_priv.h"
@@ -552,7 +553,15 @@ VSIVirtualHandleUniquePtr VSIKerchunkParquetRefFileSystem::Open(
552553
osPath.c_str());
553554
CPLConfigOptionSetter oSetter("GDAL_DISABLE_READDIR_ON_OPEN",
554555
"EMPTY_DIR", false);
555-
return VSIFilesystemHandler::OpenStatic(osPath.c_str(), "rb");
556+
auto fp = VSIFilesystemHandler::OpenStatic(osPath.c_str(), "rb",
557+
true);
558+
if (!fp)
559+
{
560+
if (!VSIToCPLError(CE_Failure, CPLE_FileIO))
561+
CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
562+
osPath.c_str());
563+
}
564+
return fp;
556565
}
557566
}
558567

port/cpl_vsil_subfile.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ int VSISubFileFilesystemHandler::DecomposePath(const char *pszPath,
337337

338338
VSIVirtualHandleUniquePtr
339339
VSISubFileFilesystemHandler::Open(const char *pszFilename,
340-
const char *pszAccess, bool /* bSetError */,
341-
CSLConstList /* papszOptions */)
340+
const char *pszAccess, bool bSetError,
341+
CSLConstList papszOptions)
342342

343343
{
344344
if (!STARTS_WITH_CI(pszFilename, "/vsisubfile/"))
@@ -368,7 +368,8 @@ VSISubFileFilesystemHandler::Open(const char *pszFilename,
368368
/* -------------------------------------------------------------------- */
369369
/* Open the underlying file. */
370370
/* -------------------------------------------------------------------- */
371-
auto fp = VSIFilesystemHandler::OpenStatic(osSubFilePath, pszAccess);
371+
auto fp = VSIFilesystemHandler::OpenStatic(osSubFilePath, pszAccess,
372+
bSetError, papszOptions);
372373

373374
if (fp == nullptr)
374375
return nullptr;

0 commit comments

Comments
 (0)