Closed GregoryConrad closed 1 year ago
Hm. That's annoying.
It looks like the standard library looks at the TMPDIR
env variable on all platforms. Have you considered setting that? I.e., std::env::set_var("TMPDIR", ...)
.
That will have the benefit of applying to any library looking for the temporary directory.
@Stebalien Sure, I can see if that does the trick! That would be a really nice solution, assuming it works. However, std::env::tempdir
is documented as only returning /data/local/tmp
on Android, so I would still be a bit hesitant to relying upon an un-documented implementation detail like that (I wouldn't want my builds to start mysteriously failing because std
changed up their implementation of tempdir
).
It's documented:
On Unix, returns the value of the TMPDIR environment variable if it is set, otherwise for non-Android it returns /tmp.
(android is a unix platform)
@Stebalien Sorry; I misinterpreted this:
On Unix, returns the value of the TMPDIR environment variable if it is set, otherwise for non-Android it returns /tmp. On Android, since there is no global temporary folder (it is usually allocated per-app), it returns /data/local/tmp.
I took that second sentence out of context; I thought it meant it will always return /data/local/tmp
, not just when TMPDIR
is unset.
std::env::set_var("TMPDIR", ...)
did the trick as you suggested! Thank you! Closing this issue as that fixes it.
Hi!
tempfile
is a transitive dependency of another crate I've been using and I noticed Android (API level <33 from what it seems) has permission issues on many different devices/emulators when using the directory returned bystd::env::tempdir
, which, on Android, always returns/data/local/tmp
.Very unfortunately, there is no clear fix because the only way to access an application's specific temporary folder in Android is via
context.getCacheDir()
, which requires acontext
object from the application itself.Instead of attempting a hacky fix for Android specifically, I think it'd be a good idea to introduce a new global in
tempfile
(which can be used by all platforms) that accepts an "override" temporary directory. If set, the global overrides the directory returned bystd::env::tempdir
for all future invocations oftempfile
. This maintains 100% backwards compatibility on all platforms and also allows anyone to override the default directory, which is necessary on platforms like Android. (To be completely clear, I really am not a fan that such a change is required, but Android doesn't give you any other option; they really didn't consider native code when they designed that API.)Was thinking something along the lines of:
Was planning on
std
'sRwLock
since this crate already relies uponstd
.parking_lot
would also be good, but it is another dependency. I'd imagineRwLock
does have some slight overhead due to callingread()
every time we currently callstd::env::tempdir
, but I would also imagine that overhead is not nearly as much as actually creating a temporary file, which is exactly whattempfile()
does. Thus, I think the overhead introduced should be negligible (thread synchronization, especially with almost exclusivelyread()
s, is not as expensive as filesystem access).Would a PR for such a feature be accepted @Stebalien? Thanks.