[Geany-devel] Changed file saving implementation for systems with GIO

Dimitar Zhekov dimitar.zhekov at xxxxx
Fri Nov 5 20:33:11 UTC 2010

On Fri, 05 Nov 2010 19:50:59 +0100
Colomban Wendling <lists.ban at herbesfolles.org> wrote:

> Le 05/11/2010 20:08, Dimitar Zhekov a ecrit :
> > 
> > a. Create filename-foo, write data to it, abort and unlink on failure.
> > [...]
> Hey, that's quite clever :) We would then avoid to have to read the
> original file.

It's a well-known algorithm, but there is a better one, assuming that
the underlying I/O system supports open for read-write and truncate. I
checked GIO, and it does: g_file_open_readwrite, g_seekable_truncate. :D

1. Open the file for reading and writing.
2. If the new data is longer, append the extra part only (thus claiming
the required disk space).
If that fails, truncate to the original size and abort.
3. Write the data (without the extra part, if any).
4. If the new data is shorter, truncate.

That's almost 100% safe (there is no apparent reason for truncate to a
smaller size to fail), preserves everything, no extra disk space, not
even extra I/O.

Of course, not all GIO files will be truncatable, that can be checked
with g_seekable_can_truncate. But why g_file_replace() for truncatables
isn't implemented like above is beyond me.

E-gards: Jimmy

More information about the Devel mailing list