diff --git a/autotest/utilities/test_gdalinfo.py b/autotest/utilities/test_gdalinfo.py index 84792d39e806..07054b9a036b 100755 --- a/autotest/utilities/test_gdalinfo.py +++ b/autotest/utilities/test_gdalinfo.py @@ -32,6 +32,7 @@ import json import os import shutil +import stat import gdaltest import pytest @@ -1018,3 +1019,28 @@ def test_gdalinfo_stac_eo_bands(gdalinfo_path, tmp_path): data = json.loads(ret) assert data["stac"]["eo:cloud_cover"] == 2 + + +def test_gdalinfo_access_to_file_without_permission(gdalinfo_path, tmp_path): + + tmpfilename = str(tmp_path / "test.bin") + with open(tmpfilename, "wb") as f: + f.write(b"\x00" * 1024) + os.chmod(tmpfilename, 0) + + # Test that file is not accessible + try: + f = open(tmpfilename, "rb") + f.close() + pytest.skip("could not set non accessible permission") + except IOError: + pass + + _, err = gdaltest.runexternal_out_and_err( + gdalinfo_path + " " + tmpfilename, + encoding="UTF-8", + ) + lines = list(filter(lambda x: len(x) > 0, err.split("\n"))) + assert (len(lines)) == 3 + + os.chmod(tmpfilename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) diff --git a/autotest/utilities/test_ogrinfo.py b/autotest/utilities/test_ogrinfo.py index 4b69eec5531a..30bc0fbfcf04 100755 --- a/autotest/utilities/test_ogrinfo.py +++ b/autotest/utilities/test_ogrinfo.py @@ -29,7 +29,9 @@ # DEALINGS IN THE SOFTWARE. ############################################################################### +import os import pathlib +import stat import gdaltest import ogrtest @@ -749,3 +751,29 @@ def test_ogrinfo_if_ko(ogrinfo_path): ogrinfo_path + " -if GeoJSON ../ogr/data/gpkg/2d_envelope.gpkg" ) assert "not recognized as being in a supported file format" in err + + +############################################################################### +def test_ogrinfo_access_to_file_without_permission(ogrinfo_path, tmp_path): + + tmpfilename = str(tmp_path / "test.bin") + with open(tmpfilename, "wb") as f: + f.write(b"\x00" * 1024) + os.chmod(tmpfilename, 0) + + # Test that file is not accessible + try: + f = open(tmpfilename, "rb") + f.close() + pytest.skip("could not set non accessible permission") + except IOError: + pass + + _, err = gdaltest.runexternal_out_and_err( + ogrinfo_path + " " + tmpfilename, + encoding="UTF-8", + ) + lines = list(filter(lambda x: len(x) > 0, err.split("\n"))) + assert (len(lines)) == 3 + + os.chmod(tmpfilename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) diff --git a/ogr/ogrsf_frmts/selafin/ogrselafindriver.cpp b/ogr/ogrsf_frmts/selafin/ogrselafindriver.cpp index c5f498e0e0f7..821cf15b7a0d 100644 --- a/ogr/ogrsf_frmts/selafin/ogrselafindriver.cpp +++ b/ogr/ogrsf_frmts/selafin/ogrselafindriver.cpp @@ -57,6 +57,11 @@ static int OGRSelafinDriverIdentify(GDALOpenInfo *poOpenInfo) return TRUE; } + // We can stat() the file but it is not a regular file or we did not + // get access to its content + if (poOpenInfo->bStatOK) + return FALSE; + return -1; }