-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqcow2reader.go
66 lines (61 loc) · 1.47 KB
/
qcow2reader.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
package qcow2reader
import (
"errors"
"fmt"
"io"
"github.com/lima-vm/go-qcow2reader/image"
"github.com/lima-vm/go-qcow2reader/image/parallels"
"github.com/lima-vm/go-qcow2reader/image/qcow2"
"github.com/lima-vm/go-qcow2reader/image/raw"
"github.com/lima-vm/go-qcow2reader/image/vdi"
"github.com/lima-vm/go-qcow2reader/image/vhdx"
"github.com/lima-vm/go-qcow2reader/image/vmdk"
"github.com/lima-vm/go-qcow2reader/image/vpc"
)
// Types is the known image types.
var Types = []image.Type{
qcow2.Type,
vmdk.Type,
vhdx.Type,
vdi.Type,
parallels.Type,
vpc.Type,
raw.Type, // raw must be the last type
}
// Open opens an image.
func Open(ra io.ReaderAt) (image.Image, error) {
for _, t := range Types {
img, err := OpenWithType(ra, t)
if err == nil {
return img, nil
}
if !errors.Is(err, image.ErrWrongType) {
err = fmt.Errorf("failed to open the image as %q: %w", t, err)
return img, err
}
}
return OpenWithType(ra, raw.Type)
}
// OpenWithType open opens an image with the specified [image.Type].
func OpenWithType(ra io.ReaderAt, t image.Type) (image.Image, error) {
switch t {
case "":
return Open(ra)
case qcow2.Type:
return qcow2.Open(ra, OpenWithType)
case vmdk.Type:
return vmdk.Open(ra)
case vhdx.Type:
return vhdx.Open(ra)
case vdi.Type:
return vdi.Open(ra)
case parallels.Type:
return parallels.Open(ra)
case vpc.Type:
return vpc.Open(ra)
case raw.Type:
return raw.Open(ra)
default:
return nil, fmt.Errorf("unknown type: %q", t)
}
}