@@ -146,6 +146,7 @@ impl Ord for Pid {
146
146
/// will not be yielded. An atomic view of processes on the system seems
147
147
/// non-trivial.
148
148
pub struct PidIter {
149
+ dir : String ,
149
150
dir_iter : ReadDir ,
150
151
query : PidQuery ,
151
152
}
@@ -159,14 +160,16 @@ impl PidIter {
159
160
/// Create a new iterator over all processes in /proc, but only yield
160
161
/// processes that match the given query.
161
162
pub fn new_query ( query : PidQuery ) -> Result < Self , ProcError > {
162
- let proc_dir = Path :: new ( "/proc" ) ;
163
+ let dir_name = "/proc" . to_owned ( ) ;
164
+ let proc_dir = Path :: new ( & dir_name) ;
163
165
let dir_iter = try!(
164
166
fs:: read_dir ( proc_dir)
165
167
. map_err ( |e|
166
168
ProcError :: new ( ProcOper :: Opening , ProcFile :: ProcDir , Some ( e) , Some ( "PidIter" ) )
167
169
)
168
170
) ;
169
171
Ok ( PidIter {
172
+ dir : dir_name. clone ( ) ,
170
173
dir_iter : dir_iter,
171
174
query : query,
172
175
} )
@@ -183,14 +186,15 @@ impl PidIter {
183
186
)
184
187
) ;
185
188
Ok ( PidIter {
189
+ dir : dir_name. clone ( ) ,
186
190
dir_iter : dir_iter,
187
191
query : query
188
192
} )
189
193
}
190
194
191
195
/// Given a DirEntry, try to create a Pid struct, and only return if
192
196
/// it matches the query, and is complete.
193
- fn proc_dir_filter ( entry_opt : Result < DirEntry , io:: Error > , query : & PidQuery )
197
+ fn proc_dir_filter ( entry_opt : Result < DirEntry , io:: Error > , query : & PidQuery , dir_name : & str )
194
198
-> Option < Result < Pid , ProcError > > {
195
199
let file = entry_opt
196
200
. map_err ( |e|
@@ -211,7 +215,7 @@ impl PidIter {
211
215
// If an error is not hard (error opening or reading file),
212
216
// do not error as it may be a now-dead process.
213
217
// If a parsing error occurs, then do return an error.
214
- let prc = match Pid :: new ( pid) {
218
+ let prc = match Pid :: new_dir ( Path :: new ( & dir_name ) , pid) {
215
219
Ok ( prc) => prc,
216
220
Err ( e) => {
217
221
if e. is_hard ( ) {
@@ -221,7 +225,7 @@ impl PidIter {
221
225
}
222
226
}
223
227
} ;
224
- match prc. query ( query) {
228
+ match prc. query ( & query) {
225
229
true => Some ( Ok ( prc) ) ,
226
230
false => None
227
231
}
@@ -236,7 +240,7 @@ impl Iterator for PidIter {
236
240
237
241
fn next ( & mut self ) -> Option < Self :: Item > {
238
242
for entry in self . dir_iter . by_ref ( ) {
239
- match Self :: proc_dir_filter ( entry, & self . query ) {
243
+ match Self :: proc_dir_filter ( entry, & self . query , & self . dir ) {
240
244
some @ Some ( _) => return some,
241
245
None => continue
242
246
}
0 commit comments