Skip to content

Commit ad8207a

Browse files
committed
new Filesystem interfaces
1 parent 51f7830 commit ad8207a

File tree

2 files changed

+102
-34
lines changed

2 files changed

+102
-34
lines changed

fs.go

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,123 @@ import (
44
"errors"
55
"io"
66
"os"
7+
"time"
78
)
89

910
var (
10-
ErrClosed = errors.New("file: Writing on closed file.")
11-
ErrReadOnly = errors.New("this is a read-only filesystem")
11+
ErrClosed = errors.New("writing on closed file")
12+
ErrReadOnly = errors.New("read-only filesystem")
1213
ErrNotSupported = errors.New("feature not supported")
1314
)
1415

1516
// Filesystem abstract the operations in a storage-agnostic interface.
16-
// It allows you to:
17-
// * Create files.
18-
// * Open existing files.
19-
// * Get info about files.
20-
// * List files in a directory.
21-
// * Get a temporal file.
22-
// * Rename files.
23-
// * Remove files.
24-
// * Create directories.
25-
// * Join parts of path.
26-
// * Obtain a filesystem starting on a subdirectory in the current filesystem.
27-
// * Get the base path for the filesystem.
28-
// Each method implementation varies from implementation to implementation. Refer to
29-
// the specific documentation for more info.
17+
// Each method implementation mimics the behavior of the equivalent functions
18+
// at the os package from the standard library.
3019
type Filesystem interface {
20+
Basic
21+
Dir
22+
Symlink
23+
TempFile
24+
25+
// Dir returns a new Filesystem from the same type of fs using as baseDir the
26+
// given path
27+
Dir(path string) Filesystem
28+
// Base returns the base path of the filesystem
29+
Base() string
30+
}
31+
32+
// Basic abstract the basic operations in a storage-agnostic interface as
33+
// an extension to the Basic interface
34+
type Basic interface {
35+
// Create creates the named file with mode 0666 (before umask), truncating
36+
// it if it already exists. If successful, methods on the returned File can
37+
// be used for I/O; the associated file descriptor has mode O_RDWR. If there
38+
// is an error, it will be of type *PathError.
3139
Create(filename string) (File, error)
40+
// Open opens the named file for reading. If successful, methods on the
41+
// returned file can be used for reading; the associated file descriptor has
42+
// mode O_RDONLY. If there is an error, it will be of type *PathError.
3243
Open(filename string) (File, error)
44+
// OpenFile is the generalized open call; most users will use Open or Create
45+
// instead. It opens the named file with specified flag (O_RDONLY etc.) and
46+
// perm, (0666 etc.) if applicable. If successful, methods on the returned
47+
// File can be used for I/O. If there is an error, it will be of type
48+
// *PathError.
3349
OpenFile(filename string, flag int, perm os.FileMode) (File, error)
34-
// Stat returns a FileInfo describing the named file.
50+
// Stat returns a FileInfo describing the named file. If there is an error,
51+
// it will be of type *PathError.
3552
Stat(filename string) (FileInfo, error)
53+
// Rename renames (moves) oldpath to newpath. If newpath already exists and
54+
// is not a directory, Rename replaces it. OS-specific restrictions may
55+
// apply when oldpath and newpath are in different directories. If there is
56+
// an error, it will be of type *LinkError.
57+
Rename(oldpath, newpath string) error
58+
// Remove removes the named file or directory. If there is an error, it will
59+
// be of type *PathError.
60+
Remove(filename string) error
61+
// Join joins any number of path elements into a single path, adding a
62+
// Separator if necessary. Join calls filepath.Clean on the result; in
63+
// particular, all empty strings are ignored. On Windows, the result is a
64+
// UNC path if and only if the first path element is a UNC path.
65+
Join(elem ...string) string
66+
}
67+
68+
type TempFile interface {
69+
// TempDir returns the default directory to use for temporary files.
70+
//TempDir() string
71+
TempFile(dir, prefix string) (File, error)
72+
}
73+
74+
// Dir abstract the dir related operations in a storage-agnostic interface as
75+
// an extension to the Basic interface.
76+
type Dir interface {
77+
ReadDir(path string) ([]FileInfo, error)
78+
// MkdirAll creates a directory named path, along with any necessary
79+
// parents, and returns nil, or else returns an error. The permission bits
80+
// perm are used for all directories that MkdirAll creates. If path is/
81+
// already a directory, MkdirAll does nothing and returns nil.
82+
MkdirAll(filename string, perm os.FileMode) error
83+
}
84+
85+
// Symlink abstract the symlink related operations in a storage-agnostic
86+
// interface as an extension to the Basic interface.
87+
type Symlink interface {
3688
// Lstat returns a FileInfo describing the named file. If the file is a
3789
// symbolic link, the returned FileInfo describes the symbolic link. Lstat
38-
// makes no attempt to follow the link.
90+
// makes no attempt to follow the link. If there is an error, it will be of
91+
// type *PathError.
3992
Lstat(filename string) (FileInfo, error)
40-
ReadDir(path string) ([]FileInfo, error)
41-
TempFile(dir, prefix string) (File, error)
42-
Rename(from, to string) error
43-
Remove(filename string) error
44-
MkdirAll(filename string, perm os.FileMode) error
45-
Join(elem ...string) string
46-
Dir(path string) Filesystem
4793
// Symlink creates a symbolic-link from link to target. target may be an
4894
// absolute or relative path, and need not refer to an existing node.
49-
// Parent directories of link are created as necessary.
95+
// Parent directories of link are created as necessary. If there is an
96+
// error, it will be of type *LinkError.
5097
Symlink(target, link string) error
51-
// Readlink returns the target path of link. An error is returned if link is
52-
// not a symbolic-link.
98+
// Readlink returns the target path of link. If there is an error, it will
99+
// be of type *PathError.
53100
Readlink(link string) (string, error)
54-
Base() string
101+
}
102+
103+
// Change abstract the FileInfo change related operations in a storage-agnostic
104+
// interface as an extension to the Basic interface
105+
type Change interface {
106+
// Chmod changes the mode of the named file to mode. If the file is a
107+
// symbolic link, it changes the mode of the link's target. If there is an
108+
// error, it will be of type *PathError.
109+
Chmod(name string, mode os.FileMode) error
110+
// Lchown changes the numeric uid and gid of the named file. If the file is
111+
// a symbolic link, it changes the uid and gid of the link itself. If there
112+
// is an error, it will be of type *PathError.
113+
Lchown(name string, uid, gid int) error
114+
// Chown changes the numeric uid and gid of the named file. If the file is a
115+
// symbolic link, it changes the uid and gid of the link's target. If there
116+
// is an error, it will be of type *PathError.
117+
Chown(name string, uid, gid int) error
118+
// Chtimes changes the access and modification times of the named file,
119+
// similar to the Unix utime() or utimes() functions.
120+
//
121+
// The underlying filesystem may truncate or round the values to a less
122+
// precise time unit. If there is an error, it will be of type *PathError.
123+
Chtimes(name string, atime time.Time, mtime time.Time) error
55124
}
56125

57126
// File implements io.Closer, io.Reader, io.Seeker, and io.Writer>

utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"os"
66
)
77

8-
// RemoveAll removes path and any children it contains.
9-
// It removes everything it can but returns the first error
10-
// it encounters. If the path does not exist, RemoveAll
11-
// returns nil (no error).
8+
// RemoveAll removes path and any children it contains. It removes everything it
9+
// can but returns the first error it encounters. If the path does not exist,
10+
// RemoveAll returns nil (no error).
1211
func RemoveAll(fs Filesystem, path string) error {
1312
r, ok := fs.(removerAll)
1413
if ok {
@@ -81,7 +80,7 @@ func removeAll(fs Filesystem, path string) error {
8180
// WriteFile writes data to a file named by filename in the given filesystem.
8281
// If the file does not exist, WriteFile creates it with permissions perm;
8382
// otherwise WriteFile truncates it before writing.
84-
func WriteFile(fs Filesystem, filename string, data []byte, perm os.FileMode) error {
83+
func WriteFile(fs Basic, filename string, data []byte, perm os.FileMode) error {
8584
f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
8685
if err != nil {
8786
return err

0 commit comments

Comments
 (0)