Skip to content

Commit 8476814

Browse files
justincliftaykevl
authored andcommitted
runtime: add several os package stubs
1 parent 66d8899 commit 8476814

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

src/os/file.go

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111

1212
// Portable analogs of some common system call errors.
1313
var (
14-
ErrUnsupported = errors.New("operation not supported")
14+
errUnsupported = errors.New("operation not supported")
15+
notImplemented = errors.New("os: not implemented")
1516
)
1617

1718
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
@@ -28,6 +29,21 @@ type File struct {
2829
name string
2930
}
3031

32+
// Readdir is a stub, not yet implemented
33+
func (f *File) Readdir(n int) ([]FileInfo, error) {
34+
return nil, notImplemented
35+
}
36+
37+
// Readdirnames is a stub, not yet implemented
38+
func (f *File) Readdirnames(n int) (names []string, err error) {
39+
return nil, notImplemented
40+
}
41+
42+
// Stat is a stub, not yet implemented
43+
func (f *File) Stat() (FileInfo, error) {
44+
return nil, notImplemented
45+
}
46+
3147
// NewFile returns a new File with the given file descriptor and name.
3248
func NewFile(fd uintptr, name string) *File {
3349
return &File{fd, name}
@@ -38,3 +54,146 @@ func NewFile(fd uintptr, name string) *File {
3854
func (f *File) Fd() uintptr {
3955
return f.fd
4056
}
57+
58+
const (
59+
PathSeparator = '/' // OS-specific path separator
60+
PathListSeparator = ':' // OS-specific path list separator
61+
)
62+
63+
// IsPathSeparator reports whether c is a directory separator character.
64+
func IsPathSeparator(c uint8) bool {
65+
return PathSeparator == c
66+
}
67+
68+
// PathError records an error and the operation and file path that caused it.
69+
type PathError struct {
70+
Op string
71+
Path string
72+
Err error
73+
}
74+
75+
func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
76+
77+
// Open is a super simple stub function (for now), only capable of opening stdin, stdout, and stderr
78+
func Open(name string) (*File, error) {
79+
fd := uintptr(999)
80+
switch name {
81+
case "/dev/stdin":
82+
fd = 0
83+
case "/dev/stdout":
84+
fd = 1
85+
case "/dev/stderr":
86+
fd = 2
87+
default:
88+
return nil, &PathError{"open", name, notImplemented}
89+
}
90+
return &File{fd, name}, nil
91+
}
92+
93+
// OpenFile is a stub, passing through to the stub Open() call
94+
func OpenFile(name string, flag int, perm FileMode) (*File, error) {
95+
return Open(name)
96+
}
97+
98+
// Create is a stub, passing through to the stub Open() call
99+
func Create(name string) (*File, error) {
100+
return Open(name)
101+
}
102+
103+
type FileMode uint32
104+
105+
// Mode constants, copied from the mainline Go source
106+
// https://github.com/golang/go/blob/4ce6a8e89668b87dce67e2f55802903d6eb9110a/src/os/types.go#L35-L63
107+
const (
108+
// The single letters are the abbreviations used by the String method's formatting.
109+
ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
110+
ModeAppend // a: append-only
111+
ModeExclusive // l: exclusive use
112+
ModeTemporary // T: temporary file; Plan 9 only
113+
ModeSymlink // L: symbolic link
114+
ModeDevice // D: device file
115+
ModeNamedPipe // p: named pipe (FIFO)
116+
ModeSocket // S: Unix domain socket
117+
ModeSetuid // u: setuid
118+
ModeSetgid // g: setgid
119+
ModeCharDevice // c: Unix character device, when ModeDevice is set
120+
ModeSticky // t: sticky
121+
ModeIrregular // ?: non-regular file; nothing else is known about this file
122+
123+
// Mask for the type bits. For regular files, none will be set.
124+
ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
125+
126+
ModePerm FileMode = 0777 // Unix permission bits
127+
)
128+
129+
// IsDir is a stub, always returning false
130+
func (m FileMode) IsDir() bool {
131+
return false
132+
}
133+
134+
// Stub constants
135+
const (
136+
O_RDONLY int = 1
137+
O_WRONLY int = 2
138+
O_RDWR int = 4
139+
O_APPEND int = 8
140+
O_CREATE int = 16
141+
O_EXCL int = 32
142+
O_SYNC int = 64
143+
O_TRUNC int = 128
144+
)
145+
146+
// A FileInfo describes a file and is returned by Stat and Lstat.
147+
type FileInfo interface {
148+
Name() string // base name of the file
149+
Size() int64 // length in bytes for regular files; system-dependent for others
150+
Mode() FileMode // file mode bits
151+
// ModTime() time.Time // modification time
152+
IsDir() bool // abbreviation for Mode().IsDir()
153+
Sys() interface{} // underlying data source (can return nil)
154+
}
155+
156+
// Stat is a stub, not yet implemented
157+
func Stat(name string) (FileInfo, error) {
158+
return nil, notImplemented
159+
}
160+
161+
// Lstat is a stub, not yet implemented
162+
func Lstat(name string) (FileInfo, error) {
163+
return nil, notImplemented
164+
}
165+
166+
// Getwd is a stub (for now), always returning an empty string
167+
func Getwd() (string, error) {
168+
return "", nil
169+
}
170+
171+
// Readlink is a stub (for now), always returning the string it was given
172+
func Readlink(name string) (string, error) {
173+
return name, nil
174+
}
175+
176+
// TempDir is a stub (for now), always returning the string "/tmp"
177+
func TempDir() string {
178+
return "/tmp"
179+
}
180+
181+
// Mkdir is a stub, not yet implemented
182+
func Mkdir(name string, perm FileMode) error {
183+
return notImplemented
184+
}
185+
186+
// IsExist is a stub (for now), always returning false
187+
func IsExist(err error) bool {
188+
return false
189+
}
190+
191+
// IsNotExist is a stub (for now), always returning false
192+
func IsNotExist(err error) bool {
193+
return false
194+
}
195+
196+
// Getpid is a stub (for now), always returning 1
197+
func Getpid() int {
198+
return 1
199+
}

src/os/file_other.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// Read is unsupported on this system.
1010
func (f *File) Read(b []byte) (n int, err error) {
11-
return 0, ErrUnsupported
11+
return 0, errUnsupported
1212
}
1313

1414
// Write writes len(b) bytes to the output. It returns the number of bytes
@@ -21,13 +21,13 @@ func (f *File) Write(b []byte) (n int, err error) {
2121
}
2222
return len(b), nil
2323
default:
24-
return 0, ErrUnsupported
24+
return 0, errUnsupported
2525
}
2626
}
2727

2828
// Close is unsupported on this system.
2929
func (f *File) Close() error {
30-
return ErrUnsupported
30+
return errUnsupported
3131
}
3232

3333
//go:linkname putchar runtime.putchar

0 commit comments

Comments
 (0)