PR c++/85515
[official-gcc.git] / libgo / go / os / file_unix.go
blob67d2ee1d10752570769af310a5ec27dcaebabd57
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 "internal/poll"
11 "runtime"
12 "syscall"
15 // fixLongPath is a noop on non-Windows platforms.
16 func fixLongPath(path string) string {
17 return path
20 func rename(oldname, newname string) error {
21 fi, err := Lstat(newname)
22 if err == nil && fi.IsDir() {
23 // There are two independent errors this function can return:
24 // one for a bad oldname, and one for a bad newname.
25 // At this point we've determined the newname is bad.
26 // But just in case oldname is also bad, prioritize returning
27 // the oldname error because that's what we did historically.
28 if _, err := Lstat(oldname); err != nil {
29 if pe, ok := err.(*PathError); ok {
30 err = pe.Err
32 return &LinkError{"rename", oldname, newname, err}
34 return &LinkError{"rename", oldname, newname, syscall.EEXIST}
36 err = syscall.Rename(oldname, newname)
37 if err != nil {
38 return &LinkError{"rename", oldname, newname, err}
40 return nil
43 // file is the real representation of *File.
44 // The extra level of indirection ensures that no clients of os
45 // can overwrite this data, which could cause the finalizer
46 // to close the wrong file descriptor.
47 type file struct {
48 pfd poll.FD
49 name string
50 dirinfo *dirInfo // nil unless directory being read
51 nonblock bool // whether we set nonblocking mode
52 stdoutOrErr bool // whether this is stdout or stderr
55 // Fd returns the integer Unix file descriptor referencing the open file.
56 // The file descriptor is valid only until f.Close is called or f is garbage collected.
57 // On Unix systems this will cause the SetDeadline methods to stop working.
58 func (f *File) Fd() uintptr {
59 if f == nil {
60 return ^(uintptr(0))
63 // If we put the file descriptor into nonblocking mode,
64 // then set it to blocking mode before we return it,
65 // because historically we have always returned a descriptor
66 // opened in blocking mode. The File will continue to work,
67 // but any blocking operation will tie up a thread.
68 if f.nonblock {
69 f.pfd.SetBlocking()
72 return uintptr(f.pfd.Sysfd)
75 // NewFile returns a new File with the given file descriptor and
76 // name. The returned value will be nil if fd is not a valid file
77 // descriptor.
78 func NewFile(fd uintptr, name string) *File {
79 return newFile(fd, name, kindNewFile)
82 // newFileKind describes the kind of file to newFile.
83 type newFileKind int
85 const (
86 kindNewFile newFileKind = iota
87 kindOpenFile
88 kindPipe
91 // newFile is like NewFile, but if called from OpenFile or Pipe
92 // (as passed in the kind parameter) it tries to add the file to
93 // the runtime poller.
94 func newFile(fd uintptr, name string, kind newFileKind) *File {
95 fdi := int(fd)
96 if fdi < 0 {
97 return nil
99 f := &File{&file{
100 pfd: poll.FD{
101 Sysfd: fdi,
102 IsStream: true,
103 ZeroReadIsEOF: true,
105 name: name,
106 stdoutOrErr: fdi == 1 || fdi == 2,
109 // Don't try to use kqueue with regular files on FreeBSD.
110 // It crashes the system unpredictably while running all.bash.
111 // Issue 19093.
112 if runtime.GOOS == "freebsd" && kind == kindOpenFile {
113 kind = kindNewFile
116 pollable := kind == kindOpenFile || kind == kindPipe
117 if err := f.pfd.Init("file", pollable); err != nil {
118 // An error here indicates a failure to register
119 // with the netpoll system. That can happen for
120 // a file descriptor that is not supported by
121 // epoll/kqueue; for example, disk files on
122 // GNU/Linux systems. We assume that any real error
123 // will show up in later I/O.
124 } else if pollable {
125 // We successfully registered with netpoll, so put
126 // the file into nonblocking mode.
127 if err := syscall.SetNonblock(fdi, true); err == nil {
128 f.nonblock = true
132 runtime.SetFinalizer(f.file, (*file).close)
133 return f
136 // Auxiliary information if the File describes a directory
137 type dirInfo struct {
138 buf []byte // buffer for directory I/O
139 dir *syscall.DIR // from opendir
142 // epipecheck raises SIGPIPE if we get an EPIPE error on standard
143 // output or standard error. See the SIGPIPE docs in os/signal, and
144 // issue 11845.
145 func epipecheck(file *File, e error) {
146 if e == syscall.EPIPE && file.stdoutOrErr {
147 sigpipe()
151 // DevNull is the name of the operating system's ``null device.''
152 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
153 const DevNull = "/dev/null"
155 // openFileNolog is the Unix implementation of OpenFile.
156 func openFileNolog(name string, flag int, perm FileMode) (*File, error) {
157 chmod := false
158 if !supportsCreateWithStickyBit && flag&O_CREATE != 0 && perm&ModeSticky != 0 {
159 if _, err := Stat(name); IsNotExist(err) {
160 chmod = true
164 var r int
165 for {
166 var e error
167 r, e = syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
168 if e == nil {
169 break
172 // On OS X, sigaction(2) doesn't guarantee that SA_RESTART will cause
173 // open(2) to be restarted for regular files. This is easy to reproduce on
174 // fuse file systems (see http://golang.org/issue/11180).
175 if runtime.GOOS == "darwin" && e == syscall.EINTR {
176 continue
179 return nil, &PathError{"open", name, e}
182 // open(2) itself won't handle the sticky bit on *BSD and Solaris
183 if chmod {
184 Chmod(name, perm)
187 // There's a race here with fork/exec, which we are
188 // content to live with. See ../syscall/exec_unix.go.
189 if !supportsCloseOnExec {
190 syscall.CloseOnExec(r)
193 return newFile(uintptr(r), name, kindOpenFile), nil
196 // Close closes the File, rendering it unusable for I/O.
197 // It returns an error, if any.
198 func (f *File) Close() error {
199 if f == nil {
200 return ErrInvalid
202 return f.file.close()
205 func (file *file) close() error {
206 if file == nil {
207 return syscall.EINVAL
209 var err error
210 if e := file.pfd.Close(); e != nil {
211 if e == poll.ErrFileClosing {
212 e = ErrClosed
214 err = &PathError{"close", file.name, e}
217 if file.dirinfo != nil {
218 syscall.Entersyscall()
219 i := libc_closedir(file.dirinfo.dir)
220 errno := syscall.GetErrno()
221 syscall.Exitsyscall()
222 file.dirinfo = nil
223 if i < 0 && err == nil {
224 err = &PathError{"closedir", file.name, errno}
228 // no need for a finalizer anymore
229 runtime.SetFinalizer(file, nil)
230 return err
233 // read reads up to len(b) bytes from the File.
234 // It returns the number of bytes read and an error, if any.
235 func (f *File) read(b []byte) (n int, err error) {
236 n, err = f.pfd.Read(b)
237 runtime.KeepAlive(f)
238 return n, err
241 // pread reads len(b) bytes from the File starting at byte offset off.
242 // It returns the number of bytes read and the error, if any.
243 // EOF is signaled by a zero count with err set to nil.
244 func (f *File) pread(b []byte, off int64) (n int, err error) {
245 n, err = f.pfd.Pread(b, off)
246 runtime.KeepAlive(f)
247 return n, err
250 // write writes len(b) bytes to the File.
251 // It returns the number of bytes written and an error, if any.
252 func (f *File) write(b []byte) (n int, err error) {
253 n, err = f.pfd.Write(b)
254 runtime.KeepAlive(f)
255 return n, err
258 // pwrite writes len(b) bytes to the File starting at byte offset off.
259 // It returns the number of bytes written and an error, if any.
260 func (f *File) pwrite(b []byte, off int64) (n int, err error) {
261 n, err = f.pfd.Pwrite(b, off)
262 runtime.KeepAlive(f)
263 return n, err
266 // seek sets the offset for the next Read or Write on file to offset, interpreted
267 // according to whence: 0 means relative to the origin of the file, 1 means
268 // relative to the current offset, and 2 means relative to the end.
269 // It returns the new offset and an error, if any.
270 func (f *File) seek(offset int64, whence int) (ret int64, err error) {
271 ret, err = f.pfd.Seek(offset, whence)
272 runtime.KeepAlive(f)
273 return ret, err
276 // Truncate changes the size of the named file.
277 // If the file is a symbolic link, it changes the size of the link's target.
278 // If there is an error, it will be of type *PathError.
279 func Truncate(name string, size int64) error {
280 if e := syscall.Truncate(name, size); e != nil {
281 return &PathError{"truncate", name, e}
283 return nil
286 // Remove removes the named file or directory.
287 // If there is an error, it will be of type *PathError.
288 func Remove(name string) error {
289 // System call interface forces us to know
290 // whether name is a file or directory.
291 // Try both: it is cheaper on average than
292 // doing a Stat plus the right one.
293 e := syscall.Unlink(name)
294 if e == nil {
295 return nil
297 e1 := syscall.Rmdir(name)
298 if e1 == nil {
299 return nil
302 // Both failed: figure out which error to return.
303 // OS X and Linux differ on whether unlink(dir)
304 // returns EISDIR, so can't use that. However,
305 // both agree that rmdir(file) returns ENOTDIR,
306 // so we can use that to decide which error is real.
307 // Rmdir might also return ENOTDIR if given a bad
308 // file path, like /etc/passwd/foo, but in that case,
309 // both errors will be ENOTDIR, so it's okay to
310 // use the error from unlink.
311 if e1 != syscall.ENOTDIR {
312 e = e1
314 return &PathError{"remove", name, e}
317 func tempDir() string {
318 dir := Getenv("TMPDIR")
319 if dir == "" {
320 if runtime.GOOS == "android" {
321 dir = "/data/local/tmp"
322 } else {
323 dir = "/tmp"
326 return dir
329 // Link creates newname as a hard link to the oldname file.
330 // If there is an error, it will be of type *LinkError.
331 func Link(oldname, newname string) error {
332 e := syscall.Link(oldname, newname)
333 if e != nil {
334 return &LinkError{"link", oldname, newname, e}
336 return nil
339 // Symlink creates newname as a symbolic link to oldname.
340 // If there is an error, it will be of type *LinkError.
341 func Symlink(oldname, newname string) error {
342 e := syscall.Symlink(oldname, newname)
343 if e != nil {
344 return &LinkError{"symlink", oldname, newname, e}
346 return nil