Skip to content

Commit 9411547

Browse files
committed
auto merge of #6161 : graydon/rust/glob-retry, r=graydon
This is a retry of pull #5832
2 parents 08dd625 + ed81e33 commit 9411547

File tree

2 files changed

+189
-2
lines changed

2 files changed

+189
-2
lines changed

src/libcore/libc.rs

+106-2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub use libc::funcs::posix88::unistd::*;
104104

105105
pub use libc::funcs::posix01::stat_::*;
106106
pub use libc::funcs::posix01::unistd::*;
107+
pub use libc::funcs::posix01::glob::*;
107108
pub use libc::funcs::posix08::unistd::*;
108109

109110
pub use libc::funcs::bsd44::*;
@@ -210,7 +211,21 @@ pub mod types {
210211
#[cfg(target_os = "android")]
211212
pub mod os {
212213
pub mod common {
213-
pub mod posix01 {}
214+
pub mod posix01 {
215+
use libc::types::common::c95::{c_void};
216+
use libc::types::os::arch::c95::{c_char, size_t};
217+
pub struct glob_t {
218+
gl_pathc: size_t,
219+
gl_pathv: **c_char,
220+
gl_offs: size_t,
221+
222+
__unused1: *c_void,
223+
__unused2: *c_void,
224+
__unused3: *c_void,
225+
__unused4: *c_void,
226+
__unused5: *c_void,
227+
}
228+
}
214229
}
215230

216231
#[cfg(target_arch = "x86")]
@@ -369,7 +384,25 @@ pub mod types {
369384
#[cfg(target_os = "freebsd")]
370385
pub mod os {
371386
pub mod common {
372-
pub mod posix01 {}
387+
pub mod posix01 {
388+
use libc::types::common::c95::{c_void};
389+
use libc::types::os::arch::c95::{c_char, c_int, size_t};
390+
pub struct glob_t {
391+
gl_pathc: size_t,
392+
__unused1: size_t,
393+
gl_offs: size_t,
394+
__unused2: c_int,
395+
gl_pathv: **c_char,
396+
397+
__unused3: *c_void,
398+
399+
__unused4: *c_void,
400+
__unused5: *c_void,
401+
__unused6: *c_void,
402+
__unused7: *c_void,
403+
__unused8: *c_void,
404+
}
405+
}
373406
}
374407

375408
#[cfg(target_arch = "x86_64")]
@@ -571,6 +604,23 @@ pub mod types {
571604
pub mod os {
572605
pub mod common {
573606
pub mod posix01 {
607+
use libc::types::common::c95::{c_void};
608+
use libc::types::os::arch::c95::{c_char, c_int, size_t};
609+
pub struct glob_t {
610+
gl_pathc: size_t,
611+
__unused1: c_int,
612+
gl_offs: size_t,
613+
__unused2: c_int,
614+
gl_pathv: **c_char,
615+
616+
__unused3: *c_void,
617+
618+
__unused4: *c_void,
619+
__unused5: *c_void,
620+
__unused6: *c_void,
621+
__unused7: *c_void,
622+
__unused8: *c_void,
623+
}
574624
}
575625
}
576626

@@ -877,6 +927,18 @@ pub mod consts {
877927
}
878928
pub mod posix01 {
879929
pub static SIGTRAP : int = 5;
930+
931+
pub static GLOB_ERR : int = 1 << 0;
932+
pub static GLOB_MARK : int = 1 << 1;
933+
pub static GLOB_NOSORT : int = 1 << 2;
934+
pub static GLOB_DOOFFS : int = 1 << 3;
935+
pub static GLOB_NOCHECK : int = 1 << 4;
936+
pub static GLOB_APPEND : int = 1 << 5;
937+
pub static GLOB_NOESCAPE : int = 1 << 6;
938+
939+
pub static GLOB_NOSPACE : int = 1;
940+
pub static GLOB_ABORTED : int = 2;
941+
pub static GLOB_NOMATCH : int = 3;
880942
}
881943
pub mod posix08 {
882944
}
@@ -956,6 +1018,18 @@ pub mod consts {
9561018
}
9571019
pub mod posix01 {
9581020
pub static SIGTRAP : int = 5;
1021+
1022+
pub static GLOB_APPEND : int = 0x0001;
1023+
pub static GLOB_DOOFFS : int = 0x0002;
1024+
pub static GLOB_ERR : int = 0x0004;
1025+
pub static GLOB_MARK : int = 0x0008;
1026+
pub static GLOB_NOCHECK : int = 0x0010;
1027+
pub static GLOB_NOSORT : int = 0x0020;
1028+
pub static GLOB_NOESCAPE : int = 0x2000;
1029+
1030+
pub static GLOB_NOSPACE : int = -1;
1031+
pub static GLOB_ABORTED : int = -2;
1032+
pub static GLOB_NOMATCH : int = -3;
9591033
}
9601034
pub mod posix08 {
9611035
}
@@ -1036,6 +1110,18 @@ pub mod consts {
10361110
}
10371111
pub mod posix01 {
10381112
pub static SIGTRAP : int = 5;
1113+
1114+
pub static GLOB_APPEND : int = 0x0001;
1115+
pub static GLOB_DOOFFS : int = 0x0002;
1116+
pub static GLOB_ERR : int = 0x0004;
1117+
pub static GLOB_MARK : int = 0x0008;
1118+
pub static GLOB_NOCHECK : int = 0x0010;
1119+
pub static GLOB_NOSORT : int = 0x0020;
1120+
pub static GLOB_NOESCAPE : int = 0x2000;
1121+
1122+
pub static GLOB_NOSPACE : int = -1;
1123+
pub static GLOB_ABORTED : int = -2;
1124+
pub static GLOB_NOMATCH : int = -3;
10391125
}
10401126
pub mod posix08 {
10411127
}
@@ -1606,6 +1692,21 @@ pub mod funcs {
16061692
-> pid_t;
16071693
}
16081694
}
1695+
1696+
#[nolink]
1697+
#[abi = "cdecl"]
1698+
pub mod glob {
1699+
use libc::types::common::c95::{c_void};
1700+
use libc::types::os::arch::c95::{c_char, c_int};
1701+
use libc::types::os::common::posix01::{glob_t};
1702+
1703+
pub extern {
1704+
unsafe fn glob(pattern: *c_char, flags: c_int,
1705+
errfunc: *c_void, // XXX callback
1706+
pglob: *mut glob_t);
1707+
unsafe fn globfree(pglob: *mut glob_t);
1708+
}
1709+
}
16091710
}
16101711

16111712
#[cfg(target_os = "win32")]
@@ -1615,6 +1716,9 @@ pub mod funcs {
16151716

16161717
pub mod unistd {
16171718
}
1719+
1720+
pub mod glob {
1721+
}
16181722
}
16191723

16201724

src/libcore/os.rs

+83
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use ptr;
3838
use str;
3939
use task;
4040
use uint;
41+
use unstable::finally::Finally;
4142
use vec;
4243

4344
pub use libc::fclose;
@@ -1183,6 +1184,88 @@ pub fn set_args(new_args: ~[~str]) {
11831184
}
11841185
}
11851186
1187+
// FIXME #6100 we should really use an internal implementation of this - using
1188+
// the POSIX glob functions isn't portable to windows, probably has slight
1189+
// inconsistencies even where it is implemented, and makes extending
1190+
// functionality a lot more difficult
1191+
// FIXME #6101 also provide a non-allocating version - each_glob or so?
1192+
/// Returns a vector of Path objects that match the given glob pattern
1193+
#[cfg(target_os = "linux")]
1194+
#[cfg(target_os = "android")]
1195+
#[cfg(target_os = "freebsd")]
1196+
#[cfg(target_os = "macos")]
1197+
pub fn glob(pattern: &str) -> ~[Path] {
1198+
#[cfg(target_os = "linux")]
1199+
#[cfg(target_os = "android")]
1200+
fn default_glob_t () -> libc::glob_t {
1201+
libc::glob_t {
1202+
gl_pathc: 0,
1203+
gl_pathv: ptr::null(),
1204+
gl_offs: 0,
1205+
__unused1: ptr::null(),
1206+
__unused2: ptr::null(),
1207+
__unused3: ptr::null(),
1208+
__unused4: ptr::null(),
1209+
__unused5: ptr::null(),
1210+
}
1211+
}
1212+
1213+
#[cfg(target_os = "freebsd")]
1214+
fn default_glob_t () -> libc::glob_t {
1215+
libc::glob_t {
1216+
gl_pathc: 0,
1217+
__unused1: 0,
1218+
gl_offs: 0,
1219+
__unused2: 0,
1220+
gl_pathv: ptr::null(),
1221+
__unused3: ptr::null(),
1222+
__unused4: ptr::null(),
1223+
__unused5: ptr::null(),
1224+
__unused6: ptr::null(),
1225+
__unused7: ptr::null(),
1226+
__unused8: ptr::null(),
1227+
}
1228+
}
1229+
1230+
#[cfg(target_os = "macos")]
1231+
fn default_glob_t () -> libc::glob_t {
1232+
libc::glob_t {
1233+
gl_pathc: 0,
1234+
__unused1: 0,
1235+
gl_offs: 0,
1236+
__unused2: 0,
1237+
gl_pathv: ptr::null(),
1238+
__unused3: ptr::null(),
1239+
__unused4: ptr::null(),
1240+
__unused5: ptr::null(),
1241+
__unused6: ptr::null(),
1242+
__unused7: ptr::null(),
1243+
__unused8: ptr::null(),
1244+
}
1245+
}
1246+
1247+
let mut g = default_glob_t();
1248+
do str::as_c_str(pattern) |c_pattern| {
1249+
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
1250+
};
1251+
do(|| {
1252+
let paths = unsafe {
1253+
vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
1254+
};
1255+
do paths.map |&c_str| {
1256+
Path(unsafe { str::raw::from_c_str(c_str) })
1257+
}
1258+
}).finally {
1259+
unsafe { libc::globfree(&mut g) };
1260+
}
1261+
}
1262+
1263+
/// Returns a vector of Path objects that match the given glob pattern
1264+
#[cfg(target_os = "win32")]
1265+
pub fn glob(pattern: &str) -> ~[Path] {
1266+
fail!(~"glob() is unimplemented on Windows")
1267+
}
1268+
11861269
#[cfg(target_os = "macos")]
11871270
extern {
11881271
// These functions are in crt_externs.h.

0 commit comments

Comments
 (0)