forked from zach-klippenstein/adbfs
/
errors.go
63 lines (55 loc) · 1.69 KB
/
errors.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
package adbfs
import (
"errors"
"os"
"syscall"
"github.com/hanwen/go-fuse/fuse"
"github.com/zach-klippenstein/goadb/util"
)
const OK = syscall.Errno(0)
var (
// A symlink cycle is detected.
ErrLinkTooDeep = errors.New("link recursion too deep")
ErrNotALink = errors.New("not a link")
// The user doesn't have permission to perform an operation.
ErrNoPermission = os.ErrPermission
// The operation is not permitted due to reasons other than user permission.
ErrNotPermitted = errors.New("operation not permitted")
)
// toFuseStatusLog converts an Errno to a Status and logs it.
func toFuseStatusLog(err error, logEntry *LogEntry) fuse.Status {
return fuse.Status(toErrnoLog(err, logEntry))
}
func fuseStatusToErrno(status fuse.Status) syscall.Errno {
return syscall.Errno(status)
}
// toErrnoLog converts an error to an Errno and logs it.
func toErrnoLog(err error, logEntry *LogEntry) (status syscall.Errno) {
status = toErrno(err)
if status == syscall.EIO {
logEntry.Error(err)
}
return logEntry.Status(status)
}
// toErrno converts a known error to an Errno, or EIO if the error is not known.
func toErrno(err error) syscall.Errno {
switch {
case err == nil:
return OK
case err == ErrLinkTooDeep:
return syscall.ELOOP
case err == ErrNotALink:
return syscall.EINVAL
case err == ErrNoPermission || err == os.ErrPermission:
// See http://blog.unclesniper.org/archives/2-Linux-programmers,-learn-the-difference-between-EACCES-and-EPERM-already!.html
return syscall.EACCES
case err == ErrNotPermitted:
return syscall.EPERM
case util.HasErrCode(err, util.FileNoExistError):
return syscall.ENOENT
}
if err, ok := err.(syscall.Errno); ok {
return err
}
return syscall.EIO
}