1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os
import syscall "syscall"
import os "os"
// Auxiliary information if the FD describes a directory
type dirInfo struct { // TODO(r): 6g bug means this can't be private
buf []byte; // buffer for directory I/O
nbuf int64; // length of buf; return value from Getdirentries
bufp int64; // location of next record in buf.
}
// FDs are wrappers for file descriptors
type FD struct {
fd int64;
name string;
dirinfo *dirInfo; // nil unless directory being read
}
func (fd *FD) Fd() int64 {
return fd.fd
}
func (fd *FD) Name() string {
return fd.name
}
func NewFD(fd int64, name string) *FD {
if fd < 0 {
return nil
}
return &FD(fd, name, nil)
}
var (
Stdin = NewFD(0, "/dev/stdin");
Stdout = NewFD(1, "/dev/stdout");
Stderr = NewFD(2, "/dev/stderr");
)
const (
O_RDONLY = syscall.O_RDONLY;
O_WRONLY = syscall.O_WRONLY;
O_RDWR = syscall.O_RDWR;
O_APPEND = syscall.O_APPEND;
O_ASYNC = syscall.O_ASYNC;
O_CREAT = syscall.O_CREAT;
O_NOCTTY = syscall.O_NOCTTY;
O_NONBLOCK = syscall.O_NONBLOCK;
O_NDELAY = O_NONBLOCK;
O_SYNC = syscall.O_SYNC;
O_TRUNC = syscall.O_TRUNC;
)
func Open(name string, mode int, flags int) (fd *FD, err *Error) {
r, e := syscall.Open(name, int64(mode), int64(flags));
return NewFD(r, name), ErrnoToError(e)
}
func (fd *FD) Close() *Error {
if fd == nil {
return EINVAL
}
r, e := syscall.Close(fd.fd);
fd.fd = -1; // so it can't be closed again
return ErrnoToError(e)
}
func (fd *FD) Read(b []byte) (ret int, err *Error) {
if fd == nil {
return 0, EINVAL
}
var r, e int64;
if len(b) > 0 { // because we access b[0]
r, e = syscall.Read(fd.fd, &b[0], int64(len(b)));
if r < 0 {
r = 0
}
}
return int(r), ErrnoToError(e)
}
func (fd *FD) Write(b []byte) (ret int, err *Error) {
if fd == nil {
return 0, EINVAL
}
var r, e int64;
if len(b) > 0 { // because we access b[0]
r, e = syscall.Write(fd.fd, &b[0], int64(len(b)));
if r < 0 {
r = 0
}
}
return int(r), ErrnoToError(e)
}
func (fd *FD) Seek(offset int64, whence int) (ret int64, err *Error) {
r, e := syscall.Seek(fd.fd, offset, int64(whence));
if e != 0 {
return -1, ErrnoToError(e)
}
if fd.dirinfo != nil && r != 0 {
return -1, ErrnoToError(syscall.EISDIR)
}
return r, nil
}
func (fd *FD) WriteString(s string) (ret int, err *Error) {
if fd == nil {
return 0, EINVAL
}
r, e := syscall.Write(fd.fd, syscall.StringBytePtr(s), int64(len(s)));
if r < 0 {
r = 0
}
return int(r), ErrnoToError(e)
}
func Pipe() (fd1 *FD, fd2 *FD, err *Error) {
var p [2]int64;
r, e := syscall.Pipe(&p);
if e != 0 {
return nil, nil, ErrnoToError(e)
}
return NewFD(p[0], "|0"), NewFD(p[1], "|1"), nil
}
func Mkdir(name string, perm int) *Error {
r, e := syscall.Mkdir(name, int64(perm));
return ErrnoToError(e)
}
func Stat(name string) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Stat(name, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(name, new(Dir), stat), nil
}
func Fstat(fd *FD) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Fstat(fd.fd, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(fd.name, new(Dir), stat), nil
}
func Lstat(name string) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Lstat(name, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(name, new(Dir), stat), nil
}
// Non-portable function defined in operating-system-dependent file.
func Readdirnames(fd *FD, count int) (names []string, err *os.Error)
// Negative count means read until EOF.
func Readdir(fd *FD, count int) (dirs []Dir, err *os.Error) {
dirname := fd.name;
if dirname == "" {
dirname = ".";
}
dirname += "/";
names, err1 := Readdirnames(fd, count);
if err1 != nil {
return nil, err1
}
dirs = make([]Dir, len(names));
for i, filename := range names {
dirp, err := Stat(dirname + filename);
if dirp == nil || err != nil {
dirs[i].Name = filename // rest is already zeroed out
} else {
dirs[i] = *dirp
}
}
return
}
|