-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.go
72 lines (61 loc) · 1.87 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package scrubber
import (
"fmt"
"os"
"path"
)
// Filesystem represents the minimal fs implementation we expect.
type Filesystem interface {
Name(file os.FileInfo) string
FullPath(file os.FileInfo, dir string) string
Remove(path string) error
Open(name string) (*os.File, error)
Create(name string) (*os.File, error)
Stat(name string) (os.FileInfo, error)
ListFiles(path string) ([]os.FileInfo, error)
Ext(file os.FileInfo) string
}
// OSFilesystem proxies calls to the underlying os and file library calls.
type OSFilesystem struct {
}
// Name returns the name of a file.
func (fs OSFilesystem) Name(file os.FileInfo) string {
return file.Name()
}
// FullPath combines a file's name, and it's path to a full path string.
func (fs OSFilesystem) FullPath(file os.FileInfo, dir string) string {
return dir + "/" + file.Name()
}
// Remove deletes a file from the filesystem.
func (fs OSFilesystem) Remove(path string) error {
return os.Remove(path)
}
// Open reads a file from the filesystem.
func (fs OSFilesystem) Open(name string) (*os.File, error) {
return os.Open(name)
}
// Create creates a file on the filesystem.
func (fs OSFilesystem) Create(name string) (*os.File, error) {
return os.Create(name)
}
// Stat returns information to a specific file.
func (fs OSFilesystem) Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
// Ext returns a file's extension.
func (fs OSFilesystem) Ext(file os.FileInfo) string {
return path.Ext(file.Name())
}
// ListFiles returns an os.FileInfo for every file in a directory.
func (fs OSFilesystem) ListFiles(path string) ([]os.FileInfo, error) {
d, err := os.Open(path)
defer d.Close()
if err != nil {
return nil, fmt.Errorf("failed to read directory %s: %s", path, err)
}
files, err := d.Readdir(-1)
if err != nil {
return nil, fmt.Errorf("failed to read files from directory %s: %s", path, err)
}
return files, nil
}