@@ -10,49 +10,119 @@ import (
10
10
)
11
11
12
12
// Portable analogs of some common system call errors.
13
+ // Note that these are exported for use in the Filesystem interface.
13
14
var (
14
- errUnsupported = errors .New ("operation not supported" )
15
- notImplemented = errors .New ("os: not implemented" )
15
+ ErrUnsupported = errors .New ("operation not supported" )
16
+ ErrNotImplemented = errors .New ("operation not implemented" )
17
+ ErrNotExist = errors .New ("file not found" )
18
+ ErrExist = errors .New ("file exists" )
16
19
)
17
20
18
- // Stdin, Stdout, and Stderr are open Files pointing to the standard input,
19
- // standard output, and standard error file descriptors.
20
- var (
21
- Stdin = & File {0 , "/dev/stdin" }
22
- Stdout = & File {1 , "/dev/stdout" }
23
- Stderr = & File {2 , "/dev/stderr" }
24
- )
21
+ // Mkdir creates a directory. If the operation fails, it will return an error of
22
+ // type *PathError.
23
+ func Mkdir (path string , perm FileMode ) error {
24
+ fs , suffix := findMount (path )
25
+ if fs == nil {
26
+ return & PathError {"mkdir" , path , ErrNotExist }
27
+ }
28
+ err := fs .Mkdir (suffix , perm )
29
+ if err != nil {
30
+ return & PathError {"mkdir" , path , err }
31
+ }
32
+ return nil
33
+ }
34
+
35
+ // Remove removes a file or (empty) directory. If the operation fails, it will
36
+ // return an error of type *PathError.
37
+ func Remove (path string ) error {
38
+ fs , suffix := findMount (path )
39
+ if fs == nil {
40
+ return & PathError {"remove" , path , ErrNotExist }
41
+ }
42
+ err := fs .Remove (suffix )
43
+ if err != nil {
44
+ return & PathError {"remove" , path , err }
45
+ }
46
+ return nil
47
+ }
25
48
26
49
// File represents an open file descriptor.
27
50
type File struct {
28
- fd uintptr
29
- name string
51
+ handle FileHandle
52
+ name string
53
+ }
54
+
55
+ // Name returns the name of the file with which it was opened.
56
+ func (f * File ) Name () string {
57
+ return f .name
58
+ }
59
+
60
+ // OpenFile opens the named file. If the operation fails, the returned error
61
+ // will be of type *PathError.
62
+ func OpenFile (name string , flag int , perm FileMode ) (* File , error ) {
63
+ fs , suffix := findMount (name )
64
+ if fs == nil {
65
+ return nil , & PathError {"open" , name , ErrNotExist }
66
+ }
67
+ handle , err := fs .OpenFile (suffix , flag , perm )
68
+ if err != nil {
69
+ return nil , & PathError {"open" , name , err }
70
+ }
71
+ return & File {name : name , handle : handle }, nil
72
+ }
73
+
74
+ // Open opens the file named for reading.
75
+ func Open (name string ) (* File , error ) {
76
+ return OpenFile (name , O_RDONLY , 0 )
77
+ }
78
+
79
+ // Create creates the named file, overwriting it if it already exists.
80
+ func Create (name string ) (* File , error ) {
81
+ return OpenFile (name , O_RDWR | O_CREATE | O_TRUNC , 0666 )
82
+ }
83
+
84
+ // Read reads up to len(b) bytes from the File. It returns the number of bytes
85
+ // read and any error encountered. At end of file, Read returns 0, io.EOF.
86
+ func (f * File ) Read (b []byte ) (n int , err error ) {
87
+ n , err = f .handle .Read (b )
88
+ if err != nil {
89
+ err = & PathError {"read" , f .name , err }
90
+ }
91
+ return
92
+ }
93
+
94
+ // Write writes len(b) bytes to the File. It returns the number of bytes written
95
+ // and an error, if any. Write returns a non-nil error when n != len(b).
96
+ func (f * File ) Write (b []byte ) (n int , err error ) {
97
+ n , err = f .handle .Write (b )
98
+ if err != nil {
99
+ err = & PathError {"write" , f .name , err }
100
+ }
101
+ return
102
+ }
103
+
104
+ // Close closes the File, rendering it unusable for I/O.
105
+ func (f * File ) Close () (err error ) {
106
+ err = f .handle .Close ()
107
+ if err != nil {
108
+ err = & PathError {"close" , f .name , err }
109
+ }
110
+ return
30
111
}
31
112
32
113
// Readdir is a stub, not yet implemented
33
114
func (f * File ) Readdir (n int ) ([]FileInfo , error ) {
34
- return nil , notImplemented
115
+ return nil , & PathError { "readdir" , f . name , ErrNotImplemented }
35
116
}
36
117
37
118
// Readdirnames is a stub, not yet implemented
38
119
func (f * File ) Readdirnames (n int ) (names []string , err error ) {
39
- return nil , notImplemented
120
+ return nil , & PathError { "readdirnames" , f . name , ErrNotImplemented }
40
121
}
41
122
42
123
// Stat is a stub, not yet implemented
43
124
func (f * File ) Stat () (FileInfo , error ) {
44
- return nil , notImplemented
45
- }
46
-
47
- // NewFile returns a new File with the given file descriptor and name.
48
- func NewFile (fd uintptr , name string ) * File {
49
- return & File {fd , name }
50
- }
51
-
52
- // Fd returns the integer Unix file descriptor referencing the open file. The
53
- // file descriptor is valid only until f.Close is called.
54
- func (f * File ) Fd () uintptr {
55
- return f .fd
125
+ return nil , & PathError {"stat" , f .name , ErrNotImplemented }
56
126
}
57
127
58
128
const (
@@ -72,32 +142,8 @@ type PathError struct {
72
142
Err error
73
143
}
74
144
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 )
145
+ func (e * PathError ) Error () string {
146
+ return e .Op + " " + e .Path + ": " + e .Err .Error ()
101
147
}
102
148
103
149
type FileMode uint32
@@ -155,12 +201,12 @@ type FileInfo interface {
155
201
156
202
// Stat is a stub, not yet implemented
157
203
func Stat (name string ) (FileInfo , error ) {
158
- return nil , notImplemented
204
+ return nil , & PathError { "stat" , name , ErrNotImplemented }
159
205
}
160
206
161
207
// Lstat is a stub, not yet implemented
162
208
func Lstat (name string ) (FileInfo , error ) {
163
- return nil , notImplemented
209
+ return nil , & PathError { "lstat" , name , ErrNotImplemented }
164
210
}
165
211
166
212
// Getwd is a stub (for now), always returning an empty string
@@ -178,11 +224,6 @@ func TempDir() string {
178
224
return "/tmp"
179
225
}
180
226
181
- // Mkdir is a stub, not yet implemented
182
- func Mkdir (name string , perm FileMode ) error {
183
- return notImplemented
184
- }
185
-
186
227
// IsExist is a stub (for now), always returning false
187
228
func IsExist (err error ) bool {
188
229
return false
0 commit comments