forked from hirochachacha/go-smb2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
143 lines (118 loc) · 2.55 KB
/
errors.go
File metadata and controls
143 lines (118 loc) · 2.55 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
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
package smb2
import (
"fmt"
"os"
. "github.com/hirochachacha/go-smb2/internal/erref"
)
// TransportError represents a error come from net.Conn layer.
type TransportError struct {
Err error
}
func (err *TransportError) Error() string {
return fmt.Sprintf("connection error: %v", err.Err)
}
// InternalError represents internal error.
type InternalError struct {
Message string
}
func (err *InternalError) Error() string {
return fmt.Sprintf("internal error: %s", err.Message)
}
// InvalidResponseError represents a data sent by the server is corrupted or unexpected.
type InvalidResponseError struct {
Message string
}
func (err *InvalidResponseError) Error() string {
return fmt.Sprintf("invalid response error: %s", err.Message)
}
// ResponseError represents a error with a nt status code sent by the server.
// The NTSTATUS is defined in [MS-ERREF].
// https://msdn.microsoft.com/en-au/library/cc704588.aspx
type ResponseError struct {
Code uint32 // NTSTATUS
data [][]byte
}
func (err *ResponseError) Error() string {
return fmt.Sprintf("response error: %v", NtStatus(err.Code))
}
type MultipleError []error
func (err MultipleError) Error() string {
msg := "multiple error:"
for _, e := range err {
msg += "\n\t" + e.Error()
}
return msg
}
func multiError(errs ...error) error {
var err MultipleError
for _, e := range errs {
switch e := e.(type) {
case nil:
case MultipleError:
err = append(err, e...)
default:
err = append(err, e)
}
}
switch len(err) {
case 0:
return nil
case 1:
return err[0]
}
return err
}
func IsExist(err error) bool {
switch e := err.(type) {
case nil:
return false
case *os.PathError:
err = e.Err
case *os.LinkError:
err = e.Err
}
if err, ok := err.(*ResponseError); ok {
switch NtStatus(err.Code) {
case STATUS_OBJECT_NAME_COLLISION:
return true
}
return false
}
return err == os.ErrExist
}
func IsNotExist(err error) bool {
switch e := err.(type) {
case nil:
return false
case *os.PathError:
err = e.Err
case *os.LinkError:
err = e.Err
}
if err, ok := err.(*ResponseError); ok {
switch NtStatus(err.Code) {
case STATUS_OBJECT_NAME_NOT_FOUND, STATUS_OBJECT_PATH_NOT_FOUND:
return true
}
return false
}
return err == os.ErrNotExist
}
func IsPermission(err error) bool {
switch e := err.(type) {
case nil:
return false
case *os.PathError:
err = e.Err
case *os.LinkError:
err = e.Err
}
if err, ok := err.(*ResponseError); ok {
switch NtStatus(err.Code) {
case STATUS_ACCESS_DENIED:
return true
}
return false
}
return err == os.ErrPermission
}