OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package os provides a platform-independent interface to operating system | 5 // Package os provides a platform-independent interface to operating system |
6 // functionality. The design is Unix-like. | 6 // functionality. The design is Unix-like. |
7 package os | 7 package os |
8 | 8 |
9 import ( | 9 import ( |
10 "runtime" | 10 "runtime" |
11 "sync" | 11 "sync" |
12 "syscall" | 12 "syscall" |
13 ) | 13 ) |
14 | 14 |
15 // File represents an open file descriptor. | 15 // File represents an open file descriptor. |
16 type File struct { | 16 type File struct { |
17 fd int | 17 fd int |
18 name string | 18 name string |
19 dirinfo *dirInfo // nil unless directory being read | 19 dirinfo *dirInfo // nil unless directory being read |
20 nepipe int // number of consecutive EPIPE in Write | 20 nepipe int // number of consecutive EPIPE in Write |
21 l sync.Mutex // used to implement windows pread/pwrite | 21 l sync.Mutex // used to implement windows pread/pwrite |
22 } | 22 } |
23 | 23 |
24 // Fd returns the integer Unix file descriptor referencing the open file. | 24 // Fd returns the integer Unix file descriptor referencing the open file. |
25 func (file *File) Fd() int { return file.fd } | 25 func (file *File) Fd() int { return file.fd } |
26 | 26 |
| 27 // SendfileSourceFd implements the io package's SendfileSource |
| 28 // interface for faster disk to network copies, staying inside |
| 29 // userspace. It returns the fd or an error if this fd cannot be used |
| 30 // as a sendfile source. |
| 31 func (file *File) SendfileSourceFd() (int, Error) { |
| 32 return file.fd, nil |
| 33 } |
| 34 |
27 // Name returns the name of the file as presented to Open. | 35 // Name returns the name of the file as presented to Open. |
28 func (file *File) Name() string { return file.name } | 36 func (file *File) Name() string { return file.name } |
29 | 37 |
30 // NewFile returns a new File with the given file descriptor and name. | 38 // NewFile returns a new File with the given file descriptor and name. |
31 func NewFile(fd int, name string) *File { | 39 func NewFile(fd int, name string) *File { |
32 if fd < 0 { | 40 if fd < 0 { |
33 return nil | 41 return nil |
34 } | 42 } |
35 f := &File{fd: fd, name: name} | 43 f := &File{fd: fd, name: name} |
36 runtime.SetFinalizer(f, (*File).Close) | 44 runtime.SetFinalizer(f, (*File).Close) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 234 } |
227 | 235 |
228 // Create creates the named file mode 0666 (before umask), truncating | 236 // Create creates the named file mode 0666 (before umask), truncating |
229 // it if it already exists. If successful, methods on the returned | 237 // it if it already exists. If successful, methods on the returned |
230 // File can be used for I/O; the associated file descriptor has mode | 238 // File can be used for I/O; the associated file descriptor has mode |
231 // O_RDWR. | 239 // O_RDWR. |
232 // It returns the File and an Error, if any. | 240 // It returns the File and an Error, if any. |
233 func Create(name string) (file *File, err Error) { | 241 func Create(name string) (file *File, err Error) { |
234 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) | 242 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) |
235 } | 243 } |
OLD | NEW |