Is Lof accurate before calling FlushFileBuffers?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Is Lof accurate before calling FlushFileBuffers?

Post by Mistrel »

It I load a file and then write to it, is the result from Lof guaranteed to be correct (versus FileSize) if I don't call FlushFileBuffers first?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Is Lof accurate before calling FlushFileBuffers?

Post by Rescator »

I was about to say yes here but then realized I too have no idea if Lof is updated along with the writebuffer or not... *scratches head*
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Is Lof accurate before calling FlushFileBuffers?

Post by DoubleDutch »

Good question. I would think that until closefile() it wouldn't be, but don't know for sure.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Is Lof accurate before calling FlushFileBuffers?

Post by UserOfPure »

Try it! :roll:
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Re: Is Lof accurate before calling FlushFileBuffers?

Post by mdp »

In Linux, if you compile

Code: Select all

If OpenFile(0,"/tmp/testlof")
   PrintN( Str(Lof(0)) )
   WriteStringN(0,"123123")
   PrintN( Str(Lof(0)) )
   WriteStringN(0,Str(Random(99999999)))
   PrintN( Str(Lof(0)) )
   CloseFile(0)
EndIf
and do an strace for if, you obtain

Code: Select all

[...]
open("/tmp/testlof", O_RDWR)            = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x731000
write(1, "4\n", 24
)                      = 2
fstat64(3, {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x64a000
_llseek(3, 0, [0], SEEK_CUR)            = 0
write(3, "123123\n", 7)                 = 7

fstat64(3, {st_mode=S_IFREG|0644, st_size=7, ...}) = 0
write(1, "7\n", 27
)                      = 2
gettimeofday({1265539169, 589739}, NULL) = 0
write(3, "36040943\n", 9)               = 9

fstat64(3, {st_mode=S_IFREG|0644, st_size=16, ...}) = 0
write(1, "16\n", 316
)                     = 3
close(3)                                = 0
[...]
I see that an fstat64 function is called everytime you call a Lof(), so I would presume that the answer would be yes.
But what if the filesystem management, in certain occasions, holds the actual filewriting... That the new file operation entails flushing would be proper engineering IMHO.
I would also test on Win and Mac.

Interesting question.
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Re: Is Lof accurate before calling FlushFileBuffers?

Post by mdp »

@UserOfPure: I believe the point is not "will it happen now", but "will it always happen"
@Mistrel: in doubt, you thought about keeping track of filesize within the code (you are the one writing into the file afterall), did not you?
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Is Lof accurate before calling FlushFileBuffers?

Post by freak »

Lof() calls FlushBuffers() if data is in the write buffer, so it will be correct.
quidquid Latine dictum sit altum videtur
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Is Lof accurate before calling FlushFileBuffers?

Post by DoubleDutch »

Freak: you and Fred seem to have thought of everything! :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Is Lof accurate before calling FlushFileBuffers?

Post by Mistrel »

Thank you, freak. That's all I needed to know. :D
Post Reply