-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix: Fallback to temp dir if cache does not exist #1784
fix: Fallback to temp dir if cache does not exist #1784
Conversation
This PR is targeting stable but it's a minor bug and could easily land in beta or dev depending on your preferences. |
I think this is a breaking change, as the doc cases pretty explicitly state that the directory can change depending on its permissions and similar stuff. So it will be necessary to either remove the cache caching (yeah, funny) logic and just let it go every time, or to retarget against dev. What is bad in using |
I agree we should create the directory if it does not exist. We should probably use Also, it's important that it's still possible to set |
pwnlib/context/__init__.py
Outdated
@@ -1232,13 +1233,22 @@ def cache_dir(self): | |||
>>> cache_dir == context.cache_dir | |||
True | |||
""" | |||
try: | |||
return self._cache_dir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't check self._cache_dir
but rather the defaults
dictionary if it's missing.
pwnlib/context/__init__.py
Outdated
@@ -1251,7 +1261,8 @@ def cache_dir(self): | |||
if not os.access(cache, os.W_OK): | |||
return None | |||
|
|||
return cache | |||
self._cache_dir = cache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is inconsistent with how everything inside of context
works, we should not be setting any attributes that are not TLS-backed, since each thread may modify the context and they should not interfere with each other.
This is why there's a lot of work in the context
module to work off of TLS. Set the value you want in pwnlib.context.context.defaults
if you really need a non-standard default .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now taken the approach of setting the base path as an entry in defaults
and generating the cache_dir as a property based on that value. cache_base_dir
is a new validated property and changing its value clears the cache_dir
stashed in _tls
. The cache_dir
is not a validated property since I didn't want to give it a setter without specific input from you in that direction. It just has a getter which reaches into _tls
for a well known key ("cache_dir"
, which should probably be a constant somewhere to avoid drift).
Let me know what you think of the new approach.
I think if you're happy with having it use |
@Arusekk - thoughts on my last comment? I"m happy to close this in favour of an alternative PR which simply does |
Yeah, that would be great :D
Email z soboty 20 lutego 2021 od maybe-sybr:
… @Arusekk - thoughts on my last comment? I"m happy to close this in favour of an alternative PR which simply does `os.makedirs(cache, exist_ok=True)` and can submit that if you'd like.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#1784 (comment)
--
Wysłane z mojego urządzenia Sailfish
|
Closing in favour of #1812 |
5786b7e
to
06be1d6
Compare
Lucky I clicked the wrong button here. #1812 uses a Python 3ism and per the feedback left on there, I think the approach here is more worthwhile to pursue. I've reworked the diff so it should be Python 2 compatible ( |
06be1d6
to
f91003c
Compare
@heapcrash - was this ever actually possible? Per my other comment, |
@maybe-sybr First, thanks for the effort you're putting into this. If you want to discuss more informally, please join our Discord #development channel here: https://discord.gg/xbFVcTzV
Agreed.
Agreed. Note that exist_ok does not work on Python2, so we will have to either catch the exception or do
I think you're correct that this was never an option to set to I think the suggested implementation at this point are:
Something that is nice to have is to refactor some of the logic to keep
And finally, having tests for each of these is important, but we can cross that bridge when the implementation stabilizes. |
Just an update, this is on my list but I've been a bit busy the past couple of weeks. I've noted the ideas above and I think I'll get everything up to but not including the separate cache management module. That seems achievable for me in the short term and then we can build from that to a separate module moving forward if that's useful to do. |
This fixes a misbehaviour in environments where `XDG_CACHE_DIR` is not set and `~/.cache` does not exist, e.g. in the docker image. We simply attempt to create a temporary directory which will be registered with `atexit` to be deleted on exit using `shutil.rmtree()`. A Python 3 approach for future would be to use a `TemporaryDirectory` instead which would get cleaned up when the held object gets dropped out of the TLS stack. It is still possible for the `context.cache_dir` to be `None` if the default directory exists but is not writable, which means some code which uses this property is liable to blow up in such a circumstance.
b4acbd5
to
09f6756
Compare
This fixes a misbehaviour in environments where `XDG_CACHE_DIR` is not set and `~/.cache` does not exist, e.g. in the docker image. We simply attempt to create a temporary directory which will be registered with `atexit` to be deleted on exit using `shutil.rmtree()`. A Python 3 approach for future would be to use a `TemporaryDirectory` instead which would get cleaned up when the held object gets dropped out of the TLS stack. It is still possible for the `context.cache_dir` to be `None` if the default directory exists but is not writable, which means some code which uses this property is liable to blow up in such a circumstance.
This fixes a misbehaviour in environments where
XDG_CACHE_DIR
is notset and
~/.cache
does not exist, e.g. in the docker image. We simplyattempt to create a
TemporaryDirectory
which will be held until thepwn.context
is destroyed and the reference is dropped, at which pointit should be cleaned up.
By avoiding situations where
cache_dir
can beNone
, things thatexpect it to be a valid path string (e.g.
libcdb.search_by_hash()
andthe ROP cache helpers) won't blow up unless there is a surprising
failure to access the cache dir we intend to use for writing.