-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.go
More file actions
117 lines (90 loc) · 2.47 KB
/
zip.go
File metadata and controls
117 lines (90 loc) · 2.47 KB
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
package fs
import (
"archive/zip"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
fsCtx "github.com/nhatthm/plugin-registry/context"
"github.com/nhatthm/plugin-registry/installer"
"github.com/nhatthm/plugin-registry/plugin"
"github.com/spf13/afero"
)
// ErrPluginNotZip indicates that the plugin is not a zip.
var ErrPluginNotZip = errors.New("plugin is not a zip")
func init() { //nolint: gochecknoinits
installer.Register("zip", isZipPlugin, func(fs afero.Fs) installer.Installer {
return NewZipInstaller(fs)
})
}
// NewZipInstaller creates a new filesystem installer.
func NewZipInstaller(fs afero.Fs) *ArchiveInstaller {
i := &ArchiveInstaller{
fs: fs,
parseURL: parseZipPath,
install: installZip,
}
return i
}
func isZipPlugin(ctx context.Context, path string) bool { //nolint: contextcheck,nolintlint
_, _, err := parseZipPath(fsCtx.Fs(ctx), path) //nolint: contextcheck,nolintlint
return err == nil
}
func parseZipPath(fs afero.Fs, path string) (string, string, error) { //nolint: contextcheck,nolintlint
fi, err := statPlugin(fs, path)
if err != nil {
return "", "", err
}
if filepath.Ext(fi.Name()) != ".zip" {
return "", "", ErrPluginNotZip
}
metadataPath := filepath.Dir(path)
metadataFile := filepath.Join(metadataPath, plugin.MetadataFile)
if _, err := fs.Stat(metadataFile); err != nil {
return "", "", metadataError(err, metadataFile)
}
return path, metadataPath, nil
}
func installZip(fs afero.Fs, dst string, p plugin.Plugin, zipFile string) error {
fi, r, err := openPluginFile(fs, zipFile)
if err != nil {
return err
}
defer r.Close() //nolint: errcheck
zr, err := zip.NewReader(r, fi.Size())
if err != nil {
return err
}
pluginDir := fmt.Sprintf("%s%c", p.Name, os.PathSeparator)
dst = filepath.Join(dst, p.Name)
if err := recreatePath(fs, dst); err != nil {
return err
}
return extractZip(fs, dst, pluginDir, zr)
}
func extractZip(fs afero.Fs, dst, pluginDir string, zr *zip.Reader) error {
for _, f := range zr.File {
path := filepath.Join(dst, strings.TrimPrefix(f.Name, pluginDir))
if !strings.HasPrefix(path, dst) {
return fmt.Errorf("%s: %w", path, ErrIllegalFilePath)
}
if f.FileInfo().IsDir() {
if err := createPathIfNotExists(fs, path); err != nil {
return err
}
continue
}
src, err := f.Open()
if err != nil {
return err
}
err = installStream(fs, path, src, f.FileInfo().Mode())
_ = src.Close() //nolint: errcheck
if err != nil {
return err
}
}
return nil
}