[comp.sys.amiga] AmigaDOS locks

ajk@goanna.oz (Alan Kent) (04/27/87)

Please Enlighten me.

I have been looking through all the manuals I can lay my hands on, but
nothing seems to help.

I want to lock a file so that no-one else will touch it while I do
I/O on the file (reads and possibly writes).

When I open it, it may only be for read access, but I might want to 
write to it later (to save a new version). I have tried Lock()ing
the file for exclusive access, but then Open() wont work (because
its locked). Doing the Open() first makes the Lock() fail. Is there
any way of using Lock() to give myself exclusive access to a file
and have it open at the same time for I/O?  I want to make sure I
dont allow the user to modify the memory version of a file if I cannot
save it later! (I am writing a sort of editor). Obviously I dont want
to write to the file if the user does not change it.

At present, I exclusive Lock() the file and if that works, I quickly
UnLock() and Open() it.  This is not perfect but its the best I can
think of. Anyone got any better ideas?

Thanks in advance


Alan Kent                     UUCP: {seismo,hplabs,mcvax,ukc,nttlab}
Dept of Computing                         !munnari!goanna.oz!ajk
Royal Melb. Inst. of Tech.    ACSnet: ajk@goanna.oz
124 La Trobe St               ARPA: munnari!goanna.oz!ajk@SEISMO.ARPA
Melbourne, Victoria 3000
AUSTRALIA

phillip@cbmvax.cbm.UUCP (Phillip Lindsay CATS) (04/27/87)

in article <798@goanna.oz>, ajk@goanna.oz (Alan Kent) says:
> Keywords: locking files with amigados
> 
> Please Enlighten me.
> 
> I have been looking through all the manuals I can lay my hands on, but
> nothing seems to help.
> 
> I want to lock a file so that no-one else will touch it while I do
> I/O on the file (reads and possibly writes).
....[crunch] 
> At present, I exclusive Lock() the file and if that works, I quickly
> UnLock() and Open() it.  This is not perfect but its the best I can
> think of. Anyone got any better ideas?
> 

There is new Open() mode offered in the 1.2 release. (Look in the
1.2 "dos.h" header file).

MODE_READWRITE = 1004

file = Open("foo",MODE_READWRITE); /* opens old file with exclusive lock */

-phil
==============================================================================
Phillip (Flip) Lindsay - Commodore Business Machines - Amiga Technical Support
  UUCP: {ihnp4|seismo|caip}!cbmvax!phillip      - Phone: (215) 431-9180
  No warranty is implied or otherwise given in the form of suggestion or 
  example. Any opinions found here are of my making.

andy@cbmvax.UUCP (Andy Finkel) (07/18/87)

In article <798@goanna.oz> ajk@goanna.oz (Alan Kent) writes:
>Please Enlighten me.
>
>I want to lock a file so that no-one else will touch it while I do
>I/O on the file (reads and possibly writes).
>
>When I open it, it may only be for read access, but I might want to 
>write to it later (to save a new version).

The easiest way to do this is Open the file MODE_READWRITE (1004)
You can read from it, write to it, whatever, and you have
exclusive access.  (As long as the file already exists, anyway.  You can
read from a file opened with MODE_NEWFILE, by the way)

The names are actually slightly inaccurate...you can read from a
file opened for write access with no problem.

			andy finkel
-- 
andy finkel		{ihnp4|seismo|allegra}!cbmvax!andy 
Commodore-Amiga, Inc.

"The goal of Computer Science is to build something that will last at
least until we've finished building it."

Any expressed opinions are mine; but feel free to share.
I disclaim all responsibilities, all shapes, all sizes, all colors.