comp.lang.ada
 help / color / mirror / Atom feed
* File manipulations in Ada.Text_IO
@ 2005-08-01 18:04 Zheng Wang
  2005-08-01 18:18 ` Georg Bauhaus
  2005-08-02 20:18 ` Gautier Write-only
  0 siblings, 2 replies; 10+ messages in thread
From: Zheng Wang @ 2005-08-01 18:04 UTC (permalink / raw)


    Hi,

  I would like to ask a simple question about Ada.Text_IO, please.
How could we check if a file exists or not? I mean is there a function
or procedure that takes a File_Type/File_Name and returns true if the
File exists, otherwise return false. 
  Many thanks in advance.




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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 18:04 File manipulations in Ada.Text_IO Zheng Wang
@ 2005-08-01 18:18 ` Georg Bauhaus
  2005-08-01 18:51   ` Jeffrey Carter
  2005-08-02 20:18 ` Gautier Write-only
  1 sibling, 1 reply; 10+ messages in thread
From: Georg Bauhaus @ 2005-08-01 18:18 UTC (permalink / raw)


Zheng Wang wrote:
>     Hi,
> 
>   I would like to ask a simple question about Ada.Text_IO, please.
> How could we check if a file exists or not? I mean is there a function
> or procedure that takes a File_Type/File_Name and returns true if the
> File exists, otherwise return false. 
>   Many thanks in advance.
> 

One way using standard Ada is an attempt to open the file.
If the external file doesn't exist, exception Name_Error
is raised.


-- Georg



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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 18:18 ` Georg Bauhaus
@ 2005-08-01 18:51   ` Jeffrey Carter
  2005-08-01 19:32     ` Larry Kilgallen
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2005-08-01 18:51 UTC (permalink / raw)


Georg Bauhaus wrote:
> 
> One way using standard Ada is an attempt to open the file.
> If the external file doesn't exist, exception Name_Error
> is raised.

Ada 0X will have Ada.Directories as part of the standard library, which 
will include such a function.

-- 
Jeff Carter
"If a sperm is wasted, God gets quite irate."
Monty Python's the Meaning of Life
56



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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 18:51   ` Jeffrey Carter
@ 2005-08-01 19:32     ` Larry Kilgallen
  2005-08-01 21:12       ` Randy Brukardt
  2005-08-02  2:34       ` Jeffrey Carter
  0 siblings, 2 replies; 10+ messages in thread
From: Larry Kilgallen @ 2005-08-01 19:32 UTC (permalink / raw)


In article <wauHe.7071$0C.1043@newsread3.news.pas.earthlink.net>, Jeffrey Carter <spam@spam.com> writes:
> Georg Bauhaus wrote:
>> 
>> One way using standard Ada is an attempt to open the file.
>> If the external file doesn't exist, exception Name_Error
>> is raised.
> 
> Ada 0X will have Ada.Directories as part of the standard library, which 
> will include such a function.

But such a function for a non-wildcarded filename is always going
to be susceptible to a race condition if the next step would be
to open the file.  The file might be deleted or created between
the test and using the information gained from the test.



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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 19:32     ` Larry Kilgallen
@ 2005-08-01 21:12       ` Randy Brukardt
  2005-08-02 12:31         ` Larry Kilgallen
  2005-08-02  2:34       ` Jeffrey Carter
  1 sibling, 1 reply; 10+ messages in thread
From: Randy Brukardt @ 2005-08-01 21:12 UTC (permalink / raw)


"Larry Kilgallen" <Kilgallen@SpamCop.net> wrote in message
news:67kFmodka+Zb@eisner.encompasserve.org...
> In article <wauHe.7071$0C.1043@newsread3.news.pas.earthlink.net>, Jeffrey
Carter <spam@spam.com> writes:
> > Georg Bauhaus wrote:
> >>
> >> One way using standard Ada is an attempt to open the file.
> >> If the external file doesn't exist, exception Name_Error
> >> is raised.
> >
> > Ada 0X will have Ada.Directories as part of the standard library, which
> > will include such a function.
>
> But such a function for a non-wildcarded filename is always going
> to be susceptible to a race condition if the next step would be
> to open the file.  The file might be deleted or created between
> the test and using the information gained from the test.

True, but that's a general problem with I/O, not specifically with the
directories operations. Generally, if you're doing I/O, you have to handle
exceptions and never assume that they won't be raised, no matter what
pretests you might have done.

For an obvious example, try appending to a file, creating it if it isn't
there. Even a version using exceptions:

    begin
          Open (...);
   exception
         when Name_Error =>
                Create (...);
   end;

has a built-in race condition. That cannot be avoided with the operations
available in Ada - it's best to just do whatever you have to do and deal
with unusual cases with global exception handlers (and avoid pretests as
much as possible).

                               Randy.






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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 19:32     ` Larry Kilgallen
  2005-08-01 21:12       ` Randy Brukardt
@ 2005-08-02  2:34       ` Jeffrey Carter
  2005-08-08 22:25         ` Robert A Duff
  1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2005-08-02  2:34 UTC (permalink / raw)


Larry Kilgallen wrote:
> 
> But such a function for a non-wildcarded filename is always going
> to be susceptible to a race condition if the next step would be
> to open the file.  The file might be deleted or created between
> the test and using the information gained from the test.

That's true in the general case, and something I've never encountered in 
30 years of actual SW development, including software that was intended 
to manipulate the same file from different applications running at the 
same time.

-- 
Jeff Carter
"C's solution to this [variable-sized arrays] has real problems,
and people who are complaining about safety definitely have a point."
Dennis Ritchie
25



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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 21:12       ` Randy Brukardt
@ 2005-08-02 12:31         ` Larry Kilgallen
  0 siblings, 0 replies; 10+ messages in thread
From: Larry Kilgallen @ 2005-08-02 12:31 UTC (permalink / raw)


In article <suydneKD44DoEnPfRVn-ug@megapath.net>, "Randy Brukardt" <randy@rrsoftware.com> writes:
> "Larry Kilgallen" <Kilgallen@SpamCop.net> wrote in message
> news:67kFmodka+Zb@eisner.encompasserve.org...
>> In article <wauHe.7071$0C.1043@newsread3.news.pas.earthlink.net>, Jeffrey
> Carter <spam@spam.com> writes:
>> > Georg Bauhaus wrote:
>> >>
>> >> One way using standard Ada is an attempt to open the file.
>> >> If the external file doesn't exist, exception Name_Error
>> >> is raised.
>> >
>> > Ada 0X will have Ada.Directories as part of the standard library, which
>> > will include such a function.
>>
>> But such a function for a non-wildcarded filename is always going
>> to be susceptible to a race condition if the next step would be
>> to open the file.  The file might be deleted or created between
>> the test and using the information gained from the test.
> 
> True, but that's a general problem with I/O, not specifically with the
> directories operations. Generally, if you're doing I/O, you have to handle
> exceptions and never assume that they won't be raised, no matter what
> pretests you might have done.

Yes.  My point is that the utility of checking first before attempting
to open a file is vastly overrated.



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

* Re: File manipulations in Ada.Text_IO
  2005-08-01 18:04 File manipulations in Ada.Text_IO Zheng Wang
  2005-08-01 18:18 ` Georg Bauhaus
@ 2005-08-02 20:18 ` Gautier Write-only
  1 sibling, 0 replies; 10+ messages in thread
From: Gautier Write-only @ 2005-08-02 20:18 UTC (permalink / raw)


Zheng Wang:

>     Hi,
> 
>   I would like to ask a simple question about Ada.Text_IO, please.
> How could we check if a file exists or not? I mean is there a function
> or procedure that takes a File_Type/File_Name and returns true if the
> File exists, otherwise return false.
>   Many thanks in advance.

You mean something like that ?

with Ada.Text_IO;
--
  function Exist(name:String) return Boolean is
    use Ada.Text_IO;
    f: File_Type;
  begin
    Open(f,in_file,name);
    Close(f);
    return True;
  exception
    when Name_Error => return False;
  end Exist;
______________________________________________________________
Gautier     --     http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: File manipulations in Ada.Text_IO
  2005-08-02  2:34       ` Jeffrey Carter
@ 2005-08-08 22:25         ` Robert A Duff
  2005-08-09  2:46           ` Jeffrey Carter
  0 siblings, 1 reply; 10+ messages in thread
From: Robert A Duff @ 2005-08-08 22:25 UTC (permalink / raw)


Jeffrey Carter <spam@spam.com> writes:

> Larry Kilgallen wrote:
> > But such a function for a non-wildcarded filename is always going
> > to be susceptible to a race condition if the next step would be
> > to open the file.  The file might be deleted or created between
> > the test and using the information gained from the test.
> 
> That's true in the general case, and something I've never encountered in
> 30 years of actual SW development, including software that was intended
> to manipulate the same file from different applications running at the
> same time.

How do you know?  ;-)  I mean, if somebody writes code that contains
such a race condition, and their customer trips over it once in a while,
the programmer might never find out about it.

The server process crashes once in a while.  Nobody knows why.  So the
solution is to restart the server by hand.  (Yuck.)

Race conditions, by their very nature, are impossible to detect with any
confidence via testing.

- Bob



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

* Re: File manipulations in Ada.Text_IO
  2005-08-08 22:25         ` Robert A Duff
@ 2005-08-09  2:46           ` Jeffrey Carter
  0 siblings, 0 replies; 10+ messages in thread
From: Jeffrey Carter @ 2005-08-09  2:46 UTC (permalink / raw)


Robert A Duff wrote:
> 
> How do you know?  ;-)  I mean, if somebody writes code that contains
> such a race condition, and their customer trips over it once in a while,
> the programmer might never find out about it.

This was internal SW, so I would have found out. I was concerned about 
what might happen, so I included logging to see how often it happened 
and what actually occurred when it did. After a year of use, there were 
no reports of conflicts, so I took the logging out. There might have 
been race conditions in the logging. Conflicts might have started 
happening left and right after I took the logging out. At least I had a 
warm fuzzy feeling.

-- 
Jeff Carter
"Whatever it is, I'm against it."
Horse Feathers
46



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

end of thread, other threads:[~2005-08-09  2:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-01 18:04 File manipulations in Ada.Text_IO Zheng Wang
2005-08-01 18:18 ` Georg Bauhaus
2005-08-01 18:51   ` Jeffrey Carter
2005-08-01 19:32     ` Larry Kilgallen
2005-08-01 21:12       ` Randy Brukardt
2005-08-02 12:31         ` Larry Kilgallen
2005-08-02  2:34       ` Jeffrey Carter
2005-08-08 22:25         ` Robert A Duff
2005-08-09  2:46           ` Jeffrey Carter
2005-08-02 20:18 ` Gautier Write-only

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