Skip to content

add os.Truncate(name, size) (see #4209) #4401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/os/file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func NewFile(fd uintptr, name string) *File {
return &File{&file{handle: unixFileHandle(fd), name: name}}
}

// Truncate changes the size of the named file.
// If the file is a symbolic link, it changes the size of the link's target.
// If there is an error, it will be of type *PathError.
func Truncate(name string, size int64) error {
e := ignoringEINTR(func() error {
return syscall.Truncate(name, size)
})
if e != nil {
return &PathError{Op: "truncate", Path: name, Err: e}
}
return nil
}

func Pipe() (r *File, w *File, err error) {
var p [2]int
err = handleSyscallError(syscall.Pipe2(p[:], syscall.O_CLOEXEC))
Expand Down Expand Up @@ -134,14 +147,7 @@ func (f *File) Truncate(size int64) (err error) {
return ErrClosed
}

e := ignoringEINTR(func() error {
return syscall.Truncate(f.name, size)
})

if e != nil {
return &PathError{Op: "truncate", Path: f.name, Err: e}
}
return
return Truncate(f.name, size)
}

// ReadAt reads up to len(b) bytes from the File starting at the given absolute offset.
Expand Down
15 changes: 10 additions & 5 deletions src/os/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func (f *unixFileHandle) Truncate(size int64) error {
return ErrNotImplemented
}

// 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 {
return &PathError{Op: "truncate", Path: name, Err: ErrNotImplemented}
}

func tempDir() string {
n := uint32(syscall.MAX_PATH)
for {
Expand Down Expand Up @@ -110,14 +116,13 @@ func (f unixFileHandle) Sync() error {
return ErrNotImplemented
}

// 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 (f *File) Truncate(size int64) error {
var err error
if f.handle == nil {
err = ErrClosed
} else {
err = ErrNotImplemented
return &PathError{Op: "truncate", Path: f.name, Err: ErrClosed}
}
return &PathError{Op: "truncate", Path: f.name, Err: err}
return Truncate(f.name, size)
}

// isWindowsNulName reports whether name is os.DevNull ('NUL') on Windows.
Expand Down
Loading