Skip to main content

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#![expect(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<Self, FileLockError> {
32        tracing::info!("Attempting to lock {path:?}");
33        loop {
34            // Create lockfile, or open pre-existing one
35            let file = File::create(&path).map_err(|err| FileLockError {
36                message: "Failed to open lock file",
37                path: path.clone(),
38                err,
39            })?;
40            // If the lock was already held, wait for it to be released
41            rustix::fs::flock(&file, FlockOperation::LockExclusive).map_err(|errno| {
42                FileLockError {
43                    message: "Failed to lock lock file",
44                    path: path.clone(),
45                    err: errno.into(),
46                }
47            })?;
48
49            match rustix::fs::fstat(&file) {
50                Ok(stat) => {
51                    if stat.st_nlink == 0 {
52                        // Lockfile was deleted, probably by the previous holder's `Drop` impl;
53                        // create a new one so our ownership is visible,
54                        // rather than hidden in an unlinked file. Not
55                        // always necessary, since the previous holder might
56                        // have exited abruptly.
57                        continue;
58                    }
59                }
60                Err(rustix::io::Errno::STALE) => {
61                    // The file handle is stale.
62                    // This can happen when using NFS,
63                    // likely caused by a remote deletion of the lockfile.
64                    // Treat this like a normal lockfile deletion and retry.
65                    continue;
66                }
67                Err(errno) => {
68                    return Err(FileLockError {
69                        message: "failed to stat lock file",
70                        path: path.clone(),
71                        err: errno.into(),
72                    });
73                }
74            }
75
76            tracing::info!("Locked {path:?}");
77            return Ok(Self { path, file });
78        }
79    }
80}
81
82impl Drop for FileLock {
83    #[instrument(skip_all)]
84    fn drop(&mut self) {
85        // Removing the file isn't strictly necessary, but reduces confusion.
86        std::fs::remove_file(&self.path).ok();
87        // Unblock any processes that tried to acquire the lock while we held it.
88        // They're responsible for creating and locking a new lockfile, since we
89        // just deleted this one.
90        rustix::fs::flock(&self.file, FlockOperation::Unlock).ok();
91    }
92}