UBIFS recovery truncates file to zero size

Jamie Lokier jamie at shareable.org
Thu Mar 12 07:11:25 EDT 2009

Iram Shahzad wrote:
> >>For the write back enabled case, I was expecting the
> >>following behaviour:
> >>    power went down before the file is sync-ed, so
> >>    on the next booting the file will be in its old state,
> >>    that is it will contain the contents which it had before
> >>    this write.
> >>
> >>But instead of being in the old state, it becomes empty file.
> >>Is this really expected(correct) behaviour?
> >
> >No.  How were you updating the file?
> The file is updated by a Java application in the following way.
> -----
> FileOutputStream str = new FileOutputStream(aFilename);

"new FileOutputStream" truncates the file.

> I switch off the power after confirming that the str.close() has been
> executed.
> P.S:
> If I add str.getFD().sync(); after the str.flush(), the file looks good in
> the next boot. However my question still remains for the case
> when I do not call str.getFD().sync:
> "why the file becomes empty rather than being in its old state?".

str.getFD().sync() won't be reliable if you lose power _between_ "new
FileOutputStream" and sync().  If that happens, the file may still be

If you are just appending data, use "new FileOutputStream(aFileName,
true)", write, flush, sync, close.

If you are changing the file contents, instead write to a temporary
file in the same directory, then sync the temporary file (after
flush), then do an atomic rename over the original file, then open the
directory and sync that somehow.

-- Jamie

More information about the linux-mtd mailing list