[En-Nut-Discussion] Strange behavior with files using file attributes '+' or 'a'

Michael Bieri mibieri at ee.ethz.ch
Wed Feb 27 22:54:43 CET 2008

> But there's another issue: When you write some bytes somewhere in the 
> file, the new bytes overwrite (as you expect) the old data. The problem 
> now is, that the filesize grows for every byte you write. After 
> overwriting some bytes somewhere in the file, the filesize grows with 
> the same number of bytes, and so you have the same number of bytes of 
> trash at the end of file.
> This problem probably could be caused in PhatFileWrite(...) on line 650 
> in phatfs.c. Is it correct, that the filesize is increased in every 
> case? I think, it should only be increased if one writes at the end of a 
> file.
The following modification seems to fix the problem: (phatfs.c, line 650)

            if((fcb -> f_pos) > (fcb -> f_dirent.dent_fsize)){
              fcb->f_dirent.dent_fsize += rc;

The modification is, that you check if the filepoiner (fcb -> f_pos) is 
greater than the old filesize stored in fcb -> f_dirent.dent_fsize. If 
the filepointer is smaller than the filesize, all writing operations 
have just overwritten existing data and the file didn't grow, so there's 
no need to update the filesize. If you wrote beyond the existing data, 
the file has grown and you have to update the filesize. As it seems, 
this works pretty well, but further tests are necessary to be really sure.

More information about the En-Nut-Discussion mailing list