@@ -11,7 +11,8 @@ import (
11
11
12
12
// Portable analogs of some common system call errors.
13
13
var (
14
- ErrUnsupported = errors .New ("operation not supported" )
14
+ errUnsupported = errors .New ("operation not supported" )
15
+ notImplemented = errors .New ("os: not implemented" )
15
16
)
16
17
17
18
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
@@ -28,6 +29,21 @@ type File struct {
28
29
name string
29
30
}
30
31
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
+
31
47
// NewFile returns a new File with the given file descriptor and name.
32
48
func NewFile (fd uintptr , name string ) * File {
33
49
return & File {fd , name }
@@ -38,3 +54,146 @@ func NewFile(fd uintptr, name string) *File {
38
54
func (f * File ) Fd () uintptr {
39
55
return f .fd
40
56
}
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
+ }
0 commit comments