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

Ernst Stippl ernst at stippl.org
Fri Feb 29 15:56:52 CET 2008


Isn't there one (unlikely?) special case where a write begins in the
existing data area, but partially writes beyond the current file size?
If the data has already been written (as is the case in line 650) and f_pos
has already been advanced (line 637) then this condition may be tested by: 

            if(((fcb -> f_pos)-rc) < (fcb -> f_dirent.dent_fsize)){

In other words: if the current position reduced by the writecount is smaller
than the filesize -> it’s a partial "past eof" write.


-----Ursprüngliche Nachricht-----
Von: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] Im Auftrag von Michael Bieri
Gesendet: Mittwoch, 27. Februar 2008 22:55
An: Ethernut User Chat (English)
Betreff: Re: [En-Nut-Discussion] Strange behavior with files using file
attributes '+' or 'a'

> 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.

No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.516 / Virus Database: 269.21.0/1296 - Release Date: 24.02.2008

More information about the En-Nut-Discussion mailing list