jj_lib/lock/
unix.rs

1// Copyright 2023 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(missing_docs)]
16
17use std::fs::File;
18use std::path::PathBuf;
19
20use rustix::fs::FlockOperation;
21use tracing::instrument;
22
23use super::FileLockError;
24
25pub struct FileLock {
26    path: PathBuf,
27    file: File,
28}
29
30impl FileLock {
31    pub fn lock(path: PathBuf) -> Result<FileLock, FileLockError> {
32        loop {
33            // Create lockfile, or open pre-existing one
34            let file = File::create(&path).map_err(|err| FileLockError {
35                message: "Failed to open lock file",
36                path: path.clone(),
37                err,
38            })?;
39            // If the lock was already held, wait for it to be released
40            rustix::fs::flock(&file, FlockOperation::LockExclusive).map_err(|errno| {
41                FileLockError {
42                    message: "Failed to lock lock file",
43                    path: path.clone(),
44                    err: errno.into(),
45                }
46            })?;
47
48            let stat = rustix::fs::fstat(&file).map_err(|errno| FileLockError {
49                message: "failed to stat lock file",
50                path: path.clone(),
51                err: errno.into(),
52            })?;
53            if stat.st_nlink == 0 {
54                // Lockfile was deleted, probably by the previous holder's `Drop` impl; create a
55                // new one so our ownership is visible, rather than hidden in an
56                // unlinked file. Not always necessary, since the previous
57                // holder might have exited abruptly.
58                continue;
59            }
60
61            return Ok(Self { path, file });
62        }
63    }
64}
65
66impl Drop for FileLock {
67    #[instrument(skip_all)]
68    fn drop(&mut self) {
69        // Removing the file isn't strictly necessary, but reduces confusion.
70        _ = std::fs::remove_file(&self.path);
71        // Unblock any processes that tried to acquire the lock while we held it.
72        // They're responsible for creating and locking a new lockfile, since we
73        // just deleted this one.
74        _ = rustix::fs::flock(&self.file, FlockOperation::Unlock);
75    }
76}