Skip to content

Commit 84048f2

Browse files
authored
os/File: add stubs for os.File Deadlines (#4465)
os/File: add stubs for os.File Deadlines * add os.SetDeadline, os.SetReadDeadline, os.SetWriteDeadline stubs for posix files. * deadline: add tests Signed-off-by: leongross <[email protected]>
1 parent a9bf981 commit 84048f2

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/os/deadline_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//go:build posix && !baremetal && !js
2+
3+
package os_test
4+
5+
import (
6+
"errors"
7+
. "os"
8+
"testing"
9+
)
10+
11+
func TestDeadlines(t *testing.T) {
12+
// Create a handle to a known-good, existing file
13+
f, err := Open("/dev/null")
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
18+
if err := f.SetDeadline(0); err == nil {
19+
if err != nil {
20+
t.Errorf("wanted nil, got %v", err)
21+
}
22+
}
23+
24+
if err := f.SetDeadline(1); err == nil {
25+
if !errors.Is(err, ErrNotImplemented) {
26+
t.Errorf("wanted ErrNotImplemented, got %v", err)
27+
}
28+
}
29+
30+
if err := f.SetReadDeadline(1); err == nil {
31+
if !errors.Is(err, ErrNotImplemented) {
32+
t.Errorf("wanted ErrNotImplemented, got %v", err)
33+
}
34+
}
35+
36+
if err := f.SetWriteDeadline(1); err == nil {
37+
if !errors.Is(err, ErrNotImplemented) {
38+
t.Errorf("wanted ErrNotImplemented, got %v", err)
39+
}
40+
}
41+
42+
// Closed files must return an error
43+
f.Close()
44+
45+
if err := f.SetDeadline(0); err == nil {
46+
if err != ErrClosed {
47+
t.Errorf("wanted ErrClosed, got %v", err)
48+
}
49+
}
50+
}

src/os/file.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,29 @@ func (f *File) SyscallConn() (conn syscall.RawConn, err error) {
257257
return
258258
}
259259

260+
// SetDeadline sets the read and write deadlines for a File.
261+
// Calls to SetDeadline for files that do not support deadlines will return ErrNoDeadline
262+
// This stub always returns ErrNoDeadline.
263+
// A zero value for t means I/O operations will not time out.
264+
func (f *File) SetDeadline(t time.Time) error {
265+
if f.handle == nil {
266+
return ErrClosed
267+
}
268+
return f.setDeadline(t)
269+
}
270+
260271
// SetReadDeadline sets the deadline for future Read calls and any
261272
// currently-blocked Read call.
262273
func (f *File) SetReadDeadline(t time.Time) error {
263274
return f.setReadDeadline(t)
264275
}
265276

277+
// SetWriteDeadline sets the deadline for any future Write calls and any
278+
// currently-blocked Write call.
279+
func (f *File) SetWriteDeadline(t time.Time) error {
280+
return f.setWriteDeadline(t)
281+
}
282+
266283
// fd is an internal interface that is used to try a type assertion in order to
267284
// call the Fd() method of the underlying file handle if it is implemented.
268285
type fd interface {

src/os/file_posix.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,35 @@ import (
44
"time"
55
)
66

7+
//TODO: re-implement the ErrNoDeadline error in the correct code path
8+
79
// Chtimes is a stub, not yet implemented
810
func Chtimes(name string, atime time.Time, mtime time.Time) error {
911
return ErrNotImplemented
1012
}
1113

14+
// setDeadline sets the read and write deadline.
15+
func (f *File) setDeadline(t time.Time) error {
16+
if t.IsZero() {
17+
return nil
18+
}
19+
return ErrNotImplemented
20+
}
21+
1222
// setReadDeadline sets the read deadline, not yet implemented
13-
func (f *File) setReadDeadline(_ time.Time) error {
23+
// A zero value for t means Read will not time out.
24+
func (f *File) setReadDeadline(t time.Time) error {
25+
if t.IsZero() {
26+
return nil
27+
}
28+
return ErrNotImplemented
29+
}
30+
31+
// setWriteDeadline sets the write deadline, not yet implemented
32+
// A zero value for t means Read will not time out.
33+
func (f *File) setWriteDeadline(t time.Time) error {
34+
if t.IsZero() {
35+
return nil
36+
}
1437
return ErrNotImplemented
1538
}

0 commit comments

Comments
 (0)