Tries to set an advisory lock for the corresponding disk image.
(&mut self)
| 928 | |
| 929 | /// Tries to set an advisory lock for the corresponding disk image. |
| 930 | pub fn try_lock_image(&mut self) -> Result<()> { |
| 931 | let lock_type = match self.read_only() { |
| 932 | true => LockType::Read, |
| 933 | false => LockType::Write, |
| 934 | }; |
| 935 | let granularity = self.lock_granularity(); |
| 936 | debug!( |
| 937 | "Attempting to acquire {lock_type:?} lock for disk image: id={},path={},granularity={granularity:?}", |
| 938 | self.id, |
| 939 | self.disk_path.display() |
| 940 | ); |
| 941 | let fd = self.disk_image.fd(); |
| 942 | fcntl::try_acquire_lock(&fd, lock_type, granularity).map_err(|error| { |
| 943 | let current_lock = get_lock_state(&fd, granularity); |
| 944 | // Don't propagate the error to the outside, as it is not useful at all. Instead, |
| 945 | // we try to log additional help to the user. |
| 946 | if let Ok(current_lock) = current_lock { |
| 947 | error!("Can't get {lock_type:?} lock for {} as there is already a {current_lock:?} lock", self.disk_path.display()); |
| 948 | } else { |
| 949 | error!("Can't get {lock_type:?} lock for {}, but also can't determine the current lock state", self.disk_path.display()); |
| 950 | } |
| 951 | Error::LockDiskImage { |
| 952 | path: self.disk_path.clone(), |
| 953 | error, |
| 954 | lock_type, |
| 955 | } |
| 956 | })?; |
| 957 | info!( |
| 958 | "Acquired {lock_type:?} lock for disk image id={},path={}", |
| 959 | self.id, |
| 960 | self.disk_path.display() |
| 961 | ); |
| 962 | Ok(()) |
| 963 | } |
| 964 | |
| 965 | /// Releases the advisory lock held for the corresponding disk image. |
| 966 | pub fn unlock_image(&mut self) -> Result<()> { |
no test coverage detected