Skip to content

Commit c17ea95

Browse files
committed
Add per-platform file-open flags to std.os. Open buffers as desired in std._io.
1 parent cbe68d4 commit c17ea95

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/lib/_io.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ fn new_buf_reader(str path) -> buf_reader {
4343
}
4444
}
4545

46-
auto fd = os.libc.open(_str.buf(path), 0);
46+
auto fd = os.libc.open(_str.buf(path),
47+
os.libc_constants.O_RDONLY() |
48+
os.libc_constants.O_BINARY(),
49+
0u);
50+
4751
if (fd < 0) {
4852
log "error opening file for reading";
4953
log sys.rustrt.last_os_error();
@@ -52,7 +56,9 @@ fn new_buf_reader(str path) -> buf_reader {
5256
ret fd_buf_reader(fd, new_buf());
5357
}
5458

55-
fn new_buf_writer(str path) -> buf_writer {
59+
type fileflag = tag(append(), create(), truncate());
60+
61+
fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
5662

5763
unsafe obj fd_buf_writer(int fd) {
5864

@@ -77,7 +83,23 @@ fn new_buf_writer(str path) -> buf_writer {
7783
}
7884
}
7985

80-
auto fd = os.libc.open(_str.buf(path), 0);
86+
let int fflags =
87+
os.libc_constants.O_WRONLY() |
88+
os.libc_constants.O_BINARY();
89+
90+
for (fileflag f in flags) {
91+
alt (f) {
92+
case (append()) { fflags |= os.libc_constants.O_APPEND(); }
93+
case (create()) { fflags |= os.libc_constants.O_CREAT(); }
94+
case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
95+
}
96+
}
97+
98+
auto fd = os.libc.open(_str.buf(path),
99+
fflags,
100+
os.libc_constants.S_IRUSR() |
101+
os.libc_constants.S_IWUSR());
102+
81103
if (fd < 0) {
82104
log "error opening file for writing";
83105
log sys.rustrt.last_os_error();

src/lib/linux_os.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import _vec.vbuf;
33

44
native mod libc = "libc.so.6" {
55

6-
fn open(sbuf s, int flags) -> int;
6+
fn open(sbuf s, int flags, uint mode) -> int;
77
fn read(int fd, vbuf buf, uint count) -> int;
88
fn write(int fd, vbuf buf, uint count) -> int;
99
fn close(int fd) -> int;
@@ -17,3 +17,18 @@ native mod libc = "libc.so.6" {
1717
fn setenv(sbuf n, sbuf v, int overwrite) -> int;
1818
fn unsetenv(sbuf n) -> int;
1919
}
20+
21+
mod libc_constants {
22+
fn O_RDONLY() -> int { ret 0x0000; }
23+
fn O_WRONLY() -> int { ret 0x0001; }
24+
fn O_RDWR() -> int { ret 0x0002; }
25+
fn O_APPEND() -> int { ret 0x0400; }
26+
fn O_CREAT() -> int { ret 0x0040; }
27+
fn O_EXCL() -> int { ret 0x0080; }
28+
fn O_TRUNC() -> int { ret 0x0200; }
29+
fn O_TEXT() -> int { ret 0x0000; } // nonexistent in linux libc
30+
fn O_BINARY() -> int { ret 0x0000; } // nonexistent in linux libc
31+
32+
fn S_IRUSR() -> uint { ret 0x0100u; }
33+
fn S_IWUSR() -> uint { ret 0x0080u; }
34+
}

src/lib/macos_os.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import _vec.vbuf;
33

44
native mod libc = "libc.dylib" {
55

6-
fn open(sbuf s, int flags) -> int;
6+
fn open(sbuf s, int flags, uint mode) -> int;
77
fn read(int fd, vbuf buf, uint count) -> int;
88
fn write(int fd, vbuf buf, uint count) -> int;
99
fn close(int fd) -> int;
@@ -17,3 +17,18 @@ native mod libc = "libc.dylib" {
1717
fn setenv(sbuf n, sbuf v, int overwrite) -> int;
1818
fn unsetenv(sbuf n) -> int;
1919
}
20+
21+
mod libc_constants {
22+
fn O_RDONLY() -> int { ret 0x0000; }
23+
fn O_WRONLY() -> int { ret 0x0001; }
24+
fn O_RDWR() -> int { ret 0x0002; }
25+
fn O_APPEND() -> int { ret 0x0008; }
26+
fn O_CREAT() -> int { ret 0x0200; }
27+
fn O_EXCL() -> int { ret 0x0800; }
28+
fn O_TRUNC() -> int { ret 0x0400; }
29+
fn O_TEXT() -> int { ret 0x0000; } // nonexistent in darwin libc
30+
fn O_BINARY() -> int { ret 0x0000; } // nonexistent in darwin libc
31+
32+
fn S_IRUSR() -> uint { ret 0x0400u; }
33+
fn S_IWUSR() -> uint { ret 0x0200u; }
34+
}

src/lib/win32_os.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@ import _str.sbuf;
22
import _vec.vbuf;
33

44
native mod libc = "msvcrt.dll" {
5-
fn open(sbuf s, int flags) -> int = "_open";
5+
fn open(sbuf s, int flags, uint mode) -> int = "_open";
66
fn read(int fd, vbuf buf, uint count) -> int = "_read";
77
fn write(int fd, vbuf buf, uint count) -> int = "_write";
88
fn close(int fd) -> int = "_close";
99
}
10+
11+
mod libc_constants {
12+
fn O_RDONLY() -> int { ret 0x0000; }
13+
fn O_WRONLY() -> int { ret 0x0001; }
14+
fn O_RDWR() -> int { ret 0x0002; }
15+
fn O_APPEND() -> int { ret 0x0400; }
16+
fn O_CREAT() -> int { ret 0x0040; }
17+
fn O_EXCL() -> int { ret 0x0080; }
18+
fn O_TRUNC() -> int { ret 0x0200; }
19+
fn O_TEXT() -> int { ret 0x4000; }
20+
fn O_BINARY() -> int { ret 0x8000; }
21+
22+
fn S_IRUSR() -> uint { ret 0x0100u; } // really _S_IREAD in win32
23+
fn S_IWUSR() -> uint { ret 0x0080u; } // really _S_IWRITE in win32
24+
}

0 commit comments

Comments
 (0)