Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"syscall"
|
|
|
|
"sirherobrine23.com.br/Sirherobrine23/cgofuse/fuse"
|
|
)
|
|
|
|
// Convert [syscall.Errno], [fuse.Error], [fs.ErrInvalid], [fs.ErrNotExist],
|
|
// [fs.ErrExist], [fs.ErrPermission], [fs.ErrClosed], [io.EOF]
|
|
// to valid negative error to system respective
|
|
func ErrorToStatus(err error) int {
|
|
if err == nil {
|
|
return 0
|
|
}
|
|
switch v := err.(type) {
|
|
case syscall.Errno:
|
|
return -int(v)
|
|
case fuse.Error:
|
|
return -int(v)
|
|
case *fs.PathError:
|
|
return ErrorToStatus(v.Err)
|
|
case *os.LinkError:
|
|
return ErrorToStatus(v.Err)
|
|
case *os.SyscallError:
|
|
return ErrorToStatus(v.Err)
|
|
}
|
|
|
|
switch err {
|
|
case fs.ErrInvalid:
|
|
return -int(syscall.EINVAL)
|
|
case fs.ErrNotExist:
|
|
return -int(syscall.ENOENT)
|
|
case fs.ErrExist:
|
|
return -int(syscall.EEXIST)
|
|
case fs.ErrPermission:
|
|
return -int(syscall.EPERM)
|
|
case fs.ErrClosed:
|
|
return -int(syscall.EBADF)
|
|
case io.EOF:
|
|
return -1 // no stand error return
|
|
}
|
|
return -int(syscall.EIO)
|
|
}
|
|
|
|
// Add to fuse file information from [io/fs.File] interface
|
|
func BasicStat(stat fs.FileInfo, fuseStat *fuse.Stat_t) {
|
|
fuseStat.Size = stat.Size()
|
|
fuseStat.Mtim = fuse.NewTimespec(stat.ModTime())
|
|
fuseStat.Ctim = fuse.NewTimespec(stat.ModTime())
|
|
fuseStat.Atim = fuse.NewTimespec(stat.ModTime())
|
|
fuseStat.Mode = uint32(stat.Mode())
|
|
}
|
|
|
|
// Add to fuse dir entry informatio, if [io/fs.DirEntry.Info]
|
|
// return [io/fs.FileInfo] append with [AppendStat]
|
|
func AppendDirEntry(stat fs.DirEntry, fuseStat *fuse.Stat_t) {
|
|
fuseStat.Mode = uint32(stat.Type() | 0555)
|
|
if extraInfo, err := stat.Info(); err == nil {
|
|
AppendStat(extraInfo, fuseStat)
|
|
}
|
|
}
|