1
2
3
4
5
6
7
8
9 package os
10
11 import (
12 "syscall"
13 )
14
15
16 func (file *File) Name() string { return file.name }
17
18
19
20 var (
21 Stdin = NewFile(syscall.Stdin, "/dev/stdin")
22 Stdout = NewFile(syscall.Stdout, "/dev/stdout")
23 Stderr = NewFile(syscall.Stderr, "/dev/stderr")
24 )
25
26
27
28 const (
29 O_RDONLY int = syscall.O_RDONLY
30 O_WRONLY int = syscall.O_WRONLY
31 O_RDWR int = syscall.O_RDWR
32 O_APPEND int = syscall.O_APPEND
33 O_ASYNC int = syscall.O_ASYNC
34 O_CREATE int = syscall.O_CREAT
35 O_EXCL int = syscall.O_EXCL
36 O_NOCTTY int = syscall.O_NOCTTY
37 O_NONBLOCK int = syscall.O_NONBLOCK
38 O_NDELAY int = O_NONBLOCK
39 O_SYNC int = syscall.O_SYNC
40 O_TRUNC int = syscall.O_TRUNC
41 )
42
43
44 const (
45 SEEK_SET int = 0
46 SEEK_CUR int = 1
47 SEEK_END int = 2
48 )
49
50 type eofError int
51
52 func (eofError) String() string { return "EOF" }
53
54
55
56
57
58
59 var EOF Error = eofError(0)
60
61
62
63
64 func (file *File) Read(b []byte) (n int, err Error) {
65 if file == nil {
66 return 0, EINVAL
67 }
68 n, e := file.read(b)
69 if n < 0 {
70 n = 0
71 }
72 if n == 0 && !iserror(e) {
73 return 0, EOF
74 }
75 if iserror(e) {
76 err = &PathError{"read", file.name, Errno(e)}
77 }
78 return n, err
79 }
80
81
82
83
84
85 func (file *File) ReadAt(b []byte, off int64) (n int, err Error) {
86 if file == nil {
87 return 0, EINVAL
88 }
89 for len(b) > 0 {
90 m, e := file.pread(b, off)
91 if m == 0 && !iserror(e) {
92 return n, EOF
93 }
94 if iserror(e) {
95 err = &PathError{"read", file.name, Errno(e)}
96 break
97 }
98 n += m
99 b = b[m:]
100 off += int64(m)
101 }
102 return
103 }
104
105
106
107
108 func (file *File) Write(b []byte) (n int, err Error) {
109 if file == nil {
110 return 0, EINVAL
111 }
112 n, e := file.write(b)
113 if n < 0 {
114 n = 0
115 }
116
117 epipecheck(file, e)
118
119 if iserror(e) {
120 err = &PathError{"write", file.name, Errno(e)}
121 }
122 return n, err
123 }
124
125
126
127
128 func (file *File) WriteAt(b []byte, off int64) (n int, err Error) {
129 if file == nil {
130 return 0, EINVAL
131 }
132 for len(b) > 0 {
133 m, e := file.pwrite(b, off)
134 if iserror(e) {
135 err = &PathError{"write", file.name, Errno(e)}
136 break
137 }
138 n += m
139 b = b[m:]
140 off += int64(m)
141 }
142 return
143 }
144
145
146
147
148
149 func (file *File) Seek(offset int64, whence int) (ret int64, err Error) {
150 r, e := file.seek(offset, whence)
151 if !iserror(e) && file.dirinfo != nil && r != 0 {
152 e = syscall.EISDIR
153 }
154 if iserror(e) {
155 return 0, &PathError{"seek", file.name, Errno(e)}
156 }
157 return r, nil
158 }
159
160
161
162 func (file *File) WriteString(s string) (ret int, err Error) {
163 if file == nil {
164 return 0, EINVAL
165 }
166 return file.Write([]byte(s))
167 }
168
169
170
171 func Mkdir(name string, perm uint32) Error {
172 e := syscall.Mkdir(name, perm)
173 if iserror(e) {
174 return &PathError{"mkdir", name, Errno(e)}
175 }
176 return nil
177 }
178
179
180 func Chdir(dir string) Error {
181 if e := syscall.Chdir(dir); iserror(e) {
182 return &PathError{"chdir", dir, Errno(e)}
183 }
184 return nil
185 }
186
187
188
189 func (f *File) Chdir() Error {
190 if e := syscall.Fchdir(f.fd); iserror(e) {
191 return &PathError{"chdir", f.name, Errno(e)}
192 }
193 return nil
194 }
195
196
197
198
199
200 func Open(name string) (file *File, err Error) {
201 return OpenFile(name, O_RDONLY, 0)
202 }
203
204
205
206
207
208
209 func Create(name string) (file *File, err Error) {
210 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
211 }