-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.go
176 lines (151 loc) · 3.76 KB
/
format.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package accessio
import (
"archive/tar"
"compress/gzip"
"io"
"sort"
"strings"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/common/compression"
"github.com/open-component-model/ocm/pkg/utils/tarutils"
)
const KIND_FILEFORMAT = "file format"
type FileFormat string
func (f FileFormat) String() string {
return string(f)
}
func (f FileFormat) Suffix() string {
return suffixes[f]
}
func (o FileFormat) ApplyOption(options Options) error {
if o != "" {
options.SetFileFormat(o)
}
return nil
}
const (
FormatTar FileFormat = "tar"
FormatTGZ FileFormat = "tgz"
FormatDirectory FileFormat = "directory"
FormatNone FileFormat = ""
)
var suffixes = map[FileFormat]string{
FormatTar: "." + string(FormatTar),
FormatTGZ: "." + string(FormatTGZ),
}
func ErrInvalidFileFormat(fmt string) error {
return errors.ErrInvalid(KIND_FILEFORMAT, fmt)
}
////////////////////////////////////////////////////////////////////////////////
func GetFormats() []string {
return []string{string(FormatDirectory), string(FormatTar), string(FormatTGZ)}
}
func GetFormatsFor[T any](fileFormats map[FileFormat]T) []string {
var def FileFormat
list := []string{}
for k := range fileFormats {
// as favorite default, directory should be the first entry in the list
if k != FormatDirectory {
list = append(list, string(k))
} else {
def = k
}
}
sort.Strings(list)
if def != "" {
return append(append(list[:0:0], string(def)), list...)
}
return list
}
// FileFormatForTypeSpec returns the format hint provided
// by a type specification.The format hint is an optional
// suffix separated by a +.
func FileFormatForTypeSpec(t string) FileFormat {
i := strings.Index(t, "+")
if i < 0 {
return ""
}
return FileFormat(t[i+1:])
}
// TypeForTypeSpec returns the pure type info provided
// by a type specification.The format hint is an optional
// suffix separated by a +.
func TypeForTypeSpec(t string) string {
i := strings.Index(t, "+")
if i < 0 {
return t
}
return t[:i]
}
////////////////////////////////////////////////////////////////////////////////
func CopyFileSystem(format FileFormat, srcfs vfs.FileSystem, src string, dstfs vfs.FileSystem, dst string, perm vfs.FileMode) error {
compr := compression.None
switch format {
case FormatDirectory:
return vfs.CopyDir(srcfs, src, dstfs, dst)
case FormatTGZ:
compr = compression.Gzip
fallthrough
case FormatTar:
file, err := dstfs.OpenFile(dst, vfs.O_CREATE|vfs.O_TRUNC|vfs.O_WRONLY, perm)
if err != nil {
return err
}
defer file.Close()
w, err := compr.Compressor(file, nil, nil)
if err != nil {
return err
}
return tarutils.PackFsIntoTar(srcfs, src, w, tarutils.TarFileSystemOptions{})
default:
return errors.ErrUnknown(KIND_FILEFORMAT, format.String())
}
}
////////////////////////////////////////////////////////////////////////////////
func DetectFormat(path string, fs vfs.FileSystem) (*FileFormat, error) {
if fs == nil {
fs = _osfs
}
fi, err := fs.Stat(path)
if err != nil {
return nil, err
}
format := FormatDirectory
if !fi.IsDir() {
file, err := fs.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return DetectFormatForFile(file)
}
return &format, nil
}
func DetectFormatForFile(file vfs.File) (*FileFormat, error) {
fi, err := file.Stat()
if err != nil {
return nil, err
}
format := FormatDirectory
if !fi.IsDir() {
var r io.Reader
defer file.Seek(0, io.SeekStart)
zip, err := gzip.NewReader(file)
if err == nil {
format = FormatTGZ
defer zip.Close()
r = zip
} else {
file.Seek(0, io.SeekStart)
format = FormatTar
r = file
}
t := tar.NewReader(r)
_, err = t.Next()
if err != nil {
return nil, err
}
}
return &format, nil
}