Alan Bawden <
[email protected]> writes:
Julieta Shem <[email protected]> writes:
Lawrence D'Oliveiro <[email protected]d> writes:
> On Sat, 20 Jan 2024 14:27:24 -0300, Julieta Shem wrote:
>
>> What I need is an atomic operation to
>> let me know if a certain name is already taken and, if it is not taken,
>> take it.
>
> Open with O_EXCL <https://manpages.debian.org/2/open.2.html>.
I don't understand. You mean I should write it in C?
If POSIX open() with O_EXCL really is a solution to your problem, then
you can do the same thing in pure Common Lisp by calling OPEN with
:IF-EXISTS :ERROR.
But you originally asked for a version of RENAME-FILE that renames
atomically but fails if the target already exists. If that's _really_
what you want, then even POSIX won't help you since POSIX rename()
always deletes the target if it exists.
If we knew what problem you were really trying to solve, then we might
be able to help you, but as it is, we're all just throwing ideas at you
in the hopes that something will stick.
I'm sorry if I didn't express myself properly. I'm writing an NNTP
service that stores articles in a directory. Each user is served by a different service-process. When a user POSTs an article (POST is a verb
in the NNTP protocol), the service will write a new file in a directory relative to the group. Two or more users could be posting at the same
time, so the strategy I came up with is to atomically write the article
by way of using a temporary file and renaming to the final name.
The final name is a string-integer that reflects the internal id of the article---it's the greatest article number currently present plus one.
If two posts happen near in time, one will take the next id ahead of the
other, so the renaming of the second one would fail (or so I wished)
because a file with that name already exists, so the process can react
by increasing the file number once again, repeating that how many times
is necessary until it finds a free greatest id available. (I suppose
this might not be called an /algorithm/, but I guess in practice it
would work.)
I'm interested in any solution. Thank you.
I think the open-with-:IF-EXISTS-:ERROR strategy would not be good: that implies I'd create the file with its final name, but then the service
could end up serving that not-yet-written article to another user who is reading the group.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)