]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/flock.rs
Convert most code to new inner attribute syntax.
[rust.git] / src / librustdoc / flock.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Simple file-locking apis for each OS.
12 //!
13 //! This is not meant to be in the standard library, it does nothing with
14 //! green/native threading. This is just a bare-bones enough solution for
15 //! librustdoc, it is not production quality at all.
16
17 #![allow(non_camel_case_types)]
18
19 pub use self::imp::Lock;
20
21 #[cfg(unix)]
22 mod imp {
23     use std::libc;
24
25     #[cfg(target_os = "linux")]
26     mod os {
27         use std::libc;
28
29         pub struct flock {
30             l_type: libc::c_short,
31             l_whence: libc::c_short,
32             l_start: libc::off_t,
33             l_len: libc::off_t,
34             l_pid: libc::pid_t,
35
36             // not actually here, but brings in line with freebsd
37             l_sysid: libc::c_int,
38         }
39
40         pub static F_WRLCK: libc::c_short = 1;
41         pub static F_UNLCK: libc::c_short = 2;
42         pub static F_SETLK: libc::c_int = 6;
43         pub static F_SETLKW: libc::c_int = 7;
44     }
45
46     #[cfg(target_os = "freebsd")]
47     mod os {
48         use std::libc;
49
50         pub struct flock {
51             l_start: libc::off_t,
52             l_len: libc::off_t,
53             l_pid: libc::pid_t,
54             l_type: libc::c_short,
55             l_whence: libc::c_short,
56             l_sysid: libc::c_int,
57         }
58
59         pub static F_UNLCK: libc::c_short = 2;
60         pub static F_WRLCK: libc::c_short = 3;
61         pub static F_SETLK: libc::c_int = 12;
62         pub static F_SETLKW: libc::c_int = 13;
63     }
64
65     #[cfg(target_os = "macos")]
66     mod os {
67         use std::libc;
68
69         pub struct flock {
70             l_start: libc::off_t,
71             l_len: libc::off_t,
72             l_pid: libc::pid_t,
73             l_type: libc::c_short,
74             l_whence: libc::c_short,
75
76             // not actually here, but brings in line with freebsd
77             l_sysid: libc::c_int,
78         }
79
80         pub static F_UNLCK: libc::c_short = 2;
81         pub static F_WRLCK: libc::c_short = 3;
82         pub static F_SETLK: libc::c_int = 8;
83         pub static F_SETLKW: libc::c_int = 9;
84     }
85
86     pub struct Lock {
87         priv fd: libc::c_int,
88     }
89
90     impl Lock {
91         pub fn new(p: &Path) -> Lock {
92             let fd = p.with_c_str(|s| unsafe {
93                 libc::open(s, libc::O_RDWR | libc::O_CREAT, libc::S_IRWXU)
94             });
95             assert!(fd > 0);
96             let flock = os::flock {
97                 l_start: 0,
98                 l_len: 0,
99                 l_pid: 0,
100                 l_whence: libc::SEEK_SET as libc::c_short,
101                 l_type: os::F_WRLCK,
102                 l_sysid: 0,
103             };
104             let ret = unsafe {
105                 libc::fcntl(fd, os::F_SETLKW, &flock as *os::flock)
106             };
107             if ret == -1 {
108                 unsafe { libc::close(fd); }
109                 fail!("could not lock `{}`", p.display())
110             }
111             Lock { fd: fd }
112         }
113     }
114
115     impl Drop for Lock {
116         fn drop(&mut self) {
117             let flock = os::flock {
118                 l_start: 0,
119                 l_len: 0,
120                 l_pid: 0,
121                 l_whence: libc::SEEK_SET as libc::c_short,
122                 l_type: os::F_UNLCK,
123                 l_sysid: 0,
124             };
125             unsafe {
126                 libc::fcntl(self.fd, os::F_SETLK, &flock as *os::flock);
127                 libc::close(self.fd);
128             }
129         }
130     }
131 }
132
133 #[cfg(windows)]
134 mod imp {
135     use std::libc;
136     use std::mem;
137     use std::os::win32::as_utf16_p;
138     use std::os;
139     use std::ptr;
140
141     static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
142
143     extern "system" {
144         fn LockFileEx(hFile: libc::HANDLE,
145                       dwFlags: libc::DWORD,
146                       dwReserved: libc::DWORD,
147                       nNumberOfBytesToLockLow: libc::DWORD,
148                       nNumberOfBytesToLockHigh: libc::DWORD,
149                       lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
150         fn UnlockFileEx(hFile: libc::HANDLE,
151                         dwReserved: libc::DWORD,
152                         nNumberOfBytesToLockLow: libc::DWORD,
153                         nNumberOfBytesToLockHigh: libc::DWORD,
154                         lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
155     }
156
157     pub struct Lock {
158         priv handle: libc::HANDLE,
159     }
160
161     impl Lock {
162         pub fn new(p: &Path) -> Lock {
163             let handle = as_utf16_p(p.as_str().unwrap(), |p| unsafe {
164                 libc::CreateFileW(p,
165                                   libc::FILE_GENERIC_READ |
166                                     libc::FILE_GENERIC_WRITE,
167                                   libc::FILE_SHARE_READ |
168                                     libc::FILE_SHARE_DELETE |
169                                     libc::FILE_SHARE_WRITE,
170                                   ptr::mut_null(),
171                                   libc::CREATE_ALWAYS,
172                                   libc::FILE_ATTRIBUTE_NORMAL,
173                                   ptr::mut_null())
174             });
175             if handle as uint == libc::INVALID_HANDLE_VALUE as uint {
176                 fail!("create file error: {}", os::last_os_error());
177             }
178             let mut overlapped: libc::OVERLAPPED = unsafe { mem::init() };
179             let ret = unsafe {
180                 LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
181                            &mut overlapped)
182             };
183             if ret == 0 {
184                 unsafe { libc::CloseHandle(handle); }
185                 fail!("could not lock `{}`: {}", p.display(),
186                       os::last_os_error())
187             }
188             Lock { handle: handle }
189         }
190     }
191
192     impl Drop for Lock {
193         fn drop(&mut self) {
194             let mut overlapped: libc::OVERLAPPED = unsafe { mem::init() };
195             unsafe {
196                 UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
197                 libc::CloseHandle(self.handle);
198             }
199         }
200     }
201 }