-
Notifications
You must be signed in to change notification settings - Fork 405
/
iofs.go
82 lines (67 loc) · 1.6 KB
/
iofs.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
package agent
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/fnproject/fn/api/common"
)
type iofs interface {
io.Closer
AgentPath() string
DockerPath() string
}
type noopIOFS struct {
}
func (n *noopIOFS) AgentPath() string {
return ""
}
func (n *noopIOFS) DockerPath() string {
return ""
}
func (n *noopIOFS) Close() error {
return nil
}
type directoryIOFS struct {
agentPath string
dockerPath string
}
func (d *directoryIOFS) AgentPath() string {
return d.agentPath
}
func (d *directoryIOFS) DockerPath() string {
return d.dockerPath
}
func (d *directoryIOFS) Close() error {
err := os.RemoveAll(d.agentPath)
if err != nil {
return err
}
return nil
}
func newDirectoryIOFS(ctx context.Context, cfg *Config) (*directoryIOFS, error) {
dir := cfg.IOFSAgentPath
// create a tmpdir
iofsAgentDir, err := ioutil.TempDir(dir, "iofs")
if err != nil {
if err := os.RemoveAll(iofsAgentDir); err != nil {
common.Logger(ctx).WithError(err).Error("failed to clean up iofs dir")
}
return nil, fmt.Errorf("cannot create tmpdir for iofs: %v", err)
}
if cfg.IOFSMountRoot != "" {
iofsRelPath, err := filepath.Rel(dir, iofsAgentDir)
if err != nil {
if err := os.RemoveAll(iofsAgentDir); err != nil {
common.Logger(ctx).WithError(err).Error("failed to clean up iofs dir")
}
return nil, fmt.Errorf("cannot relativise iofs path: %v", err)
}
iofsDockerDir := filepath.Join(cfg.IOFSMountRoot, iofsRelPath)
return &directoryIOFS{iofsAgentDir, iofsDockerDir}, nil
}
return &directoryIOFS{iofsAgentDir, iofsAgentDir}, nil
}
var _ iofs = &directoryIOFS{}