diff --git a/src/os/file.go b/src/os/file.go index 0e20d1bf1..e7dd214b7 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -318,6 +318,17 @@ func (f *File) Chmod(mode FileMode) (err error) { return } +// Chdir changes the current working directory to the file, which must be a +// directory. If there is an error, it will be of type *PathError. +func (f *File) Chdir() (err error) { + if f.handle == nil { + err = ErrClosed + } else { + err = f.chdir() + } + return +} + // LinkError records an error during a link or symlink or rename system call and // the paths that caused it. type LinkError struct { diff --git a/src/os/file_other.go b/src/os/file_other.go index e0705c78d..8e7d33e00 100644 --- a/src/os/file_other.go +++ b/src/os/file_other.go @@ -159,3 +159,7 @@ func (f *File) Truncate(size int64) (err error) { func (f *File) chmod(mode FileMode) error { return ErrUnsupported } + +func (f *File) chdir() error { + return ErrNotImplemented +} diff --git a/src/os/file_unix.go b/src/os/file_unix.go index fd4d464f5..9dc3a91e0 100644 --- a/src/os/file_unix.go +++ b/src/os/file_unix.go @@ -166,6 +166,22 @@ func (f *File) chmod(mode FileMode) error { return nil } +func (f *File) chdir() error { + if f.handle == nil { + return ErrClosed + } + + // TODO: use syscall.Fchdir instead + longName := fixLongPath(f.name) + e := ignoringEINTR(func() error { + return syscall.Chdir(longName) + }) + if e != nil { + return &PathError{Op: "chdir", Path: f.name, Err: e} + } + return nil +} + // ReadAt reads up to len(b) bytes from the File starting at the given absolute offset. // It returns the number of bytes read and any error encountered, possibly io.EOF. // At end of file, Pread returns 0, io.EOF. diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 022381dee..50c5ee7a8 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -146,3 +146,7 @@ func isWindowsNulName(name string) bool { func (f *File) chmod(mode FileMode) error { return ErrNotImplemented } + +func (f *File) chdir() error { + return ErrNotImplemented +}