love.filesystem.rename (for atomic updates)

Issue #907 wontfix
created an issue

Could you please implement a renaming function in love.filesystem, this way updating a file (e.g. savegames) can be made less risky (that is, crashes won't corrupt the file, or at least not as easily).

A typical lua example:

    TMP_PREFIX = ".update_"  -- for deriving temp write name from target

    -- writeStuff: write <what> to file named <filename>
    -- An error can happen at any point during execution of this and the
    -- old file will still be there.  Even if the system crashes during
    -- the rename, the filesystem will either have renamed the file or
    -- it hasn't -- you'll either get the intact old version or the new
    -- one, never a half-written file or somesuch...
    function writeStuff( filename, what )
        local f = TMP_PREFIX..filename, "w" )
        f:write( str )
        f:close( )
        os.rename( TMP_PREFIX..filename, filename )

Comments (5)

  1. Alex Szpakowski

    PhysFS (which love.filesystem uses internally) doesn't have a rename function, but I guess we could call the standard rename() with a correctly adjusted filepath under the hood.

  2. Alex Szpakowski

    After some consideration and testing, this isn't really feasible I think - for example, renaming a file in that manner while the file is opened through PhysFS would likely break the PhysFS file object.

  3. Log in to comment