]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/os/file_plan9.go
libgo: Update to current master library sources.
[thirdparty/gcc.git] / libgo / go / os / file_plan9.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package os
6
7 import (
8 "runtime"
9 "syscall"
10 "time"
11 )
12
13 // File represents an open file descriptor.
14 type File struct {
15 *file
16 }
17
18 // file is the real representation of *File.
19 // The extra level of indirection ensures that no clients of os
20 // can overwrite this data, which could cause the finalizer
21 // to close the wrong file descriptor.
22 type file struct {
23 fd int
24 name string
25 dirinfo *dirInfo // nil unless directory being read
26 }
27
28 // Fd returns the integer Unix file descriptor referencing the open file.
29 func (f *File) Fd() uintptr {
30 if f == nil {
31 return ^(uintptr(0))
32 }
33 return uintptr(f.fd)
34 }
35
36 // NewFile returns a new File with the given file descriptor and name.
37 func NewFile(fd uintptr, name string) *File {
38 fdi := int(fd)
39 if fdi < 0 {
40 return nil
41 }
42 f := &File{&file{fd: fdi, name: name}}
43 runtime.SetFinalizer(f.file, (*file).close)
44 return f
45 }
46
47 // Auxiliary information if the File describes a directory
48 type dirInfo struct {
49 buf [syscall.STATMAX]byte // buffer for directory I/O
50 nbuf int // length of buf; return value from Read
51 bufp int // location of next record in buf.
52 }
53
54 func epipecheck(file *File, e error) {
55 }
56
57 // DevNull is the name of the operating system's ``null device.''
58 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
59 const DevNull = "/dev/null"
60
61 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
62 func syscallMode(i FileMode) (o uint32) {
63 o |= uint32(i.Perm())
64 if i&ModeAppend != 0 {
65 o |= syscall.DMAPPEND
66 }
67 if i&ModeExclusive != 0 {
68 o |= syscall.DMEXCL
69 }
70 if i&ModeTemporary != 0 {
71 o |= syscall.DMTMP
72 }
73 return
74 }
75
76 // OpenFile is the generalized open call; most users will use Open
77 // or Create instead. It opens the named file with specified flag
78 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
79 // methods on the returned File can be used for I/O.
80 // If there is an error, it will be of type *PathError.
81 func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
82 var (
83 fd int
84 e error
85 create bool
86 excl bool
87 trunc bool
88 append bool
89 )
90
91 if flag&O_CREATE == O_CREATE {
92 flag = flag & ^O_CREATE
93 create = true
94 }
95 if flag&O_EXCL == O_EXCL {
96 excl = true
97 }
98 if flag&O_TRUNC == O_TRUNC {
99 trunc = true
100 }
101 // O_APPEND is emulated on Plan 9
102 if flag&O_APPEND == O_APPEND {
103 flag = flag &^ O_APPEND
104 append = true
105 }
106
107 syscall.ForkLock.RLock()
108 if (create && trunc) || excl {
109 fd, e = syscall.Create(name, flag, syscallMode(perm))
110 } else {
111 fd, e = syscall.Open(name, flag)
112 if e != nil && create {
113 var e1 error
114 fd, e1 = syscall.Create(name, flag, syscallMode(perm))
115 if e1 == nil {
116 e = nil
117 }
118 }
119 }
120 syscall.ForkLock.RUnlock()
121
122 if e != nil {
123 return nil, &PathError{"open", name, e}
124 }
125
126 if append {
127 if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
128 return nil, &PathError{"seek", name, e}
129 }
130 }
131
132 return NewFile(uintptr(fd), name), nil
133 }
134
135 // Close closes the File, rendering it unusable for I/O.
136 // It returns an error, if any.
137 func (f *File) Close() error {
138 return f.file.close()
139 }
140
141 func (file *file) close() error {
142 if file == nil || file.fd < 0 {
143 return ErrInvalid
144 }
145 var err error
146 syscall.ForkLock.RLock()
147 if e := syscall.Close(file.fd); e != nil {
148 err = &PathError{"close", file.name, e}
149 }
150 syscall.ForkLock.RUnlock()
151 file.fd = -1 // so it can't be closed again
152
153 // no need for a finalizer anymore
154 runtime.SetFinalizer(file, nil)
155 return err
156 }
157
158 // Stat returns the FileInfo structure describing file.
159 // If there is an error, it will be of type *PathError.
160 func (f *File) Stat() (fi FileInfo, err error) {
161 d, err := dirstat(f)
162 if err != nil {
163 return nil, err
164 }
165 return fileInfoFromStat(d), nil
166 }
167
168 // Truncate changes the size of the file.
169 // It does not change the I/O offset.
170 // If there is an error, it will be of type *PathError.
171 func (f *File) Truncate(size int64) error {
172 var d syscall.Dir
173
174 d.Null()
175 d.Length = size
176
177 var buf [syscall.STATFIXLEN]byte
178 n, err := d.Marshal(buf[:])
179 if err != nil {
180 return &PathError{"truncate", f.name, err}
181 }
182 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
183 return &PathError{"truncate", f.name, err}
184 }
185 return nil
186 }
187
188 const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
189
190 // Chmod changes the mode of the file to mode.
191 // If there is an error, it will be of type *PathError.
192 func (f *File) Chmod(mode FileMode) error {
193 var d syscall.Dir
194
195 odir, e := dirstat(f)
196 if e != nil {
197 return &PathError{"chmod", f.name, e}
198 }
199 d.Null()
200 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
201
202 var buf [syscall.STATFIXLEN]byte
203 n, err := d.Marshal(buf[:])
204 if err != nil {
205 return &PathError{"chmod", f.name, err}
206 }
207 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
208 return &PathError{"chmod", f.name, err}
209 }
210 return nil
211 }
212
213 // Sync commits the current contents of the file to stable storage.
214 // Typically, this means flushing the file system's in-memory copy
215 // of recently written data to disk.
216 func (f *File) Sync() (err error) {
217 if f == nil {
218 return ErrInvalid
219 }
220 var d syscall.Dir
221 d.Null()
222
223 var buf [syscall.STATFIXLEN]byte
224 n, err := d.Marshal(buf[:])
225 if err != nil {
226 return NewSyscallError("fsync", err)
227 }
228 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
229 return NewSyscallError("fsync", err)
230 }
231 return nil
232 }
233
234 // read reads up to len(b) bytes from the File.
235 // It returns the number of bytes read and an error, if any.
236 func (f *File) read(b []byte) (n int, err error) {
237 return syscall.Read(f.fd, b)
238 }
239
240 // pread reads len(b) bytes from the File starting at byte offset off.
241 // It returns the number of bytes read and the error, if any.
242 // EOF is signaled by a zero count with err set to nil.
243 func (f *File) pread(b []byte, off int64) (n int, err error) {
244 return syscall.Pread(f.fd, b, off)
245 }
246
247 // write writes len(b) bytes to the File.
248 // It returns the number of bytes written and an error, if any.
249 func (f *File) write(b []byte) (n int, err error) {
250 return syscall.Write(f.fd, b)
251 }
252
253 // pwrite writes len(b) bytes to the File starting at byte offset off.
254 // It returns the number of bytes written and an error, if any.
255 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
256 return syscall.Pwrite(f.fd, b, off)
257 }
258
259 // seek sets the offset for the next Read or Write on file to offset, interpreted
260 // according to whence: 0 means relative to the origin of the file, 1 means
261 // relative to the current offset, and 2 means relative to the end.
262 // It returns the new offset and an error, if any.
263 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
264 return syscall.Seek(f.fd, offset, whence)
265 }
266
267 // Truncate changes the size of the named file.
268 // If the file is a symbolic link, it changes the size of the link's target.
269 // If there is an error, it will be of type *PathError.
270 func Truncate(name string, size int64) error {
271 var d syscall.Dir
272
273 d.Null()
274 d.Length = size
275
276 var buf [syscall.STATFIXLEN]byte
277 n, err := d.Marshal(buf[:])
278 if err != nil {
279 return &PathError{"truncate", name, err}
280 }
281 if err = syscall.Wstat(name, buf[:n]); err != nil {
282 return &PathError{"truncate", name, err}
283 }
284 return nil
285 }
286
287 // Remove removes the named file or directory.
288 // If there is an error, it will be of type *PathError.
289 func Remove(name string) error {
290 if e := syscall.Remove(name); e != nil {
291 return &PathError{"remove", name, e}
292 }
293 return nil
294 }
295
296 // Rename renames a file.
297 func Rename(oldname, newname string) error {
298 var d syscall.Dir
299
300 d.Null()
301 d.Name = newname
302
303 var buf [syscall.STATFIXLEN]byte
304 n, err := d.Marshal(buf[:])
305 if err != nil {
306 return &PathError{"rename", oldname, err}
307 }
308 if err = syscall.Wstat(oldname, buf[:n]); err != nil {
309 return &PathError{"rename", oldname, err}
310 }
311 return nil
312 }
313
314 // Chmod changes the mode of the named file to mode.
315 // If the file is a symbolic link, it changes the mode of the link's target.
316 // If there is an error, it will be of type *PathError.
317 func Chmod(name string, mode FileMode) error {
318 var d syscall.Dir
319
320 odir, e := dirstat(name)
321 if e != nil {
322 return &PathError{"chmod", name, e}
323 }
324 d.Null()
325 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
326
327 var buf [syscall.STATFIXLEN]byte
328 n, err := d.Marshal(buf[:])
329 if err != nil {
330 return &PathError{"chmod", name, err}
331 }
332 if err = syscall.Wstat(name, buf[:n]); err != nil {
333 return &PathError{"chmod", name, err}
334 }
335 return nil
336 }
337
338 // Chtimes changes the access and modification times of the named
339 // file, similar to the Unix utime() or utimes() functions.
340 //
341 // The underlying filesystem may truncate or round the values to a
342 // less precise time unit.
343 // If there is an error, it will be of type *PathError.
344 func Chtimes(name string, atime time.Time, mtime time.Time) error {
345 var d syscall.Dir
346
347 d.Null()
348 d.Atime = uint32(atime.Unix())
349 d.Mtime = uint32(mtime.Unix())
350
351 var buf [syscall.STATFIXLEN]byte
352 n, err := d.Marshal(buf[:])
353 if err != nil {
354 return &PathError{"chtimes", name, err}
355 }
356 if err = syscall.Wstat(name, buf[:n]); err != nil {
357 return &PathError{"chtimes", name, err}
358 }
359 return nil
360 }
361
362 // Pipe returns a connected pair of Files; reads from r return bytes
363 // written to w. It returns the files and an error, if any.
364 func Pipe() (r *File, w *File, err error) {
365 var p [2]int
366
367 syscall.ForkLock.RLock()
368 if e := syscall.Pipe(p[0:]); e != nil {
369 syscall.ForkLock.RUnlock()
370 return nil, nil, NewSyscallError("pipe", e)
371 }
372 syscall.ForkLock.RUnlock()
373
374 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
375 }
376
377 // not supported on Plan 9
378
379 // Link creates newname as a hard link to the oldname file.
380 // If there is an error, it will be of type *LinkError.
381 func Link(oldname, newname string) error {
382 return &LinkError{"link", oldname, newname, syscall.EPLAN9}
383 }
384
385 // Symlink creates newname as a symbolic link to oldname.
386 // If there is an error, it will be of type *LinkError.
387 func Symlink(oldname, newname string) error {
388 return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
389 }
390
391 // Readlink returns the destination of the named symbolic link.
392 // If there is an error, it will be of type *PathError.
393 func Readlink(name string) (string, error) {
394 return "", &PathError{"readlink", name, syscall.EPLAN9}
395 }
396
397 // Chown changes the numeric uid and gid of the named file.
398 // If the file is a symbolic link, it changes the uid and gid of the link's target.
399 // If there is an error, it will be of type *PathError.
400 func Chown(name string, uid, gid int) error {
401 return &PathError{"chown", name, syscall.EPLAN9}
402 }
403
404 // Lchown changes the numeric uid and gid of the named file.
405 // If the file is a symbolic link, it changes the uid and gid of the link itself.
406 // If there is an error, it will be of type *PathError.
407 func Lchown(name string, uid, gid int) error {
408 return &PathError{"lchown", name, syscall.EPLAN9}
409 }
410
411 // Chown changes the numeric uid and gid of the named file.
412 // If there is an error, it will be of type *PathError.
413 func (f *File) Chown(uid, gid int) error {
414 return &PathError{"chown", f.name, syscall.EPLAN9}
415 }
416
417 // TempDir returns the default directory to use for temporary files.
418 func TempDir() string {
419 return "/tmp"
420 }