OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / os / file_unix.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 openbsd
6
7 package os
8
9 import (
10         "runtime"
11         "syscall"
12 )
13
14 // File represents an open file descriptor.
15 type File struct {
16         fd      int
17         name    string
18         dirinfo *dirInfo // nil unless directory being read
19         nepipe  int      // number of consecutive EPIPE in Write
20 }
21
22 // Fd returns the integer Unix file descriptor referencing the open file.
23 func (file *File) Fd() int {
24         if file == nil {
25                 return -1
26         }
27         return file.fd
28 }
29
30 // NewFile returns a new File with the given file descriptor and name.
31 func NewFile(fd int, name string) *File {
32         if fd < 0 {
33                 return nil
34         }
35         f := &File{fd: fd, name: name}
36         runtime.SetFinalizer(f, (*File).Close)
37         return f
38 }
39
40 // Auxiliary information if the File describes a directory
41 type dirInfo struct {
42         buf  []byte // buffer for directory I/O
43         dir *syscall.DIR // from opendir
44 }
45
46 // DevNull is the name of the operating system's ``null device.''
47 // On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
48 const DevNull = "/dev/null"
49
50 // OpenFile is the generalized open call; most users will use Open
51 // or Create instead.  It opens the named file with specified flag
52 // (O_RDONLY etc.) and perm, (0666 etc.) if applicable.  If successful,
53 // methods on the returned File can be used for I/O.
54 // It returns the File and an Error, if any.
55 func OpenFile(name string, flag int, perm uint32) (file *File, err Error) {
56         r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm)
57         if e != 0 {
58                 return nil, &PathError{"open", name, Errno(e)}
59         }
60
61         // There's a race here with fork/exec, which we are
62         // content to live with.  See ../syscall/exec.go
63         if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported
64                 syscall.CloseOnExec(r)
65         }
66
67         return NewFile(r, name), nil
68 }
69
70 // Close closes the File, rendering it unusable for I/O.
71 // It returns an Error, if any.
72 func (file *File) Close() Error {
73         if file == nil || file.fd < 0 {
74                 return EINVAL
75         }
76         var err Error
77         if e := syscall.Close(file.fd); e != 0 {
78                 err = &PathError{"close", file.name, Errno(e)}
79         }
80
81         if file.dirinfo != nil {
82                 if libc_closedir(file.dirinfo.dir) < 0  && err == nil {
83                         err = &PathError{"closedir", file.name, Errno(syscall.GetErrno())}
84                 }
85         }
86
87         file.fd = -1 // so it can't be closed again
88
89         // no need for a finalizer anymore
90         runtime.SetFinalizer(file, nil)
91         return err
92 }
93
94 // Stat returns the FileInfo structure describing file.
95 // It returns the FileInfo and an error, if any.
96 func (file *File) Stat() (fi *FileInfo, err Error) {
97         var stat syscall.Stat_t
98         e := syscall.Fstat(file.fd, &stat)
99         if e != 0 {
100                 return nil, &PathError{"stat", file.name, Errno(e)}
101         }
102         return fileInfoFromStat(file.name, new(FileInfo), &stat, &stat), nil
103 }
104
105 // Stat returns a FileInfo structure describing the named file and an error, if any.
106 // If name names a valid symbolic link, the returned FileInfo describes
107 // the file pointed at by the link and has fi.FollowedSymlink set to true.
108 // If name names an invalid symbolic link, the returned FileInfo describes
109 // the link itself and has fi.FollowedSymlink set to false.
110 func Stat(name string) (fi *FileInfo, err Error) {
111         var lstat, stat syscall.Stat_t
112         e := syscall.Lstat(name, &lstat)
113         if iserror(e) {
114                 return nil, &PathError{"stat", name, Errno(e)}
115         }
116         statp := &lstat
117         if lstat.Mode&syscall.S_IFMT == syscall.S_IFLNK {
118                 e := syscall.Stat(name, &stat)
119                 if !iserror(e) {
120                         statp = &stat
121                 }
122         }
123         return fileInfoFromStat(name, new(FileInfo), &lstat, statp), nil
124 }
125
126 // Lstat returns the FileInfo structure describing the named file and an
127 // error, if any.  If the file is a symbolic link, the returned FileInfo
128 // describes the symbolic link.  Lstat makes no attempt to follow the link.
129 func Lstat(name string) (fi *FileInfo, err Error) {
130         var stat syscall.Stat_t
131         e := syscall.Lstat(name, &stat)
132         if iserror(e) {
133                 return nil, &PathError{"lstat", name, Errno(e)}
134         }
135         return fileInfoFromStat(name, new(FileInfo), &stat, &stat), nil
136 }
137
138 // Readdir reads the contents of the directory associated with file and
139 // returns an array of up to n FileInfo structures, as would be returned
140 // by Lstat, in directory order. Subsequent calls on the same file will yield
141 // further FileInfos.
142 //
143 // If n > 0, Readdir returns at most n FileInfo structures. In this case, if
144 // Readdir returns an empty slice, it will return a non-nil error
145 // explaining why. At the end of a directory, the error is os.EOF.
146 //
147 // If n <= 0, Readdir returns all the FileInfo from the directory in
148 // a single slice. In this case, if Readdir succeeds (reads all
149 // the way to the end of the directory), it returns the slice and a
150 // nil os.Error. If it encounters an error before the end of the
151 // directory, Readdir returns the FileInfo read until that point
152 // and a non-nil error.
153 func (file *File) Readdir(n int) (fi []FileInfo, err Error) {
154         dirname := file.name
155         if dirname == "" {
156                 dirname = "."
157         }
158         dirname += "/"
159         names, err := file.Readdirnames(n)
160         fi = make([]FileInfo, len(names))
161         for i, filename := range names {
162                 fip, err := Lstat(dirname + filename)
163                 if fip == nil || err != nil {
164                         fi[i].Name = filename // rest is already zeroed out
165                 } else {
166                         fi[i] = *fip
167                 }
168         }
169         return
170 }
171
172 // read reads up to len(b) bytes from the File.
173 // It returns the number of bytes read and an error, if any.
174 func (f *File) read(b []byte) (n int, err int) {
175         return syscall.Read(f.fd, b)
176 }
177
178 // pread reads len(b) bytes from the File starting at byte offset off.
179 // It returns the number of bytes read and the error, if any.
180 // EOF is signaled by a zero count with err set to 0.
181 func (f *File) pread(b []byte, off int64) (n int, err int) {
182         return syscall.Pread(f.fd, b, off)
183 }
184
185 // write writes len(b) bytes to the File.
186 // It returns the number of bytes written and an error, if any.
187 func (f *File) write(b []byte) (n int, err int) {
188         return syscall.Write(f.fd, b)
189 }
190
191 // pwrite writes len(b) bytes to the File starting at byte offset off.
192 // It returns the number of bytes written and an error, if any.
193 func (f *File) pwrite(b []byte, off int64) (n int, err int) {
194         return syscall.Pwrite(f.fd, b, off)
195 }
196
197 // seek sets the offset for the next Read or Write on file to offset, interpreted
198 // according to whence: 0 means relative to the origin of the file, 1 means
199 // relative to the current offset, and 2 means relative to the end.
200 // It returns the new offset and an error, if any.
201 func (f *File) seek(offset int64, whence int) (ret int64, err int) {
202         return syscall.Seek(f.fd, offset, whence)
203 }
204
205 // Truncate changes the size of the named file.
206 // If the file is a symbolic link, it changes the size of the link's target.
207 func Truncate(name string, size int64) Error {
208         if e := syscall.Truncate(name, size); e != 0 {
209                 return &PathError{"truncate", name, Errno(e)}
210         }
211         return nil
212 }
213
214 // basename removes trailing slashes and the leading directory name from path name
215 func basename(name string) string {
216         i := len(name) - 1
217         // Remove trailing slashes
218         for ; i > 0 && name[i] == '/'; i-- {
219                 name = name[:i]
220         }
221         // Remove leading directory name
222         for i--; i >= 0; i-- {
223                 if name[i] == '/' {
224                         name = name[i+1:]
225                         break
226                 }
227         }
228
229         return name
230 }
231
232 // Pipe returns a connected pair of Files; reads from r return bytes written to w.
233 // It returns the files and an Error, if any.
234 func Pipe() (r *File, w *File, err Error) {
235         var p [2]int
236
237         // See ../syscall/exec.go for description of lock.
238         syscall.ForkLock.RLock()
239         e := syscall.Pipe(p[0:])
240         if iserror(e) {
241                 syscall.ForkLock.RUnlock()
242                 return nil, nil, NewSyscallError("pipe", e)
243         }
244         syscall.CloseOnExec(p[0])
245         syscall.CloseOnExec(p[1])
246         syscall.ForkLock.RUnlock()
247
248         return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
249 }