@@ -21,6 +21,10 @@ extern crate nix_test as nixtest;
21
21
// Re-exports
22
22
pub use libc:: { c_int, c_void} ;
23
23
pub use errno:: { Errno , Result } ;
24
+ pub use nix_string:: NixString ;
25
+
26
+ #[ macro_use]
27
+ mod nix_string;
24
28
25
29
pub mod errno;
26
30
pub mod features;
@@ -40,147 +44,3 @@ pub mod sched;
40
44
41
45
pub mod sys;
42
46
pub mod unistd;
43
-
44
- /*
45
- *
46
- * ===== Error =====
47
- *
48
- */
49
-
50
- use libc:: c_char;
51
- use std:: ptr;
52
- use std:: ffi:: CStr ;
53
- use std:: path:: { Path , PathBuf } ;
54
- use std:: os:: unix:: ffi:: OsStrExt ;
55
- use std:: io;
56
- use std:: fmt;
57
- use std:: error;
58
- use libc:: PATH_MAX ;
59
-
60
- #[ derive( Clone , Copy , Debug , PartialEq ) ]
61
- pub enum Error {
62
- Sys ( errno:: Errno ) ,
63
- InvalidPath ,
64
- }
65
-
66
- impl Error {
67
- pub fn from_errno ( errno : errno:: Errno ) -> Error {
68
- Error :: Sys ( errno)
69
- }
70
-
71
- pub fn last ( ) -> Error {
72
- Error :: Sys ( errno:: Errno :: last ( ) )
73
- }
74
-
75
- pub fn invalid_argument ( ) -> Error {
76
- Error :: Sys ( errno:: EINVAL )
77
- }
78
-
79
- pub fn errno ( & self ) -> errno:: Errno {
80
- match * self {
81
- Error :: Sys ( errno) => errno,
82
- Error :: InvalidPath => errno:: Errno :: EINVAL ,
83
- }
84
- }
85
- }
86
-
87
- impl From < errno:: Errno > for Error {
88
- fn from ( errno : errno:: Errno ) -> Error { Error :: from_errno ( errno) }
89
- }
90
-
91
- impl error:: Error for Error {
92
- fn description ( & self ) -> & str {
93
- match self {
94
- & Error :: InvalidPath => "Invalid path" ,
95
- & Error :: Sys ( ref errno) => errno. desc ( ) ,
96
- }
97
- }
98
- }
99
-
100
- impl fmt:: Display for Error {
101
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
102
- match self {
103
- & Error :: InvalidPath => write ! ( f, "Invalid path" ) ,
104
- & Error :: Sys ( errno) => write ! ( f, "{:?}: {}" , errno, errno. desc( ) ) ,
105
- }
106
- }
107
- }
108
-
109
- impl From < Error > for io:: Error {
110
- fn from ( err : Error ) -> Self {
111
- match err {
112
- Error :: InvalidPath => io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ,
113
- Error :: Sys ( errno) => io:: Error :: from_raw_os_error ( errno as i32 ) ,
114
- }
115
- }
116
- }
117
-
118
- pub trait NixPath {
119
- fn len ( & self ) -> usize ;
120
-
121
- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
122
- where F : FnOnce ( & CStr ) -> T ;
123
- }
124
-
125
- impl NixPath for CStr {
126
- fn len ( & self ) -> usize {
127
- self . to_bytes ( ) . len ( )
128
- }
129
-
130
- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
131
- where F : FnOnce ( & CStr ) -> T {
132
- // Equivalence with the [u8] impl.
133
- if self . len ( ) >= PATH_MAX as usize {
134
- return Err ( Error :: InvalidPath ) ;
135
- }
136
-
137
- Ok ( f ( self ) )
138
- }
139
- }
140
-
141
- impl NixPath for [ u8 ] {
142
- fn len ( & self ) -> usize {
143
- self . len ( )
144
- }
145
-
146
- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T >
147
- where F : FnOnce ( & CStr ) -> T {
148
- let mut buf = [ 0u8 ; PATH_MAX as usize ] ;
149
-
150
- if self . len ( ) >= PATH_MAX as usize {
151
- return Err ( Error :: InvalidPath ) ;
152
- }
153
-
154
- match self . iter ( ) . position ( |b| * b == 0 ) {
155
- Some ( _) => Err ( Error :: InvalidPath ) ,
156
- None => {
157
- unsafe {
158
- // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028
159
- ptr:: copy_nonoverlapping ( self . as_ptr ( ) , buf. as_mut_ptr ( ) , self . len ( ) ) ;
160
- Ok ( f ( CStr :: from_ptr ( buf. as_ptr ( ) as * const c_char ) ) )
161
- }
162
-
163
- }
164
- }
165
- }
166
- }
167
-
168
- impl NixPath for Path {
169
- fn len ( & self ) -> usize {
170
- self . as_os_str ( ) . as_bytes ( ) . len ( )
171
- }
172
-
173
- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
174
- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
175
- }
176
- }
177
-
178
- impl NixPath for PathBuf {
179
- fn len ( & self ) -> usize {
180
- self . as_os_str ( ) . as_bytes ( ) . len ( )
181
- }
182
-
183
- fn with_nix_path < T , F > ( & self , f : F ) -> Result < T > where F : FnOnce ( & CStr ) -> T {
184
- self . as_os_str ( ) . as_bytes ( ) . with_nix_path ( f)
185
- }
186
- }
0 commit comments