Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that README.txt has write permissions for subsequent imports. #5249

Merged
merged 3 commits into from
Feb 26, 2021

Conversation

erykoff
Copy link
Contributor

@erykoff erykoff commented Feb 24, 2021

Description

This PR fixes a bug in which if data/README.txt has been made read-only in the source directory, then the first import of skimage works because the file is copied into the .cache/scikit-image/ directory successfully. But subsequent imports will fail because copy2 will not be able to copy README.txt onto a read-only target. This PR fixes the problem by ensuring that README.txt has user permissions as a writable file.

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

@hmaarrfk
Copy link
Member

This happens because you are using a system installation for which you do not have write permissions to the install location of scikit image?

This is a pretty esoteric edge case (but a very important one) could you please add a comment in the source code that reiterates why it is important to update permissions.

Thanks!

@erykoff
Copy link
Contributor Author

erykoff commented Feb 24, 2021

Yes, exactly the case. This has come up when using the cvmfs installation of the Rubin Science Pipelines: https://sw.lsst.eu/

@boegel
Copy link

boegel commented Feb 24, 2021

I ran into this exact issue that this PR is fixing just now...

In my case, the situation was as follows:

  • Home directory is on a shared filesystem, that is accessible from multiple different systems with different types of CPUs (and even OS versions). They key part here is the shared aspect.
  • scikit-image was installed into a specific installation directory, which was made read-only (incl. all contents) right after the installation (to help avoid that any changes are made to it by accident).

This then resulted in errors like this when importing skimage:

PermissionError: [Errno 13] Permission denied: '/home/example/.cache/scikit-image/0.18.1/data/README.txt

So very much +1 on this proposed change!

# we need to ensure README.txt has user-write permission when it is
# put into their cache directory.
current = stat.S_IMODE(os.lstat(dest_path).st_mode)
os.chmod(dest_path, current | stat.S_IWUSR)
Copy link

Choose a reason for hiding this comment

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

Another option would be to check if the dest_path already exists, and only copy it if it doesn't.

Unless there's a particular reason why every import skimage needs to result in copying the README.txt file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, but this leaves the ability to update the README.txt ... then again, if the cache directory has the version in it, then this shouldn't happen, I just now realize.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to hear from one of the scikit-image maintainers ... if there's a reason to recopy, we can leave this, or I'd be happy to change it to a check if the file exists. (Which is probably more efficient, although it's a tiny file.)

Copy link
Member

Choose a reason for hiding this comment

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

it might just be better to read the file line by line, and just overwrite the file. The README.txt file in the data cache directory can be assume to be owned by us.

Reading and writing files with the default settings enables system admins to choose good defaults for their security model.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason it has to be overwritten, though? Can't we just check that it exists and not copy/overwrite if it's there? I'm of a mind that would be the most reliable and fastest.

Copy link
Member

Choose a reason for hiding this comment

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

I'm worried that the text won't get updated if we change the language.

I think you can skip the copy if the file exists for now. Please justify your decision in a comment in the code.

I'm rather agnostic for this small file.

Do we copy in any other files with shutil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated to only copy if it isn't there, and added an explanation. In general, I don't think this is a problem because the cache directory contains the scikit-image version number, and I can't see a path that README.txt would change within a given version/installation without shenanigans.

As for other copies, yes, the other data files are all copied in. However, they have their hash checked so will not be re-copied if their hashes match. It is possible that somebody could make a file writeable, modify it, and put it back to read only and then one of these copy2 actions would fail, but that is hard to protect against. As for the files themselves changing from version to version, that is possible but they would be put into a different cache directory bypassing the overwrite problem.

@hmaarrfk
Copy link
Member

I'm OK with this for the same reasons you listed.

We have version numbers in our cache, so that should make sure the readme is mostly up to date.

Hashing the readme is somewhat strange.

Thanks for addressing this!

@stefanv stefanv merged commit 17db79a into scikit-image:main Feb 26, 2021
@stefanv
Copy link
Member

stefanv commented Feb 26, 2021

Thanks @erykoff!

hmaarrfk pushed a commit to hmaarrfk/scikit-image that referenced this pull request Sep 12, 2021
…cikit-image#5249)

* Ensure that README.txt has write permissions for subsequent imports.

* Add comment

* Only copy the README.txt file if it isn't there.
hmaarrfk pushed a commit to hmaarrfk/scikit-image that referenced this pull request Sep 12, 2021
…cikit-image#5249)

* Ensure that README.txt has write permissions for subsequent imports.

* Add comment

* Only copy the README.txt file if it isn't there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants