-
Notifications
You must be signed in to change notification settings - Fork 3
/
ploop_error.go
135 lines (125 loc) · 2.75 KB
/
ploop_error.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
package ploop
// #include <ploop/libploop.h>
import "C"
import "fmt"
// Err contains a ploop error
type Err struct {
c int
s string
}
// SYSEXIT_* errors
const (
_ = iota
E_CREAT
E_DEVICE
E_DEVIOC
E_OPEN
E_MALLOC
E_READ
E_WRITE
E_RESERVED_8
E_SYSFS
E_RESERVED_10
E_PLOOPFMT
E_SYS
E_PROTOCOL
E_LOOP
E_FSTAT
E_FSYNC
E_EBUSY
E_FLOCK
E_FTRUNCATE
E_FALLOCATE
E_MOUNT
E_UMOUNT
E_LOCK
E_MKFS
E_RESERVED_25
E_RESIZE_FS
E_MKDIR
E_RENAME
E_ABORT
E_RELOC
E_RESERVED_31
E_RESERVED_32
E_CHANGE_GPT
E_RESERVED_34
E_UNLINK
E_MKNOD
E_PLOOPINUSE
E_PARAM
E_DISKDESCR
E_DEV_NOT_MOUNTED
E_FSCK
E_RESERVED_42
E_NOSNAP
)
// ErrCodes is a map of ploop numerical error codes to their short names
var ErrCodes = []string{
E_CREAT: "E_CREAT",
E_DEVICE: "E_DEVICE",
E_DEVIOC: "E_DEVIOC",
E_OPEN: "E_OPEN",
E_MALLOC: "E_MALLOC",
E_READ: "E_READ",
E_WRITE: "E_WRITE",
E_RESERVED_8: "E_RESERVED",
E_SYSFS: "E_SYSFS",
E_RESERVED_10: "E_RESERVED",
E_PLOOPFMT: "E_PLOOPFMT",
E_SYS: "E_SYS",
E_PROTOCOL: "E_PROTOCOL",
E_LOOP: "E_LOOP",
E_FSTAT: "E_FSTAT",
E_FSYNC: "E_FSYNC",
E_EBUSY: "E_EBUSY",
E_FLOCK: "E_FLOCK",
E_FTRUNCATE: "E_FTRUNCATE",
E_FALLOCATE: "E_FALLOCATE",
E_MOUNT: "E_MOUNT",
E_UMOUNT: "E_UMOUNT",
E_LOCK: "E_LOCK",
E_MKFS: "E_MKFS",
E_RESERVED_25: "E_RESERVED",
E_RESIZE_FS: "E_RESIZE_FS",
E_MKDIR: "E_MKDIR",
E_RENAME: "E_RENAME",
E_ABORT: "E_ABORT",
E_RELOC: "E_RELOC",
E_RESERVED_31: "E_RESERVED",
E_RESERVED_32: "E_RESERVED",
E_CHANGE_GPT: "E_CHANGE_GPT",
E_RESERVED_34: "E_RESERVED",
E_UNLINK: "E_UNLINK",
E_MKNOD: "E_MKNOD",
E_PLOOPINUSE: "E_PLOOPINUSE",
E_PARAM: "E_PARAM",
E_DISKDESCR: "E_DISKDESCR",
E_DEV_NOT_MOUNTED: "E_DEV_NOT_MOUNTED",
E_FSCK: "E_FSCK",
E_RESERVED_42: "E_RESERVED",
E_NOSNAP: "E_NOSNAP",
}
// Error returns a string representation of a ploop error
func (e *Err) Error() string {
s := "E_UNKNOWN"
if e.c > 0 && e.c < len(ErrCodes) {
s = ErrCodes[e.c]
}
return fmt.Sprintf("ploop error %d (%s): %s", e.c, s, e.s)
}
// IsError checks if an error is a specific ploop error
func IsError(err error, code int) bool {
perr, ok := err.(*Err)
return ok && perr.c == code
}
// IsNotMounted returns true if an error is ploop "device is not mounted"
func IsNotMounted(err error) bool {
return IsError(err, E_DEV_NOT_MOUNTED)
}
func mkerr(ret C.int) error {
if ret == 0 {
return nil
}
return &Err{c: int(ret), s: C.GoString(C.ploop_get_last_error())}
}