]> git.proxmox.com Git - rustc.git/blob - src/librustc_data_structures/flock.rs
New upstream version 1.45.0+dfsg1
[rustc.git] / src / librustc_data_structures / flock.rs
1 //! Simple file-locking apis for each OS.
2 //!
3 //! This is not meant to be in the standard library, it does nothing with
4 //! green/native threading. This is just a bare-bones enough solution for
5 //! librustdoc, it is not production quality at all.
6
7 #![allow(non_camel_case_types)]
8 #![allow(nonstandard_style)]
9
10 use std::fs::{File, OpenOptions};
11 use std::io;
12 use std::path::Path;
13
14 cfg_if! {
15 // We use `flock` rather than `fcntl` on Linux, because WSL1 does not support
16 // `fcntl`-style advisory locks properly (rust-lang/rust#72157).
17 //
18 // For other Unix targets we still use `fcntl` because it's more portable than
19 // `flock`.
20 if #[cfg(target_os = "linux")] {
21 use std::os::unix::prelude::*;
22
23 #[derive(Debug)]
24 pub struct Lock {
25 _file: File,
26 }
27
28 impl Lock {
29 pub fn new(p: &Path,
30 wait: bool,
31 create: bool,
32 exclusive: bool)
33 -> io::Result<Lock> {
34 let file = OpenOptions::new()
35 .read(true)
36 .write(true)
37 .create(create)
38 .mode(libc::S_IRWXU as u32)
39 .open(p)?;
40
41 let mut operation = if exclusive {
42 libc::LOCK_EX
43 } else {
44 libc::LOCK_SH
45 };
46 if !wait {
47 operation |= libc::LOCK_NB
48 }
49
50 let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
51 if ret == -1 {
52 Err(io::Error::last_os_error())
53 } else {
54 Ok(Lock { _file: file })
55 }
56 }
57 }
58
59 // Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. Lock acquired by
60 // `flock` is associated with the file descriptor and closing the file release it
61 // automatically.
62 } else if #[cfg(unix)] {
63 use std::mem;
64 use std::os::unix::prelude::*;
65
66 #[derive(Debug)]
67 pub struct Lock {
68 file: File,
69 }
70
71 impl Lock {
72 pub fn new(p: &Path,
73 wait: bool,
74 create: bool,
75 exclusive: bool)
76 -> io::Result<Lock> {
77 let file = OpenOptions::new()
78 .read(true)
79 .write(true)
80 .create(create)
81 .mode(libc::S_IRWXU as u32)
82 .open(p)?;
83
84 let lock_type = if exclusive {
85 libc::F_WRLCK
86 } else {
87 libc::F_RDLCK
88 };
89
90 let mut flock: libc::flock = unsafe { mem::zeroed() };
91 flock.l_type = lock_type as libc::c_short;
92 flock.l_whence = libc::SEEK_SET as libc::c_short;
93 flock.l_start = 0;
94 flock.l_len = 0;
95
96 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
97 let ret = unsafe {
98 libc::fcntl(file.as_raw_fd(), cmd, &flock)
99 };
100 if ret == -1 {
101 Err(io::Error::last_os_error())
102 } else {
103 Ok(Lock { file })
104 }
105 }
106 }
107
108 impl Drop for Lock {
109 fn drop(&mut self) {
110 let mut flock: libc::flock = unsafe { mem::zeroed() };
111 flock.l_type = libc::F_UNLCK as libc::c_short;
112 flock.l_whence = libc::SEEK_SET as libc::c_short;
113 flock.l_start = 0;
114 flock.l_len = 0;
115
116 unsafe {
117 libc::fcntl(self.file.as_raw_fd(), libc::F_SETLK, &flock);
118 }
119 }
120 }
121 } else if #[cfg(windows)] {
122 use std::mem;
123 use std::os::windows::prelude::*;
124
125 use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
126 use winapi::um::fileapi::LockFileEx;
127 use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
128
129 #[derive(Debug)]
130 pub struct Lock {
131 _file: File,
132 }
133
134 impl Lock {
135 pub fn new(p: &Path,
136 wait: bool,
137 create: bool,
138 exclusive: bool)
139 -> io::Result<Lock> {
140 assert!(p.parent().unwrap().exists(),
141 "Parent directory of lock-file must exist: {}",
142 p.display());
143
144 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
145
146 let mut open_options = OpenOptions::new();
147 open_options.read(true)
148 .share_mode(share_mode);
149
150 if create {
151 open_options.create(true)
152 .write(true);
153 }
154
155 debug!("attempting to open lock file `{}`", p.display());
156 let file = match open_options.open(p) {
157 Ok(file) => {
158 debug!("lock file opened successfully");
159 file
160 }
161 Err(err) => {
162 debug!("error opening lock file: {}", err);
163 return Err(err)
164 }
165 };
166
167 let ret = unsafe {
168 let mut overlapped: OVERLAPPED = mem::zeroed();
169
170 let mut dwFlags = 0;
171 if !wait {
172 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
173 }
174
175 if exclusive {
176 dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
177 }
178
179 debug!("attempting to acquire lock on lock file `{}`",
180 p.display());
181 LockFileEx(file.as_raw_handle(),
182 dwFlags,
183 0,
184 0xFFFF_FFFF,
185 0xFFFF_FFFF,
186 &mut overlapped)
187 };
188 if ret == 0 {
189 let err = io::Error::last_os_error();
190 debug!("failed acquiring file lock: {}", err);
191 Err(err)
192 } else {
193 debug!("successfully acquired lock");
194 Ok(Lock { _file: file })
195 }
196 }
197 }
198
199 // Note that we don't need a Drop impl on the Windows: The file is unlocked
200 // automatically when it's closed.
201 } else {
202 #[derive(Debug)]
203 pub struct Lock(());
204
205 impl Lock {
206 pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
207 -> io::Result<Lock>
208 {
209 let msg = "file locks not supported on this platform";
210 Err(io::Error::new(io::ErrorKind::Other, msg))
211 }
212 }
213 }
214 }