Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Santiago A.
El 21/02/2017 a las 22:12, James Richters escribió:
>
>  
>
> Thanks for any advice on this
>

My first action would be to guarantee that the file is closed with a
try...finally.

AssignFile(BitFile,'BitSave.pax');

ReWrite(BitFile);

try



finally

  CloseFile(BitFile);

end;


Second:

Do you have the file opened with an editor, or things like that, while
running the program?.

Third:

Check that all variables have valid values and that values are converted
to string properly to be printable.

Fourth:

Do you use somewhere {$I+} {$I-}? I also had some problems with that
with old programs.

Fourth:

What's the type BitFile?
BitFile: TextFile;
BitFile: File;

Maybe using writeln with non-textfile files may cause problems.

-- 
Saludos

Santiago A.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Jürgen Hestermann

Am 2017-02-21 um 22:12 schrieb James Richters:
> I have a freepascal Windows console application that I recently ported over 
from DOS Turbo Pascal.   I am have a small settings file that I write to disk that 
keeps getting corrupted.  It happens only occasionally, but it’s always this one 
file, no others.Do I need to do something specific to force windows to write 
the file to disk and not have it in some kind of cache or buffer?   I am using the 
following code to write the file:
>Assign(BitFile,'BitSave.pax');
>ReWrite(BitFile);
>WriteLn(BitFile,XADJ:1:8);
>WriteLn(BitFile,YADJ:1:8);
>WriteLn(BitFile,ZADJ:1:8);
>WriteLn(BitFile,WADJ:1:8);
>WriteLn(BitFile,AADJ:1:8);
>WriteLn(BitFile,TADJ:1:8);
>WriteLn(BitFile,VADJ:1:8);
>WriteLn(BitFile,UADJ:1:8);
>WriteLn(BitFile,CurrentTool);
>WriteLn(Bitfile,P_Value[4]);
>Close(BitFile);
> The above only happens when I make a change to one of the settings, and the 
system is never having any issue at that time, yet upon occasion I will have an 
error opening the file and when I inspect it, it’s just a long string of [nul].   
I am not leaving the file open, I’m assigning it, writing to it, then closing it, 
so there should be no opportunity for data to be lost.  I am having the same issue 
on about 8 different computers, most of which are running solid state hard drives. 
It’s always just this one file that is affected, nothing else.  No other process 
ever uses this file, so it must be my freepascal program that is leaving the file 
in a vulnerable state.
> My understanding was the Close(file); would save the file all the way to disk 
and flush any buffers that were holding it, but I’m wondering if that was only 
true for DOS and there is some other windows cache or buffer that also needs to be 
instructed to flush to truly save the file all the way to disk and close it.

How is "Bitfile" declared?
According to http://wiki.freepascal.org/Text
it should be either

var BitFile : TextFile;

or

var BitFile : Text;

Also, I am not sure whether "Close" is equivalent to "CloseFile" and  "Assign" is 
equivalent to "AssignFile".
I remenber that I had some issues using the old (TP7) notations but I am don't 
remember the details.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread James Richters
Thanks for the advice on things to look at. 

 

Here is some more information.  First of all, this happens extremely rarely, 
about once a month or two on a pc here and another there, but it’s odd that 
it’s always this one file, and it should not be happening at all.I’m using 
turbo pascal compatibility mode, and it’s a console application, there is no 
multi threading going on, it’s straight inline code.  No other programs on the 
system ever access this file and there is no reason to open it with a text 
editor or anything like that.The error I get is when trying to open the 
file to read it.  It’s something like read past end of file, because the 
corrupted file is just one long line, once I read that, the second read is past 
the end of file.  I can do checking to get the file length and avoid the error, 
but that doesn’t solve the real issue, which is that the data that is supposed 
to be in the file is just gone.  

 

Bitfile is type TEXT like this: 

 

Bitfile: Text;

 

and my variables are all doubles.  

Normally what happens is, everything is fine for a month or two, but then one 
day someone will come in and notice the error on the screen, it was fine the 
night before, the program running overnight, in the morning the program is not 
running,  and when the attempt is made to run it,  The error is found and the 
cause of the error is BitSave.pax has been corrupted and is now a long string 
of [nul]   the computers this runs on are all windows 10 with Samsung solid 
state hard drives.I suspect that the majority of the time, this has 
happened due to winders doing an automatic update and restarting without 
permission as windows 10 annoyingly likes to do, thus crashing my program in 
order to do this restart that is SO important.  I have shut off windows update 
via group policy, but still had the problem since, however I cannot be certain 
there was not a power failure.   The computers are set to re-power up after 
power loss in BIOS and the program is set to run on startup, in fact the 
computers have no other function than to run this one program, they are never 
used for anything else.  The procedure that writes out this file is only ever 
used when one of the variables is changed, and they can only be changed by 
manual input, which is not happening anymore when the issue occurs.   The thing 
is, even if windows forced a restart while my program was running this file 
should have been closed at the time, because if someone was standing there 
editing the variables, they would see the restart notification and close the 
program first, or be able to tell me they had a power failure. 

 

As a temporary measure, I’m just writing out the file twice so at least I have 
a backup, but I don’t see why the backup would not be corrupted by this same 
issue, so that’s probably pretty useless.

Unfortunately this is difficult to troubleshoot because it happens so rarely. I 
have tried to force it to happen by terminating my program with task manager 
and cannot cause the file to be corrupted.   I have not yet tried to duplicate 
the error by shutting down the system while the program is running.  I could 
try that. If it does turn out to be an issue with my program running during 
shutdown,  is there some way I can detect a pending shutdown and exit normally 
before the shutdown happens?   It wouldn’t really solve the cause of the issue, 
but it would possibly help prevent some of the occurrences related to 
shutdowns, but would not help with power failures.

 

I am unable to use Try in turbo pascal compatible mode.  If I use it, I get 
identifier not found ‘Try’   If I attempt to compile in some mode other than 
turbo pascal compatible, I end up with thousands of other errors, so I would 
pretty much have to do a complete re-write.   I can’t see how the close could 
not be run, there is no way out of my procedure without running it since it’s a 
single thread console application.

 

James

 

 

From: fpc-pascal-boun...@lists.freepascal.org 
[mailto:fpc-pascal-boun...@lists.freepascal.org] On Behalf Of Santiago A.
Sent: Wednesday, February 22, 2017 3:48 AM
To: FPC-Pascal users discussions 
Subject: Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

 

El 21/02/2017 a las 22:12, James Richters escribió:

 

Thanks for any advice on this


My first action would be to guarantee that the file is closed with a 
try...finally.

AssignFile(BitFile,'BitSave.pax'); 

ReWrite(BitFile);

try



finally

  CloseFile(BitFile);

end;


Second:

Do you have the file opened with an editor, or things like that, while running 
the program?.

Third:

Check that all variables have valid values and that values are converted to 
string properly to be printable.

Fourth:

Do you use somewhere {$I+} {$I-}? I also had some problems with that with old 
programs.

Fourth:

What's the type BitFile? 
BitFile: TextFile;
BitFile: File;

Maybe using writeln with non-textfile files may cause 

Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Peter
On 21/02/17 21:12, James Richters wrote:


> My understanding was the Close(file); would save the file all the way to
> disk and flush any buffers that were holding it, 

I would try adding a Flush(File) before the Close, even though that
should not really be needed.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Sven Barth
Am 22.02.2017 13:12 schrieb "Jürgen Hestermann" :
> Also, I am not sure whether "Close" is equivalent to "CloseFile" and
"Assign" is equivalent to "AssignFile".
> I remenber that I had some issues using the old (TP7) notations but I am
don't remember the details.

They are the same. The ones without "File" are from TP times and were
renamed in Delphi to avoid confusing them with the Assign and Close methods
of the component hierarchy.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Santiago A.
El 22/02/2017 a las 13:17, James Richters escribió:

> The error I get is when trying to open the file to read it.  It’s
> something like read past end of file, because the corrupted file is
> just one long line, once I read that, the second read is past the end
> of file.  I can do checking to get the file length and avoid the
> error, but that doesn’t solve the real issue, which is that the data
> that is supposed to be in the file is just gone. 
>

Just two lines of research ;-)

A) While you are reading the file you call the procedure that writes it.

B) The is a bug, not in writing the part, but in the reading code:
Somewhere when you read the file, you don't close it, so the file
remains open. Usually it's not a big issue, you can open the file
several times, and when you close the program everything is closed. But
some times, when a parameter is changed you rewrite it while it is still
open and everything gets messed. So this two events must happen, the
program has executed the part of code that lets the file open and next a
parameter is changed.

>  
>
> The thing is, even if windows forced a restart while my program was
> running this file should have been closed at the time, because if
> someone was standing there editing the variables, they would see the
> restart notification and close the program first, or be able to tell
> me they had a power failure.
>
>  
>
> As a temporary measure, I’m just writing out the file twice so at
> least I have a backup, but I don’t see why the backup would not be
> corrupted by this same issue, so that’s probably pretty useless.
>
Yes, I think that's the way to go.

1) Before writing, read it and if it's ok make a backup of the file
2) Write the file.
3) Read again what you have written to check whether it's ok.
4) Make a second backup.

That way, when you load parameters you have two backups to recover. And
maybe a clue of what's going wrong.
Beside, I would add a timestamp inside the file.

In addition, you could use flush and {$I-}


-- 
Saludos

Santiago A.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread James Richters
Here is the procedure that reads the file. It is only read once when the 
program is started, then written to when changes are made.

I don’t have a good reason for having a different file variable name in my read 
procedure than my write procedure, but those are local varaibles to the 
procedures anyway so it shouldn’t matter what I call them.

 

Var

   locfile : text;

If FileExists('Bitsave.pax') Then

Begin

   Writeln('BitSave.pax Found, Loading... ');

   Assign(locfile,'BitSave.pax');

   reset(locfile);

   ReadLn(locfile,XAdj);

   ReadLn(locfile,YAdj);

   ReadLn(locfile,ZAdj);

   ReadLn(locfile,WAdj);

   ReadLn(locfile,AAdj);

   ReadLn(locfile,TAdj);

   ReadLn(locfile,VAdj);

   ReadLn(locfile,UAdj);

   ReadLn(locfile,CurrentTool);

   ReadLn(locfile,P_Value[4]);

   Close(locfile);

End

 

I will do as you suggest and read what I just thought I wrote back to verify 
it.   I may also incorporate some kind of checksum as well and it wouldn’t be a 
bad idea to check how many lines are in the file before I try to read it, just 
to make sure I don’t get read past end of file error.  It seems like a lot just 
to save just 10 numbers, but it’s a pain if those numbers are gone!I will 
also try flush and {$i-}  as well.  

 

Thanks to everyone for your advice and help

 

Jim

 

From: fpc-pascal-boun...@lists.freepascal.org 
[mailto:fpc-pascal-boun...@lists.freepascal.org] On Behalf Of Santiago A.
Sent: Wednesday, February 22, 2017 11:07 AM
To: FPC-Pascal users discussions 
Subject: Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

 

El 22/02/2017 a las 13:17, James Richters escribió:

The error I get is when trying to open the file to read it.  It’s something 
like read past end of file, because the corrupted file is just one long line, 
once I read that, the second read is past the end of file.  I can do checking 
to get the file length and avoid the error, but that doesn’t solve the real 
issue, which is that the data that is supposed to be in the file is just gone.  


Just two lines of research ;-)

A) While you are reading the file you call the procedure that writes it.

B) The is a bug, not in writing the part, but in the reading code: Somewhere 
when you read the file, you don't close it, so the file remains open. Usually 
it's not a big issue, you can open the file several times, and when you close 
the program everything is closed. But some times, when a parameter is changed 
you rewrite it while it is still open and everything gets messed. So this two 
events must happen, the program has executed the part of code that lets the 
file open and next a parameter is changed.




 

The thing is, even if windows forced a restart while my program was running 
this file should have been closed at the time, because if someone was standing 
there editing the variables, they would see the restart notification and close 
the program first, or be able to tell me they had a power failure. 

 

As a temporary measure, I’m just writing out the file twice so at least I have 
a backup, but I don’t see why the backup would not be corrupted by this same 
issue, so that’s probably pretty useless.

Yes, I think that's the way to go.

1) Before writing, read it and if it's ok make a backup of the file
2) Write the file.
3) Read again what you have written to check whether it's ok. 
4) Make a second backup.

That way, when you load parameters you have two backups to recover. And maybe a 
clue of what's going wrong.
Beside, I would add a timestamp inside the file.

In addition, you could use flush and {$I-}





-- 
Saludos
 
Santiago A.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Jonas Maebe

On 22/02/17 13:17, James Richters wrote:

No other programs on the system ever access this file


Not even virus scanners?


Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread James Richters
I suppose a virus scanner could access it, I didn't think of that.   I guess
I should have said no other program intentionally access the file.   It's
just a way for me to store some variables and get them back, it serves no
other purpose.   

-Original Message-
From: fpc-pascal-boun...@lists.freepascal.org
[mailto:fpc-pascal-boun...@lists.freepascal.org] On Behalf Of Jonas Maebe
Sent: Wednesday, February 22, 2017 3:47 PM
To: FPC-Pascal users discussions 
Subject: Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

On 22/02/17 13:17, James Richters wrote:
> No other programs on the system ever access this file

Not even virus scanners?


Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FreePascal Windows - Force files to write to disk

2017-02-22 Thread Mark Morgan Lloyd

On 22/02/17 21:00, James Richters wrote:

I suppose a virus scanner could access it, I didn't think of that.   I guess
I should have said no other program intentionally access the file.   It's
just a way for me to store some variables and get them back, it serves no
other purpose.


A virus scanner might disagree. .pax is a known type which might contain 
an archive hence a whole lot of nasty stuff, and it probably isn't wise 
to use it casually... call it .txt or use the standard .ini format.


--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal