Skip to content

Commit 81883c7

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 998a784 commit 81883c7

File tree

6 files changed

+51
-13
lines changed

6 files changed

+51
-13
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# SPDX-License-Identifier: MIT
1212
###############################################################################
1313

14+
import os
15+
import shutil
16+
1417
import pytest
1518

1619
from osgeo import gdal
@@ -318,6 +321,26 @@ def test_zarr_kerchunk_parquet_gdal_open_dim2():
318321
###############################################################################
319322

320323

324+
def test_zarr_kerchunk_parquet_gdal_open_missing_bin_file(tmp_path):
325+
326+
shutil.copytree(
327+
"data/zarr/kerchunk_parquet/parquet_ref_2_dim", tmp_path / "parquet_ref_2_dim"
328+
)
329+
os.unlink(tmp_path / "parquet_ref_2_dim/ar/4.bin")
330+
331+
with gdal.OpenEx(
332+
f"ZARR:/vsikerchunk_parquet_ref/{{{tmp_path}/parquet_ref_2_dim}}",
333+
gdal.OF_MULTIDIM_RASTER,
334+
) as ds:
335+
rg = ds.GetRootGroup()
336+
ar = rg.OpenMDArray("ar")
337+
with pytest.raises(Exception):
338+
ar.Read()
339+
340+
341+
###############################################################################
342+
343+
321344
@pytest.mark.require_driver("PARQUET")
322345
def test_zarr_kerchunk_parquet_invalid_parquet_struct():
323346
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"
@@ -1575,7 +1576,14 @@ VSIKerchunkJSONRefFileSystem::Open(const char *pszFilename,
15751576
osPath.c_str());
15761577
CPLConfigOptionSetter oSetter("GDAL_DISABLE_READDIR_ON_OPEN",
15771578
"EMPTY_DIR", false);
1578-
return VSIFOpenL(osPath.c_str(), "rb");
1579+
auto fp = VSIFOpenEx2L(osPath.c_str(), "rb", true, nullptr);
1580+
if (!fp)
1581+
{
1582+
if (!VSIToCPLError(CE_Failure, CPLE_FileIO))
1583+
CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
1584+
osPath.c_str());
1585+
}
1586+
return fp;
15791587
}
15801588
}
15811589

frmts/zarr/vsikerchunk_parquet_ref.cpp

Lines changed: 9 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"
@@ -550,7 +551,14 @@ VSIVirtualHandle *VSIKerchunkParquetRefFileSystem::Open(
550551
osPath.c_str());
551552
CPLConfigOptionSetter oSetter("GDAL_DISABLE_READDIR_ON_OPEN",
552553
"EMPTY_DIR", false);
553-
return VSIFOpenL(osPath.c_str(), "rb");
554+
auto fp = VSIFOpenEx2L(osPath.c_str(), "rb", true, nullptr);
555+
if (!fp)
556+
{
557+
if (!VSIToCPLError(CE_Failure, CPLE_FileIO))
558+
CPLError(CE_Failure, CPLE_FileIO, "Cannot open %s",
559+
osPath.c_str());
560+
}
561+
return fp;
554562
}
555563
}
556564

port/cpl_vsil_subfile.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ int VSISubFileFilesystemHandler::DecomposePath(const char *pszPath,
338338
/* Open() */
339339
/************************************************************************/
340340

341-
VSIVirtualHandle *
342-
VSISubFileFilesystemHandler::Open(const char *pszFilename,
343-
const char *pszAccess, bool /* bSetError */,
344-
CSLConstList /* papszOptions */)
341+
VSIVirtualHandle *VSISubFileFilesystemHandler::Open(const char *pszFilename,
342+
const char *pszAccess,
343+
bool bSetError,
344+
CSLConstList papszOptions)
345345

346346
{
347347
if (!STARTS_WITH_CI(pszFilename, "/vsisubfile/"))
@@ -371,7 +371,8 @@ VSISubFileFilesystemHandler::Open(const char *pszFilename,
371371
/* -------------------------------------------------------------------- */
372372
/* Open the underlying file. */
373373
/* -------------------------------------------------------------------- */
374-
VSILFILE *fp = VSIFOpenL(osSubFilePath, pszAccess);
374+
VSILFILE *fp =
375+
VSIFOpenEx2L(osSubFilePath, pszAccess, bSetError, papszOptions);
375376

376377
if (fp == nullptr)
377378
return nullptr;

0 commit comments

Comments
 (0)