Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-05

File errors

22 messages · 7 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Mills

Hi,

How do you check for file errors such as "Disk Full" before attempting to write out a file to disk ?
You can easily check if a file exists with the fileExists function... Can something similar be done for other file errors ?

cheers
James

Reply by email

Peter Vreman
to James Mills
Show quoted text 6 lines
Hi, How do you check for file errors such as "Disk Full" before attempting to write out a file to disk ? You can easily check if a file exists with the fileExists function... Can something similar be done for other file errors ?

You can check with diskfree() how much space is left on a disk

Peter

Reply by email

James Mills
to Peter Vreman
Peter Vreman wrote 8 lines
> Hi, > > How do you check for file errors such as "Disk Full" before attempting > to write out a file to disk ? > You can easily check if a file exists with the fileExists function... > Can something similar be done for other file errors ? You can check with diskfree() how much space is left on a disk

Well the program I'm developing is irc services, any particular way you'd reccomend recoding it so it doesn't crash because of a 'Disk Full' error ?
I actually found out that my disk got full because iroffer (www.iroffer.org) has a bug in it and went into an infinite loop today 100% cpu usage writing out data to a log file using up my entire disk drive.

But this eventually caused services to crash because it couldn't write out it's own log...

cheers
James

Show quoted text 7 lines
Peter _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Peter Vreman
to James Mills
Show quoted text 13 lines
On Mon, May 05, 2003 at 11:35:14AM +0200, Peter Vreman wrote: > > Hi, > > > > How do you check for file errors such as "Disk Full" before attempting > > to write out a file to disk ? > > You can easily check if a file exists with the fileExists function... > > Can something similar be done for other file errors ? > > You can check with diskfree() how much space is left on a disk Well the program I'm developing is irc services, any particular way you'd reccomend recoding it so it doesn't crash because of a 'Disk Full' error ?

You can turn of io-checking and check the result of ioresult

{$I-}
 writeln(flog,logmessage)
{$I+}
if ioresult0 then
 writeln(stderr,'Error writing to disk');

Reply by email

James Mills
to Peter Vreman
Peter Vreman wrote 21 lines
> On Mon, May 05, 2003 at 11:35:14AM +0200, Peter Vreman wrote: >> > Hi, >> > >> > How do you check for file errors such as "Disk Full" before attempting >> > to write out a file to disk ? >> > You can easily check if a file exists with the fileExists function... >> > Can something similar be done for other file errors ? >> >> You can check with diskfree() how much space is left on a disk > > Well the program I'm developing is irc services, any particular way > you'd reccomend recoding it so it doesn't crash because of a 'Disk Full' > error ? You can turn of io-checking and check the result of ioresult {$I-} writeln(flog,logmessage) {$I+} if ioresult0 then writeln(stderr,'Error writing to disk');

Is this considered good programming practise ? Reason I ask is that I used to use borland's turbo pascal years ago and am aware of ioresult but am interested in how people before me have done it (eg: on linux systems).

The program I've been developing obviously have to be 100% stable :)

cheers
James

Show quoted text 6 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Peter Brooks

-----Original Message-----
From:
On Behalf Of James Mills
Sent: 05 May 2003 12:12
To:
Subject: Re: [fpc-pascal]File errors

Peter Vreman wrote
> On Mon, May 05, 2003 at 11:35:14AM +0200, Peter Vreman wrote: >> > Hi, >> > >> > How do you check for file errors such as "Disk Full" before

attempting

Show quoted text 17 lines
>> > to write out a file to disk ? >> > You can easily check if a file exists with the fileExists function... >> > Can something similar be done for other file errors ? >> >> You can check with diskfree() how much space is left on a disk > > Well the program I'm developing is irc services, any particular way > you'd reccomend recoding it so it doesn't crash because of a 'Disk Full' > error ? You can turn of io-checking and check the result of ioresult {$I-} writeln(flog,logmessage) {$I+} if ioresult0 then writeln(stderr,'Error writing to disk');

Is this considered good programming practise ? Reason I ask is that I used to use borland's turbo pascal years ago and am aware of ioresult but am interested in how people before me have done it (eg: on linux systems).

The program I've been developing obviously have to be 100% stable :)

You can't be 100% stable on a multi-user system unless you use a write where you flush the buffer to the disc and then do the error checking - even after you have written your buffer another program of higher priority might fill the disk so your buffer can't get to it and your error will only come at your next write.

The safest writes are to space that you already have allocated - random writes. That is what databases do. In the case of a log-file, make your file circular and pre-allocate it a certain size, then you will overwrite old messages, but you will never fill up the disk.

This is a bit more work for people who read the log-file later, but you can write a small reader to do it for them. There are lots of ways of doing this, but to be safe it is best to write your new record to the circular file - making sure it writes through any system buffers (a write with wait)
- then write the record number to the start of the file. That way your log-file reader can easily show the last n records which are usually the ones of interest.

On an msdros system you can make it fairly friendly by giving your circular file an extension that you map to your reader - then people using it don't need to be aware of how its done.

Reply by email

James Mills
to Peter Brooks
Peter Brooks wrote 64 lines
-----Original Message----- From: On Behalf Of James Mills Sent: 05 May 2003 12:12 To: Subject: Re: [fpc-pascal]File errors On Mon, May 05, 2003 at 11:52:46AM +0200, Peter Vreman wrote: > > On Mon, May 05, 2003 at 11:35:14AM +0200, Peter Vreman wrote: > >> > Hi, > >> > > >> > How do you check for file errors such as "Disk Full" before attempting > >> > to write out a file to disk ? > >> > You can easily check if a file exists with the fileExists function... > >> > Can something similar be done for other file errors ? > >> > >> You can check with diskfree() how much space is left on a disk > > > > Well the program I'm developing is irc services, any particular way > > you'd reccomend recoding it so it doesn't crash because of a 'Disk Full' > > error ? > > You can turn of io-checking and check the result of ioresult > > {$I-} > writeln(flog,logmessage) > {$I+} > if ioresult0 then > writeln(stderr,'Error writing to disk'); Is this considered good programming practise ? Reason I ask is that I used to use borland's turbo pascal years ago and am aware of ioresult but am interested in how people before me have done it (eg: on linux systems). The program I've been developing obviously have to be 100% stable :) You can't be 100% stable on a multi-user system unless you use a write where you flush the buffer to the disc and then do the error checking - even after you have written your buffer another program of higher priority might fill the disk so your buffer can't get to it and your error will only come at your next write. The safest writes are to space that you already have allocated - random writes. That is what databases do. In the case of a log-file, make your file circular and pre-allocate it a certain size, then you will overwrite old messages, but you will never fill up the disk. This is a bit more work for people who read the log-file later, but you can write a small reader to do it for them. There are lots of ways of doing this, but to be safe it is best to write your new record to the circular file - making sure it writes through any system buffers (a write with wait) - then write the record number to the start of the file. That way your log-file reader can easily show the last n records which are usually the ones of interest. On an msdros system you can make it fairly friendly by giving your circular file an extension that you map to your reader - then people using it don't need to be aware of how its done.

Thanks for that response, I guess is isn't possible to do on a multi-user system as you said. However a circular file would mean you'd only have a log-file history of length x ... Interesting way of doing it, would mean you'd never be able to view any logs beyond x since they don't exist.

cheers
James

Reply by email

Jilani Khaldi
to James Mills: Is this considered good programming practise ? Reason I ask is that I used to use…
Show quoted text 12 lines
> You can turn of io-checking and check the result of ioresult > > {$I-} > writeln(flog,logmessage) > {$I+} > if ioresult0 then > writeln(stderr,'Error writing to disk'); Is this considered good programming practise ? Reason I ask is that I used to use borland's turbo pascal years ago and am aware of ioresult but am interested in how people before me have done it (eg: on linux systems).

You can use exceptions:
...
try
writeln(flog,logmessage)
except
if sometheng goes wrong :(
end;
...

jk

Reply by email

Marco van de Voort
to Peter Vreman: You can check with diskfree() how much space is left on a disk Peter

[ Charset ISO-8859-1 unsupported, converting... ]

Show quoted text 8 lines
> Hi, > > How do you check for file errors such as "Disk Full" before attempting > to write out a file to disk ? > You can easily check if a file exists with the fileExists function... > Can something similar be done for other file errors ? You can check with diskfree() how much space is left on a disk

Can be dangerous in multi tasking and/or multi user system. Also that doesn't keep track of e.g. the reserved rootspace on unix.

Best way is to check ioresult after writing each block to see if it is zero?

Reply by email

James Mills
to Jilani Khaldi: You can use exceptions: ... try writeln(flog,logmessage) except if sometheng goes wrong :(
Jilani Khaldi wrote 22 lines
>> You can turn of io-checking and check the result of ioresult >> >> {$I-} >> writeln(flog,logmessage) >> {$I+} >> if ioresult0 then >> writeln(stderr,'Error writing to disk'); > >Is this considered good programming practise ? Reason I ask is that I >used to use borland's turbo pascal years ago and am aware of ioresult >but am interested in how people before me have done it (eg: on linux >systems). You can use exceptions: ... try writeln(flog,logmessage) except if sometheng goes wrong :( end; ... jk

Oh right. I forgot FPC had support for exceptions. Afterall it is an OO Language :P haha

Thank you, try except is probably more what I want.

cheers
James

Show quoted text 7 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

James Mills
to Marco van de Voort: [ Charset ISO-8859-1 unsupported, converting... ] Can be dangerous in multi tasking…
Marco van de Voort wrote 14 lines
[ Charset ISO-8859-1 unsupported, converting... ] > > Hi, > > > > How do you check for file errors such as "Disk Full" before attempting > > to write out a file to disk ? > > You can easily check if a file exists with the fileExists function... > > Can something similar be done for other file errors ? > > You can check with diskfree() how much space is left on a disk Can be dangerous in multi tasking and/or multi user system. Also that doesn't keep track of e.g. the reserved rootspace on unix. Best way is to check ioresult after writing each block to see if it is zero?

Or use a try except ?

cheers
James

_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Peter Brooks
The program I've been developing obviously have to be 100% stable :) You can't be 100% stable on a multi-user system unless you use a write

where

you flush the buffer to the disc and then do the error checking - even

after

Show quoted text 6 lines
you have written your buffer another program of higher priority might fill the disk so your buffer can't get to it and your error will only come at your next write. The safest writes are to space that you already have allocated - random writes. That is what databases do. In the case of a log-file, make your

file

circular and pre-allocate it a certain size, then you will overwrite old messages, but you will never fill up the disk. This is a bit more work for people who read the log-file later, but you

can

write a small reader to do it for them. There are lots of ways of doing this, but to be safe it is best to write your new record to the circular file - making sure it writes through any system buffers (a write with

wait)

Show quoted text 5 lines
- then write the record number to the start of the file. That way your log-file reader can easily show the last n records which are usually the ones of interest. On an msdros system you can make it fairly friendly by giving your

circular

file an extension that you map to your reader - then people using it don't need to be aware of how its done.

Thanks for that response, I guess is isn't possible to do on a multi-user system as you said. However a circular file would mean you'd only have a log-file history of length x ... Interesting way of doing it, would mean you'd never be able to view any logs beyond x since they don't exist.

Not necessarily. Good practice, say on a security logging system, is to use two circular log files. When one fills up you switch to the other - then you compress and back up the full log-file to tape/CD ROM/ftp server and send an alarm if you find that the log-files are filling up to quickly. That way one security denial of service (causing log-files to fill up discs or wrap around) is avoided. Over time you can size your log files so that they automatically back up and switch every day or two.

A really sensible approach on a unix system is to treat syslog this way and use syslog calls for all your messages. That means that they are interleaved with system and other messages (easy to disentangle with greps) so that you can get the full context of an error. If everybody did that then there wouldn't be all those hundreds of irritating log files all over the place!

Reply by email

Marco van de Voort
to James Mills: Or use a try except ? cheers James
Show quoted text 17 lines
On Mon, May 05, 2003 at 01:36:41PM +0200, Marco van de Voort wrote: > [ Charset ISO-8859-1 unsupported, converting... ] > > > Hi, > > > > > > How do you check for file errors such as "Disk Full" before attempting > > > to write out a file to disk ? > > > You can easily check if a file exists with the fileExists function... > > > Can something similar be done for other file errors ? > > > > You can check with diskfree() how much space is left on a disk > > Can be dangerous in multi tasking and/or multi user system. Also that > doesn't keep track of e.g. the reserved rootspace on unix. > > Best way is to check ioresult after writing each block to see if it is zero? Or use a try except ?

Standard fileoperations don't raise exceptions. That is for higher level constructs (like FCL file streams).

If basic block I/O would use exceptions, that would make it slow.

Reply by email

Peter Vreman
to Marco van de Voort
Show quoted text 25 lines
> On Mon, May 05, 2003 at 01:36:41PM +0200, Marco van de Voort wrote: > > [ Charset ISO-8859-1 unsupported, converting... ] > > > > Hi, > > > > > > > > How do you check for file errors such as "Disk Full" before > attempting > > > > to write out a file to disk ? > > > > You can easily check if a file exists with the fileExists > function... > > > > Can something similar be done for other file errors ? > > > > > > You can check with diskfree() how much space is left on a disk > > > > Can be dangerous in multi tasking and/or multi user system. Also that > > doesn't keep track of e.g. the reserved rootspace on unix. > > > > Best way is to check ioresult after writing each block to see if it is > zero? > > Or use a try except ? Standard fileoperations don't raise exceptions. That is for higher level constructs (like FCL file streams). If basic block I/O would use exceptions, that would make it slow.

basic IO also generates exceptions when SysUtils is used. The normal runtime errors will then be mapped to their corresponding exceptions.

Reply by email

James Mills
to Marco van de Voort: Standard fileoperations don't raise exceptions. That is for higher level constructs (like…
Marco van de Voort wrote 22 lines
> On Mon, May 05, 2003 at 01:36:41PM +0200, Marco van de Voort wrote: > > [ Charset ISO-8859-1 unsupported, converting... ] > > > > Hi, > > > > > > > > How do you check for file errors such as "Disk Full" before attempting > > > > to write out a file to disk ? > > > > You can easily check if a file exists with the fileExists function... > > > > Can something similar be done for other file errors ? > > > > > > You can check with diskfree() how much space is left on a disk > > > > Can be dangerous in multi tasking and/or multi user system. Also that > > doesn't keep track of e.g. the reserved rootspace on unix. > > > > Best way is to check ioresult after writing each block to see if it is zero? > > Or use a try except ? Standard fileoperations don't raise exceptions. That is for higher level constructs (like FCL file streams). If basic block I/O would use exceptions, that would make it slow.

Then why did Jilani Khaldi suggest using exceptions ?

cheers
James

_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

James Mills
to Peter Vreman: basic IO also generates exceptions when SysUtils is used. The normal runtime errors will…
Peter Vreman wrote 28 lines
>> On Mon, May 05, 2003 at 01:36:41PM +0200, Marco van de Voort wrote: >> > [ Charset ISO-8859-1 unsupported, converting... ] >> > > > Hi, >> > > > >> > > > How do you check for file errors such as "Disk Full" before >> attempting >> > > > to write out a file to disk ? >> > > > You can easily check if a file exists with the fileExists >> function... >> > > > Can something similar be done for other file errors ? >> > > >> > > You can check with diskfree() how much space is left on a disk >> > >> > Can be dangerous in multi tasking and/or multi user system. Also that >> > doesn't keep track of e.g. the reserved rootspace on unix. >> > >> > Best way is to check ioresult after writing each block to see if it is >> zero? >> >> Or use a try except ? > > Standard fileoperations don't raise exceptions. That is for higher level > constructs (like FCL file streams). > > If basic block I/O would use exceptions, that would make it slow. basic IO also generates exceptions when SysUtils is used. The normal runtime errors will then be mapped to their corresponding exceptions.

Really ? Thank you, That's rather usefull to know. I would then be able to use exceptions then without any performance penalty dispite what Peter wrote...

cheers
James

Show quoted text 5 lines
_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Florian Klaempfl
to James Mills: Then why did Jilani Khaldi suggest using exceptions ? cheers James
James Mills wrote 29 lines
On Mon, May 05, 2003 at 02:18:27PM +0200, Marco van de Voort wrote: >>On Mon, May 05, 2003 at 01:36:41PM +0200, Marco van de Voort wrote: >> >>>[ Charset ISO-8859-1 unsupported, converting... ] >>> >>>>>Hi, >>>>> >>>>>How do you check for file errors such as "Disk Full" before attempting >>>>>to write out a file to disk ? >>>>>You can easily check if a file exists with the fileExists function... >>>>>Can something similar be done for other file errors ? >>>> >>>>You can check with diskfree() how much space is left on a disk >>> >>>Can be dangerous in multi tasking and/or multi user system. Also that >>>doesn't keep track of e.g. the reserved rootspace on unix. >>> >>>Best way is to check ioresult after writing each block to see if it is zero? >> >>Or use a try except ? > >Standard fileoperations don't raise exceptions. That is for higher level >constructs (like FCL file streams). > >If basic block I/O would use exceptions, that would make it slow. Then why did Jilani Khaldi suggest using exceptions ?

They do if you have the sysutils unit in your uses statement (sysutils converts rtes to exceptions).

Reply by email