From: glen herrmannsfeldt on
Gib Bogle <g.bogle(a)auckland.no.spam.ac.nz> wrote:
> Richard Maine wrote:
>> Gib Bogle <g.bogle(a)auckland.no.spam.ac.nz> wrote:

>>> I want to check for the existence of a file, in order to determine whether
>>> the coast is clear to create this file. There is another process that
>>> opens the file, reads it, then removes it. The plan is to wait (sleep)
>>> until the file has been processed and deleted. INQUIRE(filename,EXIST=ex)
>>> doesn't seem to fit the bill, since this will return ex = .false. if the
>>> other process currently has the file open. What's the best way to do what
>>> I want?

>> I have no idea why you are getting a false from that just because some
>> other process has the file open. That makes little sense to me. Seems
>> like that ought to be an answer and that if this is accurately reported,
>> it would be a bug. A file doesn't cease to exist just because it is
>> open.

It sounds like what Windows might do...

> I'm not getting a false, I haven't coded this yet.
> Maybe I misinterpreted the

You might check to see what it really does, but I agree that
sounds like what it might do. My best understanding is that other
programs can open the file for read, but not read/write, even if
the program doesn't actually plan to write to the file.

> Intel help info:

> ex
> Is a scalar default logical variable that is assigned one of
> the following values:

> .TRUE.
> If the specified file exists and can be opened, or if the specified
> unit exists

> .FALSE.
> If the specified file or unit does not exist or if the file
> exists but cannot be opened

> I was assuming that the file couldn't be opened if the other
> process had it open.

If you find the right OPEN parameters, such that it will create
a new file if it doesn't exist, won't overwrite the old one,
and will appropriately fail if someone else had it open for reading,
then you can do a sleep/open loop until it succeeds. Otherwise,
it seems that the problem is Windows specific and you need
Windows API calls to find out about the file.

>> However...

>> Using an OPEN with status='new' will get you an error on many systems if
>> the file already exists. A caveat though is that on systems with file
>> versions (VAX VMS), it wil just create a new version.

> I don't think I have to worry about VAXen.

VMS is still around, with new versions only for Alpha and Itanium.

-- glen
From: Arjen Markus on
On 11 mrt, 04:29, Gib Bogle <g.bo...(a)auckland.no.spam.ac.nz> wrote:
> I want to check for the existence of a file, in order to determine whether the
> coast is clear to create this file.  There is another process that opens the
> file, reads it, then removes it.  The plan is to wait (sleep) until the file has
> been processed and deleted.  INQUIRE(filename,EXIST=ex) doesn't seem to fit the
> bill, since this will return ex = .false. if the other process currently has the
> file open.  What's the best way to do what I want?

If INQUIRE does behave that way, perhaps:

OPEN( 10, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT =
ierr )

will help?

ierr should be unequal zero, if the file exists and can be read (you
should
be able to read it even if another process has opened it). Possibly
another
check (try open the file as new) is required to be fully certain it
does
not exist and the first check did not fail because the file could not
be
opened for reading only - this would solve the situation of versioned
files.

Regards,

Arjen
From: Paul van Delst on
Gib Bogle wrote:
> Richard Maine wrote:
>> Gib Bogle <g.bogle(a)auckland.no.spam.ac.nz> wrote:
>>
>>> I want to check for the existence of a file, in order to determine
>>> whether
>>> the coast is clear to create this file. There is another process that
>>> opens the file, reads it, then removes it. The plan is to wait (sleep)
>>> until the file has been processed and deleted.
>>> INQUIRE(filename,EXIST=ex)
>>> doesn't seem to fit the bill, since this will return ex = .false. if the
>>> other process currently has the file open. What's the best way to do
>>> what
>>> I want?
>>
>> I have no idea why you are getting a false from that just because some
>> other process has the file open. That makes little sense to me. Seems
>> like that ought to be an answer and that if this is accurately reported,
>> it would be a bug. A file doesn't cease to exist just because it is
>> open.
>
> I'm not getting a false, I haven't coded this yet. Maybe I
> misinterpreted the Intel help info:
>
> ex
> Is a scalar default logical variable that is assigned one of the
> following values:
>
> .TRUE.
> If the specified file exists and can be opened, or if the specified
> unit exists
>
> .FALSE.
> If the specified file or unit does not exist or if the file exists but
> cannot be opened
>
> I was assuming that the file couldn't be opened if the other process had
> it open.

It's been my experience that many processes can open and use the same file (in different
spots) at the same time. Although, that was on a IBM SP. Maybe "can be opened" means the
user in question has read and/or write permission for the file?

FWIW, the following snippet from my "File_Utility" module shows the functions I use (since
2000/2001) to test for existence and open-ness. I've never had a problem with them on any
machine I've used (yet, at least :o)


INTERFACE File_Exists
MODULE PROCEDURE File_Unit_Exists
MODULE PROCEDURE File_Name_Exists
END INTERFACE File_Exists

INTERFACE File_Open
MODULE PROCEDURE File_Open_by_Unit
MODULE PROCEDURE File_Open_by_Name
END INTERFACE File_Open

CONTAINS

FUNCTION File_Unit_Exists( FileID ) RESULT( Existence )
INTEGER, INTENT(IN) :: FileID
LOGICAL :: Existence
INQUIRE( UNIT = FileID, EXIST = Existence )
END FUNCTION File_Unit_Exists

FUNCTION File_Name_Exists( Filename ) RESULT( Existence )
CHARACTER(*), INTENT(IN) :: Filename
LOGICAL :: Existence
INQUIRE( FILE = Filename, EXIST = Existence )
END FUNCTION File_Name_Exists


FUNCTION File_Open_by_Unit( FileID ) RESULT( Is_Open )
INTEGER, INTENT(IN) :: FileID
LOGICAL :: Is_Open
INQUIRE( UNIT = FileID, OPENED = Is_Open )
END FUNCTION File_Open_by_Unit

FUNCTION File_Open_by_Name( Filename ) RESULT( Is_Open )
CHARACTER(*), INTENT(IN) :: Filename
LOGICAL :: Is_Open
INQUIRE( FILE = Filename, OPENED = Is_Open )
END FUNCTION File_Open_by_Name


cheers,

paulv
From: Gib Bogle on
Paul van Delst wrote:
>
> It's been my experience that many processes can open and use the same file (in different
> spots) at the same time. Although, that was on a IBM SP. Maybe "can be opened" means the
> user in question has read and/or write permission for the file?
>
> FWIW, the following snippet from my "File_Utility" module shows the functions I use (since
> 2000/2001) to test for existence and open-ness. I've never had a problem with them on any
> machine I've used (yet, at least :o)
>
>
> INTERFACE File_Exists
> MODULE PROCEDURE File_Unit_Exists
> MODULE PROCEDURE File_Name_Exists
> END INTERFACE File_Exists
>
etc.

I'm going to start with this approach, and stick with it until I have a problem.
From: Gib Bogle on
Arjen Markus wrote:
>
> If INQUIRE does behave that way, perhaps:
>
> OPEN( 10, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT =
> ierr )
>
> will help?
>
> ierr should be unequal zero, if the file exists and can be read (you
> should
> be able to read it even if another process has opened it). Possibly
> another
> check (try open the file as new) is required to be fully certain it
> does
> not exist and the first check did not fail because the file could not
> be
> opened for reading only - this would solve the situation of versioned
> files.
>
> Regards,
>
> Arjen

I'll resort to this if the simple INQUIRE(EXIST..) fails.