-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfile_utils.go
65 lines (51 loc) · 1.49 KB
/
file_utils.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package checks
import (
"fmt"
"os"
"path/filepath"
"github.com/DataDog/datadog-agent/pkg/compliance/checks/env"
"github.com/DataDog/datadog-agent/pkg/compliance/eval"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
type pathMapper struct {
hostMountPath string
}
func (m pathMapper) normalizeToHostRoot(path string) string {
return filepath.Join(m.hostMountPath, path)
}
func (m pathMapper) relativeToHostRoot(path string) string {
if filepath.HasPrefix(path, m.hostMountPath) {
p, err := filepath.Rel(m.hostMountPath, path)
if err != nil {
log.Warnf("Unable to return original path for: %s", path)
return path
}
return string(os.PathSeparator) + p
}
return path
}
func resolvePath(e env.Env, path string) (string, error) {
pathExpr, err := eval.Cache.ParsePath(path)
if err != nil {
return "", err
}
if pathExpr.Path != nil {
return *pathExpr.Path, nil
}
v, err := e.EvaluateFromCache(pathExpr.Expression)
if err != nil {
return "", fmt.Errorf("failed to resolve path: %w", err)
}
res, ok := v.(string)
if !ok {
return "", fmt.Errorf(`failed to resolve path: expected string from %s got "%v"`, path, v)
}
if res == "" {
return "", fmt.Errorf("failed to resolve path: empty path from %s", path)
}
return res, nil
}