Skip to content

Commit

Permalink
fs: various fixes:
Browse files Browse the repository at this point in the history
* Setattr output

* Fix and test readdir

* Suppress XAttrs.

* Show default perms, default UID/GID.
  • Loading branch information
hanwen committed Apr 28, 2019
1 parent 7c06741 commit 15e02c2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
5 changes: 1 addition & 4 deletions fs/android.go
Expand Up @@ -84,11 +84,8 @@ func (n *androidNode) Setattr(ctx context.Context, file fs.FileHandle, in *fuse.
return syscall.EIO
}
}

out.Size = size
out.Mode = syscall.S_IFREG | 0644
}
return 0
return n.Getattr(ctx, file, out)
}

var _ = mtpNode((*androidNode)(nil))
Expand Down
6 changes: 1 addition & 5 deletions fs/classic.go
Expand Up @@ -178,11 +178,7 @@ func (n *classicNode) Setattr(ctx context.Context, file fs.FileHandle, in *fuse.
return file.(fs.FileSetattrer).Setattr(ctx, in, out)
}

if mt, ok := in.GetMTime(); ok {
n.setTime(&mt)
}

return 0
return n.mtpNodeImpl.Setattr(ctx, file, in, out)
}

////////////////
Expand Down
19 changes: 13 additions & 6 deletions fs/device_test.go
Expand Up @@ -66,17 +66,18 @@ func startFs(t *testing.T, useAndroid bool) (storageRoot string, cleanup func())
&fs.Options{
MountOptions: fuse.MountOptions{
SingleThreaded: true,
Debug: VerboseTest(),
// Debug: VerboseTest(),
},
})
if err != nil {
t.Fatalf("mount failed: %v", err)
}

dev.MTPDebug = VerboseTest()
/* dev.USBDebug = VerboseTest()
if false {
dev.MTPDebug = VerboseTest()
dev.USBDebug = VerboseTest()
dev.DataDebug = VerboseTest()
*/
}

go server.Serve()
server.WaitMount()

Expand Down Expand Up @@ -125,6 +126,10 @@ func testDevice(t *testing.T, useAndroid bool) {
t.Fatalf("Mkdir: %v", err)
}

if _, err := ioutil.ReadDir(dirName); err != nil {
t.Fatalf("ReadDir: %v", err)
}

if err := os.Remove(dirName); err != nil {
t.Fatalf("Rmdir: %v", err)
}
Expand All @@ -133,6 +138,7 @@ func testDevice(t *testing.T, useAndroid bool) {
if err := ioutil.WriteFile(name, []byte("abcpxq134"), 0644); err != nil {
t.Fatal("WriteFile failed", err)
}
defer os.Remove(name)
got, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal("ReadFile failed", err)
Expand Down Expand Up @@ -177,6 +183,7 @@ func testDevice(t *testing.T, useAndroid bool) {
if err != nil {
t.Fatal("Rename failed", err)
}
defer os.Remove(newName)

if fi, err := os.Lstat(name); err == nil {
t.Fatal("should have disappeared after rename", fi)
Expand All @@ -188,7 +195,7 @@ func testDevice(t *testing.T, useAndroid bool) {

err = os.Remove(newName)
if err != nil {
t.Fatal("Remove failed", err)
t.Fatalf("Remove failed: %v", err)
}
if fi, err := os.Lstat(newName); err == nil {
t.Fatal("should have disappeared after Remove", fi)
Expand Down
42 changes: 40 additions & 2 deletions fs/fs.go
Expand Up @@ -254,10 +254,27 @@ func (n *mtpNodeImpl) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.E
return 0
}

var _ = (fs.NodeGetxattrer)((*mtpNodeImpl)(nil))

func (n *mtpNodeImpl) Getxattr(ctx context.Context, attr string, dest []byte) (uint32, syscall.Errno) {
return 0, syscall.ENOSYS
}

var _ = (fs.NodeSetxattrer)((*mtpNodeImpl)(nil))

func (n *mtpNodeImpl) Setxattr(ctx context.Context, attr string, dest []byte, flags uint32) syscall.Errno {
return syscall.ENOSYS
}

var _ = (fs.NodeGetattrer)((*mtpNodeImpl)(nil))

func (n *mtpNodeImpl) Getattr(ctx context.Context, file fs.FileHandle, out *fuse.AttrOut) (code syscall.Errno) {
out.Mode = 0644
if n.IsDir() {
out.Mode = 0755
} else {
out.Mode = 0644
}

f := n.obj
if f != nil {
out.Size = uint64(n.Size)
Expand Down Expand Up @@ -291,6 +308,19 @@ func (n *mtpNodeImpl) StorageID() uint32 {
return n.obj.StorageID
}

func (n *mtpNodeImpl) Setattr(ctx context.Context, file fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) (code syscall.Errno) {
if mt, ok := in.GetMTime(); ok {
n.setTime(&mt)
var atime = mt
if a, ok := in.GetATime(); ok {
atime = a
}
out.SetTimes(&atime, &mt, nil)
}

return 0
}

var _ = mtpNode((*folderNode)(nil))

////////////////
Expand Down Expand Up @@ -391,7 +421,15 @@ func (n *folderNode) Readdir(ctx context.Context) (stream fs.DirStream, status s
if !n.fetch(ctx) {
return nil, syscall.EIO
}
return n.Operations().(fs.NodeReaddirer).Readdir(ctx)

r := []fuse.DirEntry{}
for k, ch := range n.Children() {
r = append(r, fuse.DirEntry{Mode: ch.Mode(),
Name: k,
Ino: ch.StableAttr().Ino})
}

return fs.NewListDirStream(r), 0
}

func (n *folderNode) basenameRename(oldName string, newName string) error {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Expand Up @@ -9,6 +9,8 @@ import (
"log"
"os"
"strings"
"syscall"
"time"

fusefs "github.com/hanwen/go-fuse/fs"
"github.com/hanwen/go-fuse/fuse"
Expand Down Expand Up @@ -64,12 +66,18 @@ func main() {
log.Fatalf("NewDeviceFs failed: %v", err)
}

sec := time.Second
mountOpts := &fusefs.Options{
MountOptions: fuse.MountOptions{
SingleThreaded: true,
AllowOther: *other,
Debug: debugs["fuse"] || debugs["fs"],
},
DefaultPermissions: true,
UID: uint32(syscall.Getuid()),
GID: uint32(syscall.Getgid()),
AttrTimeout: &sec,
EntryTimeout: &sec,
}
server, err := fusefs.Mount(mountpoint, root, mountOpts)
if err != nil {
Expand Down

0 comments on commit 15e02c2

Please sign in to comment.