Skip to content

Commit cdea833

Browse files
aykevldeadprogram
authored andcommitted
os: add File.Chdir support
We should really be using syscall.Fchdir here, but this is a fix to get Go 1.24 working.
1 parent c2eaa49 commit cdea833

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

src/os/file.go

+11
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ func (f *File) Chmod(mode FileMode) (err error) {
318318
return
319319
}
320320

321+
// Chdir changes the current working directory to the file, which must be a
322+
// directory. If there is an error, it will be of type *PathError.
323+
func (f *File) Chdir() (err error) {
324+
if f.handle == nil {
325+
err = ErrClosed
326+
} else {
327+
err = f.chdir()
328+
}
329+
return
330+
}
331+
321332
// LinkError records an error during a link or symlink or rename system call and
322333
// the paths that caused it.
323334
type LinkError struct {

src/os/file_other.go

+4
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,7 @@ func (f *File) Truncate(size int64) (err error) {
159159
func (f *File) chmod(mode FileMode) error {
160160
return ErrUnsupported
161161
}
162+
163+
func (f *File) chdir() error {
164+
return ErrNotImplemented
165+
}

src/os/file_unix.go

+16
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ func (f *File) chmod(mode FileMode) error {
166166
return nil
167167
}
168168

169+
func (f *File) chdir() error {
170+
if f.handle == nil {
171+
return ErrClosed
172+
}
173+
174+
// TODO: use syscall.Fchdir instead
175+
longName := fixLongPath(f.name)
176+
e := ignoringEINTR(func() error {
177+
return syscall.Chdir(longName)
178+
})
179+
if e != nil {
180+
return &PathError{Op: "chdir", Path: f.name, Err: e}
181+
}
182+
return nil
183+
}
184+
169185
// ReadAt reads up to len(b) bytes from the File starting at the given absolute offset.
170186
// It returns the number of bytes read and any error encountered, possibly io.EOF.
171187
// At end of file, Pread returns 0, io.EOF.

src/os/file_windows.go

+4
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ func isWindowsNulName(name string) bool {
146146
func (f *File) chmod(mode FileMode) error {
147147
return ErrNotImplemented
148148
}
149+
150+
func (f *File) chdir() error {
151+
return ErrNotImplemented
152+
}

0 commit comments

Comments
 (0)