@@ -10,49 +10,119 @@ import (
1010)
1111
1212// Portable analogs of some common system call errors.
13+ // Note that these are exported for use in the Filesystem interface.
1314var (
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" )
1619)
1720
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+ }
2548
2649// File represents an open file descriptor.
2750type 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
30111}
31112
32113// Readdir is a stub, not yet implemented
33114func (f * File ) Readdir (n int ) ([]FileInfo , error ) {
34- return nil , notImplemented
115+ return nil , & PathError { "readdir" , f . name , ErrNotImplemented }
35116}
36117
37118// Readdirnames is a stub, not yet implemented
38119func (f * File ) Readdirnames (n int ) (names []string , err error ) {
39- return nil , notImplemented
120+ return nil , & PathError { "readdirnames" , f . name , ErrNotImplemented }
40121}
41122
42123// Stat is a stub, not yet implemented
43124func (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 }
56126}
57127
58128const (
@@ -72,32 +142,8 @@ type PathError struct {
72142 Err error
73143}
74144
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 ()
101147}
102148
103149type FileMode uint32
@@ -155,12 +201,12 @@ type FileInfo interface {
155201
156202// Stat is a stub, not yet implemented
157203func Stat (name string ) (FileInfo , error ) {
158- return nil , notImplemented
204+ return nil , & PathError { "stat" , name , ErrNotImplemented }
159205}
160206
161207// Lstat is a stub, not yet implemented
162208func Lstat (name string ) (FileInfo , error ) {
163- return nil , notImplemented
209+ return nil , & PathError { "lstat" , name , ErrNotImplemented }
164210}
165211
166212// Getwd is a stub (for now), always returning an empty string
@@ -178,11 +224,6 @@ func TempDir() string {
178224 return "/tmp"
179225}
180226
181- // Mkdir is a stub, not yet implemented
182- func Mkdir (name string , perm FileMode ) error {
183- return notImplemented
184- }
185-
186227// IsExist is a stub (for now), always returning false
187228func IsExist (err error ) bool {
188229 return false
0 commit comments