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