Skip to content

Commit 5b3bb51

Browse files
committed
---
yaml --- r: 146160 b: refs/heads/try2 c: 9110a38 h: refs/heads/master v: v3
1 parent 578a8a9 commit 5b3bb51

File tree

11 files changed

+132
-209
lines changed

11 files changed

+132
-209
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 0cad9847652088b35ee4c13c04539ca3a67611f7
8+
refs/heads/try2: 9110a38cbfd801983a838775c690c83e9189b4c3
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/c_str.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,56 @@ impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
348348
}
349349
}
350350

351+
/// Parses a C "multistring", eg windows env values or
352+
/// the req->ptr result in a uv_fs_readdir() call.
353+
///
354+
/// Optionally, a `count` can be passed in, limiting the
355+
/// parsing to only being done `count`-times.
356+
///
357+
/// The specified closure is invoked with each string that
358+
/// is found, and the number of strings found is returned.
359+
pub unsafe fn from_c_multistring(buf: *libc::c_char,
360+
count: Option<uint>,
361+
f: &fn(&CString)) -> uint {
362+
363+
let mut curr_ptr: uint = buf as uint;
364+
let mut ctr = 0;
365+
let (limited_count, limit) = match count {
366+
Some(limit) => (true, limit),
367+
None => (false, 0)
368+
};
369+
while ((limited_count && ctr < limit) || !limited_count)
370+
&& *(curr_ptr as *libc::c_char) != 0 as libc::c_char {
371+
let cstr = CString::new(curr_ptr as *libc::c_char, false);
372+
f(&cstr);
373+
curr_ptr += cstr.len() + 1;
374+
ctr += 1;
375+
}
376+
return ctr;
377+
}
378+
351379
#[cfg(test)]
352380
mod tests {
353381
use super::*;
354382
use libc;
355383
use ptr;
356384
use option::{Some, None};
357385

386+
#[test]
387+
fn test_str_multistring_parsing() {
388+
unsafe {
389+
let input = bytes!("zero", "\x00", "one", "\x00", "\x00");
390+
let ptr = vec::raw::to_ptr(input);
391+
let expected = ["zero", "one"];
392+
let mut it = expected.iter();
393+
let result = do from_c_multistring(ptr as *libc::c_char, None) |c| {
394+
assert_eq!(c.as_str(), expected.next().unwrap());
395+
};
396+
assert_eq!(result, 2);
397+
assert!(it.next().is_none());
398+
}
399+
}
400+
358401
#[test]
359402
fn test_str_to_c_str() {
360403
do "".to_c_str().with_ref |buf| {

branches/try2/src/libstd/os.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn close(fd: c_int) -> c_int {
6262
// which are for Windows and for non-Windows, if necessary.
6363
// See https://github.com/mozilla/rust/issues/9822 for more information.
6464

65-
pub mod rustrt {
65+
mod rustrt {
6666
use libc::{c_char, c_int};
6767
use libc;
6868

@@ -200,7 +200,10 @@ pub fn env() -> ~[(~str,~str)] {
200200
fail!("os::env() failure getting env string from OS: {}",
201201
os::last_os_error());
202202
}
203-
let result = str::raw::from_c_multistring(ch as *libc::c_char, None);
203+
let mut result = ~[];
204+
do c_str::from_c_multistring(ch as *libc::c_char, None) |cstr| {
205+
result.push(cstr.as_str().to_owned());
206+
};
204207
FreeEnvironmentStringsA(ch);
205208
result
206209
}

branches/try2/src/libstd/rt/io/file.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ with regular files & directories on a filesystem.
1515
1616
At the top-level of the module are a set of freestanding functions,
1717
associated with various filesystem operations. They all operate
18-
on a `PathLike` object.
18+
on a `ToCStr` object. This trait is already defined for common
19+
objects such as strings and `Path` instances.
1920
2021
All operations in this module, including those as part of `FileStream` et al
2122
block the task during execution. Most will raise `std::rt::io::{io_error,read_error}`
@@ -30,7 +31,7 @@ free function counterparts.
3031
*/
3132

3233
use prelude::*;
33-
use super::support::PathLike;
34+
use c_str::ToCStr;
3435
use super::{Reader, Writer, Seek};
3536
use super::{SeekStyle, Read, Write};
3637
use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
@@ -48,7 +49,6 @@ use path::Path;
4849
///
4950
/// use std;
5051
/// use std::path::Path;
51-
/// use std::rt::io::support::PathLike;
5252
/// use std::rt::io::file::open;
5353
/// use std::rt::io::{FileMode, FileAccess};
5454
///
@@ -87,13 +87,13 @@ use path::Path;
8787
/// * Attempting to open a file with a `FileAccess` that the user lacks permissions
8888
/// for
8989
/// * Filesystem-level errors (full disk, etc)
90-
pub fn open<P: PathLike>(path: &P,
91-
mode: FileMode,
92-
access: FileAccess
93-
) -> Option<FileStream> {
90+
pub fn open<P: ToCStr>(path: &P,
91+
mode: FileMode,
92+
access: FileAccess
93+
) -> Option<FileStream> {
9494
let open_result = unsafe {
9595
let io: *mut IoFactoryObject = Local::unsafe_borrow();
96-
(*io).fs_open(path, mode, access)
96+
(*io).fs_open(&path.to_c_str(), mode, access)
9797
};
9898
match open_result {
9999
Ok(fd) => Some(FileStream {
@@ -113,7 +113,6 @@ pub fn open<P: PathLike>(path: &P,
113113
///
114114
/// use std;
115115
/// use std::path::Path;
116-
/// use std::rt::io::support::PathLike;
117116
/// use std::rt::io::file::unlink;
118117
///
119118
/// let p = &Path("/some/file/path.txt");
@@ -129,10 +128,10 @@ pub fn open<P: PathLike>(path: &P,
129128
///
130129
/// This function will raise an `io_error` condition if the user lacks permissions to
131130
/// remove the file or if some other filesystem-level error occurs
132-
pub fn unlink<P: PathLike>(path: &P) {
131+
pub fn unlink<P: ToCStr>(path: &P) {
133132
let unlink_result = unsafe {
134133
let io: *mut IoFactoryObject = Local::unsafe_borrow();
135-
(*io).fs_unlink(path)
134+
(*io).fs_unlink(&path.to_c_str())
136135
};
137136
match unlink_result {
138137
Ok(_) => (),
@@ -148,7 +147,6 @@ pub fn unlink<P: PathLike>(path: &P) {
148147
///
149148
/// use std;
150149
/// use std::path::Path;
151-
/// use std::rt::io::support::PathLike;
152150
/// use std::rt::io::file::mkdir;
153151
///
154152
/// let p = &Path("/some/dir");
@@ -159,10 +157,10 @@ pub fn unlink<P: PathLike>(path: &P) {
159157
///
160158
/// This call will raise an `io_error` condition if the user lacks permissions to make a
161159
/// new directory at the provided path, or if the directory already exists
162-
pub fn mkdir<P: PathLike>(path: &P) {
160+
pub fn mkdir<P: ToCStr>(path: &P) {
163161
let mkdir_result = unsafe {
164162
let io: *mut IoFactoryObject = Local::unsafe_borrow();
165-
(*io).fs_mkdir(path)
163+
(*io).fs_mkdir(&path.to_c_str())
166164
};
167165
match mkdir_result {
168166
Ok(_) => (),
@@ -178,7 +176,6 @@ pub fn mkdir<P: PathLike>(path: &P) {
178176
///
179177
/// use std;
180178
/// use std::path::Path;
181-
/// use std::rt::io::support::PathLike;
182179
/// use std::rt::io::file::rmdir;
183180
///
184181
/// let p = &Path("/some/dir");
@@ -189,10 +186,10 @@ pub fn mkdir<P: PathLike>(path: &P) {
189186
///
190187
/// This call will raise an `io_error` condition if the user lacks permissions to remove the
191188
/// directory at the provided path, or if the directory isn't empty
192-
pub fn rmdir<P: PathLike>(path: &P) {
189+
pub fn rmdir<P: ToCStr>(path: &P) {
193190
let rmdir_result = unsafe {
194191
let io: *mut IoFactoryObject = Local::unsafe_borrow();
195-
(*io).fs_rmdir(path)
192+
(*io).fs_rmdir(&path.to_c_str())
196193
};
197194
match rmdir_result {
198195
Ok(_) => (),
@@ -204,16 +201,15 @@ pub fn rmdir<P: PathLike>(path: &P) {
204201

205202
/// Get information on the file, directory, etc at the provided path
206203
///
207-
/// Given a `rt::io::support::PathLike`, query the file system to get
208-
/// information about a file, directory, etc.
204+
/// Given a path, query the file system to get information about a file,
205+
/// directory, etc.
209206
///
210207
/// Returns a `Some(std::rt::io::PathInfo)` on success
211208
///
212209
/// # Example
213210
///
214211
/// use std;
215212
/// use std::path::Path;
216-
/// use std::rt::io::support::PathLike;
217213
/// use std::rt::io::file::stat;
218214
///
219215
/// let p = &Path("/some/file/path.txt");
@@ -238,10 +234,10 @@ pub fn rmdir<P: PathLike>(path: &P) {
238234
/// This call will raise an `io_error` condition if the user lacks the requisite
239235
/// permissions to perform a `stat` call on the given path or if there is no
240236
/// entry in the filesystem at the provided path.
241-
pub fn stat<P: PathLike>(path: &P) -> Option<FileStat> {
237+
pub fn stat<P: ToCStr>(path: &P) -> Option<FileStat> {
242238
let open_result = unsafe {
243239
let io: *mut IoFactoryObject = Local::unsafe_borrow();
244-
(*io).fs_stat(path)
240+
(*io).fs_stat(&path.to_c_str())
245241
};
246242
match open_result {
247243
Ok(p) => {
@@ -260,7 +256,6 @@ pub fn stat<P: PathLike>(path: &P) -> Option<FileStat> {
260256
///
261257
/// use std;
262258
/// use std::path::Path;
263-
/// use std::rt::io::support::PathLike;
264259
/// use std::rt::io::file::readdir;
265260
///
266261
/// fn visit_dirs(dir: &Path, cb: &fn(&Path)) {
@@ -279,10 +274,10 @@ pub fn stat<P: PathLike>(path: &P) -> Option<FileStat> {
279274
/// Will raise an `io_error` condition if the provided `path` doesn't exist,
280275
/// the process lacks permissions to view the contents or if the `path` points
281276
/// at a non-directory file
282-
pub fn readdir<P: PathLike>(path: &P) -> Option<~[Path]> {
277+
pub fn readdir<P: ToCStr>(path: &P) -> Option<~[Path]> {
283278
let readdir_result = unsafe {
284279
let io: *mut IoFactoryObject = Local::unsafe_borrow();
285-
(*io).fs_readdir(path, 0)
280+
(*io).fs_readdir(&path.to_c_str(), 0)
286281
};
287282
match readdir_result {
288283
Ok(p) => {

branches/try2/src/libstd/rt/io/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,6 @@ pub mod comm_adapters;
298298
/// Extension traits
299299
pub mod extensions;
300300

301-
/// Non-I/O things needed by the I/O module
302-
// XXX: shouldn this really be pub?
303-
pub mod support;
304-
305301
/// Basic Timer
306302
pub mod timer;
307303

branches/try2/src/libstd/rt/io/net/unix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ instances as clients.
2424

2525
use prelude::*;
2626

27-
use super::super::support::PathLike;
27+
use c_str::ToCStr;
2828
use rt::rtio::{IoFactory, IoFactoryObject, RtioUnixListener};
2929
use rt::rtio::{RtioUnixAcceptor, RtioPipe, RtioUnixListenerObject};
3030
use rt::io::pipe::PipeStream;
@@ -59,7 +59,7 @@ impl UnixStream {
5959
/// let mut stream = UnixStream::connect(&server);
6060
/// stream.write([1, 2, 3]);
6161
///
62-
pub fn connect<P: PathLike>(path: &P) -> Option<UnixStream> {
62+
pub fn connect<P: ToCStr>(path: &P) -> Option<UnixStream> {
6363
let pipe = unsafe {
6464
let io: *mut IoFactoryObject = Local::unsafe_borrow();
6565
(*io).unix_connect(path)
@@ -112,7 +112,7 @@ impl UnixListener {
112112
/// client.write([1, 2, 3, 4]);
113113
/// }
114114
///
115-
pub fn bind<P: PathLike>(path: &P) -> Option<UnixListener> {
115+
pub fn bind<P: ToCStr>(path: &P) -> Option<UnixListener> {
116116
let listener = unsafe {
117117
let io: *mut IoFactoryObject = Local::unsafe_borrow();
118118
(*io).unix_bind(path)

branches/try2/src/libstd/rt/io/support.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

branches/try2/src/libstd/rt/rtio.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use libc;
1212
use option::*;
1313
use result::*;
1414
use libc::c_int;
15+
use c_str::CString;
1516

1617
use ai = rt::io::net::addrinfo;
1718
use rt::io::IoError;
1819
use super::io::process::ProcessConfig;
1920
use super::io::net::ip::{IpAddr, SocketAddr};
2021
use rt::uv::uvio;
2122
use path::Path;
22-
use super::io::support::PathLike;
2323
use super::io::{SeekStyle};
2424
use super::io::{FileMode, FileAccess, FileStat};
2525

@@ -65,15 +65,14 @@ pub trait IoFactory {
6565
fn udp_bind(&mut self, addr: SocketAddr) -> Result<~RtioUdpSocket, IoError>;
6666
fn timer_init(&mut self) -> Result<~RtioTimer, IoError>;
6767
fn fs_from_raw_fd(&mut self, fd: c_int, close_on_drop: bool) -> ~RtioFileStream;
68-
fn fs_open<P: PathLike>(&mut self, path: &P, fm: FileMode, fa: FileAccess)
68+
fn fs_open(&mut self, path: &CString, fm: FileMode, fa: FileAccess)
6969
-> Result<~RtioFileStream, IoError>;
70-
fn fs_unlink<P: PathLike>(&mut self, path: &P) -> Result<(), IoError>;
7170
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
7271
hint: Option<ai::Hint>) -> Result<~[ai::Info], IoError>;
73-
fn fs_stat<P: PathLike>(&mut self, path: &P) -> Result<FileStat, IoError>;
74-
fn fs_mkdir<P: PathLike>(&mut self, path: &P) -> Result<(), IoError>;
75-
fn fs_rmdir<P: PathLike>(&mut self, path: &P) -> Result<(), IoError>;
76-
fn fs_readdir<P: PathLike>(&mut self, path: &P, flags: c_int) ->
72+
fn fs_stat(&mut self, path: &CString) -> Result<FileStat, IoError>;
73+
fn fs_mkdir(&mut self, path: &CString) -> Result<(), IoError>;
74+
fn fs_rmdir(&mut self, path: &CString) -> Result<(), IoError>;
75+
fn fs_readdir(&mut self, path: &CString, flags: c_int) ->
7776
Result<~[Path], IoError>;
7877
fn spawn(&mut self, config: ProcessConfig)
7978
-> Result<(~RtioProcess, ~[Option<~RtioPipe>]), IoError>;

0 commit comments

Comments
 (0)