]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/flock.rs
Auto merge of #69227 - Marwes:buffer_stderr, r=varkor
[rust.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::io;
11 use std::path::Path;
12
13 cfg_if! {
14     if #[cfg(unix)] {
15         use std::ffi::{CString, OsStr};
16         use std::mem;
17         use std::os::unix::prelude::*;
18
19         #[derive(Debug)]
20         pub struct Lock {
21             fd: libc::c_int,
22         }
23
24         impl Lock {
25             pub fn new(p: &Path,
26                        wait: bool,
27                        create: bool,
28                        exclusive: bool)
29                        -> io::Result<Lock> {
30                 let os: &OsStr = p.as_ref();
31                 let buf = CString::new(os.as_bytes()).unwrap();
32                 let open_flags = if create {
33                     libc::O_RDWR | libc::O_CREAT
34                 } else {
35                     libc::O_RDWR
36                 };
37
38                 let fd = unsafe {
39                     libc::open(buf.as_ptr(), open_flags,
40                                libc::S_IRWXU as libc::c_int)
41                 };
42
43                 if fd < 0 {
44                     return Err(io::Error::last_os_error());
45                 }
46
47                 let lock_type = if exclusive {
48                     libc::F_WRLCK
49                 } else {
50                     libc::F_RDLCK
51                 };
52
53                 let mut flock: libc::flock = unsafe { mem::zeroed() };
54                 flock.l_type = lock_type as libc::c_short;
55                 flock.l_whence = libc::SEEK_SET as libc::c_short;
56                 flock.l_start = 0;
57                 flock.l_len = 0;
58
59                 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
60                 let ret = unsafe {
61                     libc::fcntl(fd, cmd, &flock)
62                 };
63                 if ret == -1 {
64                     let err = io::Error::last_os_error();
65                     unsafe { libc::close(fd); }
66                     Err(err)
67                 } else {
68                     Ok(Lock { fd })
69                 }
70             }
71         }
72
73         impl Drop for Lock {
74             fn drop(&mut self) {
75                 let mut flock: libc::flock = unsafe { mem::zeroed() };
76                 flock.l_type = libc::F_UNLCK as libc::c_short;
77                 flock.l_whence = libc::SEEK_SET as libc::c_short;
78                 flock.l_start = 0;
79                 flock.l_len = 0;
80
81                 unsafe {
82                     libc::fcntl(self.fd, libc::F_SETLK, &flock);
83                     libc::close(self.fd);
84                 }
85             }
86         }
87     } else if #[cfg(windows)] {
88         use std::mem;
89         use std::os::windows::prelude::*;
90         use std::fs::{File, OpenOptions};
91
92         use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
93         use winapi::um::fileapi::LockFileEx;
94         use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
95
96         #[derive(Debug)]
97         pub struct Lock {
98             _file: File,
99         }
100
101         impl Lock {
102             pub fn new(p: &Path,
103                        wait: bool,
104                        create: bool,
105                        exclusive: bool)
106                        -> io::Result<Lock> {
107                 assert!(p.parent().unwrap().exists(),
108                     "Parent directory of lock-file must exist: {}",
109                     p.display());
110
111                 let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
112
113                 let mut open_options = OpenOptions::new();
114                 open_options.read(true)
115                             .share_mode(share_mode);
116
117                 if create {
118                     open_options.create(true)
119                                 .write(true);
120                 }
121
122                 debug!("attempting to open lock file `{}`", p.display());
123                 let file = match open_options.open(p) {
124                     Ok(file) => {
125                         debug!("lock file opened successfully");
126                         file
127                     }
128                     Err(err) => {
129                         debug!("error opening lock file: {}", err);
130                         return Err(err)
131                     }
132                 };
133
134                 let ret = unsafe {
135                     let mut overlapped: OVERLAPPED = mem::zeroed();
136
137                     let mut dwFlags = 0;
138                     if !wait {
139                         dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
140                     }
141
142                     if exclusive {
143                         dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
144                     }
145
146                     debug!("attempting to acquire lock on lock file `{}`",
147                            p.display());
148                     LockFileEx(file.as_raw_handle(),
149                                dwFlags,
150                                0,
151                                0xFFFF_FFFF,
152                                0xFFFF_FFFF,
153                                &mut overlapped)
154                 };
155                 if ret == 0 {
156                     let err = io::Error::last_os_error();
157                     debug!("failed acquiring file lock: {}", err);
158                     Err(err)
159                 } else {
160                     debug!("successfully acquired lock");
161                     Ok(Lock { _file: file })
162                 }
163             }
164         }
165
166         // Note that we don't need a Drop impl on the Windows: The file is unlocked
167         // automatically when it's closed.
168     } else {
169         #[derive(Debug)]
170         pub struct Lock(());
171
172         impl Lock {
173             pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool)
174                 -> io::Result<Lock>
175             {
176                 let msg = "file locks not supported on this platform";
177                 Err(io::Error::new(io::ErrorKind::Other, msg))
178             }
179         }
180     }
181 }