Skip to content

Commit

Permalink
Merge pull request #9419 from rouault/fix_9410
Browse files Browse the repository at this point in the history
Selafin: avoid emitting additional error message on file that can't be opened (fixes #9410)
  • Loading branch information
rouault authored Mar 14, 2024
2 parents 83d73eb + 2e3f4f4 commit 59d502f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
26 changes: 26 additions & 0 deletions autotest/utilities/test_gdalinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import json
import os
import shutil
import stat

import gdaltest
import pytest
Expand Down Expand Up @@ -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)
28 changes: 28 additions & 0 deletions autotest/utilities/test_ogrinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################

import os
import pathlib
import stat

import gdaltest
import ogrtest
Expand Down Expand Up @@ -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)
5 changes: 5 additions & 0 deletions ogr/ogrsf_frmts/selafin/ogrselafindriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 59d502f

Please sign in to comment.