2017-07-25 Tamar Christina <tamar.christina@arm.com>
[official-gcc.git] / libgo / go / os / file_unix.go
blob1bba4ed9d63d9a2edfc3e3723fef1e86488db99a
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 aix darwin dragonfly freebsd linux nacl netbsd openbsd solaris
7 package os
9 import (
10 "runtime"
11 "syscall"
14 // fixLongPath is a noop on non-Windows platforms.
15 func fixLongPath(path string) string {
16 return path
19 func rename(oldname, newname string) error {
20 fi, err := Lstat(newname)
21 if err == nil && fi.IsDir() {
22 return &LinkError{"rename", oldname, newname, syscall.EEXIST}
24 e := syscall.Rename(oldname, newname)
25 if e != nil {
26 return &LinkError{"rename", oldname, newname, e}
28 return nil
31 // file is the real representation of *File.
32 // The extra level of indirection ensures that no clients of os
33 // can overwrite this data, which could cause the finalizer
34 // to close the wrong file descriptor.
35 type file struct {
36 fd int
37 name string
38 dirinfo *dirInfo // nil unless directory being read
41 // Fd returns the integer Unix file descriptor referencing the open file.
42 // The file descriptor is valid only until f.Close is called or f is garbage collected.
43 func (f *File) Fd() uintptr {
44 if f == nil {
45 return ^(uintptr(0))
47 return uintptr(f.fd)
50 // NewFile returns a new File with the given file descriptor and name.
51 func NewFile(fd uintptr, name string) *File {
52 fdi := int(fd)
53 if fdi < 0 {
54 return nil
56 f := &File{&file{fd: fdi, name: name}}
57 runtime.SetFinalizer(f.file, (*file).close)
58 return f
61 // Auxiliary information if the File describes a directory
62 type dirInfo struct {
63 buf []byte // buffer for directory I/O
64 dir *syscall.DIR // from opendir
67 // epipecheck raises SIGPIPE if we get an EPIPE error on standard
68 // output or standard error. See the SIGPIPE docs in os/signal, and
69 // issue 11845.
70 func epipecheck(file *File, e error) {
71 if e == syscall.EPIPE && (file.fd == 1 || file.fd == 2) {
72 sigpipe()
76 // DevNull is the name of the operating system's ``null device.''
77 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
78 const DevNull = "/dev/null"
80 // OpenFile is the generalized open call; most users will use Open
81 // or Create instead. It opens the named file with specified flag
82 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
83 // methods on the returned File can be used for I/O.
84 // If there is an error, it will be of type *PathError.
85 func OpenFile(name string, flag int, perm FileMode) (*File, error) {
86 chmod := false
87 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
88 if _, err := Stat(name); IsNotExist(err) {
89 chmod = true
93 var r int
94 for {
95 var e error
96 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
97 if e == nil {
98 break
101 // On OS X, sigaction(2) doesn't guarantee that SA_RESTART will cause
102 // open(2) to be restarted for regular files. This is easy to reproduce on
103 // fuse file systems (see http://golang.org/issue/11180).
104 if runtime.GOOS == "darwin" && e == syscall.EINTR {
105 continue
108 return nil, &PathError{"open", name, e}
111 // open(2) itself won't handle the sticky bit on *BSD and Solaris
112 if chmod {
113 Chmod(name, perm)
116 // There's a race here with fork/exec, which we are
117 // content to live with. See ../syscall/exec_unix.go.
118 if !supportsCloseOnExec {
119 syscall.CloseOnExec(r)
122 return NewFile(uintptr(r), name), nil
125 // Close closes the File, rendering it unusable for I/O.
126 // It returns an error, if any.
127 func (f *File) Close() error {
128 if f == nil {
129 return ErrInvalid
131 return f.file.close()
134 func (file *file) close() error {
135 if file == nil || file.fd == badFd {
136 return syscall.EINVAL
138 var err error
139 if e := syscall.Close(file.fd); e != nil {
140 err = &PathError{"close", file.name, e}
143 if file.dirinfo != nil {
144 syscall.Entersyscall()
145 i := libc_closedir(file.dirinfo.dir)
146 errno := syscall.GetErrno()
147 syscall.Exitsyscall()
148 file.dirinfo = nil
149 if i < 0 && err == nil {
150 err = &PathError{"closedir", file.name, errno}
154 file.fd = -1 // so it can't be closed again
156 // no need for a finalizer anymore
157 runtime.SetFinalizer(file, nil)
158 return err
161 // Darwin and FreeBSD can't read or write 2GB+ at a time,
162 // even on 64-bit systems. See golang.org/issue/7812.
163 // Use 1GB instead of, say, 2GB-1, to keep subsequent
164 // reads aligned.
165 const (
166 needsMaxRW = runtime.GOOS == "darwin" || runtime.GOOS == "freebsd"
167 maxRW = 1 << 30
170 // read reads up to len(b) bytes from the File.
171 // It returns the number of bytes read and an error, if any.
172 func (f *File) read(b []byte) (n int, err error) {
173 if needsMaxRW && len(b) > maxRW {
174 b = b[:maxRW]
176 return fixCount(syscall.Read(f.fd, b))
179 // pread reads len(b) bytes from the File starting at byte offset off.
180 // It returns the number of bytes read and the error, if any.
181 // EOF is signaled by a zero count with err set to nil.
182 func (f *File) pread(b []byte, off int64) (n int, err error) {
183 if needsMaxRW && len(b) > maxRW {
184 b = b[:maxRW]
186 return fixCount(syscall.Pread(f.fd, b, off))
189 // write writes len(b) bytes to the File.
190 // It returns the number of bytes written and an error, if any.
191 func (f *File) write(b []byte) (n int, err error) {
192 for {
193 bcap := b
194 if needsMaxRW && len(bcap) > maxRW {
195 bcap = bcap[:maxRW]
197 m, err := fixCount(syscall.Write(f.fd, bcap))
198 n += m
200 // If the syscall wrote some data but not all (short write)
201 // or it returned EINTR, then assume it stopped early for
202 // reasons that are uninteresting to the caller, and try again.
203 if 0 < m && m < len(bcap) || err == syscall.EINTR {
204 b = b[m:]
205 continue
208 if needsMaxRW && len(bcap) != len(b) && err == nil {
209 b = b[m:]
210 continue
213 return n, err
217 // pwrite writes len(b) bytes to the File starting at byte offset off.
218 // It returns the number of bytes written and an error, if any.
219 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
220 if needsMaxRW && len(b) > maxRW {
221 b = b[:maxRW]
223 return fixCount(syscall.Pwrite(f.fd, b, off))
226 // seek sets the offset for the next Read or Write on file to offset, interpreted
227 // according to whence: 0 means relative to the origin of the file, 1 means
228 // relative to the current offset, and 2 means relative to the end.
229 // It returns the new offset and an error, if any.
230 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
231 return syscall.Seek(f.fd, offset, whence)
234 // Truncate changes the size of the named file.
235 // If the file is a symbolic link, it changes the size of the link's target.
236 // If there is an error, it will be of type *PathError.
237 func Truncate(name string, size int64) error {
238 if e := syscall.Truncate(name, size); e != nil {
239 return &PathError{"truncate", name, e}
241 return nil
244 // Remove removes the named file or directory.
245 // If there is an error, it will be of type *PathError.
246 func Remove(name string) error {
247 // System call interface forces us to know
248 // whether name is a file or directory.
249 // Try both: it is cheaper on average than
250 // doing a Stat plus the right one.
251 e := syscall.Unlink(name)
252 if e == nil {
253 return nil
255 e1 := syscall.Rmdir(name)
256 if e1 == nil {
257 return nil
260 // Both failed: figure out which error to return.
261 // OS X and Linux differ on whether unlink(dir)
262 // returns EISDIR, so can't use that. However,
263 // both agree that rmdir(file) returns ENOTDIR,
264 // so we can use that to decide which error is real.
265 // Rmdir might also return ENOTDIR if given a bad
266 // file path, like /etc/passwd/foo, but in that case,
267 // both errors will be ENOTDIR, so it's okay to
268 // use the error from unlink.
269 if e1 != syscall.ENOTDIR {
270 e = e1
272 return &PathError{"remove", name, e}
275 // TempDir returns the default directory to use for temporary files.
276 func TempDir() string {
277 dir := Getenv("TMPDIR")
278 if dir == "" {
279 if runtime.GOOS == "android" {
280 dir = "/data/local/tmp"
281 } else {
282 dir = "/tmp"
285 return dir
288 // Link creates newname as a hard link to the oldname file.
289 // If there is an error, it will be of type *LinkError.
290 func Link(oldname, newname string) error {
291 e := syscall.Link(oldname, newname)
292 if e != nil {
293 return &LinkError{"link", oldname, newname, e}
295 return nil
298 // Symlink creates newname as a symbolic link to oldname.
299 // If there is an error, it will be of type *LinkError.
300 func Symlink(oldname, newname string) error {
301 e := syscall.Symlink(oldname, newname)
302 if e != nil {
303 return &LinkError{"symlink", oldname, newname, e}
305 return nil