/
sub_path.go
136 lines (122 loc) · 3.21 KB
/
sub_path.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
// Copyright 2022 Matrix Origin
//
// 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 fileservice
import (
"context"
"path"
"strings"
)
type subPathFS struct {
upstream FileService
path string
name string
}
// SubPath returns a FileService instance that operates at specified sub path of the upstream instance
func SubPath(upstream FileService, path string) FileService {
return &subPathFS{
upstream: upstream,
path: path,
name: strings.Join([]string{
"sub",
upstream.Name(),
path,
}, ","),
}
}
var _ FileService = new(subPathFS)
func (s *subPathFS) Name() string {
return s.name
}
func (s *subPathFS) toUpstreamPath(p string) (string, error) {
parsed, err := ParsePathAtService(p, s.name)
if err != nil {
return "", err
}
parsed.File = path.Join(s.path, parsed.File)
parsed.Service = s.upstream.Name()
parsed.ServiceArguments = nil
return parsed.String(), nil
}
func (s *subPathFS) Write(ctx context.Context, vector IOVector) error {
p, err := s.toUpstreamPath(vector.FilePath)
if err != nil {
return err
}
vector.FilePath = p
return s.upstream.Write(ctx, vector)
}
func (s *subPathFS) Read(ctx context.Context, vector *IOVector) error {
subVector := *vector
p, err := s.toUpstreamPath(subVector.FilePath)
if err != nil {
return err
}
subVector.FilePath = p
return s.upstream.Read(ctx, &subVector)
}
func (s *subPathFS) ReadCache(ctx context.Context, vector *IOVector) error {
subVector := *vector
p, err := s.toUpstreamPath(subVector.FilePath)
if err != nil {
return err
}
subVector.FilePath = p
return s.upstream.ReadCache(ctx, &subVector)
}
func (s *subPathFS) List(ctx context.Context, dirPath string) ([]DirEntry, error) {
p, err := s.toUpstreamPath(dirPath)
if err != nil {
return nil, err
}
entries, err := s.upstream.List(ctx, p)
if err != nil {
return nil, err
}
return entries, nil
}
func (s *subPathFS) Delete(ctx context.Context, filePaths ...string) error {
if len(filePaths) == 0 {
return nil
}
if len(filePaths) == 1 {
p, err := s.toUpstreamPath(filePaths[0])
if err != nil {
return err
}
return s.upstream.Delete(ctx, p)
}
subPaths := make([]string, 0, len(filePaths))
for _, p := range filePaths {
pp, err := s.toUpstreamPath(p)
if err != nil {
return err
}
subPaths = append(subPaths, pp)
}
return s.upstream.Delete(ctx, subPaths...)
}
func (s *subPathFS) StatFile(ctx context.Context, filePath string) (*DirEntry, error) {
p, err := s.toUpstreamPath(filePath)
if err != nil {
return nil, err
}
return s.upstream.StatFile(ctx, p)
}
func (s *subPathFS) PrefetchFile(ctx context.Context, filePath string) error {
p, err := s.toUpstreamPath(filePath)
if err != nil {
return err
}
return s.upstream.PrefetchFile(ctx, p)
}