Skip to content

Commit

Permalink
Follow symlinks when reading files
Browse files Browse the repository at this point in the history
Now that we no longer use realpath() when locating files, there is a
possibility that paths will still contain symlinks when we read them.

Signed-off-by: John Pennycook <[email protected]>
  • Loading branch information
Pennycook committed Oct 10, 2024
1 parent 95a280f commit de026ea
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion codebasin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def from_file(cls, filename: str | os.PathLike[str]):
-------
A CompilationDatbase corresponding to the provided JSON file.
"""
with codebasin.util.safe_open_read_nofollow(filename, "r") as f:
with open(filename) as f:
db = codebasin.util._load_json(f, schema_name="compiledb")
return CompilationDatabase.from_json(db)

Expand Down
2 changes: 1 addition & 1 deletion codebasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def main():
if not os.path.splitext(path)[1] == ".toml":
raise RuntimeError(f"Analysis file {path} must end in .toml.")

with util.safe_open_read_nofollow(path, "rb") as f:
with open(path, "rb") as f:
try:
analysis_toml = util._load_toml(f, "analysis")
except BaseException:
Expand Down
2 changes: 1 addition & 1 deletion codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def load_importcfg():
path = ".cbi/config"
if os.path.exists(path):
log.info(f"Found configuration file at {path}")
with util.safe_open_read_nofollow(path, "rb") as f:
with open(path, "rb") as f:
try:
_importcfg_toml = util._load_toml(f, "cbiconfig")
for name, compiler in _importcfg_toml["compiler"].items():
Expand Down
8 changes: 2 additions & 6 deletions codebasin/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import os

from codebasin import preprocessor, util
from codebasin import preprocessor
from codebasin.file_source import get_file_source

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -170,11 +170,7 @@ def parse_file(self, *, summarize_only=True, language=None):
f"{filename} doesn't appear "
+ "to be a language this tool can process",
)
with util.safe_open_read_nofollow(
filename,
mode="r",
errors="replace",
) as source_file:
with open(filename, errors="replace") as source_file:
groups = {
"code": LineGroup(),
"directive": LineGroup(),
Expand Down
2 changes: 1 addition & 1 deletion codebasin/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def __init__(self, _filename):
def __compute_file_hash(self):
chunk_size = 4096
hasher = hashlib.sha512()
with util.safe_open_read_nofollow(self.filename, "rb") as in_file:
with open(self.filename, "rb") as in_file:
for chunk in iter(lambda: in_file.read(chunk_size), b""):
hasher.update(chunk)

Expand Down
6 changes: 0 additions & 6 deletions codebasin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ def safe_open_write_binary(fname):
return os.fdopen(fpid, "wb")


def safe_open_read_nofollow(fname, *args, **kwargs):
"""Open fname for reading, but don't follow links."""
fpid = os.open(fname, os.O_RDONLY | os.O_NOFOLLOW)
return os.fdopen(fpid, *args, **kwargs)


def valid_path(path):
"""Return true if the path passed in is valid"""
valid = True
Expand Down

0 comments on commit de026ea

Please sign in to comment.