Lisp HUG Maillist Archive

File opened already by someone

Hi,

 

I’m pondering this one topic.

 

So, let’s say that we have a network drive and several different users may access the same files. This would happen on MS Windows.

 

If the file being opened would be, let’s say, a MS Word document, then other individuals opening the same file would get a message that the file has already been opened by someone and it can only be opened in read-only mode.

 

What I’m asking is that would someone have some experience on building a similar kind of system with Common Lisp?

 

There are answers e.g. https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use, but as it is typical, it’s difficult to find answers that address Common Lisp directly.

 

I think that one may need to define a specific foreing function for testing the properties of the file in question. (Didn’t yet see anything suitable in the win32 package.)

 

I’m quite sure that making this kind of a feature is possible. Any experiences or tips would be appreciated.

 

With Best Regards,

Teemu Liikala

 

Teemu Liikala
Associate, Dynamic Simulation, NAPCON
Neste Engineering Solutions Oy
P.O. Box 310, FI-06101 Porvoo, Finland
Visiting address:
Teknologiantie 36, Kilpilahti, 06850 Kulloo
Tel. +358 50 458 2280
teemu.liikala@neste.com
www.napconsuite.com
www.neste.com

 

Re: File opened already by someone

Hi,

I would write first the Win32 C code to test the approach, and then port the code to FFI/Common Lisp.

BR
/Alexey

On Thu, Oct 25, 2018 at 9:56 AM Liikala Teemu <Teemu.Liikala@neste.com> wrote:

Hi,

 

I’m pondering this one topic.

 

So, let’s say that we have a network drive and several different users may access the same files. This would happen on MS Windows.

 

If the file being opened would be, let’s say, a MS Word document, then other individuals opening the same file would get a message that the file has already been opened by someone and it can only be opened in read-only mode.

 

What I’m asking is that would someone have some experience on building a similar kind of system with Common Lisp?

 

There are answers e.g. https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use, but as it is typical, it’s difficult to find answers that address Common Lisp directly.

 

I think that one may need to define a specific foreing function for testing the properties of the file in question. (Didn’t yet see anything suitable in the win32 package.)

 

I’m quite sure that making this kind of a feature is possible. Any experiences or tips would be appreciated.

 

With Best Regards,

Teemu Liikala

 

Teemu Liikala
Associate, Dynamic Simulation, NAPCON
Neste Engineering Solutions Oy
P.O. Box 310, FI-06101 Porvoo, Finland
Visiting address:
Teknologiantie 36, Kilpilahti, 06850 Kulloo
Tel. +358 50 458 2280
teemu.liikala@neste.com
www.napconsuite.com
www.neste.com

 

RE: File opened already by someone

Hi,

 

Thank you for the tips!

 

Trying to rename the file with same name under handler-case seems easy, doable and fast.

 

With Best Regards,

Teemu

 

From: owner-lisp-hug@lispworks.com [mailto:owner-lisp-hug@lispworks.com] On Behalf Of Ron Lewis
Sent: torstai 25. lokakuuta 2018 20.25
To: 'Lispworks HUG' <lisp-hug@lispworks.com>
Subject: RE: File opened already by someone

 

This is an interesting problem. I am thinking how this might be handled. Here is what I am thinking:

 

 

APPROACH 1, FLI

 

Use Create functions such as CreateFileA() or CreateFileW() to open the file, even though an old file is being opened.

Use CloseHandle() to close the file.

 

(See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/)

 

Header files <Fileapi.h> or <Windows.h>

 

I think this means all file I/O is done outside of Lisp. So, you also need API functions to read and write the files.

 

 

 

APPROACH 2, All in Lisp, No C, No FLI

 

I think you could use (rename) and (handler-case) to get the effects you want.

 

An app wanting to open a file for possible modification would rename the file, then open. When a second app tries to open the same file, the file will not be there. So the second app’s rename will cause an error. You catch this error using (handler-case) and tell the user that the file is temporarily not available or whatever you want to do. When the first app is done, the app renames the file back so that it is available to the other apps.

 

If two apps are trying to rename the file at the same time (a race condition) the operating system will only let one of them succeed, the other one gets the error. So, I think race conditions are handled automatically.

 

If you want the file to be available for read-only, you could have a copy of the file.

 

I am just thinking, but I have not tried this.

 

For catching and handling errors with (handler-case), I am looking at 9.1.4 of Common Lisp HyperSpec.

 

 

Ron Lewis

5719 Narcissus Avenue

Baltimore, MD 21215-3551

 

 

 

From: owner-lisp-hug@lispworks.com <owner-lisp-hug@lispworks.com> On Behalf Of Alexey Veretennikov
Sent: Thursday, October 25, 2018 7:13 AM
To: Teemu.Liikala@neste.com
Cc: Lispworks HUG <lisp-hug@lispworks.com>
Subject: Re: File opened already by someone

 

Hi,

I would write first the Win32 C code to test the approach, and then port the code to FFI/Common Lisp.

BR

/Alexey

 

On Thu, Oct 25, 2018 at 9:56 AM Liikala Teemu <Teemu.Liikala@neste.com> wrote:

Hi,

 

I’m pondering this one topic.

 

So, let’s say that we have a network drive and several different users may access the same files. This would happen on MS Windows.

 

If the file being opened would be, let’s say, a MS Word document, then other individuals opening the same file would get a message that the file has already been opened by someone and it can only be opened in read-only mode.

 

What I’m asking is that would someone have some experience on building a similar kind of system with Common Lisp?

 

There are answers e.g. https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use, but as it is typical, it’s difficult to find answers that address Common Lisp directly.

 

I think that one may need to define a specific foreing function for testing the properties of the file in question. (Didn’t yet see anything suitable in the win32 package.)

 

I’m quite sure that making this kind of a feature is possible. Any experiences or tips would be appreciated.

 

With Best Regards,

Teemu Liikala

 

Teemu Liikala
Associate, Dynamic Simulation, NAPCON
Neste Engineering Solutions Oy
P.O. Box 310, FI-06101 Porvoo, Finland
Visiting address:
Teknologiantie 36, Kilpilahti, 06850 Kulloo
Tel. +358 50 458 2280
teemu.liikala@neste.com
www.napconsuite.com
www.neste.com

 

Re: File opened already by someone

File (and general resource) contention is a huge area.  Almost certainly you will be better off interfacing to whatever facilities the OS provides than trying to reinvent them yourself I think.

On 25 Oct 2018, at 08:55, Liikala Teemu <Teemu.Liikala@neste.com> wrote:

Hi,

 

I’m pondering this one topic.

 

So, let’s say that we have a network drive and several different users may access the same files. This would happen on MS Windows.

 

If the file being opened would be, let’s say, a MS Word document, then other individuals opening the same file would get a message that the file has already been opened by someone and it can only be opened in read-only mode.

 

What I’m asking is that would someone have some experience on building a similar kind of system with Common Lisp?

 

There are answers e.g. https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use, but as it is typical, it’s difficult to find answers that address Common Lisp directly.

 

I think that one may need to define a specific foreing function for testing the properties of the file in question. (Didn’t yet see anything suitable in the win32 package.)

 

I’m quite sure that making this kind of a feature is possible. Any experiences or tips would be appreciated.

 

With Best Regards,

Teemu Liikala

 

Teemu Liikala
Associate, Dynamic Simulation, NAPCON
Neste Engineering Solutions Oy
P.O. Box 310, FI-06101 Porvoo, Finland
Visiting address:
Teknologiantie 36, Kilpilahti, 06850 Kulloo
Tel. +358 50 458 2280
teemu.liikala@neste.com
www.napconsuite.com
www.neste.com

 

Updated at: 2020-12-10 08:29 UTC