libgo: Update to Go 1.3 release.
[official-gcc.git] / libgo / go / os / file_posix.go
blobb3466b15cc8446477bec4ff602df29dd96a53953
1 // Copyright 2009 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 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
7 package os
9 import (
10 "syscall"
11 "time"
14 func sigpipe() // implemented in package runtime
16 // Link creates newname as a hard link to the oldname file.
17 // If there is an error, it will be of type *LinkError.
18 func Link(oldname, newname string) error {
19 e := syscall.Link(oldname, newname)
20 if e != nil {
21 return &LinkError{"link", oldname, newname, e}
23 return nil
26 // Symlink creates newname as a symbolic link to oldname.
27 // If there is an error, it will be of type *LinkError.
28 func Symlink(oldname, newname string) error {
29 e := syscall.Symlink(oldname, newname)
30 if e != nil {
31 return &LinkError{"symlink", oldname, newname, e}
33 return nil
36 // Readlink returns the destination of the named symbolic link.
37 // If there is an error, it will be of type *PathError.
38 func Readlink(name string) (string, error) {
39 for len := 128; ; len *= 2 {
40 b := make([]byte, len)
41 n, e := syscall.Readlink(name, b)
42 if e != nil {
43 return "", &PathError{"readlink", name, e}
45 if n < len {
46 return string(b[0:n]), nil
51 func rename(oldname, newname string) error {
52 e := syscall.Rename(oldname, newname)
53 if e != nil {
54 return &LinkError{"rename", oldname, newname, e}
56 return nil
59 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
60 func syscallMode(i FileMode) (o uint32) {
61 o |= uint32(i.Perm())
62 if i&ModeSetuid != 0 {
63 o |= syscall.S_ISUID
65 if i&ModeSetgid != 0 {
66 o |= syscall.S_ISGID
68 if i&ModeSticky != 0 {
69 o |= syscall.S_ISVTX
71 // No mapping for Go's ModeTemporary (plan9 only).
72 return
75 // Chmod changes the mode of the named file to mode.
76 // If the file is a symbolic link, it changes the mode of the link's target.
77 // If there is an error, it will be of type *PathError.
78 func Chmod(name string, mode FileMode) error {
79 if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
80 return &PathError{"chmod", name, e}
82 return nil
85 // Chmod changes the mode of the file to mode.
86 // If there is an error, it will be of type *PathError.
87 func (f *File) Chmod(mode FileMode) error {
88 if f == nil {
89 return ErrInvalid
91 if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
92 return &PathError{"chmod", f.name, e}
94 return nil
97 // Chown changes the numeric uid and gid of the named file.
98 // If the file is a symbolic link, it changes the uid and gid of the link's target.
99 // If there is an error, it will be of type *PathError.
100 func Chown(name string, uid, gid int) error {
101 if e := syscall.Chown(name, uid, gid); e != nil {
102 return &PathError{"chown", name, e}
104 return nil
107 // Lchown changes the numeric uid and gid of the named file.
108 // If the file is a symbolic link, it changes the uid and gid of the link itself.
109 // If there is an error, it will be of type *PathError.
110 func Lchown(name string, uid, gid int) error {
111 if e := syscall.Lchown(name, uid, gid); e != nil {
112 return &PathError{"lchown", name, e}
114 return nil
117 // Chown changes the numeric uid and gid of the named file.
118 // If there is an error, it will be of type *PathError.
119 func (f *File) Chown(uid, gid int) error {
120 if f == nil {
121 return ErrInvalid
123 if e := syscall.Fchown(f.fd, uid, gid); e != nil {
124 return &PathError{"chown", f.name, e}
126 return nil
129 // Truncate changes the size of the file.
130 // It does not change the I/O offset.
131 // If there is an error, it will be of type *PathError.
132 func (f *File) Truncate(size int64) error {
133 if f == nil {
134 return ErrInvalid
136 if e := syscall.Ftruncate(f.fd, size); e != nil {
137 return &PathError{"truncate", f.name, e}
139 return nil
142 // Sync commits the current contents of the file to stable storage.
143 // Typically, this means flushing the file system's in-memory copy
144 // of recently written data to disk.
145 func (f *File) Sync() (err error) {
146 if f == nil {
147 return ErrInvalid
149 if e := syscall.Fsync(f.fd); e != nil {
150 return NewSyscallError("fsync", e)
152 return nil
155 // Chtimes changes the access and modification times of the named
156 // file, similar to the Unix utime() or utimes() functions.
158 // The underlying filesystem may truncate or round the values to a
159 // less precise time unit.
160 // If there is an error, it will be of type *PathError.
161 func Chtimes(name string, atime time.Time, mtime time.Time) error {
162 var utimes [2]syscall.Timespec
163 utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
164 utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
165 if e := syscall.UtimesNano(name, utimes[0:]); e != nil {
166 return &PathError{"chtimes", name, e}
168 return nil