Skip to content

Avoid opening Specfile with write-access#532

Closed
praiskup wants to merge 1 commit into
packit:mainfrom
praiskup:open-read-only
Closed

Avoid opening Specfile with write-access#532
praiskup wants to merge 1 commit into
packit:mainfrom
praiskup:open-read-only

Conversation

@praiskup
Copy link
Copy Markdown

Per discussion in Copr issue tracker:
fedora-copr/copr#4266

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies specfile/specfile.py to open files in read-only mode ('r') instead of read-write mode ('r+') during initialization, path updates, and file reopening. A critical issue was identified where these changes will cause the save() method to fail with an io.UnsupportedOperation error, as it relies on _reopen_named_file() providing a writable file handle. It is recommended to update _reopen_named_file() to accept a mode parameter to support both read and write operations.

Comment thread specfile/specfile.py
return
self._file.close()
self._file = self.path.open("r+", **self.ENCODING_ARGS)
self._file = self.path.open("r", **self.ENCODING_ARGS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Changing the file opening mode to "r" in _reopen_named_file() will cause the save() method to fail. The save() method (lines 294-306) calls _reopen_named_file() and then attempts to truncate and write to the file handle. Since the file is now opened in read-only mode, these operations will raise an io.UnsupportedOperation error.

To resolve this, _reopen_named_file() should be updated to accept a mode parameter (defaulting to "r"), and save() should be updated to call it with mode="r+". Since save() is not currently part of this diff, you should include it in the pull request to apply the necessary changes.

@centosinfra-prod-github-app
Copy link
Copy Markdown
Contributor

@praiskup
Copy link
Copy Markdown
Author

Seems this change is indeed wrong. Do whatever you prefer with this PR (guide me to provide a better patch, but even closing is fine). It was easier for me to open this than filling a proper issue...

@nforro
Copy link
Copy Markdown
Member

nforro commented Apr 23, 2026

specfile is primarily read-write by design. I think the proper fix here would be to try to open the path with r+, falling back to r on PermissionError and preserving the mode during reopen (or perhaps trying again r+ with r as fallback).

@praiskup
Copy link
Copy Markdown
Author

Ok, lemme close this one and have an issue instead: #534

@praiskup praiskup closed this Apr 23, 2026
@github-project-automation github-project-automation Bot moved this from New to Done in Packit pull requests Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants