std.fs.lock

std.fs

lock(path, how) -> fd, err

Lock a path for as long as the answered fd stays open

Opens path and takes flock(fd, how) on it, without blocking. path is a directory or a file; a missing file is created, mode 0600. The fd is close-on-exec, so a program run by exec does not keep the lock.

Answers the raw fd, which holds the lock until close or until the process ends. A refusal answers nil and "locked"; a failure answers nil and the errno text. flock converts or drops the lock on the answered fd.

local fd, err = std.fs.lock(data_dir, "ex")
if not fd and err == "locked" then
	-- another process holds the directory
end

Never lock a .mneme store file itself. MNEME's write transaction takes a blocking flock on its own fd, and would wait for this lock forever. Lock the store's directory, or a file beside it.