2008-09-11 14:03:46 -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.
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// The os package provides a platform-independent interface to operating
|
|
|
|
// system functionality. The design is Unix-like.
|
2008-09-11 14:03:46 -06:00
|
|
|
package os
|
|
|
|
|
2009-02-15 20:35:52 -07:00
|
|
|
import (
|
|
|
|
"syscall";
|
|
|
|
)
|
2008-09-11 14:03:46 -06:00
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Auxiliary information if the File describes a directory
|
2009-04-14 19:52:39 -06:00
|
|
|
type dirInfo struct {
|
2009-02-10 12:27:45 -07:00
|
|
|
buf []byte; // buffer for directory I/O
|
2009-06-01 23:14:39 -06:00
|
|
|
nbuf int; // length of buf; return value from Getdirentries
|
|
|
|
bufp int; // location of next record in buf.
|
2009-02-10 12:27:45 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// File represents an open file descriptor.
|
|
|
|
type File struct {
|
2009-10-06 20:40:35 -06:00
|
|
|
fd int;
|
2009-02-06 18:54:26 -07:00
|
|
|
name string;
|
2009-02-10 17:44:04 -07:00
|
|
|
dirinfo *dirInfo; // nil unless directory being read
|
2009-10-06 20:40:35 -06:00
|
|
|
nepipe int; // number of consecutive EPIPE in Write
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Fd returns the integer Unix file descriptor referencing the open file.
|
2009-11-06 15:24:38 -07:00
|
|
|
func (file *File) Fd() int { return file.fd }
|
2009-02-06 18:54:26 -07:00
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Name returns the name of the file as presented to Open.
|
2009-11-06 15:24:38 -07:00
|
|
|
func (file *File) Name() string { return file.name }
|
2009-02-06 18:54:26 -07:00
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// NewFile returns a new File with the given file descriptor and name.
|
2009-06-25 21:24:55 -06:00
|
|
|
func NewFile(fd int, name string) *File {
|
|
|
|
if fd < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return &File{fd, name, nil, 0};
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
|
2009-03-07 17:56:44 -07:00
|
|
|
// standard output, and standard error file descriptors.
|
2009-01-20 15:40:40 -07:00
|
|
|
var (
|
2009-10-06 20:40:35 -06:00
|
|
|
Stdin = NewFile(0, "/dev/stdin");
|
|
|
|
Stdout = NewFile(1, "/dev/stdout");
|
|
|
|
Stderr = NewFile(2, "/dev/stderr");
|
2008-09-11 14:03:46 -06:00
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Flags to Open wrapping those of the underlying system. Not all flags
|
|
|
|
// may be implemented on a given system.
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2009-10-06 20:40:35 -06:00
|
|
|
O_RDONLY = syscall.O_RDONLY; // open the file read-only.
|
|
|
|
O_WRONLY = syscall.O_WRONLY; // open the file write-only.
|
|
|
|
O_RDWR = syscall.O_RDWR; // open the file read-write.
|
|
|
|
O_APPEND = syscall.O_APPEND; // open the file append-only.
|
|
|
|
O_ASYNC = syscall.O_ASYNC; // generate a signal when I/O is available.
|
|
|
|
O_CREAT = syscall.O_CREAT; // create a new file if none exists.
|
2009-11-01 12:13:27 -07:00
|
|
|
O_EXCL = syscall.O_EXCL; // used with O_CREAT, file must not exist
|
2009-10-06 20:40:35 -06:00
|
|
|
O_NOCTTY = syscall.O_NOCTTY; // do not make file the controlling tty.
|
|
|
|
O_NONBLOCK = syscall.O_NONBLOCK; // open in non-blocking mode.
|
|
|
|
O_NDELAY = O_NONBLOCK; // synonym for O_NONBLOCK
|
|
|
|
O_SYNC = syscall.O_SYNC; // open for synchronous I/O.
|
|
|
|
O_TRUNC = syscall.O_TRUNC; // if possible, truncate file when opened.
|
2009-11-10 16:05:15 -07:00
|
|
|
O_CREATE = O_CREAT; // create a new file if none exists.
|
2008-09-11 16:09:10 -06:00
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.)
|
2009-03-11 13:51:10 -06:00
|
|
|
// if applicable. If successful, methods on the returned File can be used for I/O.
|
|
|
|
// It returns the File and an Error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Open(name string, flag int, perm int) (file *File, err Error) {
|
2009-11-09 22:23:52 -07:00
|
|
|
r, e := syscall.Open(name, flag|syscall.O_CLOEXEC, perm);
|
2009-02-15 20:35:52 -07:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &PathError{"open", name, Errno(e)}
|
2009-02-15 20:35:52 -07: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
|
2009-11-09 13:07:39 -07:00
|
|
|
syscall.CloseOnExec(r)
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
|
2009-06-25 21:24:55 -06:00
|
|
|
return NewFile(r, name), nil;
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Close closes the File, rendering it unusable for I/O.
|
2009-03-07 17:56:44 -07:00
|
|
|
// It returns an Error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (file *File) Close() Error {
|
2009-03-11 13:51:10 -06:00
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return EINVAL
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2009-08-10 23:02:51 -06:00
|
|
|
var err Error;
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Close(file.fd); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = &PathError{"close", file.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
file.fd = -1; // so it can't be closed again
|
2009-06-01 23:14:39 -06:00
|
|
|
return err;
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-06-22 14:26:13 -06:00
|
|
|
type eofError int
|
2009-10-06 20:40:35 -06:00
|
|
|
|
2009-11-06 15:24:38 -07:00
|
|
|
func (eofError) String() string { return "EOF" }
|
2009-06-22 14:26:13 -06:00
|
|
|
|
|
|
|
// EOF is the Error returned by Read when no more input is available.
|
|
|
|
// Functions should return EOF only to signal a graceful end of input.
|
|
|
|
// If the EOF occurs unexpectedly in a structured data stream,
|
|
|
|
// the appropriate error is either io.ErrUnexpectedEOF or some other error
|
|
|
|
// giving more detail.
|
|
|
|
var EOF Error = eofError(0)
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Read reads up to len(b) bytes from the File.
|
2009-03-07 17:56:44 -07:00
|
|
|
// It returns the number of bytes read and an Error, if any.
|
2009-06-22 14:26:13 -06:00
|
|
|
// EOF is signaled by a zero count with err set to EOF.
|
2009-08-27 19:36:45 -06:00
|
|
|
func (file *File) Read(b []byte) (n int, err Error) {
|
2009-03-11 13:51:10 -06:00
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EINVAL
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
n, e := syscall.Read(file.fd, b);
|
|
|
|
if n < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 0
|
2008-11-14 16:13:29 -07:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if n == 0 && e == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EOF
|
2009-06-22 14:26:13 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = &PathError{"read", file.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return n, err;
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-08-27 19:36:45 -06:00
|
|
|
// ReadAt 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 EOF.
|
|
|
|
// ReadAt always returns a non-nil Error when n != len(b).
|
|
|
|
func (file *File) ReadAt(b []byte, off int64) (n int, err Error) {
|
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EINVAL
|
2009-08-27 19:36:45 -06:00
|
|
|
}
|
|
|
|
for len(b) > 0 {
|
|
|
|
m, e := syscall.Pread(file.fd, b, off);
|
2009-11-19 12:51:23 -07:00
|
|
|
if m == 0 && e == 0 {
|
|
|
|
return n, EOF
|
|
|
|
}
|
2009-08-27 19:36:45 -06:00
|
|
|
if e != 0 {
|
|
|
|
err = &PathError{"read", file.name, Errno(e)};
|
|
|
|
break;
|
|
|
|
}
|
2009-12-09 15:18:32 -07:00
|
|
|
n += m;
|
2009-11-20 12:45:05 -07:00
|
|
|
b = b[m:];
|
2009-08-27 19:36:45 -06:00
|
|
|
off += int64(m);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Write writes len(b) bytes to the File.
|
2009-03-07 17:56:44 -07:00
|
|
|
// It returns the number of bytes written and an Error, if any.
|
2009-08-27 19:36:45 -06:00
|
|
|
// Write returns a non-nil Error when n != len(b).
|
|
|
|
func (file *File) Write(b []byte) (n int, err Error) {
|
2009-03-11 13:51:10 -06:00
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EINVAL
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
n, e := syscall.Write(file.fd, b);
|
|
|
|
if n < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 0
|
2008-11-14 16:13:29 -07:00
|
|
|
}
|
2009-04-29 19:18:42 -06:00
|
|
|
if e == syscall.EPIPE {
|
|
|
|
file.nepipe++;
|
|
|
|
if file.nepipe >= 10 {
|
2009-11-09 13:07:39 -07:00
|
|
|
Exit(syscall.EPIPE)
|
2009-04-29 19:18:42 -06:00
|
|
|
}
|
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
file.nepipe = 0
|
2009-04-29 19:18:42 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = &PathError{"write", file.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return n, err;
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
|
|
|
|
2009-08-27 19:36:45 -06:00
|
|
|
// WriteAt writes len(b) bytes to the File starting at byte offset off.
|
|
|
|
// It returns the number of bytes written and an Error, if any.
|
|
|
|
// WriteAt returns a non-nil Error when n != len(b).
|
|
|
|
func (file *File) WriteAt(b []byte, off int64) (n int, err Error) {
|
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EINVAL
|
2009-08-27 19:36:45 -06:00
|
|
|
}
|
|
|
|
for len(b) > 0 {
|
|
|
|
m, e := syscall.Pwrite(file.fd, b, off);
|
|
|
|
if e != 0 {
|
|
|
|
err = &PathError{"write", file.name, Errno(e)};
|
|
|
|
break;
|
|
|
|
}
|
2009-12-09 15:18:32 -07:00
|
|
|
n += m;
|
2009-11-20 12:45:05 -07:00
|
|
|
b = b[m:];
|
2009-08-27 19:36:45 -06:00
|
|
|
off += int64(m);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Seek sets the offset for the next Read or Write on file to offset, interpreted
|
2009-03-07 17:56:44 -07:00
|
|
|
// 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.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (file *File) Seek(offset int64, whence int) (ret int64, err Error) {
|
2009-06-01 23:14:39 -06:00
|
|
|
r, e := syscall.Seek(file.fd, offset, whence);
|
2009-06-25 21:24:55 -06:00
|
|
|
if e == 0 && file.dirinfo != nil && r != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
e = syscall.EISDIR
|
2009-02-10 12:27:45 -07:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, &PathError{"seek", file.name, Errno(e)}
|
2009-02-10 12:27:45 -07:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return r, nil;
|
2009-02-10 12:27:45 -07:00
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// WriteString is like Write, but writes the contents of string s rather than
|
|
|
|
// an array of bytes.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (file *File) WriteString(s string) (ret int, err Error) {
|
2009-03-11 13:51:10 -06:00
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, EINVAL
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
b := syscall.StringByteSlice(s);
|
2009-10-06 20:40:35 -06:00
|
|
|
b = b[0 : len(b)-1];
|
2009-06-25 21:24:55 -06:00
|
|
|
return file.Write(b);
|
2008-09-11 14:03:46 -06:00
|
|
|
}
|
2008-09-26 15:31:17 -06:00
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Pipe returns a connected pair of Files; reads from r return bytes written to w.
|
|
|
|
// It returns the files and an Error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Pipe() (r *File, w *File, err Error) {
|
2009-06-01 23:14:39 -06:00
|
|
|
var p [2]int;
|
2009-02-15 20:35:52 -07:00
|
|
|
|
|
|
|
// See ../syscall/exec.go for description of lock.
|
|
|
|
syscall.ForkLock.RLock();
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Pipe(&p);
|
2008-09-26 15:31:17 -06:00
|
|
|
if e != 0 {
|
2009-02-15 20:35:52 -07:00
|
|
|
syscall.ForkLock.RUnlock();
|
2009-06-25 21:24:55 -06:00
|
|
|
return nil, nil, NewSyscallError("pipe", e);
|
2008-09-26 15:31:17 -06:00
|
|
|
}
|
2009-02-15 20:35:52 -07:00
|
|
|
syscall.CloseOnExec(p[0]);
|
|
|
|
syscall.CloseOnExec(p[1]);
|
|
|
|
syscall.ForkLock.RUnlock();
|
|
|
|
|
2009-10-06 20:40:35 -06:00
|
|
|
return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil;
|
2008-09-26 15:31:17 -06:00
|
|
|
}
|
2008-10-09 01:15:37 -06:00
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Mkdir creates a new directory with the specified name and permission bits.
|
|
|
|
// It returns an error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Mkdir(name string, perm int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
e := syscall.Mkdir(name, perm);
|
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"mkdir", name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2008-10-09 01:15:37 -06:00
|
|
|
}
|
2009-02-06 18:54:26 -07:00
|
|
|
|
2009-05-15 12:04:49 -06:00
|
|
|
// Stat returns a Dir structure describing the named file and an error, if any.
|
|
|
|
// If name names a valid symbolic link, the returned Dir describes
|
|
|
|
// the file pointed at by the link and has dir.FollowedSymlink set to true.
|
|
|
|
// If name names an invalid symbolic link, the returned Dir describes
|
|
|
|
// the link itself and has dir.FollowedSymlink set to false.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Stat(name string) (dir *Dir, err Error) {
|
2009-05-15 12:04:49 -06:00
|
|
|
var lstat, stat syscall.Stat_t;
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Lstat(name, &lstat);
|
2009-02-06 18:54:26 -07:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &PathError{"stat", name, Errno(e)}
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
2009-05-15 12:04:49 -06:00
|
|
|
statp := &lstat;
|
2009-11-09 22:23:52 -07:00
|
|
|
if lstat.Mode&syscall.S_IFMT == syscall.S_IFLNK {
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Stat(name, &stat);
|
2009-05-15 12:04:49 -06:00
|
|
|
if e == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
statp = &stat
|
2009-05-15 12:04:49 -06:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return dirFromStat(name, new(Dir), &lstat, statp), nil;
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Stat returns the Dir structure describing file.
|
2009-03-07 17:56:44 -07:00
|
|
|
// It returns the Dir and an error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (file *File) Stat() (dir *Dir, err Error) {
|
2009-05-15 12:04:49 -06:00
|
|
|
var stat syscall.Stat_t;
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Fstat(file.fd, &stat);
|
2009-02-06 18:54:26 -07:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &PathError{"stat", file.name, Errno(e)}
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return dirFromStat(file.name, new(Dir), &stat, &stat), nil;
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
|
|
|
|
2009-05-15 12:04:49 -06:00
|
|
|
// Lstat returns the Dir structure describing the named file and an error, if any.
|
|
|
|
// If the file is a symbolic link, the returned Dir describes the
|
|
|
|
// symbolic link. Lstat makes no attempt to follow the link.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Lstat(name string) (dir *Dir, err Error) {
|
2009-05-15 12:04:49 -06:00
|
|
|
var stat syscall.Stat_t;
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Lstat(name, &stat);
|
2009-02-06 18:54:26 -07:00
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &PathError{"lstat", name, Errno(e)}
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return dirFromStat(name, new(Dir), &stat, &stat), nil;
|
2009-02-06 18:54:26 -07:00
|
|
|
}
|
2009-02-10 12:55:48 -07:00
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Readdir reads the contents of the directory associated with file and
|
2009-05-15 12:04:49 -06:00
|
|
|
// returns an array of up to count Dir structures, as would be returned
|
|
|
|
// by Stat, in directory order. Subsequent calls on the same file will yield further Dirs.
|
2009-03-07 17:56:44 -07:00
|
|
|
// A negative count means to read until EOF.
|
2009-05-15 12:04:49 -06:00
|
|
|
// Readdir returns the array and an Error, if any.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (file *File) Readdir(count int) (dirs []Dir, err Error) {
|
2009-03-11 13:51:10 -06:00
|
|
|
dirname := file.name;
|
2009-02-10 12:55:48 -07:00
|
|
|
if dirname == "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
dirname = "."
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
|
|
|
dirname += "/";
|
2009-03-11 13:51:10 -06:00
|
|
|
names, err1 := file.Readdirnames(count);
|
2009-02-10 12:55:48 -07:00
|
|
|
if err1 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err1
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
|
|
|
dirs = make([]Dir, len(names));
|
|
|
|
for i, filename := range names {
|
2009-11-09 22:23:52 -07:00
|
|
|
dirp, err := Lstat(dirname + filename);
|
2009-10-06 20:40:35 -06:00
|
|
|
if dirp == nil || err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
dirs[i].Name = filename // rest is already zeroed out
|
2009-02-10 12:55:48 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
dirs[i] = *dirp
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-06 20:40:35 -06:00
|
|
|
return;
|
2009-02-10 12:55:48 -07:00
|
|
|
}
|
|
|
|
|
2009-04-07 01:40:36 -06:00
|
|
|
// Chdir changes the current working directory to the named directory.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Chdir(dir string) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Chdir(dir); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chdir", dir, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-04-07 01:40:36 -06:00
|
|
|
}
|
|
|
|
|
2009-05-18 11:49:34 -06:00
|
|
|
// Chdir changes the current working directory to the file,
|
|
|
|
// which must be a directory.
|
|
|
|
func (f *File) Chdir() Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Fchdir(f.fd); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chdir", f.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-18 11:49:34 -06:00
|
|
|
}
|
|
|
|
|
2009-04-13 17:50:42 -06:00
|
|
|
// Remove removes the named file or directory.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Remove(name string) Error {
|
2009-04-13 17:50:42 -06:00
|
|
|
// System call interface forces us to know
|
|
|
|
// whether name is a file or directory.
|
|
|
|
// Try both: it is cheaper on average than
|
|
|
|
// doing a Stat plus the right one.
|
2009-06-01 23:14:39 -06:00
|
|
|
e := syscall.Unlink(name);
|
2009-04-13 17:50:42 -06:00
|
|
|
if e == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-04-13 17:50:42 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
e1 := syscall.Rmdir(name);
|
2009-04-13 17:50:42 -06:00
|
|
|
if e1 == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-04-13 17:50:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Both failed: figure out which error to return.
|
|
|
|
// OS X and Linux differ on whether unlink(dir)
|
|
|
|
// returns EISDIR, so can't use that. However,
|
|
|
|
// both agree that rmdir(file) returns ENOTDIR,
|
|
|
|
// so we can use that to decide which error is real.
|
2009-04-13 20:14:09 -06:00
|
|
|
// Rmdir might also return ENOTDIR if given a bad
|
2009-04-13 17:50:42 -06:00
|
|
|
// file path, like /etc/passwd/foo, but in that case,
|
|
|
|
// both errors will be ENOTDIR, so it's okay to
|
|
|
|
// use the error from unlink.
|
|
|
|
if e1 != syscall.ENOTDIR {
|
2009-11-09 13:07:39 -07:00
|
|
|
e = e1
|
2009-04-13 17:50:42 -06:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
return &PathError{"remove", name, Errno(e)};
|
|
|
|
}
|
|
|
|
|
2009-12-04 12:46:56 -07:00
|
|
|
// LinkError records an error during a link or symlink or rename
|
2009-06-25 21:24:55 -06:00
|
|
|
// system call and the paths that caused it.
|
|
|
|
type LinkError struct {
|
2009-10-06 20:40:35 -06:00
|
|
|
Op string;
|
|
|
|
Old string;
|
|
|
|
New string;
|
|
|
|
Error Error;
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LinkError) String() string {
|
2009-11-09 13:07:39 -07:00
|
|
|
return e.Op + " " + e.Old + " " + e.New + ": " + e.Error.String()
|
2009-04-13 17:50:42 -06:00
|
|
|
}
|
|
|
|
|
2009-05-13 11:16:46 -06:00
|
|
|
// Link creates a hard link.
|
2009-05-15 15:11:24 -06:00
|
|
|
func Link(oldname, newname string) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
e := syscall.Link(oldname, newname);
|
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &LinkError{"link", oldname, newname, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-13 11:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Symlink creates a symbolic link.
|
2009-05-15 15:11:24 -06:00
|
|
|
func Symlink(oldname, newname string) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
e := syscall.Symlink(oldname, newname);
|
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &LinkError{"symlink", oldname, newname, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-13 11:16:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readlink reads the contents of a symbolic link: the destination of
|
|
|
|
// the link. It returns the contents and an Error, if any.
|
2009-05-15 15:11:24 -06:00
|
|
|
func Readlink(name string) (string, Error) {
|
2009-06-01 23:14:39 -06:00
|
|
|
for len := 128; ; len *= 2 {
|
2009-05-13 11:16:46 -06:00
|
|
|
b := make([]byte, len);
|
2009-06-01 23:14:39 -06:00
|
|
|
n, e := syscall.Readlink(name, b);
|
|
|
|
if e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "", &PathError{"readlink", name, Errno(e)}
|
2009-06-01 23:14:39 -06:00
|
|
|
}
|
|
|
|
if n < len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return string(b[0:n]), nil
|
2009-05-13 11:16:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Silence 6g.
|
|
|
|
return "", nil;
|
|
|
|
}
|
2009-05-15 15:11:24 -06:00
|
|
|
|
2009-12-04 12:46:56 -07:00
|
|
|
// Rename renames a file.
|
|
|
|
func Rename(oldname, newname string) Error {
|
|
|
|
e := syscall.Rename(oldname, newname);
|
|
|
|
if e != 0 {
|
|
|
|
return &LinkError{"rename", oldname, newname, Errno(e)}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-05-15 15:11:24 -06:00
|
|
|
// Chmod changes the mode of the named file to mode.
|
|
|
|
// If the file is a symbolic link, it changes the uid and gid of the link's target.
|
|
|
|
func Chmod(name string, mode int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Chmod(name, mode); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chmod", name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chmod changes the mode of the file to mode.
|
|
|
|
func (f *File) Chmod(mode int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Fchmod(f.fd, mode); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chmod", f.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the uid and gid of the link's target.
|
|
|
|
func Chown(name string, uid, gid int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Chown(name, uid, gid); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chown", name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lchown changes the numeric uid and gid of the named file.
|
|
|
|
// If the file is a symbolic link, it changes the uid and gid of the link itself.
|
|
|
|
func Lchown(name string, uid, gid int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Lchown(name, uid, gid); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"lchown", name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chown changes the numeric uid and gid of the named file.
|
|
|
|
func (f *File) Chown(uid, gid int) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Fchown(f.fd, uid, gid); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"chown", f.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -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.
|
|
|
|
func Truncate(name string, size int64) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Truncate(name, size); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"truncate", name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate changes the size of the file.
|
|
|
|
// It does not change the I/O offset.
|
|
|
|
func (f *File) Truncate(size int64) Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if e := syscall.Ftruncate(f.fd, size); e != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &PathError{"truncate", f.name, Errno(e)}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
return nil;
|
2009-05-15 15:11:24 -06:00
|
|
|
}
|