summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/src-d/go-billy.v4/helper/polyfill/polyfill.go
blob: f613c255d9c167ba3374e1f5dd5d85e5c6cff311 (plain)
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
package polyfill

import (
	"os"
	"path/filepath"

	"gopkg.in/src-d/go-billy.v4"
)

// Polyfill is a helper that implements all missing method from billy.Filesystem.
type Polyfill struct {
	billy.Basic
	c capabilities
}

type capabilities struct{ tempfile, dir, symlink, chroot bool }

// New creates a new filesystem wrapping up 'fs' the intercepts all the calls
// made and errors if fs doesn't implement any of the billy interfaces.
func New(fs billy.Basic) billy.Filesystem {
	if original, ok := fs.(billy.Filesystem); ok {
		return original
	}

	h := &Polyfill{Basic: fs}

	_, h.c.tempfile = h.Basic.(billy.TempFile)
	_, h.c.dir = h.Basic.(billy.Dir)
	_, h.c.symlink = h.Basic.(billy.Symlink)
	_, h.c.chroot = h.Basic.(billy.Chroot)
	return h
}

func (h *Polyfill) TempFile(dir, prefix string) (billy.File, error) {
	if !h.c.tempfile {
		return nil, billy.ErrNotSupported
	}

	return h.Basic.(billy.TempFile).TempFile(dir, prefix)
}

func (h *Polyfill) ReadDir(path string) ([]os.FileInfo, error) {
	if !h.c.dir {
		return nil, billy.ErrNotSupported
	}

	return h.Basic.(billy.Dir).ReadDir(path)
}

func (h *Polyfill) MkdirAll(filename string, perm os.FileMode) error {
	if !h.c.dir {
		return billy.ErrNotSupported
	}

	return h.Basic.(billy.Dir).MkdirAll(filename, perm)
}

func (h *Polyfill) Symlink(target, link string) error {
	if !h.c.symlink {
		return billy.ErrNotSupported
	}

	return h.Basic.(billy.Symlink).Symlink(target, link)
}

func (h *Polyfill) Readlink(link string) (string, error) {
	if !h.c.symlink {
		return "", billy.ErrNotSupported
	}

	return h.Basic.(billy.Symlink).Readlink(link)
}

func (h *Polyfill) Lstat(path string) (os.FileInfo, error) {
	if !h.c.symlink {
		return nil, billy.ErrNotSupported
	}

	return h.Basic.(billy.Symlink).Lstat(path)
}

func (h *Polyfill) Chroot(path string) (billy.Filesystem, error) {
	if !h.c.chroot {
		return nil, billy.ErrNotSupported
	}

	return h.Basic.(billy.Chroot).Chroot(path)
}

func (h *Polyfill) Root() string {
	if !h.c.chroot {
		return string(filepath.Separator)
	}

	return h.Basic.(billy.Chroot).Root()
}

func (h *Polyfill) Underlying() billy.Basic {
	return h.Basic
}

// Capabilities implements the Capable interface.
func (h *Polyfill) Capabilities() billy.Capability {
	return billy.Capabilities(h.Basic)
}