comp.lang.ada
 help / color / mirror / Atom feed
* Opening a file vs. creating if not yet exists
@ 2008-08-18 15:03 Maciej Sobczak
  2008-08-19 12:36 ` Rob Norris
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Maciej Sobczak @ 2008-08-18 15:03 UTC (permalink / raw)


Hi,

The Open procedure for opening files expects the given external file
to already exist (otherwise the Name_Error is signalled).

The Create procedure truncates the file no matter what, even if the
mode for opening a file is Append_File.

I find this a bit annoying for example with log files, where the
natural scheme should be:
- if the file exists, open and append to it
- if the file does not exist, create it and append to it

What is the recommended way to deal with this?

begin
   Open (File, Append_File, "file.txt");
exception
   when Name_Error =>
      Create (File, Append_File, "file.txt");
end;

Is the above idiomatic (ie. well understood and accepted as common
practice)?

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-18 15:03 Opening a file vs. creating if not yet exists Maciej Sobczak
@ 2008-08-19 12:36 ` Rob Norris
  2008-08-19 12:58 ` amado.alves
  2008-08-19 14:23 ` Jacob Sparre Andersen
  2 siblings, 0 replies; 8+ messages in thread
From: Rob Norris @ 2008-08-19 12:36 UTC (permalink / raw)


On Mon, 18 Aug 2008 08:03:29 -0700 (PDT), Maciej Sobczak <see.my.homepage@gmail.com> wrote:

>
>What is the recommended way to deal with this?
>
>begin
>   Open (File, Append_File, "file.txt");
>exception
>   when Name_Error =>
>      Create (File, Append_File, "file.txt");
>end;
>
>Is the above idiomatic (ie. well understood and accepted as common
>practice)?

I don't know if it's common practice, but that's the main way we handle it for log files.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-18 15:03 Opening a file vs. creating if not yet exists Maciej Sobczak
  2008-08-19 12:36 ` Rob Norris
@ 2008-08-19 12:58 ` amado.alves
  2008-08-19 14:23 ` Jacob Sparre Andersen
  2 siblings, 0 replies; 8+ messages in thread
From: amado.alves @ 2008-08-19 12:58 UTC (permalink / raw)


> begin
>    Open (File, Append_File, "file.txt");
> exception
>    when Name_Error =>
>       Create (File, Append_File, "file.txt");
> end;
>
> Is the above idiomatic (ie. well understood and accepted as common
> practice)?

There seems to be two schools of though regarding the use of
exceptions for logic.
Some of us accept the idiom happily.
But probably the most conventional school is the "don't"
So in this particular case and if you are using Ada 2005 then you
might consider the uncontroversial

if Ada.Directories.File_Exists (Filename) then
   Open (...)
else
   Create (...)
end if;



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-18 15:03 Opening a file vs. creating if not yet exists Maciej Sobczak
  2008-08-19 12:36 ` Rob Norris
  2008-08-19 12:58 ` amado.alves
@ 2008-08-19 14:23 ` Jacob Sparre Andersen
  2008-08-19 14:58   ` Maciej Sobczak
  2 siblings, 1 reply; 8+ messages in thread
From: Jacob Sparre Andersen @ 2008-08-19 14:23 UTC (permalink / raw)


Maciej Sobczak wrote:

> I find this a bit annoying for example with log files, where the
> natural scheme should be:
> - if the file exists, open and append to it
> - if the file does not exist, create it and append to it
> 
> What is the recommended way to deal with this?
> 
> begin
>    Open (File, Append_File, "file.txt");
> exception
>    when Name_Error =>
>       Create (File, Append_File, "file.txt");
> end;
> 
> Is the above idiomatic (ie. well understood and accepted as common
> practice)?

I am certain it is well understood and commonly used, but in general
it is not advised to use exceptions in ordinary control flow.  Amado's
suggestion using Ada.Directories.File_Exists is definitely closer to
the advised practice.

The problem with both of these solutions is that they are not atomic.
POSIX.IO.Open_Or_Create is more appropriate (if you are targeting a
standards compliant operating system), since it solves the problem as
an atomic operation.

Greetings,

Jacob
-- 
"Any, sufficiently advanced, technology is indistinguishable from magic."



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-19 14:23 ` Jacob Sparre Andersen
@ 2008-08-19 14:58   ` Maciej Sobczak
  2008-08-21 15:41     ` Martin Krischik
  2008-08-21 15:42     ` Martin Krischik
  0 siblings, 2 replies; 8+ messages in thread
From: Maciej Sobczak @ 2008-08-19 14:58 UTC (permalink / raw)


On 19 Sie, 16:23, Jacob Sparre Andersen <spa...@nbi.dk> wrote:

> I am certain it is well understood and commonly used, but in general
> it is not advised to use exceptions in ordinary control flow.  Amado's
> suggestion using Ada.Directories.File_Exists is definitely closer to
> the advised practice.

I understand this, but for some reasons I have to stick to Ada 95
features in this particular case.

The atomicity of the complete solution is not an issue, since there is
no chance for the log file to be deleted by some other entity.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-19 14:58   ` Maciej Sobczak
@ 2008-08-21 15:41     ` Martin Krischik
  2008-08-21 21:06       ` Maciej Sobczak
  2008-08-21 15:42     ` Martin Krischik
  1 sibling, 1 reply; 8+ messages in thread
From: Martin Krischik @ 2008-08-21 15:41 UTC (permalink / raw)
  To: Maciej Sobczak

Maciej Sobczak schrieb:

> The atomicity of the complete solution is not an issue, since there is
> no chance for the log file to be deleted by some other entity.

Realy no chance? Did you consider

rm -rf *.log

or

del *.log

Ok I am pulling your leg here.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-19 14:58   ` Maciej Sobczak
  2008-08-21 15:41     ` Martin Krischik
@ 2008-08-21 15:42     ` Martin Krischik
  1 sibling, 0 replies; 8+ messages in thread
From: Martin Krischik @ 2008-08-21 15:42 UTC (permalink / raw)


Maciej Sobczak schrieb:

> The atomicity of the complete solution is not an issue, since there is
> no chance for the log file to be deleted by some other entity.

Realy no chance? Did you consider

rm -rf *.log

or

del *.log

Ok I am pulling your leg here.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Opening a file vs. creating if not yet exists
  2008-08-21 15:41     ` Martin Krischik
@ 2008-08-21 21:06       ` Maciej Sobczak
  0 siblings, 0 replies; 8+ messages in thread
From: Maciej Sobczak @ 2008-08-21 21:06 UTC (permalink / raw)


On 21 Sie, 17:41, Martin Krischik <krisc...@users.sourceforge.net>
wrote:

> > The atomicity of the complete solution is not an issue, since there is
> > no chance for the log file to be deleted by some other entity.
>
> Realy no chance? Did you consider
>
> rm -rf *.log

Yes, and this is not an issue for the reason of hazards.
Since the action of removing files this way would be asynchronous with
regards to the logging program, we should really protect against
calling it in both cases: "just before" opening the file as well as
"just after". The latter is a problem - no amount of trickery in the
program (atomic or not) can protect this case and if that happens the
log file becomes invisible (except for the program which would keep
the only, but unnamed, link to the file) and the file would vanish at
the program exit. Not very useful.
That's why we have to either assume that this never happens - and then
the first case need no protection - or that it is hopeless anyway.
Nothing to do in either case.

(the above assumes POSIX, Windows may behave differently)

> Ok I am pulling your leg here.

Sure. That's what makes usenet more funny. :-)

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-08-21 21:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-18 15:03 Opening a file vs. creating if not yet exists Maciej Sobczak
2008-08-19 12:36 ` Rob Norris
2008-08-19 12:58 ` amado.alves
2008-08-19 14:23 ` Jacob Sparre Andersen
2008-08-19 14:58   ` Maciej Sobczak
2008-08-21 15:41     ` Martin Krischik
2008-08-21 21:06       ` Maciej Sobczak
2008-08-21 15:42     ` Martin Krischik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox