28
28
//! ```
29
29
30
30
use libc:: {
31
+ c_char,
31
32
c_int,
32
33
uint32_t
33
34
} ;
34
- use std:: ffi:: { CString , CStr } ;
35
+ use std:: ffi:: { OsString , OsStr , CStr } ;
36
+ use std:: os:: unix:: ffi:: OsStrExt ;
35
37
use std:: mem:: size_of;
36
- use std:: os:: raw:: c_char;
37
38
use std:: os:: unix:: io:: { RawFd , AsRawFd , FromRawFd } ;
38
39
use unistd:: read;
39
40
use Result ;
40
41
use NixPath ;
41
42
use errno:: Errno ;
42
43
43
- pub mod ffi {
44
- use libc:: {
45
- c_char,
46
- c_int,
47
- uint32_t
48
- } ;
49
-
50
- extern {
51
- pub fn inotify_init1 ( flags : c_int ) -> c_int ;
52
- pub fn inotify_add_watch ( fd : c_int ,
53
- path : * const c_char ,
54
- mask : uint32_t ) -> c_int ;
55
- pub fn inotify_rm_watch ( fd : c_int , wd : c_int ) -> c_int ;
56
- }
57
-
58
- #[ derive( Debug , Clone , Copy ) ]
59
- #[ repr( C ) ]
60
- pub struct inotify_event {
61
- pub wd : c_int ,
62
- pub mask : uint32_t ,
63
- pub cookie : uint32_t ,
64
- pub len : uint32_t
65
- }
66
- }
67
-
68
- bitflags ! {
44
+ libc_bitflags ! {
69
45
/// Configuration options for [`inotify_add_watch`](fn.inotify_add_watch.html).
70
46
pub struct EventFlags : uint32_t {
71
- const IN_ACCESS = 0x00000001 ;
72
- const IN_MODIFY = 0x00000002 ;
73
- const IN_ATTRIB = 0x00000004 ;
74
- const IN_CLOSE_WRITE = 0x00000008 ;
75
- const IN_CLOSE_NOWRITE = 0x00000010 ;
76
- const IN_OPEN = 0x00000020 ;
77
- const IN_MOVED_FROM = 0x00000040 ;
78
- const IN_MOVED_TO = 0x00000080 ;
79
- const IN_CREATE = 0x00000100 ;
80
- const IN_DELETE = 0x00000200 ;
81
- const IN_DELETE_SELF = 0x00000400 ;
82
- const IN_MOVE_SELF = 0x00000800 ;
83
-
84
- const IN_UNMOUNT = 0x00002000 ;
85
- const IN_Q_OVERFLOW = 0x00004000 ;
86
- const IN_IGNORED = 0x00008000 ;
87
-
88
- const IN_ONLYDIR = 0x01000000 ;
89
- const IN_DONT_FOLLOW = 0x02000000 ;
90
- const IN_EXCL_UNLINK = 0x04000000 ;
91
- const IN_MASK_ADD = 0x20000000 ;
92
- const IN_ISDIR = 0x40000000 ;
93
- const IN_ONESHOT = 0x80000000 ;
94
-
95
- const IN_CLOSE = Self :: IN_CLOSE_WRITE . bits |
96
- Self :: IN_CLOSE_NOWRITE . bits;
97
- const IN_MOVE = Self :: IN_MOVED_FROM . bits |
98
- Self :: IN_MOVED_TO . bits;
99
- const IN_ALL_EVENTS =
100
- Self :: IN_ACCESS . bits | Self :: IN_MODIFY . bits | Self :: IN_ATTRIB . bits |
101
- Self :: IN_CLOSE_WRITE . bits | Self :: IN_CLOSE_NOWRITE . bits |
102
- Self :: IN_OPEN . bits | Self :: IN_MOVED_FROM . bits |
103
- Self :: IN_MOVED_TO . bits | Self :: IN_DELETE . bits |
104
- Self :: IN_CREATE . bits | Self :: IN_DELETE_SELF . bits |
105
- Self :: IN_MOVE_SELF . bits;
47
+ IN_ACCESS ;
48
+ IN_MODIFY ;
49
+ IN_ATTRIB ;
50
+ IN_CLOSE_WRITE ;
51
+ IN_CLOSE_NOWRITE ;
52
+ IN_OPEN ;
53
+ IN_MOVED_FROM ;
54
+ IN_MOVED_TO ;
55
+ IN_CREATE ;
56
+ IN_DELETE ;
57
+ IN_DELETE_SELF ;
58
+ IN_MOVE_SELF ;
59
+
60
+ IN_UNMOUNT ;
61
+ IN_Q_OVERFLOW ;
62
+ IN_IGNORED ;
63
+
64
+ IN_CLOSE ;
65
+ IN_MOVE ;
66
+
67
+ IN_ONLYDIR ;
68
+ IN_DONT_FOLLOW ;
69
+ IN_EXCL_UNLINK ;
70
+
71
+ IN_MASK_CREATE ;
72
+ IN_MASK_ADD ;
73
+ IN_ISDIR ;
74
+ IN_ONESHOT ;
75
+ IN_ALL_EVENTS ;
106
76
}
107
77
}
108
78
109
- bitflags ! {
79
+ libc_bitflags ! {
110
80
/// Configuration options for [`inotify_init1`](fn.inotify_init1.html).
111
81
pub struct InotifyInitFlags : c_int {
112
- const IN_CLOEXEC = 0o02000000 ;
113
- const IN_NONBLOCK = 0o00004000 ;
82
+ IN_CLOEXEC ;
83
+ IN_NONBLOCK ;
114
84
}
115
85
}
116
86
@@ -145,7 +115,7 @@ pub struct InotifyEvent {
145
115
pub cookie : u32 ,
146
116
/// Filename. This field exists only if the event was triggered for a file
147
117
/// inside the watched directory.
148
- pub name : Option < CString >
118
+ pub name : Option < OsString >
149
119
}
150
120
151
121
impl Inotify {
@@ -156,7 +126,7 @@ impl Inotify {
156
126
/// For more information see, [inotify_init(2)](http://man7.org/linux/man-pages/man2/inotify_init.2.html).
157
127
pub fn init ( flags : InotifyInitFlags ) -> Result < Inotify > {
158
128
let res = Errno :: result ( unsafe {
159
- ffi :: inotify_init1 ( flags. bits ( ) )
129
+ libc :: inotify_init1 ( flags. bits ( ) )
160
130
} ) ;
161
131
162
132
res. map ( |fd| Inotify { fd } )
@@ -173,7 +143,7 @@ impl Inotify {
173
143
{
174
144
let res = path. with_nix_path ( |cstr| {
175
145
unsafe {
176
- ffi :: inotify_add_watch ( self . fd , cstr. as_ptr ( ) , mask. bits ( ) )
146
+ libc :: inotify_add_watch ( self . fd , cstr. as_ptr ( ) , mask. bits ( ) )
177
147
}
178
148
} ) ?;
179
149
@@ -187,7 +157,7 @@ impl Inotify {
187
157
///
188
158
/// For more information see, [inotify_rm_watch(2)](http://man7.org/linux/man-pages/man2/inotify_rm_watch.2.html).
189
159
pub fn rm_watch ( & self , wd : WatchDescriptor ) -> Result < ( ) > {
190
- let res = unsafe { ffi :: inotify_rm_watch ( self . fd , wd. wd ) } ;
160
+ let res = unsafe { libc :: inotify_rm_watch ( self . fd , wd. wd ) } ;
191
161
192
162
Errno :: result ( res) . map ( drop)
193
163
}
@@ -196,10 +166,10 @@ impl Inotify {
196
166
/// can either be blocking or non blocking depending on whether IN_NONBLOCK
197
167
/// was set at initialization.
198
168
///
199
- /// Returns as many event as available. If the call was non blocking and no
200
- /// event could be read then the EAGAIN error is returned.
169
+ /// Returns as many events as available. If the call was non blocking and no
170
+ /// events could be read then the EAGAIN error is returned.
201
171
pub fn read_events ( & self ) -> Result < Vec < InotifyEvent > > {
202
- let header_size = size_of :: < ffi :: inotify_event > ( ) ;
172
+ let header_size = size_of :: < libc :: inotify_event > ( ) ;
203
173
let mut buffer = [ 0u8 ; 4096 ] ;
204
174
let mut events = Vec :: new ( ) ;
205
175
let mut offset = 0 ;
@@ -211,7 +181,7 @@ impl Inotify {
211
181
& * (
212
182
buffer
213
183
. as_ptr ( )
214
- . offset ( offset as isize ) as * const ffi :: inotify_event
184
+ . offset ( offset as isize ) as * const libc :: inotify_event
215
185
)
216
186
} ;
217
187
@@ -226,7 +196,7 @@ impl Inotify {
226
196
} ;
227
197
let cstr = unsafe { CStr :: from_ptr ( ptr) } ;
228
198
229
- Some ( cstr. to_owned ( ) )
199
+ Some ( OsStr :: from_bytes ( cstr. to_bytes ( ) ) . to_owned ( ) )
230
200
}
231
201
} ;
232
202
0 commit comments