summaryrefslogtreecommitdiff
path: root/src/pkg/os/file.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-02-08 21:41:54 -0800
committerRuss Cox <rsc@golang.org>2010-02-08 21:41:54 -0800
commit4227e0fcd8ab454433d9c3d94da6eae68dfc1689 (patch)
tree97dc24869f546fd7032625f3b2159ab8afbd1000 /src/pkg/os/file.go
parent572ffc76eca67ff28370cb29fc76ebcbe2aeb8cd (diff)
downloadgolang-4227e0fcd8ab454433d9c3d94da6eae68dfc1689.tar.gz
runtime: allow arbitrary return type in SetFinalizer.
finalize chan, to free OS X semaphore inside Lock. os: finalize File, to close fd. Fixes issue 503. R=ken2 CC=golang-dev http://codereview.appspot.com/204065
Diffstat (limited to 'src/pkg/os/file.go')
-rw-r--r--src/pkg/os/file.go7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/pkg/os/file.go b/src/pkg/os/file.go
index b92384e2c..83b022aa0 100644
--- a/src/pkg/os/file.go
+++ b/src/pkg/os/file.go
@@ -7,6 +7,7 @@
package os
import (
+ "runtime"
"syscall"
)
@@ -36,7 +37,9 @@ func NewFile(fd int, name string) *File {
if fd < 0 {
return nil
}
- return &File{fd, name, nil, 0}
+ f := &File{fd, name, nil, 0}
+ runtime.SetFinalizer(f, (*File).Close)
+ return f
}
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
@@ -86,7 +89,7 @@ func Open(name string, flag int, perm int) (file *File, err Error) {
// Close closes the File, rendering it unusable for I/O.
// It returns an Error, if any.
func (file *File) Close() Error {
- if file == nil {
+ if file == nil || file.fd < 0 {
return EINVAL
}
var err Error