-
Notifications
You must be signed in to change notification settings - Fork 62
/
local.go
111 lines (99 loc) · 3.01 KB
/
local.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
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storage
import (
"io/ioutil"
"os"
pathutil "path"
"path/filepath"
)
// LocalFilesystemBackend is a storage backend for local filesystem storage
type LocalFilesystemBackend struct {
RootDirectory string
}
// NewLocalFilesystemBackend creates a new instance of LocalFilesystemBackend
func NewLocalFilesystemBackend(rootDirectory string) *LocalFilesystemBackend {
absPath, err := filepath.Abs(rootDirectory)
if err != nil {
panic(err)
}
b := &LocalFilesystemBackend{RootDirectory: absPath}
return b
}
// ListObjects lists all objects in root directory (depth 1)
func (b LocalFilesystemBackend) ListObjects(prefix string) ([]Object, error) {
var objects []Object
files, err := ioutil.ReadDir(pathutil.Join(b.RootDirectory, prefix))
if err != nil {
if os.IsNotExist(err) { // OK if the directory doesnt exist yet
err = nil
}
return objects, err
}
for _, f := range files {
if f.IsDir() {
continue
}
object := Object{Path: f.Name(), Content: []byte{}, LastModified: f.ModTime()}
objects = append(objects, object)
}
return objects, nil
}
// GetObject retrieves an object from root directory
func (b LocalFilesystemBackend) GetObject(path string) (Object, error) {
var object Object
object.Path = path
fullpath := pathutil.Join(b.RootDirectory, path)
content, err := ioutil.ReadFile(fullpath)
if err != nil {
return object, err
}
object.Content = content
info, err := os.Stat(fullpath)
if err != nil {
return object, err
}
object.LastModified = info.ModTime()
return object, err
}
// PutObject puts an object in root directory
func (b LocalFilesystemBackend) PutObject(path string, content []byte) error {
fullpath := pathutil.Join(b.RootDirectory, path)
folderPath := pathutil.Dir(fullpath)
_, err := os.Stat(folderPath)
if err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(folderPath, 0774)
if err != nil {
return err
}
// os.MkdirAll set the dir permissions before the umask
// we need to use os.Chmod to ensure the permissions of the created directory are 774
// because the default umask will prevent that and cause the permissions to be 755
err = os.Chmod(folderPath, 0774)
if err != nil {
return err
}
} else {
return err
}
}
err = ioutil.WriteFile(fullpath, content, 0644)
return err
}
// DeleteObject removes an object from root directory
func (b LocalFilesystemBackend) DeleteObject(path string) error {
fullpath := pathutil.Join(b.RootDirectory, path)
err := os.Remove(fullpath)
return err
}