Debian Codegraph @

dir/fs.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package dir
15
16 import (
17 "io/fs"
18 "os"
19 "path/filepath"
20 )
21
22 // SysFS is virtual file system interface that support fs.FS and SysPath method.
23 type SysFS interface {
24 fs.FS
25
26 // SysPath returns the real system path of the given path items in the SysFS.
27 SysPath(items ...string) (string, error)
28 }
29
30 type sysFS struct {
31 fs.FS
32 root string
33 }
34
35 // SysPath returns the real system path of the given name in the SysFS.
36 func (s sysFS) SysPath(items ...string) (string, error) {
37 pathItems := []string{s.root}
38 pathItems = append(pathItems, items...)
39 return filepath.Join(pathItems...), nil
40 }
41
42 // NewSysFS returns the SysFS for the given root directory.
43 //
44 // Support one root directory for rc.1, and may support union directories FS
45 // after rc.1.
46 func NewSysFS(root string) SysFS {
47 return sysFS{
48 FS: os.DirFS(root),
49 root: root}
50 }
51
52 // ConfigFS is the config SysFS
53 func ConfigFS() SysFS {
54 return NewSysFS(userConfigDirPath())
55 }
56
57 // PluginFS is the plugin SysFS
58 func PluginFS() SysFS {
59 return NewSysFS(filepath.Join(userLibexecDirPath(), PathPlugins))
60 }
61
62 // CacheFS is the cache SysFS.
63 //
64 // To get the root of crl file cache, use `CacheFS().SysFS(PathCRLCache)`.
65 func CacheFS() SysFS {
66 return NewSysFS(userCacheDirPath())
67 }