Added FUSE support #55

Merged
Elara6331 merged 65 commits from yannickulrich/itd:fuse into master 2023-03-25 22:23:52 +00:00
Showing only changes of commit 1a5970a041 - Show all commits

View File

@ -1,7 +1,7 @@
package main
import (
"log"
"go.arsenm.dev/logger/log"
"os"
"context"
"syscall"
@ -100,6 +100,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
case 2:
// on device
files, _ := myfs.ReadDir(n.path)
log.Info("readdir").Str("path", n.path).Int("objects", len(files)).Send()
r := make([]fuse.DirEntry, len(files))
n.lst = files
for ind, file := range files {
@ -174,7 +175,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
if file.path != n.path + "/" + name {
continue;
}
log.Println("matched", name)
log.Info("LookUp successful").Str("path", file.path).Send()
if file.flags & 0b1 == 1 {
stable := fs.StableAttr{
@ -198,6 +199,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
}
break;
}
log.Warn("LookUp failed").Str("path", n.path + "/" + name).Send()
}
return nil, syscall.ENOENT
}
@ -208,6 +210,7 @@ type bytesFileReadHandle struct {
var _ = (fs.FileReader)((*bytesFileReadHandle)(nil))
func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
log.Info("Executing Read").Int("size", len(fh.content)).Send()
end := off + int64(len(dest))
if end > int64(len(fh.content)) {
end = int64(len(fh.content))
@ -222,6 +225,7 @@ type bytesFileWriteHandle struct {
var _ = (fs.FileWriter)((*bytesFileWriteHandle)(nil))
func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int64) (written uint32, errno syscall.Errno) {
log.Info("Executing Write").Str("path", fh.path).Int("size", len(fh.content)).Send()
if off != int64(len(fh.content)) {
}
fh.content = append(fh.content[:], data[:]...)
@ -230,14 +234,19 @@ func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int6
var _ = (fs.FileFlusher)((*bytesFileWriteHandle)(nil))
func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) {
log.Info("Attempting flush").Str("path", fh.path).Send()
fp, err := myfs.Create(fh.path, uint32(len(fh.content)))
if err != nil {
log.Error("Flush failed: create").Str("path", fh.path).Err(err).Send()
return syscall.EROFS
}
nread, err := fp.Write(fh.content)
if err != nil || nread != len(fh.content) {
log.Error("Flush failed: write").Str("path", fh.path).Err(err).Send()
fp.Close()
return syscall.EROFS
}
log.Info("Flush done").Str("path", fh.path).Int("size", len(fh.content)).Send()
return 0
}
@ -248,6 +257,7 @@ func (fh *bytesFileWriteHandle) Fsync(ctx context.Context, flags uint32) (errno
var _ = (fs.NodeGetattrer)((*ITNode)(nil))
func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
log.Info("getattr").Str("path", bn.path).Send();
out.Ino = bn.Ino
out.Mtime = bn.self.modtime
out.Ctime = bn.self.modtime
@ -258,6 +268,7 @@ func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu
var _ = (fs.NodeSetattrer)((*ITNode)(nil))
func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
log.Info("setattr").Str("path", bn.path).Send()
out.Size = 0;
out.Mtime = 0;
return 0
@ -269,24 +280,29 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
case 2:
// FS file
if openFlags&syscall.O_RDWR != 0 {
log.Warn("open: failed RDWR").Str("path", f.path).Send()
return nil, 0, syscall.EROFS
}
if openFlags & syscall.O_WRONLY != 0 {
log.Info("Opening file: write").Str("path", f.path).Send()
fh = &bytesFileWriteHandle{
path : f.path,
content : make([]byte, 0),
}
return fh, fuse.FOPEN_DIRECT_IO, 0
} else {
log.Info("Opening file: read").Str("path", f.path).Send();
fp, err := myfs.Open(f.path)
if err != nil {
log.Error("Opening file failed").Str("path", f.path).Err(err).Send();
return nil, 0, syscall.EROFS
}
buf := make([]byte, f.self.size);
nread, err := fp.Read(buf)
if err != nil || nread != int(f.self.size) {
log.Error("Reading file failed").Str("path", f.path).Err(err).Send();
return nil, 0, syscall.EROFS
}
@ -346,6 +362,8 @@ func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uin
content : make([]byte, 0),
}
log.Info("Creating file").Str("path", path).Send()
errno = 0
return node, fh, fuseFlags, 0
}
@ -359,6 +377,10 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
path := f.path + "/" + name
err := myfs.Mkdir(path)
if err != nil {
log.Info("Mkdir failed").
Str("path", path).
Err(err).
Send()
return nil, syscall.EROFS
}
@ -374,6 +396,11 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
path : path,
}
node := f.NewInode(ctx, operations, stable)
log.Info("Mkdir sucess").
Str("path", path).
Int("ino", int(ino)).
Send()
return node, 0
}
@ -384,8 +411,18 @@ func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbe
err := myfs.Rename(p1, p2)
if err != nil {
log.Error("Rename failed").
Str("src", p1).
Str("dest", p2).
Err(err).
Send()
return syscall.EROFS
}
log.Info("Rename sucess").
Str("src", p1).
Str("dest", p2).
Send()
ino := inodemap[p1]
delete(inodemap, p1)
@ -399,8 +436,18 @@ func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
delete(inodemap, f.path + "/" + name)
err := myfs.Remove(f.path + "/" + name)
if err != nil {
log.Error("Unlink failed").
Str("file", f.path + "/" + name).
Err(err).
Send()
return syscall.EROFS
}
log.Info("Unlink success").
Str("file", f.path + "/" + name).
Err(err).
Send()
return 0
}
@ -421,10 +468,17 @@ func main() {
},
})
if err != nil {
log.Panic(err)
log.Error("Mounting failed").
Str("target", mntDir).
Err(err).
Send()
return err
}
dev := Device{};
log.Info("Mounted on target").
Str("target", mntDir).
Send()
properties[0].f = dev.HeartRateBytes;
properties[1].f = dev.BatteryLevelBytes;
@ -433,9 +487,6 @@ func main() {
properties[4].f = dev.VersionBytes;
properties[5].f = dev.AddressBytes;
log.Printf("Mounted on %s", mntDir)
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
myfs = &FS{};
inodemap = make(map[string]uint64)