-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
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
Add temp utilities (alias OS::get_temp_dir()
, FileAccess::create_temp()
, and DirAccess::create_temp()
)
#98397
Conversation
c11d9ff
to
67a2fa5
Compare
06160c8
to
0c00c5a
Compare
46e4d80
to
9496552
Compare
I've tried to use the following code, but despite func _ready() -> void:
var tmp_file := FileAccess.create_tmp(FileAccess.WRITE_READ, ".txt")
tmp_file.store_string("I am *not* persisted after the file is closed.")
tmp_file.close()
OS.shell_open(tmp_file.get_path_absolute()) # Should fail because the file has been removed at this point.
var tmp_file_keep := FileAccess.create_tmp(FileAccess.WRITE_READ, ".txt", true)
tmp_file_keep.store_string("I am persisted after the file is closed.")
tmp_file.close()
OS.shell_open(tmp_file_keep.get_path_absolute()) # Should work because `keep` is `true`.
PS: Do modes other than |
9496552
to
7e0f779
Compare
@Calinou The cleanup is done on the I did this because |
This comment was marked as resolved.
This comment was marked as resolved.
d480cc4
to
152e24c
Compare
What I did is that I create the file first with WRITE, then close, then open it again with the mode wished. |
https://chat.godotengine.org/channel/android?msg=3peujMoKbDJCQ0eul
https://github.com/Alex2782/godot/tree/get_tmp_dir_for_android 5 files changed, +41 lines: Alex2782@9dd4af4 func _ready() -> void:
print("get_cache_dir(): ", OS.get_cache_dir())
print("get_tmp_dir(): ", OS.get_tmp_dir()) Tested on Android 13, Samsung Tab S7. I'm still not sure if I've taken everything into account correctly. |
3f1822b
to
cb553c1
Compare
5a7f5fe
to
e69feae
Compare
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.
Tested locally on Linux, it works as expected.
const ITERATIONS = 1_000
func _ready() -> void:
for __ in ITERATIONS:
FileAccess.create_tmp(FileAccess.WRITE_READ, "hello", "txt", true)
DirAccess.create_tmp("world", true)
After checking the contents of /tmp
, there are 1,000 files and folders created respectively.
We try to avoid abbreviations in the API, so we should probably avoid We could go all the way with Thoughts? |
|
|
OS::get_tmp_dir()
, FileAccess::create_tmp()
, and DirAccess::create_tmp()
)OS::get_temp_dir()
, FileAccess::create_temp()
, and DirAccess::create_temp()
)
I added a commit that renames |
349bb6f
to
eb7707a
Compare
OS::get_temp_dir()
, FileAccess::create_temp()
, and DirAccess::create_temp()
)OS::get_temp_dir()
, FileAccess::create_temp()
, and DirAccess::create_temp()
)
eb7707a
to
128950e
Compare
I would still suggest |
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.
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
I prefer the plain |
90d7d3d
to
a025ca0
Compare
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.
Tested on macOS, iOS and Windows, works as expected.
Co-authored by @Alex2782 for the Android bindings. Many thanks to the reviewers also. Co-authored-by: Alex <alex.hart.278@gmail.com>
a025ca0
to
1b3e483
Compare
Thanks! |
This PR adds
OS::get_temp_dir()
,FileAccess::create_temp()
, andDirAccess::create_temp()
functions (the PR originally spelledtmp
instead oftemp
), for all the supported platforms.The concept of temporary files and directories
Temporary files and cache files are really similar, but they are distinct.
So this is why Godot should support and provide an API for this special kind of files.
Methods
OS.get_temp_dir()
This function returns the global OS temporary dir for the application. It may be the root
/tmp
directory for most of the Unix/Linux systems, but for other platforms, it is the temporary dir given by the OS.If no such thing as a temporary directory exist for the OS (ie. the Android platform), a directory inside the cache directory is chosen.
FileAccess.create_temp(int mode_flags, String prefix = "", String extension = "", bool keep = false)
This static function returns a
FileAccess
object with a temporary file open.For sorting purposes, you can set
prefix
to the name of your project, just to be able to quickly debug temporary files if needed.As ResourceLoaders usually detect the type of the file with the extension, you can pass a custom extension to add to the file name.
And if you set
keep
to true, the file will not get cleaned up when theFileAccess
instance will be freed.DirAccess.create_temp(String prefix = "", bool keep = false)
This static function returns a
DirAccess
object with a temporary directory open. Useful if you need temporary named files and directories.prefix
andkeep
exist such as inFileAccess.create_temp()
.If
keep
is false, it will recursively delete the contents of the temporary directory when theDirAccess
instance will be freed.