libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / os / file_plan9.go
blobe6496558ca5ed95253e9e5912f92f77627eb5e1c
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.
5 package os
7 import (
8 "runtime"
9 "syscall"
10 "time"
13 // File represents an open file descriptor.
14 type File struct {
15 *file
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
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))
33 return uintptr(f.fd)
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
42 f := &File{&file{fd: fdi, name: name}}
43 runtime.SetFinalizer(f.file, (*file).close)
44 return f
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.
54 func epipecheck(file *File, e error) {
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"
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
67 if i&ModeExclusive != 0 {
68 o |= syscall.DMEXCL
70 if i&ModeTemporary != 0 {
71 o |= syscall.DMTMP
73 return
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
91 if flag&O_CREATE == O_CREATE {
92 flag = flag & ^O_CREATE
93 create = true
95 if flag&O_EXCL == O_EXCL {
96 excl = true
98 if flag&O_TRUNC == O_TRUNC {
99 trunc = true
101 // O_APPEND is emulated on Plan 9
102 if flag&O_APPEND == O_APPEND {
103 flag = flag &^ O_APPEND
104 append = true
107 if (create && trunc) || excl {
108 fd, e = syscall.Create(name, flag, syscallMode(perm))
109 } else {
110 fd, e = syscall.Open(name, flag)
111 if e != nil && create {
112 var e1 error
113 fd, e1 = syscall.Create(name, flag, syscallMode(perm))
114 if e1 == nil {
115 e = nil
120 if e != nil {
121 return nil, &PathError{"open", name, e}
124 if append {
125 if _, e = syscall.Seek(fd, 0, SEEK_END); e != nil {
126 return nil, &PathError{"seek", name, e}
130 return NewFile(uintptr(fd), name), nil
133 // Close closes the File, rendering it unusable for I/O.
134 // It returns an error, if any.
135 func (f *File) Close() error {
136 if f == nil {
137 return ErrInvalid
139 return f.file.close()
142 func (file *file) close() error {
143 if file == nil || file.fd < 0 {
144 return ErrInvalid
146 var err error
147 syscall.ForkLock.RLock()
148 if e := syscall.Close(file.fd); e != nil {
149 err = &PathError{"close", file.name, e}
151 syscall.ForkLock.RUnlock()
152 file.fd = -1 // so it can't be closed again
154 // no need for a finalizer anymore
155 runtime.SetFinalizer(file, nil)
156 return err
159 // Stat returns the FileInfo structure describing file.
160 // If there is an error, it will be of type *PathError.
161 func (f *File) Stat() (fi FileInfo, err error) {
162 if f == nil {
163 return nil, ErrInvalid
165 d, err := dirstat(f)
166 if err != nil {
167 return nil, err
169 return fileInfoFromStat(d), nil
172 // Truncate changes the size of the file.
173 // It does not change the I/O offset.
174 // If there is an error, it will be of type *PathError.
175 func (f *File) Truncate(size int64) error {
176 if f == nil {
177 return ErrInvalid
180 var d syscall.Dir
181 d.Null()
182 d.Length = size
184 var buf [syscall.STATFIXLEN]byte
185 n, err := d.Marshal(buf[:])
186 if err != nil {
187 return &PathError{"truncate", f.name, err}
189 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
190 return &PathError{"truncate", f.name, err}
192 return nil
195 const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | ModePerm)
197 // Chmod changes the mode of the file to mode.
198 // If there is an error, it will be of type *PathError.
199 func (f *File) Chmod(mode FileMode) error {
200 if f == nil {
201 return ErrInvalid
203 var d syscall.Dir
205 odir, e := dirstat(f)
206 if e != nil {
207 return &PathError{"chmod", f.name, e}
209 d.Null()
210 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
212 var buf [syscall.STATFIXLEN]byte
213 n, err := d.Marshal(buf[:])
214 if err != nil {
215 return &PathError{"chmod", f.name, err}
217 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
218 return &PathError{"chmod", f.name, err}
220 return nil
223 // Sync commits the current contents of the file to stable storage.
224 // Typically, this means flushing the file system's in-memory copy
225 // of recently written data to disk.
226 func (f *File) Sync() (err error) {
227 if f == nil {
228 return ErrInvalid
230 var d syscall.Dir
231 d.Null()
233 var buf [syscall.STATFIXLEN]byte
234 n, err := d.Marshal(buf[:])
235 if err != nil {
236 return NewSyscallError("fsync", err)
238 if err = syscall.Fwstat(f.fd, buf[:n]); err != nil {
239 return NewSyscallError("fsync", err)
241 return nil
244 // read reads up to len(b) bytes from the File.
245 // It returns the number of bytes read and an error, if any.
246 func (f *File) read(b []byte) (n int, err error) {
247 return syscall.Read(f.fd, b)
250 // pread reads len(b) bytes from the File starting at byte offset off.
251 // It returns the number of bytes read and the error, if any.
252 // EOF is signaled by a zero count with err set to nil.
253 func (f *File) pread(b []byte, off int64) (n int, err error) {
254 return syscall.Pread(f.fd, b, off)
257 // write writes len(b) bytes to the File.
258 // It returns the number of bytes written and an error, if any.
259 // Since Plan 9 preserves message boundaries, never allow
260 // a zero-byte write.
261 func (f *File) write(b []byte) (n int, err error) {
262 if len(b) == 0 {
263 return 0, nil
265 return syscall.Write(f.fd, b)
268 // pwrite writes len(b) bytes to the File starting at byte offset off.
269 // It returns the number of bytes written and an error, if any.
270 // Since Plan 9 preserves message boundaries, never allow
271 // a zero-byte write.
272 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
273 if len(b) == 0 {
274 return 0, nil
276 return syscall.Pwrite(f.fd, b, off)
279 // seek sets the offset for the next Read or Write on file to offset, interpreted
280 // according to whence: 0 means relative to the origin of the file, 1 means
281 // relative to the current offset, and 2 means relative to the end.
282 // It returns the new offset and an error, if any.
283 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
284 return syscall.Seek(f.fd, offset, whence)
287 // Truncate changes the size of the named file.
288 // If the file is a symbolic link, it changes the size of the link's target.
289 // If there is an error, it will be of type *PathError.
290 func Truncate(name string, size int64) error {
291 var d syscall.Dir
293 d.Null()
294 d.Length = size
296 var buf [syscall.STATFIXLEN]byte
297 n, err := d.Marshal(buf[:])
298 if err != nil {
299 return &PathError{"truncate", name, err}
301 if err = syscall.Wstat(name, buf[:n]); err != nil {
302 return &PathError{"truncate", name, err}
304 return nil
307 // Remove removes the named file or directory.
308 // If there is an error, it will be of type *PathError.
309 func Remove(name string) error {
310 if e := syscall.Remove(name); e != nil {
311 return &PathError{"remove", name, e}
313 return nil
316 // HasPrefix from the strings package.
317 func hasPrefix(s, prefix string) bool {
318 return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
321 // Variant of LastIndex from the strings package.
322 func lastIndex(s string, sep byte) int {
323 for i := len(s) - 1; i >= 0; i-- {
324 if s[i] == sep {
325 return i
328 return -1
331 func rename(oldname, newname string) error {
332 dirname := oldname[:lastIndex(oldname, '/')+1]
333 if hasPrefix(newname, dirname) {
334 newname = newname[len(dirname):]
337 // If newname still contains slashes after removing the oldname
338 // prefix, the rename is cross-directory and must be rejected.
339 // This case is caught by d.Marshal below.
341 var d syscall.Dir
343 d.Null()
344 d.Name = newname
346 buf := make([]byte, syscall.STATFIXLEN+len(d.Name))
347 n, err := d.Marshal(buf[:])
348 if err != nil {
349 return &LinkError{"rename", oldname, newname, err}
351 if err = syscall.Wstat(oldname, buf[:n]); err != nil {
352 return &LinkError{"rename", oldname, newname, err}
354 return nil
357 // Chmod changes the mode of the named file to mode.
358 // If the file is a symbolic link, it changes the mode of the link's target.
359 // If there is an error, it will be of type *PathError.
360 func Chmod(name string, mode FileMode) error {
361 var d syscall.Dir
363 odir, e := dirstat(name)
364 if e != nil {
365 return &PathError{"chmod", name, e}
367 d.Null()
368 d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask
370 var buf [syscall.STATFIXLEN]byte
371 n, err := d.Marshal(buf[:])
372 if err != nil {
373 return &PathError{"chmod", name, err}
375 if err = syscall.Wstat(name, buf[:n]); err != nil {
376 return &PathError{"chmod", name, err}
378 return nil
381 // Chtimes changes the access and modification times of the named
382 // file, similar to the Unix utime() or utimes() functions.
384 // The underlying filesystem may truncate or round the values to a
385 // less precise time unit.
386 // If there is an error, it will be of type *PathError.
387 func Chtimes(name string, atime time.Time, mtime time.Time) error {
388 var d syscall.Dir
390 d.Null()
391 d.Atime = uint32(atime.Unix())
392 d.Mtime = uint32(mtime.Unix())
394 var buf [syscall.STATFIXLEN]byte
395 n, err := d.Marshal(buf[:])
396 if err != nil {
397 return &PathError{"chtimes", name, err}
399 if err = syscall.Wstat(name, buf[:n]); err != nil {
400 return &PathError{"chtimes", name, err}
402 return nil
405 // Pipe returns a connected pair of Files; reads from r return bytes
406 // written to w. It returns the files and an error, if any.
407 func Pipe() (r *File, w *File, err error) {
408 var p [2]int
410 syscall.ForkLock.RLock()
411 if e := syscall.Pipe(p[0:]); e != nil {
412 syscall.ForkLock.RUnlock()
413 return nil, nil, NewSyscallError("pipe", e)
415 syscall.ForkLock.RUnlock()
417 return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
420 // not supported on Plan 9
422 // Link creates newname as a hard link to the oldname file.
423 // If there is an error, it will be of type *LinkError.
424 func Link(oldname, newname string) error {
425 return &LinkError{"link", oldname, newname, syscall.EPLAN9}
428 // Symlink creates newname as a symbolic link to oldname.
429 // If there is an error, it will be of type *LinkError.
430 func Symlink(oldname, newname string) error {
431 return &LinkError{"symlink", oldname, newname, syscall.EPLAN9}
434 // Readlink returns the destination of the named symbolic link.
435 // If there is an error, it will be of type *PathError.
436 func Readlink(name string) (string, error) {
437 return "", &PathError{"readlink", name, syscall.EPLAN9}
440 // Chown changes the numeric uid and gid of the named file.
441 // If the file is a symbolic link, it changes the uid and gid of the link's target.
442 // If there is an error, it will be of type *PathError.
443 func Chown(name string, uid, gid int) error {
444 return &PathError{"chown", name, syscall.EPLAN9}
447 // Lchown changes the numeric uid and gid of the named file.
448 // If the file is a symbolic link, it changes the uid and gid of the link itself.
449 // If there is an error, it will be of type *PathError.
450 func Lchown(name string, uid, gid int) error {
451 return &PathError{"lchown", name, syscall.EPLAN9}
454 // Chown changes the numeric uid and gid of the named file.
455 // If there is an error, it will be of type *PathError.
456 func (f *File) Chown(uid, gid int) error {
457 if f == nil {
458 return ErrInvalid
460 return &PathError{"chown", f.name, syscall.EPLAN9}
463 // TempDir returns the default directory to use for temporary files.
464 func TempDir() string {
465 return "/tmp"