2010-04-13 17:30:11 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-11-01 19:49:08 -06:00
|
|
|
"io"
|
2010-04-13 17:30:11 -06:00
|
|
|
"runtime"
|
2011-07-01 08:18:07 -06:00
|
|
|
"sync"
|
2010-04-13 17:30:11 -06:00
|
|
|
"syscall"
|
2011-11-15 10:48:22 -07:00
|
|
|
"unicode/utf16"
|
2010-04-13 17:30:11 -06:00
|
|
|
)
|
|
|
|
|
2011-07-01 08:18:07 -06:00
|
|
|
// File represents an open file descriptor.
|
|
|
|
type File struct {
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 10:20:59 -07:00
|
|
|
*file
|
|
|
|
}
|
|
|
|
|
|
|
|
// file is the real representation of *File.
|
|
|
|
// The extra level of indirection ensures that no clients of os
|
|
|
|
// can overwrite this data, which could cause the finalizer
|
|
|
|
// to close the wrong file descriptor.
|
|
|
|
type file struct {
|
2011-07-01 08:18:07 -06:00
|
|
|
fd syscall.Handle
|
|
|
|
name string
|
|
|
|
dirinfo *dirInfo // nil unless directory being read
|
|
|
|
nepipe int // number of consecutive EPIPE in Write
|
|
|
|
l sync.Mutex // used to implement windows pread/pwrite
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fd returns the Windows handle referencing the open file.
|
|
|
|
func (file *File) Fd() syscall.Handle {
|
|
|
|
if file == nil {
|
|
|
|
return syscall.InvalidHandle
|
|
|
|
}
|
|
|
|
return file.fd
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFile returns a new File with the given file descriptor and name.
|
|
|
|
func NewFile(fd syscall.Handle, name string) *File {
|
|
|
|
if fd < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 10:20:59 -07:00
|
|
|
f := &File{&file{fd: fd, name: name}}
|
|
|
|
runtime.SetFinalizer(f.file, (*file).close)
|
2011-07-01 08:18:07 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2010-04-13 17:30:11 -06:00
|
|
|
// Auxiliary information if the File describes a directory
|
|
|
|
type dirInfo struct {
|
2011-09-05 17:59:08 -06:00
|
|
|
data syscall.Win32finddata
|
|
|
|
needdata bool
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
|
|
|
|
2010-08-03 14:03:50 -06:00
|
|
|
const DevNull = "NUL"
|
|
|
|
|
2012-01-10 21:26:11 -07:00
|
|
|
func (f *file) isdir() bool { return f != nil && f.dirinfo != nil }
|
2010-04-13 17:30:11 -06:00
|
|
|
|
2012-01-19 16:45:18 -07:00
|
|
|
func openFile(name string, flag int, perm FileMode) (file *File, err error) {
|
|
|
|
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, syscallMode(perm))
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"open", name, e}
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's a race here with fork/exec, which we are
|
|
|
|
// content to live with. See ../syscall/exec.go
|
|
|
|
if syscall.O_CLOEXEC == 0 { // O_CLOEXEC not supported
|
|
|
|
syscall.CloseOnExec(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewFile(r, name), nil
|
|
|
|
}
|
|
|
|
|
2011-11-01 19:49:08 -06:00
|
|
|
func openDir(name string) (file *File, err error) {
|
2010-04-13 17:30:11 -06:00
|
|
|
d := new(dirInfo)
|
2011-09-05 17:59:08 -06:00
|
|
|
r, e := syscall.FindFirstFile(syscall.StringToUTF16Ptr(name+`\*`), &d.data)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"open", name, e}
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2011-07-01 08:18:07 -06:00
|
|
|
f := NewFile(r, name)
|
2010-04-13 17:30:11 -06:00
|
|
|
f.dirinfo = d
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2011-04-05 00:42:14 -06:00
|
|
|
// OpenFile is the generalized open call; most users will use Open
|
|
|
|
// or Create instead. It opens the named file with specified flag
|
|
|
|
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
|
|
|
|
// methods on the returned File can be used for I/O.
|
2011-11-01 19:49:08 -06:00
|
|
|
// It returns the File and an error, if any.
|
2012-01-19 16:45:18 -07:00
|
|
|
func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
|
2011-11-25 17:01:49 -07:00
|
|
|
if name == "" {
|
|
|
|
return nil, &PathError{"open", name, syscall.ENOENT}
|
|
|
|
}
|
2010-04-13 17:30:11 -06:00
|
|
|
// TODO(brainman): not sure about my logic of assuming it is dir first, then fall back to file
|
|
|
|
r, e := openDir(name)
|
|
|
|
if e == nil {
|
2010-10-04 00:31:49 -06:00
|
|
|
if flag&O_WRONLY != 0 || flag&O_RDWR != 0 {
|
|
|
|
r.Close()
|
|
|
|
return nil, &PathError{"open", name, EISDIR}
|
|
|
|
}
|
2010-04-13 17:30:11 -06:00
|
|
|
return r, nil
|
|
|
|
}
|
2010-04-13 23:30:41 -06:00
|
|
|
r, e = openFile(name, flag, perm)
|
2010-04-13 17:30:11 -06:00
|
|
|
if e == nil {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the File, rendering it unusable for I/O.
|
2011-11-01 19:49:08 -06:00
|
|
|
// It returns an error, if any.
|
|
|
|
func (file *File) Close() error {
|
allow copy of struct containing unexported fields
An experiment: allow structs to be copied even if they
contain unexported fields. This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.
In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.
All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.
Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef
R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
2011-11-15 10:20:59 -07:00
|
|
|
return file.file.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *file) close() error {
|
2010-04-13 17:30:11 -06:00
|
|
|
if file == nil || file.fd < 0 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
var e error
|
2010-04-13 17:30:11 -06:00
|
|
|
if file.isdir() {
|
2011-07-01 08:18:07 -06:00
|
|
|
e = syscall.FindClose(syscall.Handle(file.fd))
|
2010-04-13 17:30:11 -06:00
|
|
|
} else {
|
2011-07-01 08:18:07 -06:00
|
|
|
e = syscall.CloseHandle(syscall.Handle(file.fd))
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2011-11-01 19:49:08 -06:00
|
|
|
var err error
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
err = &PathError{"close", file.name, e}
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2011-07-01 08:18:07 -06:00
|
|
|
file.fd = syscall.InvalidHandle // so it can't be closed again
|
2010-04-13 17:30:11 -06:00
|
|
|
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(file, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-01-16 22:51:54 -07:00
|
|
|
func (file *File) readdir(n int) (fi []FileInfo, err error) {
|
2010-09-23 20:06:59 -06:00
|
|
|
if file == nil || file.fd < 0 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
if !file.isdir() {
|
|
|
|
return nil, &PathError{"Readdir", file.name, ENOTDIR}
|
|
|
|
}
|
2011-05-28 19:59:35 -06:00
|
|
|
wantAll := n <= 0
|
2011-05-16 10:26:16 -06:00
|
|
|
size := n
|
2011-05-28 19:59:35 -06:00
|
|
|
if wantAll {
|
|
|
|
n = -1
|
2010-04-13 17:30:11 -06:00
|
|
|
size = 100
|
|
|
|
}
|
|
|
|
fi = make([]FileInfo, 0, size) // Empty with room to grow.
|
2011-09-05 17:59:08 -06:00
|
|
|
d := &file.dirinfo.data
|
2011-05-16 10:26:16 -06:00
|
|
|
for n != 0 {
|
2011-09-05 17:59:08 -06:00
|
|
|
if file.dirinfo.needdata {
|
|
|
|
e := syscall.FindNextFile(syscall.Handle(file.fd), d)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2010-04-13 17:30:11 -06:00
|
|
|
if e == syscall.ERROR_NO_MORE_FILES {
|
|
|
|
break
|
|
|
|
} else {
|
2011-11-13 20:44:52 -07:00
|
|
|
err = &PathError{"FindNextFile", file.name, e}
|
2011-05-16 10:26:16 -06:00
|
|
|
if !wantAll {
|
|
|
|
fi = nil
|
|
|
|
}
|
|
|
|
return
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-05 17:59:08 -06:00
|
|
|
file.dirinfo.needdata = true
|
2011-11-30 10:04:16 -07:00
|
|
|
name := string(syscall.UTF16ToString(d.FileName[0:]))
|
|
|
|
if name == "." || name == ".." { // Useless names
|
2010-04-13 17:30:11 -06:00
|
|
|
continue
|
|
|
|
}
|
2011-11-30 10:04:16 -07:00
|
|
|
f := toFileInfo(name, d.FileAttributes, d.FileSizeHigh, d.FileSizeLow, d.CreationTime, d.LastAccessTime, d.LastWriteTime)
|
2011-05-16 10:26:16 -06:00
|
|
|
n--
|
2010-10-27 20:47:23 -06:00
|
|
|
fi = append(fi, f)
|
2010-04-13 17:30:11 -06:00
|
|
|
}
|
2011-05-16 10:26:16 -06:00
|
|
|
if !wantAll && len(fi) == 0 {
|
2011-11-01 19:49:08 -06:00
|
|
|
return fi, io.EOF
|
2011-05-16 10:26:16 -06:00
|
|
|
}
|
2010-04-13 17:30:11 -06:00
|
|
|
return fi, nil
|
|
|
|
}
|
2010-04-27 00:17:14 -06:00
|
|
|
|
2011-04-26 02:09:46 -06:00
|
|
|
// read reads up to len(b) bytes from the File.
|
|
|
|
// It returns the number of bytes read and an error, if any.
|
2011-11-13 20:44:52 -07:00
|
|
|
func (f *File) read(b []byte) (n int, err error) {
|
2011-04-26 02:09:46 -06:00
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
return syscall.Read(f.fd, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pread reads len(b) bytes from the File starting at byte offset off.
|
|
|
|
// It returns the number of bytes read and the error, if any.
|
|
|
|
// EOF is signaled by a zero count with err set to 0.
|
2011-11-13 20:44:52 -07:00
|
|
|
func (f *File) pread(b []byte, off int64) (n int, err error) {
|
2011-04-26 02:09:46 -06:00
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
curoffset, e := syscall.Seek(f.fd, 0, 1)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2011-04-26 02:09:46 -06:00
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
defer syscall.Seek(f.fd, curoffset, 0)
|
|
|
|
o := syscall.Overlapped{
|
|
|
|
OffsetHigh: uint32(off >> 32),
|
|
|
|
Offset: uint32(off),
|
|
|
|
}
|
|
|
|
var done uint32
|
2011-07-01 08:18:07 -06:00
|
|
|
e = syscall.ReadFile(syscall.Handle(f.fd), b, &done, &o)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2011-04-26 02:09:46 -06:00
|
|
|
return 0, e
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return int(done), nil
|
2011-04-26 02:09:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// write writes len(b) bytes to the File.
|
|
|
|
// It returns the number of bytes written and an error, if any.
|
2011-11-13 20:44:52 -07:00
|
|
|
func (f *File) write(b []byte) (n int, err error) {
|
2011-04-26 02:09:46 -06:00
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
return syscall.Write(f.fd, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pwrite writes len(b) bytes to the File starting at byte offset off.
|
|
|
|
// It returns the number of bytes written and an error, if any.
|
2011-11-13 20:44:52 -07:00
|
|
|
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
|
2011-04-26 02:09:46 -06:00
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
curoffset, e := syscall.Seek(f.fd, 0, 1)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2011-04-26 02:09:46 -06:00
|
|
|
return 0, e
|
|
|
|
}
|
|
|
|
defer syscall.Seek(f.fd, curoffset, 0)
|
|
|
|
o := syscall.Overlapped{
|
|
|
|
OffsetHigh: uint32(off >> 32),
|
|
|
|
Offset: uint32(off),
|
|
|
|
}
|
|
|
|
var done uint32
|
2011-07-01 08:18:07 -06:00
|
|
|
e = syscall.WriteFile(syscall.Handle(f.fd), b, &done, &o)
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2011-04-26 02:09:46 -06:00
|
|
|
return 0, e
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return int(done), nil
|
2011-04-26 02:09:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// seek sets the offset for the next Read or Write on file to offset, interpreted
|
|
|
|
// according to whence: 0 means relative to the origin of the file, 1 means
|
|
|
|
// relative to the current offset, and 2 means relative to the end.
|
|
|
|
// It returns the new offset and an error, if any.
|
2011-11-13 20:44:52 -07:00
|
|
|
func (f *File) seek(offset int64, whence int) (ret int64, err error) {
|
2011-04-26 02:09:46 -06:00
|
|
|
f.l.Lock()
|
|
|
|
defer f.l.Unlock()
|
|
|
|
return syscall.Seek(f.fd, offset, whence)
|
|
|
|
}
|
|
|
|
|
2010-04-27 00:17:14 -06:00
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the size of the link's target.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Truncate(name string, size int64) error {
|
2011-04-05 00:57:08 -06:00
|
|
|
f, e := OpenFile(name, O_WRONLY|O_CREATE, 0666)
|
2010-04-27 00:17:14 -06:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
e1 := f.Truncate(size)
|
|
|
|
if e1 != nil {
|
|
|
|
return e1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-07-01 08:18:07 -06:00
|
|
|
|
2011-12-19 17:52:20 -07:00
|
|
|
// Remove removes the named file or directory.
|
|
|
|
func Remove(name string) error {
|
|
|
|
p := &syscall.StringToUTF16(name)[0]
|
|
|
|
|
|
|
|
// Go file interface forces us to know whether
|
|
|
|
// name is a file or directory. Try both.
|
|
|
|
e := syscall.DeleteFile(p)
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e1 := syscall.RemoveDirectory(p)
|
|
|
|
if e1 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both failed: figure out which error to return.
|
|
|
|
if e1 != e {
|
|
|
|
a, e2 := syscall.GetFileAttributes(p)
|
|
|
|
if e2 != nil {
|
|
|
|
e = e2
|
|
|
|
} else {
|
|
|
|
if a&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
|
|
|
e = e1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &PathError{"remove", name, e}
|
|
|
|
}
|
|
|
|
|
2011-07-01 08:18:07 -06:00
|
|
|
// Pipe returns a connected pair of Files; reads from r return bytes written to w.
|
2011-11-01 19:49:08 -06:00
|
|
|
// It returns the files and an error, if any.
|
|
|
|
func Pipe() (r *File, w *File, err error) {
|
2011-07-01 08:18:07 -06:00
|
|
|
var p [2]syscall.Handle
|
|
|
|
|
|
|
|
// See ../syscall/exec.go for description of lock.
|
|
|
|
syscall.ForkLock.RLock()
|
|
|
|
e := syscall.Pipe(p[0:])
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
2011-07-01 08:18:07 -06:00
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
return nil, nil, NewSyscallError("pipe", e)
|
|
|
|
}
|
|
|
|
syscall.CloseOnExec(p[0])
|
|
|
|
syscall.CloseOnExec(p[1])
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
|
|
|
|
return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
|
|
|
|
}
|
2011-11-14 12:06:50 -07:00
|
|
|
|
|
|
|
// TempDir returns the default directory to use for temporary files.
|
|
|
|
func TempDir() string {
|
|
|
|
const pathSep = '\\'
|
2011-11-15 10:48:22 -07:00
|
|
|
dirw := make([]uint16, syscall.MAX_PATH)
|
2011-11-14 12:06:50 -07:00
|
|
|
n, _ := syscall.GetTempPath(uint32(len(dirw)), &dirw[0])
|
|
|
|
if n > uint32(len(dirw)) {
|
|
|
|
dirw = make([]uint16, n)
|
|
|
|
n, _ = syscall.GetTempPath(uint32(len(dirw)), &dirw[0])
|
|
|
|
if n > uint32(len(dirw)) {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n > 0 && dirw[n-1] == pathSep {
|
|
|
|
n--
|
|
|
|
}
|
|
|
|
return string(utf16.Decode(dirw[0:n]))
|
|
|
|
}
|