love
love copied to clipboard
Feature request: love.filesystem.newTempFile
there are two ways this could be implemented:
- create a file in /tmp and delete it when the program exits as part of the cleanup hook
- emulate a file via an in-memory buffer. this could be used on platforms that don't have a ramdisk, although it is a bit more difficult since love2d files have a more complicated interface than lua files
additionally, some way to easily move a temporary file to a permanent file would be nice.
What sort of things would you use this for?
I'm not sure I like the idea of having a step to clean up files, because LÖVE ultimately doesn't control how it closes (it may crash or be force-quit etc. depending on what people do.)
Not the OP, but it sounds like they just want access to the platform-equivalent 'tmpfile' system call, which if used would mean there'd be no cleanup to register as the operating system itself would handle that.
I apologize but I'm not able to find a good direct link for a pre-built system call on Apple iOS devices to do this.
This is mostly used on desktop platforms for various purposes relating to building a file such as a game save then copying it over to a permanent location as a single operation when done, so that in case of a crash temporary files won't accumulate and won't corrupt the save game without having to assemble the whole save-file in memory first.
Overall I'd rather implement exactly what people actually want/need rather than a more general temp file API - otherwise situations like this can happen https://github.com/libsdl-org/SDL/issues/9256
That being said, from what I understand some uses of temp files are actually extremely hard or even impossible to do in a way that's atomic and safe across different platforms. I also don't want to add an API that seems like it's intended to be safe but actually can't be in practice. So we'd need to do a lot of research into the specifics of individual use cases before implementing anything, I think.
The only two I can think of that's a reasonably sane use, one would be the example I gave above honestly and the other a strained variation of it that doesn't map well to Lua semantics at all so I'll skip it.
Realistically since there's no piecemeal assembling of files in Love2D anyways only an atomic-ish "replace the whole file with this data" there's no way to really benefit from this, for all use-cases I can think of looking across all manner of game engines I'm poking at... the existing filesystem.write() just does what's needed.
If in doubt write to a fixed file for the purpose/phase in the code, then .write(.read()) to its final destination is as atomic as one can get in Love2D AFAIK?
Realistically since there's no piecemeal assembling of files in Love2D
this hasn't been true for some time, see File:write and File:seek
Fwiw on our game we had a numbered backup system for the purposes of safe save files, you don't necessarily need a temporary file; instead you rotate your files and do redundancy checks
yeah, i was actually implementing a replay system, in my case i just write it to a certain position in the save dir.