[REBOL] Re: Double trouble
From: petr:krenzelok:trz:cz at: 26-Jun-2001 10:02
[Sanghabum--aol--com] wrote:
> [petr--krenzelok--trz--cz]:
> > I solved the case one month ago. The only thing I found for file locking was
> > tcp server based. I have complete solution for non server based locking. I
> > have the script at my work, so you will have to wait till monday.
>
> Hi Petr,
> Did you forget to post this? I know it's not (as Gabriele points out)
> multi-platform, but it's likely to solve the problem I have.
>
OK, so here we go:
lock: context [
sem-port-spec: func [path file-name][to-file join to-string path head change
find copy file-name "." ".SEM"]
locked: copy []
get-lock: func [path file-name][
semaphore: sem-port-spec path file-name
; file doesn't exist, or does exist, but semaphore can be closed (dead one)
either not exists? semaphore [
; store pair of file-name, opened port
append locked semaphore
append locked open/new/direct semaphore
true
][
; semaphore does exist - is locked or dead one? (possible app crash)
either remove-sem-file semaphore [
; store pair of file-name, opened port
append locked semaphore
append locked open/new/direct semaphore
true
][false] ; can't get lock, sorry ...
]
]
close-lock: func [path file-name][
semaphore: sem-port-spec path file-name
if found? find locked semaphore [
close select locked semaphore
remove remove find locked semaphore
]
either remove-sem-file semaphore [true][false]
]
remove-sem-file: func [semaphore][either not error? try [delete
semaphore][true][false]]
]
example:
lock/get-lock target-path "file-name.txt" ; will create
file-name.sem on 'target-path
if not lock/get-lock [print "unable to lock file"] ; should print
unable to lock file
message ...
probe lock/locked ; will show
you
lock/close-lock target-path "file-name.txt" ; will unlock file
... so, the problem is, that unix will allow you to delete file even if you would
own regular lock. A little bit lame imo.
-pekr-