@@ -13,96 +13,9 @@ use std::path::Path;
13
13
cfg_if ! {
14
14
if #[ cfg( unix) ] {
15
15
use std:: ffi:: { CString , OsStr } ;
16
+ use std:: mem;
16
17
use std:: os:: unix:: prelude:: * ;
17
18
18
- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
19
- mod os {
20
- #[ repr( C ) ]
21
- pub struct flock {
22
- pub l_type: libc:: c_short,
23
- pub l_whence: libc:: c_short,
24
- pub l_start: libc:: off_t,
25
- pub l_len: libc:: off_t,
26
- pub l_pid: libc:: pid_t,
27
-
28
- // not actually here, but brings in line with freebsd
29
- pub l_sysid: libc:: c_int,
30
- }
31
- }
32
-
33
- #[ cfg( target_os = "freebsd" ) ]
34
- mod os {
35
- #[ repr( C ) ]
36
- pub struct flock {
37
- pub l_start: libc:: off_t,
38
- pub l_len: libc:: off_t,
39
- pub l_pid: libc:: pid_t,
40
- pub l_type: libc:: c_short,
41
- pub l_whence: libc:: c_short,
42
- pub l_sysid: libc:: c_int,
43
- }
44
- }
45
-
46
- #[ cfg( any( target_os = "dragonfly" ,
47
- target_os = "netbsd" ,
48
- target_os = "openbsd" ) ) ]
49
- mod os {
50
- #[ repr( C ) ]
51
- pub struct flock {
52
- pub l_start: libc:: off_t,
53
- pub l_len: libc:: off_t,
54
- pub l_pid: libc:: pid_t,
55
- pub l_type: libc:: c_short,
56
- pub l_whence: libc:: c_short,
57
-
58
- // not actually here, but brings in line with freebsd
59
- pub l_sysid: libc:: c_int,
60
- }
61
- }
62
-
63
- #[ cfg( target_os = "haiku" ) ]
64
- mod os {
65
- #[ repr( C ) ]
66
- pub struct flock {
67
- pub l_type: libc:: c_short,
68
- pub l_whence: libc:: c_short,
69
- pub l_start: libc:: off_t,
70
- pub l_len: libc:: off_t,
71
- pub l_pid: libc:: pid_t,
72
-
73
- // not actually here, but brings in line with freebsd
74
- pub l_sysid: libc:: c_int,
75
- }
76
- }
77
-
78
- #[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
79
- mod os {
80
- #[ repr( C ) ]
81
- pub struct flock {
82
- pub l_start: libc:: off_t,
83
- pub l_len: libc:: off_t,
84
- pub l_pid: libc:: pid_t,
85
- pub l_type: libc:: c_short,
86
- pub l_whence: libc:: c_short,
87
-
88
- // not actually here, but brings in line with freebsd
89
- pub l_sysid: libc:: c_int,
90
- }
91
- }
92
-
93
- #[ cfg( target_os = "solaris" ) ]
94
- mod os {
95
- #[ repr( C ) ]
96
- pub struct flock {
97
- pub l_type: libc:: c_short,
98
- pub l_whence: libc:: c_short,
99
- pub l_start: libc:: off_t,
100
- pub l_len: libc:: off_t,
101
- pub l_sysid: libc:: c_int,
102
- pub l_pid: libc:: pid_t,
103
- }
104
- }
105
-
106
19
#[ derive( Debug ) ]
107
20
pub struct Lock {
108
21
fd: libc:: c_int,
@@ -132,19 +45,17 @@ cfg_if! {
132
45
}
133
46
134
47
let lock_type = if exclusive {
135
- libc:: F_WRLCK as libc :: c_short
48
+ libc:: F_WRLCK
136
49
} else {
137
- libc:: F_RDLCK as libc :: c_short
50
+ libc:: F_RDLCK
138
51
} ;
139
52
140
- let flock = os:: flock {
141
- l_start: 0 ,
142
- l_len: 0 ,
143
- l_pid: 0 ,
144
- l_whence: libc:: SEEK_SET as libc:: c_short,
145
- l_type: lock_type,
146
- l_sysid: 0 ,
147
- } ;
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
+
148
59
let cmd = if wait { libc:: F_SETLKW } else { libc:: F_SETLK } ;
149
60
let ret = unsafe {
150
61
libc:: fcntl( fd, cmd, & flock)
@@ -161,14 +72,12 @@ cfg_if! {
161
72
162
73
impl Drop for Lock {
163
74
fn drop( & mut self ) {
164
- let flock = os:: flock {
165
- l_start: 0 ,
166
- l_len: 0 ,
167
- l_pid: 0 ,
168
- l_whence: libc:: SEEK_SET as libc:: c_short,
169
- l_type: libc:: F_UNLCK as libc:: c_short,
170
- l_sysid: 0 ,
171
- } ;
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
+
172
81
unsafe {
173
82
libc:: fcntl( self . fd, libc:: F_SETLK , & flock) ;
174
83
libc:: close( self . fd) ;
0 commit comments