@@ -32,7 +32,7 @@ use libc::{c_char, c_int};
32
32
use std:: ffi:: { CStr , OsStr , OsString } ;
33
33
use std:: mem:: { size_of, MaybeUninit } ;
34
34
use std:: os:: unix:: ffi:: OsStrExt ;
35
- use std:: os:: unix:: io:: { AsRawFd , FromRawFd , RawFd } ;
35
+ use std:: os:: unix:: io:: { AsRawFd , FromRawFd , OwnedFd , RawFd } ;
36
36
use std:: ptr;
37
37
38
38
libc_bitflags ! {
@@ -101,9 +101,9 @@ libc_bitflags! {
101
101
102
102
/// An inotify instance. This is also a file descriptor, you can feed it to
103
103
/// other interfaces consuming file descriptors, epoll for example.
104
- #[ derive( Debug , Clone , Copy ) ]
104
+ #[ derive( Debug ) ]
105
105
pub struct Inotify {
106
- fd : RawFd ,
106
+ fd : OwnedFd ,
107
107
}
108
108
109
109
/// This object is returned when you create a new watch on an inotify instance.
@@ -143,7 +143,7 @@ impl Inotify {
143
143
pub fn init ( flags : InitFlags ) -> Result < Inotify > {
144
144
let res = Errno :: result ( unsafe { libc:: inotify_init1 ( flags. bits ( ) ) } ) ;
145
145
146
- res. map ( |fd| Inotify { fd } )
146
+ res. map ( |fd| Inotify { fd : unsafe { OwnedFd :: from_raw_fd ( fd ) } } )
147
147
}
148
148
149
149
/// Adds a new watch on the target file or directory.
@@ -152,12 +152,12 @@ impl Inotify {
152
152
///
153
153
/// For more information see, [inotify_add_watch(2)](https://man7.org/linux/man-pages/man2/inotify_add_watch.2.html).
154
154
pub fn add_watch < P : ?Sized + NixPath > (
155
- self ,
155
+ & self ,
156
156
path : & P ,
157
157
mask : AddWatchFlags ,
158
158
) -> Result < WatchDescriptor > {
159
159
let res = path. with_nix_path ( |cstr| unsafe {
160
- libc:: inotify_add_watch ( self . fd , cstr. as_ptr ( ) , mask. bits ( ) )
160
+ libc:: inotify_add_watch ( self . fd . as_raw_fd ( ) , cstr. as_ptr ( ) , mask. bits ( ) )
161
161
} ) ?;
162
162
163
163
Errno :: result ( res) . map ( |wd| WatchDescriptor { wd } )
@@ -169,15 +169,15 @@ impl Inotify {
169
169
/// Returns an EINVAL error if the watch descriptor is invalid.
170
170
///
171
171
/// For more information see, [inotify_rm_watch(2)](https://man7.org/linux/man-pages/man2/inotify_rm_watch.2.html).
172
- pub fn rm_watch ( self , wd : WatchDescriptor ) -> Result < ( ) > {
172
+ pub fn rm_watch ( & self , wd : WatchDescriptor ) -> Result < ( ) > {
173
173
cfg_if ! {
174
174
if #[ cfg( target_os = "linux" ) ] {
175
175
let arg = wd. wd;
176
176
} else if #[ cfg( target_os = "android" ) ] {
177
177
let arg = wd. wd as u32 ;
178
178
}
179
179
}
180
- let res = unsafe { libc:: inotify_rm_watch ( self . fd , arg) } ;
180
+ let res = unsafe { libc:: inotify_rm_watch ( self . fd . as_raw_fd ( ) , arg) } ;
181
181
182
182
Errno :: result ( res) . map ( drop)
183
183
}
@@ -188,14 +188,14 @@ impl Inotify {
188
188
///
189
189
/// Returns as many events as available. If the call was non blocking and no
190
190
/// events could be read then the EAGAIN error is returned.
191
- pub fn read_events ( self ) -> Result < Vec < InotifyEvent > > {
191
+ pub fn read_events ( & self ) -> Result < Vec < InotifyEvent > > {
192
192
let header_size = size_of :: < libc:: inotify_event > ( ) ;
193
193
const BUFSIZ : usize = 4096 ;
194
194
let mut buffer = [ 0u8 ; BUFSIZ ] ;
195
195
let mut events = Vec :: new ( ) ;
196
196
let mut offset = 0 ;
197
197
198
- let nread = read ( self . fd , & mut buffer) ?;
198
+ let nread = read ( self . fd . as_raw_fd ( ) , & mut buffer) ?;
199
199
200
200
while ( nread - offset) >= header_size {
201
201
let event = unsafe {
@@ -235,14 +235,8 @@ impl Inotify {
235
235
}
236
236
}
237
237
238
- impl AsRawFd for Inotify {
239
- fn as_raw_fd ( & self ) -> RawFd {
240
- self . fd
241
- }
242
- }
243
-
244
238
impl FromRawFd for Inotify {
245
239
unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
246
- Inotify { fd }
240
+ Inotify { fd : OwnedFd :: from_raw_fd ( fd ) }
247
241
}
248
242
}
0 commit comments