Skip to content

Commit 2ec9c3d

Browse files
committed
Removed ffi from nix and into libc; Switched from CStr to OsStr; Minor corrections on comments;
1 parent d586d10 commit 2ec9c3d

File tree

3 files changed

+60
-91
lines changed

3 files changed

+60
-91
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = [
1616
]
1717

1818
[dependencies]
19-
libc = { git = "https://github.com/rust-lang/libc" }
19+
libc = { path = "../libc" }
2020
bitflags = "1.0"
2121
cfg-if = "0.1.0"
2222
void = "1.0.2"

src/sys/inotify.rs

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,89 +28,59 @@
2828
//! ```
2929
3030
use libc::{
31+
c_char,
3132
c_int,
3233
uint32_t
3334
};
34-
use std::ffi::{CString,CStr};
35+
use std::ffi::{OsString,OsStr,CStr};
36+
use std::os::unix::ffi::OsStrExt;
3537
use std::mem::size_of;
36-
use std::os::raw::c_char;
3738
use std::os::unix::io::{RawFd,AsRawFd,FromRawFd};
3839
use unistd::read;
3940
use Result;
4041
use NixPath;
4142
use errno::Errno;
4243

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! {
6945
/// Configuration options for [`inotify_add_watch`](fn.inotify_add_watch.html).
7046
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;
10676
}
10777
}
10878

109-
bitflags! {
79+
libc_bitflags! {
11080
/// Configuration options for [`inotify_init1`](fn.inotify_init1.html).
11181
pub struct InotifyInitFlags: c_int {
112-
const IN_CLOEXEC = 0o02000000;
113-
const IN_NONBLOCK = 0o00004000;
82+
IN_CLOEXEC;
83+
IN_NONBLOCK;
11484
}
11585
}
11686

@@ -145,7 +115,7 @@ pub struct InotifyEvent {
145115
pub cookie: u32,
146116
/// Filename. This field exists only if the event was triggered for a file
147117
/// inside the watched directory.
148-
pub name: Option<CString>
118+
pub name: Option<OsString>
149119
}
150120

151121
impl Inotify {
@@ -156,7 +126,7 @@ impl Inotify {
156126
/// For more information see, [inotify_init(2)](http://man7.org/linux/man-pages/man2/inotify_init.2.html).
157127
pub fn init(flags: InotifyInitFlags) -> Result<Inotify> {
158128
let res = Errno::result(unsafe {
159-
ffi::inotify_init1(flags.bits())
129+
libc::inotify_init1(flags.bits())
160130
});
161131

162132
res.map(|fd| Inotify { fd })
@@ -173,7 +143,7 @@ impl Inotify {
173143
{
174144
let res = path.with_nix_path(|cstr| {
175145
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())
177147
}
178148
})?;
179149

@@ -187,7 +157,7 @@ impl Inotify {
187157
///
188158
/// For more information see, [inotify_rm_watch(2)](http://man7.org/linux/man-pages/man2/inotify_rm_watch.2.html).
189159
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) };
191161

192162
Errno::result(res).map(drop)
193163
}
@@ -196,10 +166,10 @@ impl Inotify {
196166
/// can either be blocking or non blocking depending on whether IN_NONBLOCK
197167
/// was set at initialization.
198168
///
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.
201171
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>();
203173
let mut buffer = [0u8; 4096];
204174
let mut events = Vec::new();
205175
let mut offset = 0;
@@ -211,7 +181,7 @@ impl Inotify {
211181
&*(
212182
buffer
213183
.as_ptr()
214-
.offset(offset as isize) as *const ffi::inotify_event
184+
.offset(offset as isize) as *const libc::inotify_event
215185
)
216186
};
217187

@@ -226,7 +196,7 @@ impl Inotify {
226196
};
227197
let cstr = unsafe { CStr::from_ptr(ptr) };
228198

229-
Some(cstr.to_owned())
199+
Some(OsStr::from_bytes(cstr.to_bytes()).to_owned())
230200
}
231201
};
232202

test/sys/test_inotify.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use nix::sys::inotify::{EventFlags,InotifyInitFlags,Inotify};
22
use nix::Error;
33
use nix::errno::Errno;
44
use tempfile;
5-
use std::ffi::CString;
5+
use std::ffi::OsString;
66
use std::fs::{rename, File};
77

88
#[test]
@@ -11,16 +11,15 @@ pub fn test_inotify() {
1111
.unwrap();
1212
let tempdir = tempfile::tempdir().unwrap();
1313

14-
let wd = instance.add_watch(tempdir.path(), EventFlags::IN_ALL_EVENTS);
15-
assert!(wd.is_ok());
14+
instance.add_watch(tempdir.path(), EventFlags::IN_ALL_EVENTS).unwrap();
1615

17-
let event = instance.read_events();
18-
assert_eq!(event.unwrap_err(), Error::Sys(Errno::EAGAIN));
16+
let events = instance.read_events();
17+
assert_eq!(events.unwrap_err(), Error::Sys(Errno::EAGAIN));
1918

2019
File::create(tempdir.path().join("test")).unwrap();
2120

2221
let events = instance.read_events().unwrap();
23-
assert_eq!(events[0].name, Some(CString::new("test").unwrap()));
22+
assert_eq!(events[0].name, Some(OsString::from("test")));
2423
}
2524

2625
#[test]
@@ -29,11 +28,10 @@ pub fn test_inotify_multi_events() {
2928
.unwrap();
3029
let tempdir = tempfile::tempdir().unwrap();
3130

32-
let wd = instance.add_watch(tempdir.path(), EventFlags::IN_ALL_EVENTS);
33-
assert!(wd.is_ok());
31+
instance.add_watch(tempdir.path(), EventFlags::IN_ALL_EVENTS).unwrap();
3432

35-
let event = instance.read_events();
36-
assert_eq!(event.unwrap_err(), Error::Sys(Errno::EAGAIN));
33+
let events = instance.read_events();
34+
assert_eq!(events.unwrap_err(), Error::Sys(Errno::EAGAIN));
3735

3836
File::create(tempdir.path().join("test")).unwrap();
3937
rename(tempdir.path().join("test"), tempdir.path().join("test2")).unwrap();
@@ -49,19 +47,20 @@ pub fn test_inotify_multi_events() {
4947
assert_eq!(events.len(), 5);
5048

5149
assert_eq!(events[0].mask, EventFlags::IN_CREATE);
52-
assert_eq!(events[0].name, Some(CString::new("test").unwrap()));
50+
assert_eq!(events[0].name, Some(OsString::from("test")));
5351

5452
assert_eq!(events[1].mask, EventFlags::IN_OPEN);
55-
assert_eq!(events[1].name, Some(CString::new("test").unwrap()));
53+
assert_eq!(events[1].name, Some(OsString::from("test")));
5654

5755
assert_eq!(events[2].mask, EventFlags::IN_CLOSE_WRITE);
58-
assert_eq!(events[2].name, Some(CString::new("test").unwrap()));
56+
assert_eq!(events[2].name, Some(OsString::from("test")));
5957

6058
assert_eq!(events[3].mask, EventFlags::IN_MOVED_FROM);
61-
assert_eq!(events[3].name, Some(CString::new("test").unwrap()));
59+
assert_eq!(events[3].name, Some(OsString::from("test")));
6260

6361
assert_eq!(events[4].mask, EventFlags::IN_MOVED_TO);
64-
assert_eq!(events[4].name, Some(CString::new("test2").unwrap()));
62+
assert_eq!(events[4].name, Some(OsString::from("test2")));
6563

6664
assert_eq!(events[3].cookie, events[4].cookie);
6765
}
66+

0 commit comments

Comments
 (0)