-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_hsds_storer.go
143 lines (127 loc) · 3 KB
/
fs_hsds_storer.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
// Copyright 2022 UL Method Park GmbH. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
type pathError struct {
path string
}
func (err *pathError) Error() string {
return fmt.Sprintf("filesystem: '%s' is not a valid filename", err.path)
}
// filesystemHSDSStorer is an implementation of the DomainStorer and
// ObjectStorer interfaces that uses the local filesystem as its underlying
// storage.
type filesystemHSDSStorer struct {
// Root is the storer's root directory. All domains and domain objects
// stored by the storer will reside in this directory.
Root string
}
func sanitizePath(root, name string) (string, error) {
name = filepath.Join("/", filepath.FromSlash(name))
if name == "/" {
return "", &pathError{path: name}
}
name = filepath.Join(root, name)
return name, nil
}
func openForWriting(root, name string) (io.WriteCloser, error) {
name, err := sanitizePath(root, name)
if err != nil {
return nil, err
}
f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
return f, err
}
func createParentDomains(root, name string, domain *hsdsDomain) error {
name = filepath.Clean(name)
if name == "." {
return nil
}
dirName, err := sanitizePath(root, name)
if err != nil {
return err
}
err = os.MkdirAll(dirName, 0744)
if err != nil {
return err
}
parentDir, _ := filepath.Split(name)
parentDirs := filepath.SplitList(parentDir)
// Directory domains do not have a root group.
parent := *domain
parent.Root = nil
dn := root
for _, subDir := range parentDirs {
dn = filepath.Join(dn, subDir, ".domain.json")
f, err := os.OpenFile(dn, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0644)
// We only create domain files for parent directories that do not already exist.
if errors.Is(err, os.ErrExist) {
continue
} else if err != nil {
return err
}
enc := json.NewEncoder(f)
err = enc.Encode(parent)
if err != nil {
f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
}
return nil
}
func (s *filesystemHSDSStorer) StoreDomain(ctx context.Context, name string, domain *hsdsDomain) error {
err := createParentDomains(s.Root, name, domain)
if err != nil {
return err
}
name = filepath.Join(name, ".domain.json")
f, err := openForWriting(s.Root, name)
enc := json.NewEncoder(f)
err = enc.Encode(domain)
if err != nil {
f.Close()
return err
}
return f.Close()
}
func (s *filesystemHSDSStorer) StoreObject(ctx context.Context, name string, data []byte) error {
dir, err := sanitizePath(s.Root, name)
if err != nil {
return err
}
dir, _ = filepath.Split(dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
f, err := openForWriting(s.Root, name)
if err != nil {
return err
}
_, err = f.Write(data)
if err != nil {
f.Close()
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}