@@ -4,82 +4,135 @@ 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 " )
12
- ErrNotSupported = errors .New ("feature not supported " )
11
+ ErrReadOnly = errors .New ("read-only filesystem " )
12
+ ErrNotSupported = errors .New ("feature not supported " )
13
+ ErrCrossedBoundary = errors .New ("chroot boundary crossed " )
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
+ TempFile
22
+ Dir
23
+ Symlink
24
+ Chroot
25
+ }
26
+
27
+ // Basic abstract the basic operations in a storage-agnostic interface as
28
+ // an extension to the Basic interface.
29
+ type Basic interface {
30
+ // Create creates the named file with mode 0666 (before umask), truncating
31
+ // it if it already exists. If successful, methods on the returned File can
32
+ // be used for I/O; the associated file descriptor has mode O_RDWR.
31
33
Create (filename string ) (File , error )
34
+ // Open opens the named file for reading. If successful, methods on the
35
+ // returned file can be used for reading; the associated file descriptor has
36
+ // mode O_RDONLY.
32
37
Open (filename string ) (File , error )
38
+ // OpenFile is the generalized open call; most users will use Open or Create
39
+ // instead. It opens the named file with specified flag (O_RDONLY etc.) and
40
+ // perm, (0666 etc.) if applicable. If successful, methods on the returned
41
+ // File can be used for I/O.
33
42
OpenFile (filename string , flag int , perm os.FileMode ) (File , error )
34
43
// Stat returns a FileInfo describing the named file.
35
- Stat (filename string ) (FileInfo , error )
44
+ Stat (filename string ) (os.FileInfo , error )
45
+ // Rename renames (moves) oldpath to newpath. If newpath already exists and
46
+ // is not a directory, Rename replaces it. OS-specific restrictions may
47
+ // apply when oldpath and newpath are in different directories.
48
+ Rename (oldpath , newpath string ) error
49
+ // Remove removes the named file or directory.
50
+ Remove (filename string ) error
51
+ // Join joins any number of path elements into a single path, adding a
52
+ // Separator if necessary. Join calls filepath.Clean on the result; in
53
+ // particular, all empty strings are ignored. On Windows, the result is a
54
+ // UNC path if and only if the first path element is a UNC path.
55
+ Join (elem ... string ) string
56
+ }
57
+
58
+ type TempFile interface {
59
+ // TempFile creates a new temporary file in the directory dir with a name
60
+ // beginning with prefix, opens the file for reading and writing, and
61
+ // returns the resulting *os.File. If dir is the empty string, TempFile
62
+ // uses the default directory for temporary files (see os.TempDir).
63
+ // Multiple programs calling TempFile simultaneously will not choose the
64
+ // same file. The caller can use f.Name() to find the pathname of the file.
65
+ // It is the caller's responsibility to remove the file when no longer
66
+ // needed.
67
+ TempFile (dir , prefix string ) (File , error )
68
+ }
69
+
70
+ // Dir abstract the dir related operations in a storage-agnostic interface as
71
+ // an extension to the Basic interface.
72
+ type Dir interface {
73
+ // ReadDir reads the directory named by dirname and returns a list of
74
+ // directory entries sorted by filename.
75
+ ReadDir (path string ) ([]os.FileInfo , error )
76
+ // MkdirAll creates a directory named path, along with any necessary
77
+ // parents, and returns nil, or else returns an error. The permission bits
78
+ // perm are used for all directories that MkdirAll creates. If path is/
79
+ // already a directory, MkdirAll does nothing and returns nil.
80
+ MkdirAll (filename string , perm os.FileMode ) error
81
+ }
82
+
83
+ // Symlink abstract the symlink related operations in a storage-agnostic
84
+ // interface as an extension to the Basic interface.
85
+ type Symlink interface {
36
86
// Lstat returns a FileInfo describing the named file. If the file is a
37
87
// symbolic link, the returned FileInfo describes the symbolic link. Lstat
38
88
// makes no attempt to follow the link.
39
- 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
89
+ Lstat (filename string ) (os.FileInfo , error )
47
90
// Symlink creates a symbolic-link from link to target. target may be an
48
91
// absolute or relative path, and need not refer to an existing node.
49
92
// Parent directories of link are created as necessary.
50
93
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.
94
+ // Readlink returns the target path of link.
53
95
Readlink (link string ) (string , error )
54
- Base () string
55
96
}
56
97
57
- // File implements io.Closer, io.Reader, io.Seeker, and io.Writer>
58
- // Provides method to obtain the file name and the state of the file (open or closed).
98
+ // Change abstract the FileInfo change related operations in a storage-agnostic
99
+ // interface as an extension to the Basic interface
100
+ type Change interface {
101
+ // Chmod changes the mode of the named file to mode. If the file is a
102
+ // symbolic link, it changes the mode of the link's target.
103
+ Chmod (name string , mode os.FileMode ) error
104
+ // Lchown changes the numeric uid and gid of the named file. If the file is
105
+ // a symbolic link, it changes the uid and gid of the link itself.
106
+ Lchown (name string , uid , gid int ) error
107
+ // Chown changes the numeric uid and gid of the named file. If the file is a
108
+ // symbolic link, it changes the uid and gid of the link's target.
109
+ Chown (name string , uid , gid int ) error
110
+ // Chtimes changes the access and modification times of the named file,
111
+ // similar to the Unix utime() or utimes() functions.
112
+ //
113
+ // The underlying filesystem may truncate or round the values to a less
114
+ // precise time unit.
115
+ Chtimes (name string , atime time.Time , mtime time.Time ) error
116
+ }
117
+
118
+ // Chroot abstract the chroot related operations in a storage-agnostic interface
119
+ // as an extension to the Basic interface.
120
+ type Chroot interface {
121
+ // Chroot returns a new filesystem from the same type where the new root is
122
+ // the given path. Files outside of the designated directory tree cannot be
123
+ // accessed.
124
+ Chroot (path string ) (Basic , error )
125
+ // Root returns the root path of the filesystem.
126
+ Root () string
127
+ }
128
+
129
+ // File represent a file, being a subset of the os.File
59
130
type File interface {
60
- Filename () string
61
- IsClosed () bool
131
+ // Name returns the name of the file as presented to Open.
132
+ Name () string
62
133
io.Writer
63
134
io.Reader
135
+ io.ReaderAt
64
136
io.Seeker
65
137
io.Closer
66
138
}
67
-
68
- type FileInfo os.FileInfo
69
-
70
- type BaseFile struct {
71
- BaseFilename string
72
- Closed bool
73
- }
74
-
75
- func (f * BaseFile ) Filename () string {
76
- return f .BaseFilename
77
- }
78
-
79
- func (f * BaseFile ) IsClosed () bool {
80
- return f .Closed
81
- }
82
-
83
- type removerAll interface {
84
- RemoveAll (string ) error
85
- }
0 commit comments