forked from openshift/origin
/
fs.go
114 lines (94 loc) · 2.76 KB
/
fs.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package util
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/openshift/source-to-image/pkg/sti/errors"
)
// FileSystem allows STI to work with the file system and
// perform tasks such as creating and deleting directories
type FileSystem interface {
Chmod(file string, mode os.FileMode) error
Rename(from, to string) error
MkdirAll(dirname string) error
Mkdir(dirname string) error
Exists(file string) bool
Copy(sourcePath, targetPath string) error
RemoveDirectory(dir string) error
CreateWorkingDirectory() (string, error)
Open(file string) (io.ReadCloser, error)
WriteFile(file string, data []byte) error
}
// NewFileSystem creates a new instance of the default FileSystem
// implementation
func NewFileSystem() FileSystem {
return &fs{
runner: NewCommandRunner(),
}
}
type fs struct {
runner CommandRunner
}
// Chmod sets the file mode
func (h *fs) Chmod(file string, mode os.FileMode) error {
return os.Chmod(file, mode)
}
// Rename renames or moves a file
func (h *fs) Rename(from, to string) error {
return os.Rename(from, to)
}
// MkdirAll creates the directory and all its parents
func (h *fs) MkdirAll(dirname string) error {
return os.MkdirAll(dirname, 0700)
}
// Mkdir creates the specified directory
func (h *fs) Mkdir(dirname string) error {
return os.Mkdir(dirname, 0700)
}
// Exists determines whether the given file exists
func (h *fs) Exists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
// Copy copies a set of files from sourcePath to targetPath
func (h *fs) Copy(sourcePath string, targetPath string) error {
info, err := os.Stat(sourcePath)
if err != nil {
return err
}
if !info.IsDir() {
err = os.Mkdir(targetPath, 0700)
if err != nil {
return err
}
targetPath = filepath.Join(targetPath, filepath.Base(sourcePath))
}
return h.runner.Run("cp", "-ad", sourcePath, targetPath)
}
// RemoveDirectory removes the specified directory and all its contents
func (h *fs) RemoveDirectory(dir string) error {
glog.V(2).Infof("Removing directory '%s'", dir)
err := os.RemoveAll(dir)
if err != nil {
glog.Errorf("Error removing directory '%s': %v", dir, err)
}
return err
}
// CreateWorkingDirectory creates a directory to be used for STI
func (h *fs) CreateWorkingDirectory() (directory string, err error) {
directory, err = ioutil.TempDir("", "sti")
if err != nil {
return "", errors.NewWorkDirError(directory, err)
}
return directory, err
}
// Open opens a file and returns a ReadCloser interface to that file
func (h *fs) Open(filename string) (io.ReadCloser, error) {
return os.Open(filename)
}
// Write opens a file and writes data to it, returning error if such occured
func (h *fs) WriteFile(filename string, data []byte) error {
return ioutil.WriteFile(filename, data, 0700)
}