@@ -4,54 +4,123 @@ import (
4
4
"errors"
5
5
"io"
6
6
"os"
7
+ "time"
7
8
)
8
9
9
10
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" )
12
13
ErrNotSupported = errors .New ("feature not supported" )
13
14
)
14
15
15
16
// 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.
30
19
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.
31
39
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.
32
43
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.
33
49
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.
35
52
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 {
36
88
// Lstat returns a FileInfo describing the named file. If the file is a
37
89
// 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.
39
92
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
47
93
// Symlink creates a symbolic-link from link to target. target may be an
48
94
// 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.
50
97
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 .
53
100
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
55
124
}
56
125
57
126
// File implements io.Closer, io.Reader, io.Seeker, and io.Writer>
0 commit comments