Imported Upstream version 4.7.3
[platform/upstream/gcc48.git] / libgo / go / os / file_posix.go
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.
4
5 // +build darwin freebsd linux netbsd openbsd windows
6
7 package os
8
9 import (
10         "syscall"
11         "time"
12 )
13
14 func sigpipe() // implemented in package runtime
15
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}
22         }
23         return nil
24 }
25
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}
32         }
33         return nil
34 }
35
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}
44                 }
45                 if n < len {
46                         return string(b[0:n]), nil
47                 }
48         }
49         // Silence 6g.
50         return "", nil
51 }
52
53 // Rename renames a file.
54 func Rename(oldname, newname string) error {
55         e := syscall.Rename(oldname, newname)
56         if e != nil {
57                 return &LinkError{"rename", oldname, newname, e}
58         }
59         return nil
60 }
61
62 // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
63 func syscallMode(i FileMode) (o uint32) {
64         o |= uint32(i.Perm())
65         if i&ModeSetuid != 0 {
66                 o |= syscall.S_ISUID
67         }
68         if i&ModeSetgid != 0 {
69                 o |= syscall.S_ISGID
70         }
71         if i&ModeSticky != 0 {
72                 o |= syscall.S_ISVTX
73         }
74         // No mapping for Go's ModeTemporary (plan9 only).
75         return
76 }
77
78 // Chmod changes the mode of the named file to mode.
79 // If the file is a symbolic link, it changes the mode of the link's target.
80 // If there is an error, it will be of type *PathError.
81 func Chmod(name string, mode FileMode) error {
82         if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
83                 return &PathError{"chmod", name, e}
84         }
85         return nil
86 }
87
88 // Chmod changes the mode of the file to mode.
89 // If there is an error, it will be of type *PathError.
90 func (f *File) Chmod(mode FileMode) error {
91         if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
92                 return &PathError{"chmod", f.name, e}
93         }
94         return nil
95 }
96
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}
103         }
104         return nil
105 }
106
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}
113         }
114         return nil
115 }
116
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 e := syscall.Fchown(f.fd, uid, gid); e != nil {
121                 return &PathError{"chown", f.name, e}
122         }
123         return nil
124 }
125
126 // Truncate changes the size of the file.
127 // It does not change the I/O offset.
128 // If there is an error, it will be of type *PathError.
129 func (f *File) Truncate(size int64) error {
130         if e := syscall.Ftruncate(f.fd, size); e != nil {
131                 return &PathError{"truncate", f.name, e}
132         }
133         return nil
134 }
135
136 // Sync commits the current contents of the file to stable storage.
137 // Typically, this means flushing the file system's in-memory copy
138 // of recently written data to disk.
139 func (f *File) Sync() (err error) {
140         if f == nil {
141                 return syscall.EINVAL
142         }
143         if e := syscall.Fsync(f.fd); e != nil {
144                 return NewSyscallError("fsync", e)
145         }
146         return nil
147 }
148
149 // Chtimes changes the access and modification times of the named
150 // file, similar to the Unix utime() or utimes() functions.
151 //
152 // The underlying filesystem may truncate or round the values to a
153 // less precise time unit.
154 // If there is an error, it will be of type *PathError.
155 func Chtimes(name string, atime time.Time, mtime time.Time) error {
156         var utimes [2]syscall.Timeval
157         atime_ns := atime.Unix()*1e9 + int64(atime.Nanosecond())
158         mtime_ns := mtime.Unix()*1e9 + int64(mtime.Nanosecond())
159         utimes[0] = syscall.NsecToTimeval(atime_ns)
160         utimes[1] = syscall.NsecToTimeval(mtime_ns)
161         if e := syscall.Utimes(name, utimes[0:]); e != nil {
162                 return &PathError{"chtimes", name, e}
163         }
164         return nil
165 }