-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder.go
170 lines (150 loc) · 4.56 KB
/
folder.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package fs
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"github.com/wal-g/tracelog"
"github.com/apecloud/dataprotection-wal-g/pkg/storages/storage"
)
const dirDefaultMode = 0755
func NewError(err error, format string, args ...interface{}) storage.Error {
return storage.NewError(err, "FS", format, args...)
}
// Folder represents folder of file system
type Folder struct {
rootPath string
subpath string
}
func NewFolder(rootPath string, subPath string) *Folder {
return &Folder{rootPath, subPath}
}
func NewFolderError(err error, format string, args ...interface{}) storage.Error {
return storage.NewError(err, "GCS", format, args...)
}
func ConfigureFolder(path string, settings map[string]string) (storage.Folder, error) {
if _, err := os.Stat(path); err != nil {
return nil, NewError(err, "Folder not exists or is inaccessible")
}
return NewFolder(path, ""), nil
}
func (folder *Folder) GetPath() string {
return folder.subpath
}
func (folder *Folder) ListFolder() (objects []storage.Object, subFolders []storage.Folder, err error) {
files, err := ioutil.ReadDir(path.Join(folder.rootPath, folder.subpath))
if err != nil {
return nil, nil, NewError(err, "Unable to read folder")
}
for _, fileInfo := range files {
if fileInfo.IsDir() {
// I do not use GetSubfolder() intentially
subPath := path.Join(folder.subpath, fileInfo.Name()) + "/"
subFolders = append(subFolders, NewFolder(folder.rootPath, subPath))
} else {
objects = append(objects, storage.NewLocalObject(fileInfo.Name(), fileInfo.ModTime(), fileInfo.Size()))
}
}
return
}
func (folder *Folder) DeleteObjects(objectRelativePaths []string) error {
for _, fileName := range objectRelativePaths {
err := os.RemoveAll(folder.GetFilePath(fileName))
if os.IsNotExist(err) {
continue
}
if err != nil {
return NewError(err, "Unable to delete object %v", fileName)
}
}
return nil
}
func (folder *Folder) Exists(objectRelativePath string) (bool, error) {
_, err := os.Stat(folder.GetFilePath(objectRelativePath))
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, NewError(err, "Unable to stat object %v", objectRelativePath)
}
return true, nil
}
func (folder *Folder) GetSubFolder(subFolderRelativePath string) storage.Folder {
sf := Folder{folder.rootPath, path.Join(folder.subpath, subFolderRelativePath)}
_ = sf.EnsureExists()
// This is something unusual when we cannot be sure that our subfolder exists in FS
// But we do not have to guarantee folder persistence, but any subsequent calls will fail
// Just like in all other Storage Folders
return &sf
}
func (folder *Folder) ReadObject(objectRelativePath string) (io.ReadCloser, error) {
filePath := folder.GetFilePath(objectRelativePath)
file, err := os.Open(filePath)
if os.IsNotExist(err) {
return nil, storage.NewObjectNotFoundError(filePath)
}
if err != nil {
return nil, NewError(err, "Unable to read object %v", filePath)
}
return file, nil
}
func (folder *Folder) PutObject(name string, content io.Reader) error {
tracelog.DebugLogger.Printf("Put %v into %v\n", name, folder.subpath)
filePath := folder.GetFilePath(name)
file, err := OpenFileWithDir(filePath)
if err != nil {
return NewError(err, "Unable to open file %v", filePath)
}
_, err = io.Copy(file, content)
if err != nil {
closerErr := file.Close()
if closerErr != nil {
tracelog.InfoLogger.Println("Error during closing failed upload ", closerErr)
}
return NewError(err, "Unable to copy data to %v", filePath)
}
err = file.Close()
if err != nil {
return NewError(err, "Unable to close %v", filePath)
}
return nil
}
func (folder *Folder) CopyObject(srcPath string, dstPath string) error {
src := path.Join(folder.rootPath, srcPath)
srcStat, err := os.Stat(src)
if err != nil {
return err
}
if !srcStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", srcPath)
}
file, err := os.Open(src)
if err != nil {
return err
}
err = folder.PutObject(dstPath, file)
return err
}
func OpenFileWithDir(filePath string) (*os.File, error) {
file, err := os.Create(filePath)
if os.IsNotExist(err) {
err = os.MkdirAll(path.Dir(filePath), dirDefaultMode)
if err != nil {
return nil, err
}
file, err = os.Create(filePath)
}
return file, err
}
func (folder *Folder) GetFilePath(objectRelativePath string) string {
return path.Join(folder.rootPath, folder.subpath, objectRelativePath)
}
func (folder *Folder) EnsureExists() error {
dirname := path.Join(folder.rootPath, folder.subpath)
_, err := os.Stat(dirname)
if os.IsNotExist(err) {
return os.MkdirAll(dirname, dirDefaultMode)
}
return err
}